Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strip-dev as get_version argument #798

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/setuptools_scm/__init__.py
Expand Up @@ -134,6 +134,7 @@ def get_version(
version_cls: Any | None = None,
normalize: bool = True,
search_parent_directories: bool = False,
strip_dev: bool = False,
) -> str:
"""
If supplied, relative_to should be a file from which root may
Expand Down Expand Up @@ -166,6 +167,9 @@ def _get_version(config: Configuration) -> str | None:
template=config.write_to_template,
)

if config.strip_dev:
version_string = version_string.partition(".dev")[0]

return version_string


Expand Down
9 changes: 5 additions & 4 deletions src/setuptools_scm/_cli.py
Expand Up @@ -32,11 +32,10 @@ def main(args: list[str] | None = None) -> None:
)
config = Configuration(inferred_root)

config.strip_dev = opts.strip_dev
version = _get_version(config)
if version is None:
raise SystemExit("ERROR: no version found for", opts)
if opts.strip_dev:
version = version.partition(".dev")[0]
print(version)

if opts.command == "ls":
Expand All @@ -60,8 +59,10 @@ def _get_cli_opts(args: list[str] | None) -> argparse.Namespace:
"--config",
default=None,
metavar="PATH",
help="path to 'pyproject.toml' with setuptools_scm config, "
"default: looked up in the current or parent directories",
help=(
"path to 'pyproject.toml' with setuptools_scm config, "
"default: looked up in the current or parent directories"
),
)
parser.add_argument(
"--strip-dev",
Expand Down
3 changes: 3 additions & 0 deletions src/setuptools_scm/config.py
Expand Up @@ -131,6 +131,7 @@ def __init__(
version_cls: type[_VersionT] | type | str | None = None,
normalize: bool = True,
search_parent_directories: bool = False,
strip_dev: bool = False,
):
# TODO:
self._relative_to = None if relative_to is None else os.fspath(relative_to)
Expand All @@ -153,6 +154,8 @@ def __init__(

self.version_cls = _validate_version_cls(version_cls, normalize)

self.strip_dev = strip_dev

@property
def fallback_root(self) -> str:
return self._fallback_root
Expand Down
18 changes: 18 additions & 0 deletions testing/test_basic_api.py
Expand Up @@ -138,6 +138,24 @@ def test_pretended(version: str, monkeypatch: pytest.MonkeyPatch) -> None:
assert setuptools_scm.get_version() == version


@pytest.mark.parametrize(
["version", "striped_version"],
[
["1.0", "1.0"],
["1.2.3.dev1+ge871260", "1.2.3"],
["1.2.3.dev15+ge871260.d20180625", "1.2.3"],
["2345", "2345"],
["1.2.3.rc0.dev1+ge871260", "1.2.3.rc0"],
["1.2.3.rc1.dev15+ge871260.d20180625", "1.2.3.rc1"],
],
)
def test_strip_dev(
version: str, striped_version: str, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv(setuptools_scm.PRETEND_KEY, version)
assert setuptools_scm.get_version(strip_dev=True) == striped_version


def test_root_relative_to(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
tmp_path.joinpath("setup.cfg").touch()
assert_root(monkeypatch, str(tmp_path / "alt"))
Expand Down