def test_pull_existing_with_submodules_default(self): self.mock_path_exists.return_value = True git = sources.Git("git://my-source", "source_dir") git.pull() self.mock_run.assert_has_calls( [ mock.call( [ "git", "-C", "source_dir", "fetch", "--prune", "--recurse-submodules=yes", ] ), mock.call( ["git", "-C", "source_dir", "reset", "--hard", "origin/master"] ), mock.call( [ "git", "-C", "source_dir", "submodule", "update", "--recursive", "--force", ] ), ] )
def test_pull_commit(self): git = sources.Git( "git://my-source", "source_dir", source_commit="2514f9533ec9b45d07883e10a561b248497a8e3c", ) git.pull() self.mock_run.assert_has_calls( [ mock.call( ["git", "clone", "--recursive", "git://my-source", "source_dir"] ), mock.call( [ "git", "-C", "source_dir", "fetch", "origin", "2514f9533ec9b45d07883e10a561b248497a8e3c", ] ), mock.call( [ "git", "-C", "source_dir", "checkout", "2514f9533ec9b45d07883e10a561b248497a8e3c", ] ), ] )
def test_pull(self): git = sources.Git("git://my-source", "source_dir") git.pull() self.mock_run.assert_called_once_with( ["git", "clone", "--recursive", "git://my-source", "source_dir"] )
def test_git_details_tag(self): self.git = sources.Git( self.working_tree, self.source_dir, silent=True, source_tag="test-tag" ) self.git.pull() self.source_details = self.git._get_source_details() self.assertThat(self.source_details["source-tag"], Equals(self.expected_tag))
def test_pull_with_submodules_empty(self): git = sources.Git("git://my-source", "source_dir", source_submodules=[]) git.pull() self.mock_run.assert_called_once_with( ["git", "clone", "git://my-source", "source_dir"] )
def test_pull_failure(self): self.mock_run.side_effect = subprocess.CalledProcessError(1, []) git = sources.Git("git://my-source", "source_dir") raised = self.assertRaises(sources.errors.SnapcraftPullError, git.pull) self.assertThat( raised.command, Equals("git clone --recursive git://my-source source_dir") ) self.assertThat(raised.exit_code, Equals(1))
def test_init(self): url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) git.init() self.fake_check_output.assert_called_once_with( ["git", "-C", "source_dir", "init"] )
def test_add_abs_path(self): url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) git.add(os.path.join(source_dir, "file")) self.fake_check_output.assert_called_once_with( ["git", "-C", "source_dir", "add", "file"] )
def test_push_force(self): url = "git://my-source" refspec = "HEAD:master" source_dir = "source_dir" git = sources.Git(url, source_dir) git.push(url, refspec, force=True) self.fake_check_output.assert_called_once_with( ["git", "-C", "source_dir", "push", url, refspec, "--force"] )
def test_add_error(self): self.fake_check_output.side_effect = fake_git_command_error url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) error = self.assertRaises(errors.GitCommandError, git.add, "file") self.assertThat( error.get_brief(), Equals("Failed to execute git command: git -C source_dir add file"), )
def test_pull_existing_with_submodules_empty(self): self.mock_path_exists.return_value = True git = sources.Git("git://my-source", "source_dir", source_submodules=[]) git.pull() self.mock_run.assert_has_calls( [ mock.call(["git", "-C", "source_dir", "fetch", "--prune"]), mock.call( ["git", "-C", "source_dir", "reset", "--hard", "origin/master"] ), ] )
def test_git_details_branch(self): shutil.rmtree(self.source_dir) self.git = sources.Git( self.working_tree, self.source_dir, silent=True, source_branch=self.expected_branch, ) self.git.pull() self.source_details = self.git._get_source_details() self.assertThat( self.source_details["source-branch"], Equals(self.expected_branch) )
def test_push_error(self): self.fake_check_output.side_effect = fake_git_command_error url = "git://my-source" refspec = "HEAD:master" source_dir = "source_dir" git = sources.Git(url, source_dir) error = self.assertRaises(errors.GitCommandError, git.push, url, refspec) self.assertThat( error.get_brief(), Equals( "Failed to execute git command: git -C source_dir push git://my-source HEAD:master" ), )
def test_pull_tag(self): git = sources.Git("git://my-source", "source_dir", source_tag="tag") git.pull() self.mock_run.assert_called_once_with( [ "git", "clone", "--recursive", "--branch", "tag", "git://my-source", "source_dir", ] )
def test_pull_with_depth(self): git = sources.Git("git://my-source", "source_dir", source_depth=2) git.pull() self.mock_run.assert_called_once_with( [ "git", "clone", "--recursive", "--depth", "2", "git://my-source", "source_dir", ] )
def test_commit_error(self): self.fake_check_output.side_effect = fake_git_command_error url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) error = self.assertRaises( errors.GitCommandError, git.commit, message="message", author="author" ) self.assertThat( error.get_brief(), Equals( "Failed to execute git command: git -C source_dir commit --no-gpg-sign --message message --author author" ), )
def test_pull_existing_with_commit(self): self.mock_path_exists.return_value = True git = sources.Git( "git://my-source", "source_dir", source_commit="2514f9533ec9b45d07883e10a561b248497a8e3c", ) git.pull() self.mock_run.assert_has_calls( [ mock.call( [ "git", "-C", "source_dir", "fetch", "--prune", "--recurse-submodules=yes", ] ), mock.call( [ "git", "-C", "source_dir", "reset", "--hard", "2514f9533ec9b45d07883e10a561b248497a8e3c", ] ), mock.call( [ "git", "-C", "source_dir", "submodule", "update", "--recursive", "--force", ] ), ] )
def test_pull_with_submodules(self): git = sources.Git( "git://my-source", "source_dir", source_submodules=["submodule_1", "dir/submodule_2"], ) git.pull() self.mock_run.assert_called_once_with( [ "git", "clone", "--recursive=submodule_1", "--recursive=dir/submodule_2", "git://my-source", "source_dir", ] )
def setUp(self): def _add_and_commit_file(filename, content=None, message=None): if not content: content = filename if not message: message = filename with open(filename, "w") as fp: fp.write(content) call(["git", "add", filename]) call(["git", "commit", "-am", message]) super().setUp() self.working_tree = "git-test" self.source_dir = "git-checkout" self.clean_dir(self.working_tree) os.chdir(self.working_tree) call(["git", "init"]) call(["git", "config", "user.name", '"Example Dev"']) call(["git", "config", "user.email", "*****@*****.**"]) _add_and_commit_file("testing") self.expected_commit = call_with_output(["git", "rev-parse", "HEAD"]) _add_and_commit_file("testing-2") call(["git", "tag", "test-tag"]) self.expected_tag = "test-tag" _add_and_commit_file("testing-3") self.expected_branch = "test-branch" call(["git", "branch", self.expected_branch]) os.chdir("..") self.git = sources.Git( self.working_tree, self.source_dir, silent=True, source_commit=self.expected_commit, ) self.git.pull() self.source_details = self.git._get_source_details()
def test_commit(self): url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) git.commit(message="message", author="author") self.fake_check_output.assert_called_once_with( [ "git", "-C", "source_dir", "commit", "--no-gpg-sign", "--message", "message", "--author", "author", ] )
def test_pull_existing_with_branch(self): self.mock_path_exists.return_value = True git = sources.Git("git://my-source", "source_dir", source_branch="my-branch") git.pull() self.mock_run.assert_has_calls( [ mock.call( [ "git", "-C", "source_dir", "fetch", "--prune", "--recurse-submodules=yes", ] ), mock.call( [ "git", "-C", "source_dir", "reset", "--hard", "refs/heads/my-branch", ] ), mock.call( [ "git", "-C", "source_dir", "submodule", "update", "--recursive", "--force", ] ), ] )
def test_git_conflicts(self): repo = "/tmp/conflict-test.git" working_tree = "/tmp/git-conflict-test" conflicting_tree = "{}-conflict".format(working_tree) git = sources.Git(repo, working_tree, silent=True) self.clean_dir(repo) self.clean_dir(working_tree) self.clean_dir(conflicting_tree) os.chdir(repo) call(["git", "init", "--bare"]) self.clone_repo(repo, working_tree) # check out the original repo self.clone_repo(repo, conflicting_tree) # add a file to the repo os.chdir(working_tree) self.add_file("fake", "fake 1", "fake 1") call(["git", "push", repo]) git.pull() os.chdir(conflicting_tree) self.add_file("fake", "fake 2", "fake 2") call(["git", "push", "-f", repo]) os.chdir(working_tree) git.pull() body = None with open(os.path.join(working_tree, "fake")) as fp: body = fp.read() self.assertThat(body, Equals("fake 2"))
def test_git_submodules(self): """Test that updates to submodules are pulled""" repo = "/tmp/submodules.git" sub_repo = "/tmp/subrepo" working_tree = "/tmp/git-submodules" working_tree_two = "{}-two".format(working_tree) sub_working_tree = "/tmp/git-submodules-sub" git = sources.Git(repo, working_tree, silent=True) self.clean_dir(repo) self.clean_dir(sub_repo) self.clean_dir(working_tree) self.clean_dir(working_tree_two) self.clean_dir(sub_working_tree) os.chdir(sub_repo) call(["git", "init", "--bare"]) self.clone_repo(sub_repo, sub_working_tree) self.add_file("sub-file", "sub-file", "sub-file") call(["git", "push", sub_repo]) os.chdir(repo) call(["git", "init", "--bare"]) self.clone_repo(repo, working_tree) call(["git", "submodule", "add", sub_repo]) call(["git", "commit", "-am", "added submodule"]) call(["git", "push", repo]) git.pull() self.check_file_contents( os.path.join(working_tree, "subrepo", "sub-file"), "sub-file" ) # add a file to the repo os.chdir(sub_working_tree) self.add_file("fake", "fake 1", "fake 1") call(["git", "push", sub_repo]) os.chdir(working_tree) git.pull() # this shouldn't cause any change self.check_file_contents( os.path.join(working_tree, "subrepo", "sub-file"), "sub-file" ) self.assertFalse(os.path.exists(os.path.join(working_tree, "subrepo", "fake"))) # update the submodule self.clone_repo(repo, working_tree_two) call(["git", "submodule", "update", "--init", "--recursive", "--remote"]) call(["git", "add", "subrepo"]) call(["git", "commit", "-am", "updated submodule"]) call(["git", "push"]) os.chdir(working_tree) git.pull() # new file should be there now self.check_file_contents( os.path.join(working_tree, "subrepo", "sub-file"), "sub-file" ) self.check_file_contents( os.path.join(working_tree, "subrepo", "fake"), "fake 1" )