def test_parent_directory_not_checked_if_traverse_is_false(self): with self.assertRaisesRegex( FileNotFoundError, f"Could not find config file in {self.data_dir / 'config'}.", ): config.load_config_from_path(self.data_dir / "config", "pyproject.toml", False)
def test_config_exception_is_raised_if_trouble_reading_file( self, mock_toml: mock.MagicMock): mock_toml.side_effect = toml.TomlDecodeError("Bad TOML!", "foo", 42) with self.assertRaisesRegex( types.ConfigException, "Error reading configuration file: Bad TOML!"): config.load_config_from_path(self.data_dir)
def test_config_exception_is_raised_if_trouble_reading_file( self, mock_toml: mock.MagicMock): mock_toml.side_effect = tomlkit.exceptions.ParseError(21, 42) with self.assertRaisesRegex( types.ConfigException, "TOML parse error at line 21 col 42", ): config.load_config_from_path(self.data_dir)
def load_repo(self, repo_path: str) -> git.Repo: config_file: Optional[pathlib.Path] = None data: MutableMapping[str, Any] = {} try: (config_file, data) = config.load_config_from_path( pathlib.Path(repo_path), traverse=False ) except (FileNotFoundError, types.ConfigException) as exc: config_file = None if config_file and config_file != self.global_options.config: signatures = data.get("exclude_signatures", None) if signatures: self.global_options.exclude_signatures = tuple( set(self.global_options.exclude_signatures + tuple(signatures)) ) extras_path = data.get("include_paths", None) if extras_path: extras_file = pathlib.Path(repo_path, extras_path) if extras_file.exists(): includes = self.included_paths with extras_file.open() as handle: includes += config.compile_path_rules(handle.readlines()) self._included_paths = includes extras_path = data.get("exclude_paths", None) if extras_path: extras_file = pathlib.Path(repo_path, extras_path) if extras_file.exists(): excludes = self.excluded_paths with extras_file.open() as handle: excludes += config.compile_path_rules(handle.readlines()) self._excluded_paths = excludes try: return git.Repo(repo_path) except git.GitError as exc: raise types.GitLocalException(str(exc)) from exc
def load_repo(self, repo_path: str) -> pygit2.Repository: config_file: Optional[pathlib.Path] = None data: MutableMapping[str, Any] = {} try: (config_file, data) = config.load_config_from_path(pathlib.Path(repo_path), traverse=False) except (FileNotFoundError, types.ConfigException): config_file = None if config_file and str(config_file) != self.global_options.config: self.config_data = data try: repo = pygit2.Repository(repo_path) if not repo.is_bare: if not self.git_options.include_submodules: self.filter_submodules(repo) return repo except git.GitError as exc: raise types.GitLocalException(str(exc)) from exc
def test_config_keys_are_normalized(self, mock_load: mock.MagicMock): mock_load.return_value = {"tool": {"tartufo": {"--repo-path": "."}}} (_, data) = config.load_config_from_path(self.data_dir) self.assertEqual(data, {"repo_path": "."})
def test_specified_file_gets_read(self): (config_path, _) = config.load_config_from_path(self.data_dir / "config", "other_config.toml") self.assertEqual(config_path, self.data_dir / "config" / "other_config.toml")
def test_prefer_tartufo_toml_config_if_both_are_present(self): (config_path, _) = config.load_config_from_path(self.data_dir / "multiConfig") self.assertEqual(config_path, self.data_dir / "multiConfig" / "tartufo.toml")
def test_tartufo_toml_is_discovered_if_present(self): (config_path, _) = config.load_config_from_path(self.data_dir / "config") self.assertEqual(config_path, self.data_dir / "config" / "tartufo.toml")
def test_pyproject_toml_is_discovered_if_present(self): (config_path, _) = config.load_config_from_path(self.data_dir) self.assertEqual(config_path, self.data_dir / "pyproject.toml")