def test_all_branches_are_scanned_for_commits( self, mock_repo: mock.MagicMock, mock_iter_commits: mock.MagicMock): mock_repo.return_value.remotes.origin.fetch.return_value = [ "foo", "bar" ] test_scanner = scanner.GitRepoScanner(self.global_options, self.git_options, ".") mock_iter_commits.return_value = [] for _ in test_scanner.chunks: pass mock_iter_commits.assert_has_calls(( mock.call(mock_repo.return_value, "foo"), mock.call(mock_repo.return_value, "bar"), ))
def test_explicit_exception_is_raised_if_fetch_fails( self, mock_repo: mock.MagicMock ): self.git_options.fetch = True mock_repo.return_value.remotes.origin.fetch.side_effect = git.GitCommandError( command="git fetch -v origin", status=42, stderr="Fetch failed!" ) test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) with self.assertRaisesRegex( types.GitRemoteException, "stderr: 'Fetch failed!'" ): for _ in test_scanner.chunks: pass
def test_extra_inclusions_get_added(self, mock_load: mock.MagicMock): mock_load.return_value = ( self.data_dir / "pyproject.toml", { "include_paths": "include-files" }, ) test_scanner = scanner.GitRepoScanner(self.global_options, self.git_options, str(self.data_dir)) test_scanner.load_repo("../tartufo") self.assertEqual( test_scanner.included_paths, [re.compile("tartufo/"), re.compile("scripts/")], )
def test_extra_exclusions_get_added(self, mock_load: mock.MagicMock): mock_load.return_value = ( self.data_dir / "pyproject.toml", {"exclude_paths": "exclude-files", "exclude_path_patterns": ("bar/",)}, ) test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, str(self.data_dir) ) test_scanner.load_repo("../tartufo") self.assertCountEqual( test_scanner.excluded_paths, [ re.compile("bar/"), re.compile("tests/"), re.compile(r"\.venv/"), re.compile(r".*\.egg-info/"), ], )
def test_single_branch_is_loaded_if_specified(self): self.git_options.branch = "foo" mock_branch_foo = mock.MagicMock() mock_branch_bar = mock.MagicMock() self.mock_repo.return_value.listall_branches.return_value = ["foo", "bar"] self.mock_repo.return_value.branches = { "foo": mock_branch_foo, "bar": mock_branch_bar, } test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) self.mock_iter_diff.return_value = [] for _ in test_scanner.chunks: pass self.mock_repo.return_value.walk.assert_called_once_with( mock_branch_foo.resolve().target, pygit2.GIT_SORT_TOPOLOGICAL )
def test_head_is_scanned_when_shallow_clone_is_found(self): self.mock_shallow.return_value = True self.mock_iter_diff.return_value = [] self.mock_repo.return_value.head.target = "commit-hash" mock_head = mock.MagicMock(spec=pygit2.Commit) self.mock_repo.return_value.get.return_value = mock_head test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) for _ in test_scanner.chunks: pass # This is all the stuff that happens for yielding the "first commit". self.mock_repo.return_value.get.assert_called_once_with("commit-hash") revparse = self.mock_repo.return_value.revparse_single revparse.assert_called_once_with(mock_head.hex) tree = revparse.return_value.tree.diff_to_tree tree.assert_called_once_with(swap=True) self.mock_iter_diff.assert_called_with(tree.return_value)
def test_scan_single_branch_fetch_true( self, mock_repo: mock.MagicMock, mock_iter_commits: mock.MagicMock ): self.git_options.fetch = True self.git_options.branch = "bar" mock_foo = mock.MagicMock() mock_foo.name = "foo" mock_bar = mock.MagicMock() mock_bar.name = "bar" mock_repo.return_value.remotes.origin.fetch.return_value = ["foo", "bar"] mock_repo.return_value.branches = [mock_foo, mock_bar] test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) mock_iter_commits.return_value = [] for _ in test_scanner.chunks: pass mock_repo.return_value.remotes.origin.fetch.assert_called() mock_iter_commits.assert_has_calls( (mock.call(mock_repo.return_value, mock_bar),) )
def test_all_commits_are_scanned_for_files(self): self.mock_repo.return_value.branches = {"foo": mock.MagicMock()} test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) mock_commit_1 = mock.MagicMock(name="commit1") mock_commit_1.parents = None mock_commit_2 = mock.MagicMock(name="commit2") mock_commit_2.parents = [mock_commit_1] mock_commit_3 = mock.MagicMock(name="commit3") mock_commit_3.parents = [mock_commit_2] self.mock_repo.return_value.walk.return_value = [ mock_commit_3, mock_commit_2, mock_commit_1, ] self.mock_iter_diff.return_value = [] for _ in test_scanner.chunks: pass self.mock_repo.return_value.diff.assert_has_calls( ( mock.call(mock_commit_2, mock_commit_3), mock.call().find_similar(), mock.call(mock_commit_1, mock_commit_2), mock.call().find_similar(), ) ) self.mock_iter_diff.assert_has_calls( ( mock.call( self.mock_repo.return_value.diff(mock_commit_3, mock_commit_2), ), mock.call( self.mock_repo.return_value.diff(mock_commit_2, mock_commit_1), ), mock.call( self.mock_repo.return_value.revparse_single().tree.diff_to_tree(), ), ) )
def test_all_branches_are_scanned_for_commits(self): mock_branch_foo = mock.MagicMock() mock_branch_bar = mock.MagicMock() self.mock_repo.return_value.listall_branches.return_value = ["foo", "bar"] self.mock_repo.return_value.branches = { "foo": mock_branch_foo, "bar": mock_branch_bar, } test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) mock_commit_1 = mock.MagicMock() mock_commit_1.parents = None mock_commit_2 = mock.MagicMock() mock_commit_2.parents = [mock_commit_1] mock_commit_3 = mock.MagicMock() mock_commit_3.parents = [mock_commit_2] self.mock_repo.return_value.walk.return_value = [ mock_commit_3, mock_commit_2, mock_commit_1, ] self.mock_iter_diff.return_value = [] for _ in test_scanner.chunks: pass self.mock_repo.return_value.walk.assert_has_calls( ( mock.call( mock_branch_foo.resolve().target, pygit2.GIT_SORT_TOPOLOGICAL ), mock.call( mock_branch_bar.resolve().target, pygit2.GIT_SORT_TOPOLOGICAL ), ) )
def test_iteration_stops_when_since_commit_is_reached(self): self.git_options.since_commit = "42" test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) mock_repo = mock.MagicMock() mock_branch = mock.MagicMock(name="foo") mock_commit_1 = mock.MagicMock() mock_commit_2 = mock.MagicMock() mock_commit_3 = mock.MagicMock() mock_commit_3.hexsha = "42" mock_commit_4 = mock.MagicMock() mock_repo.iter_commits.return_value = [ mock_commit_1, mock_commit_2, mock_commit_3, mock_commit_4, ] commits = list(test_scanner._iter_branch_commits(mock_repo, mock_branch)) # Because "since commit" is exclusive, only the 2 commits before it are ever yielded self.assertEqual( commits, [(mock_commit_2, mock_commit_1)], )
def test_all_commits_are_scanned_for_files( self, mock_repo: mock.MagicMock, mock_iter_diff_index: mock.MagicMock, mock_iter_commits: mock.MagicMock, ): self.git_options.fetch = True mock_repo.return_value.remotes.origin.fetch.return_value = ["foo"] mock_repo.return_value.branches = ["foo"] test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) mock_commit_1 = mock.MagicMock() mock_commit_2 = mock.MagicMock() mock_commit_3 = mock.MagicMock() mock_iter_commits.return_value = [ (mock_commit_2, mock_commit_3), (mock_commit_1, mock_commit_2), ] mock_iter_diff_index.return_value = [] for _ in test_scanner.chunks: pass mock_commit_2.diff.assert_called_once_with(mock_commit_3, create_patch=True) mock_commit_1.diff.assert_has_calls( ( mock.call(mock_commit_2, create_patch=True), mock.call(git.NULL_TREE, create_patch=True), ) ) mock_iter_diff_index.assert_has_calls( ( mock.call(mock_commit_2.diff.return_value), mock.call(mock_commit_1.diff.return_value), mock.call(mock_commit_1.diff.return_value), ) )
def test_load_repo_loads_new_repo(self, mock_repo: mock.MagicMock): test_scanner = scanner.GitRepoScanner(self.global_options, self.git_options, ".") test_scanner.load_repo("../tartufo") mock_repo.assert_has_calls((mock.call("."), mock.call("../tartufo")))
def test_repo_is_loaded_on_init(self, mock_repo: mock.MagicMock): scanner.GitRepoScanner(self.global_options, self.git_options, ".") mock_repo.assert_called_once_with(".")
def test_load_repo_does_not_filter_submodules_when_requested( self, mock_filter: mock.MagicMock ): self.git_options.include_submodules = True scanner.GitRepoScanner(self.global_options, self.git_options, ".") mock_filter.assert_not_called()
def test_old_style_excluded_paths_are_processed(self): self.global_options.exclude_path_patterns = ["bar/"] test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) self.assertEqual(test_scanner.excluded_paths, [re.compile("bar/")])
def test_old_style_signatures_are_processed(self): self.global_options.exclude_signatures = ["bar/"] test_scanner = scanner.GitRepoScanner( self.global_options, self.git_options, "." ) self.assertEqual(test_scanner.excluded_signatures, ("bar/",))