diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | Makefile.tests | 6 | ||||
-rw-r--r-- | devbot/git.py | 38 | ||||
-rw-r--r-- | tests/devbot/test_git.py | 106 |
4 files changed, 152 insertions, 1 deletions
@@ -1,6 +1,6 @@ SOURCE_DIR=$(CURDIR)/source COMMANDS_DIR=$(CURDIR)/commands -HELPERS_DIR=$(COMMANDS_DIR)/helpers +HOME_DIR=$(CURDIR)/home TOOLS_DIR=$(CURDIR)/tools .PHONY: all @@ -10,3 +10,4 @@ all: check-system pull build include Makefile.config include Makefile.commands include Makefile.buildbot +include Makefile.tests diff --git a/Makefile.tests b/Makefile.tests new file mode 100644 index 0000000..c99d23e --- /dev/null +++ b/Makefile.tests @@ -0,0 +1,6 @@ +TESTS_DATA=$(HOME_DIR)/testsdata + +test-devbot: + mkdir -p $(TESTS_DATA) + TMPDIR=$(TESTS_DATA) PYTHONPATH=. python tests/devbot/test_git.py + rm -rf $(TESTS_DATA) diff --git a/devbot/git.py b/devbot/git.py new file mode 100644 index 0000000..55fd9f5 --- /dev/null +++ b/devbot/git.py @@ -0,0 +1,38 @@ +import os + +from devbot import command + +class Module: + def __init__(self, path, name, remote, branch="master", tag=None): + self.remote = remote + self.local = os.path.join(path, name) + self.tag = tag + + self._path = path + self._name = name + self._branch = branch + + def _clone(self): + os.chdir(self._path) + + command.run(["git", "clone", "--progress", + self.remote, self._name], + retry=10) + + os.chdir(self.local) + + command.run(["git", "checkout", self._branch]) + + def update(self): + if not os.path.exists(os.path.join(self.local, ".git")): + self._clone() + return + + os.chdir(self.local) + + command.run(["git", "fetch"]) + + if self.tag: + command.run(["git", "checkout", self.tag]) + else: + command.run(["git", "merge", "origin", self._branch]) diff --git a/tests/devbot/test_git.py b/tests/devbot/test_git.py new file mode 100644 index 0000000..9468814 --- /dev/null +++ b/tests/devbot/test_git.py @@ -0,0 +1,106 @@ +import os +import tempfile +import unittest +import subprocess + +from devbot import git + +class TestGit(unittest.TestCase): + def _create_repo(self): + path = tempfile.mkdtemp() + os.chdir(path) + + subprocess.check_call(["git", "init"]) + + with open("README", "w") as f: + f.write("") + + subprocess.check_call(["git", "add", "README"]) + + self._commit(path, "Initial commit") + + return path + + def _commit(self, remote, log): + os.chdir(remote) + subprocess.check_call(["git", "commit", "-a", "-m", log]) + + def _get_head(self, remote): + os.chdir(remote) + return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip() + + def _read_file(self, module): + content = None + + f = open(os.path.join(module.local, "README")) + content = f.read() + f.close() + + return content + + def _create_branch(self, remote, name): + os.chdir(remote) + subprocess.check_call(["git", "checkout", "-b", name]) + + def _write_file(self, remote, content): + f = open(os.path.join(remote, "README"), "w") + f.write(content) + f.close() + + def _create_module(self, remote, branch="master", tag=None): + path = tempfile.mkdtemp() + name = "test" + + return git.Module(path, name, remote, branch, tag) + + def _setup_module(self): + remote = self._create_repo() + + module = self._create_module(remote) + module.update() + + return module + + def test_clone(self): + module = self._setup_module() + self.assertTrue(os.path.exists(os.path.join(module.local, "README"))) + + def test_update_on_master(self): + module = self._setup_module() + + self._write_file(module.remote, "masterchange") + self._commit(module.remote, "masterchange") + + module.update() + + self.assertEquals("masterchange", self._read_file(module)) + + def test_update_on_branch(self): + remote = self._create_repo() + self._create_branch(remote, "test") + + module = self._create_module(remote, branch="test") + + self._write_file(module.remote, "branchchange") + self._commit(module.remote, "branchchange") + + module.update() + + self.assertEquals("branchchange", self._read_file(module)) + + def test_update_detached(self): + remote = self._create_repo() + + module = self._create_module(remote, tag=self._get_head(remote)) + module.update() + + self._write_file(module.remote, "detachedchange") + self._commit(module.remote, "detachedchange") + + module.tag = self._get_head(remote) + module.update() + + self.assertEquals("detachedchange", self._read_file(module)) + +if __name__ == '__main__': + unittest.main() |