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', '--depth', '1', '--recursive', '--branch', 'tag', 'git://my-source', 'source_dir'])
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_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 setUp(self): super().setUp() self.working_tree = 'git-test' self.source_dir = 'git-checkout' self.clean_dir(self.working_tree) os.chdir(self.working_tree) self.call(['git', 'init']) self.call(['git', 'config', '--local', 'user.name', '"Example Dev"']) self.call( ['git', 'config', '--local', 'user.email', '*****@*****.**']) with open('testing', 'w') as fp: fp.write('testing') self.call(['git', 'add', 'testing']) self.call(['git', 'commit', '-am', 'testing']) self.call(['git', 'tag', 'test-tag']) self.expected_commit = self.call_with_output(['git', 'log']).split()[1] self.expected_branch = self.call_with_output( ['git', 'rev-parse', '--abbrev-ref', 'HEAD']) self.expected_tag = 'test-tag' os.chdir('..') self.git = sources.Git(self.working_tree, self.source_dir, silent=True) self.git.pull() self.source_details = self.git._get_source_details()
def test_pull_existing_with_submodules(self): self.mock_path_exists.return_value = True git = sources.Git( "git://my-source", "source_dir", source_submodules=["submodule_1", "dir/submodule_2"], ) 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", "submodule_1", "dir/submodule_2", ]), ])
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_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_add(self): url = "git://my-source" source_dir = "source_dir" git = sources.Git(url, source_dir) git.add("file") self.mock_run.assert_called_once_with( ["git", "-C", "source_dir", "add", "file"])
def test_init_with_source_branch_and_tag_raises_exception(self): with self.assertRaises(sources.IncompatibleOptionsError) as raised: sources.Git('git://mysource', 'source_dir', source_tag='tag', source_branch='branch') expected_message = \ 'can\'t specify both source-tag and source-branch for a git source' self.assertEqual(raised.exception.message, expected_message)
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_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_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_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.assertEqual(self.expected_tag, self.source_details['tag'])
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_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(self): url = "git://my-source" refspec = "HEAD:master" source_dir = "source_dir" git = sources.Git(url, source_dir) git.push(url, refspec) self.mock_run.assert_called_once_with( ["git", "-C", "source_dir", "push", url, refspec])
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_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_init_with_source_tag_and_commit_raises_exception(self): with self.assertRaises(sources.IncompatibleOptionsError) as raised: sources.Git( 'git://mysource', 'source_dir', source_commit='2514f9533ec9b45d07883e10a561b248497a8e3c', source_tag='tag') expected_message = \ 'can\'t specify both source-tag and source-commit for ' \ 'a git source' self.assertEqual(raised.exception.message, expected_message)
def test_init_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.init) self.assertThat( error.get_brief(), Equals("Failed to execute git command: git -C source_dir init"), )
def test_pull_commit(self): git = sources.Git( 'git://my-source', 'source_dir', source_commit='2514f9533ec9b45d07883e10a561b248497a8e3c') git.pull() self.mock_run.assert_has_calls([ unittest.mock.call(['git', 'clone', '--recursive', 'git://my-source', 'source_dir']), unittest.mock.call(['git', '-C', 'source_dir', 'checkout', '2514f9533ec9b45d07883e10a561b248497a8e3c']) ])
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_existing(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', 'pull', '--recurse-submodules=yes', 'git://my-source', 'HEAD' ]), mock.call(['git', '-C', 'source_dir', 'submodule', 'update']) ])
def test_pull_existing_with_tag(self): self.mock_path_exists.return_value = True git = sources.Git('git://my-source', 'source_dir', source_tag='tag') git.pull() self.mock_run.assert_has_calls([ unittest.mock.call(['git', '-C', 'source_dir', 'pull', '--recurse-submodules=yes', 'git://my-source', 'refs/tags/tag']), unittest.mock.call(['git', '-C', 'source_dir', 'submodule', 'update']) ])
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_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_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_existing(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', '--remote']) ])
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_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([ unittest.mock.call(['git', '-C', 'source_dir', 'pull', '--recurse-submodules=yes', 'git://my-source', '2514f9533ec9b45d07883e10a561b248497a8e3c']), unittest.mock.call(['git', '-C', 'source_dir', 'submodule', 'update']) ])