diff --git a/src/incremental/__init__.py b/src/incremental/__init__.py index 12ec2fa..7a84a9e 100644 --- a/src/incremental/__init__.py +++ b/src/incremental/__init__.py @@ -503,7 +503,10 @@ def _load_pyproject_toml(toml_path, opt_in): # type: (str, bool) -> Optional[_I # Do we have an affirmative opt-in to use Incremental? Otherwise # we must *never* raise exceptions. opt_in = opt_in or tool_incremental is not None + if not opt_in: + return None + # Extract the project name package = None if tool_incremental is not None and "name" in tool_incremental: package = tool_incremental["name"] @@ -514,11 +517,9 @@ def _load_pyproject_toml(toml_path, opt_in): # type: (str, bool) -> Optional[_I except KeyError: pass if package is None: - # We can't proceed without a project name, but that's only an error - # if [tool.incremental] is present. - if opt_in: - raise ValueError("""\ -Incremental faied to extract the package name from pyproject.toml. Specify it like: + # We can't proceed without a project name. + raise ValueError("""\ +Incremental failed to extract the package name from pyproject.toml. Specify it like: [project] name = "Foo" @@ -529,29 +530,15 @@ def _load_pyproject_toml(toml_path, opt_in): # type: (str, bool) -> Optional[_I name = "Foo" """) - else: - return None - if not isinstance(package, str): - if opt_in: - raise TypeError( - "Package name must be a string, but found {}".format(type(package)) - ) - else: - return None - - try: - path = _findPath(os.path.dirname(toml_path), package) - except ValueError: - if opt_in: - raise - else: - return None + raise TypeError( + "Package name must be a string, but found {}".format(type(package)) + ) return _IncrementalConfig( opt_in=opt_in, package=package, - path=path, + path=_findPath(os.path.dirname(toml_path), package), ) diff --git a/src/incremental/tests/test_pyproject.py b/src/incremental/tests/test_pyproject.py index 998e0b3..939d8b8 100644 --- a/src/incremental/tests/test_pyproject.py +++ b/src/incremental/tests/test_pyproject.py @@ -158,11 +158,13 @@ def test_setuptoolsOptIn(self): def test_noToolIncrementalSection(self): """ - The ``opt_in`` flag is false when there isn't a - ``[tool.incremental]`` section. + We don't produce config unless we find opt-in. + + The ``[project]`` section doesn't imply opt-in, even if we can + recover the project name from it. """ root = Path(self.mktemp()) - pkg = root / "foo" + pkg = root / "foo" # A valid package directory. pkg.mkdir(parents=True) config = self._loadToml( @@ -171,11 +173,43 @@ def test_noToolIncrementalSection(self): path=root / "pyproject.toml", ) + self.assertIsNone(config) + + def test_pathNotFoundOptIn(self): + """ + Once opted in, raise `ValueError` when the package root can't + be resolved. + """ + root = Path(self.mktemp()) + root.mkdir() # Contains no package directory. + + with self.assertRaisesRegex(ValueError, "Can't find the directory of package"): + self._loadToml( + '[project]\nname = "foo"\n', + opt_in=True, + path=root / "pyproject.toml", + ) + + def test_noToolIncrementalSectionOptIn(self): + """ + If opted in (i.e. in the Hatch plugin) then the [tool.incremental] + table is completely optional. + """ + root = Path(self.mktemp()) + pkg = root / "src" / "foo" + pkg.mkdir(parents=True) + + config = self._loadToml( + '[project]\nname = "Foo"\n', + opt_in=True, + path=root / "pyproject.toml", + ) + self.assertEqual( config, _IncrementalConfig( - opt_in=False, - package="foo", + opt_in=True, + package="Foo", path=str(pkg), ), )