Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Jul 27, 2024
1 parent f821c9a commit e613ab6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
33 changes: 10 additions & 23 deletions src/incremental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
Expand All @@ -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),
)


Expand Down
34 changes: 29 additions & 5 deletions src/incremental/tests/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ def test_toolIncrementalUnexpecteKeys(self):
]:
self.assertRaises(ValueError, self._loadToml, toml, False)

def test_pathNotFoundOptIn(self):
"""
Once opted in, raise `ValueError` when the package root can't
be resolved.
"""

def test_setuptoolsOptIn(self):
"""
The package has opted-in to Incremental version management when
Expand Down Expand Up @@ -158,11 +164,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(
Expand All @@ -171,11 +179,27 @@ def test_noToolIncrementalSection(self):
path=root / "pyproject.toml",
)

self.assertIsNone(config)

def test_noToolIncrementalSectionOptIn(self):
"""
If opted in (i.e. in the Hatch plugin)
"""
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),
),
)

0 comments on commit e613ab6

Please sign in to comment.