Skip to content

Commit

Permalink
fix: Fix guessing project files for python projest with dashes in nam…
Browse files Browse the repository at this point in the history
…es (#202)

When `pyproject.toml` contain project name with dashes, such as
`aiohttp-middlewares`, try to find version files in `aiohttp_middlewares/`
directory next to `pyproject.toml` file or within `src/` directory.

Fixes: #114
  • Loading branch information
playpauseandstop authored Dec 11, 2022
1 parent 4e92855 commit 8a4c1e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ test: install clean test-only
.PHONY: test-only
test-only:
TOXENV=$(TOXENV) $(TOX)

.PHONY: test-%
test-%: install clean
TOXENV=$(subst test-,,$@) $(TOX)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ commands =
git config init.defaultBranch
git config user.name
git config user.email
python3 -m pytest
python3 -m pytest {tty:--color=yes}
[testenv:py310-minimum-requirements]
commands_pre =
Expand Down
2 changes: 1 addition & 1 deletion src/badabump/cli/ci_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def prepare_tag(args: argparse.Namespace, *, config: ProjectConfig) -> int:


def main(argv: Union[Argv, None] = None) -> int:
args = parse_args(argv or sys.argv[1:])
args = parse_args(argv if argv is not None else sys.argv[1:])
# TODO: Fix this by providing required flag on adding subparsers
if getattr(args, "func", None) is None:
print(
Expand Down
28 changes: 18 additions & 10 deletions src/badabump/cli/commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import itertools
import subprocess
from pathlib import Path
from typing import Set, Tuple, Union
from typing import Iterator, Set, Tuple, Union

from badabump.changelog import ChangeLog, in_development_header, version_header
from badabump.cli.output import diff, echo_message
Expand Down Expand Up @@ -55,33 +56,40 @@ def guess_version_files(config: ProjectConfig) -> Tuple[str, ...]:
if maybe_pyproject_toml_path.exists():
version_files.append(FILE_PYPROJECT_TOML)

project_name = (
real_project_name = (
loads_toml(maybe_pyproject_toml_path.read_text())
.get("tool", {})
.get("poetry", {})
.get("name")
)

if project_name:
for package in (".", "./src"):
package_path = path / package
if real_project_name:
for project_name, package in itertools.product(
iter_python_project_names(real_project_name), ("", "src")
):
package_path = path / package if package else path
prefix = f"{package}/" if package else ""

if (package_path / project_name / "__init__.py").exists():
version_files.append(
f"{package}/{project_name}/__init__.py"
)
version_files.append(f"{prefix}{project_name}/__init__.py")

if (package_path / project_name / "__version__.py").exists():
version_files.append(
f"{package}/{project_name}/__version__.py"
f"{prefix}{project_name}/__version__.py"
)

if (package_path / f"{project_name}.py").exists():
version_files.append(f"{package}/{project_name}.py")
version_files.append(f"{prefix}{project_name}.py")

return tuple(version_files)


def iter_python_project_names(project_name: str) -> Iterator[str]:
yield project_name
if "-" in project_name:
yield project_name.replace("-", "_")


def run_post_bump_hook(
config: ProjectConfig, *, is_dry_run: bool = False
) -> None:
Expand Down
22 changes: 10 additions & 12 deletions tests/test_cli_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,23 @@ def test_guess_javascript_version_files(tmpdir):
(
((), ("pyproject.toml",)),
(
("project.py",),
("pyproject.toml", "./project.py"),
("my_project.py",),
("pyproject.toml", "my_project.py"),
),
(
("project/__init__.py", "project/__version__.py"),
("my_project/__init__.py", "my_project/__version__.py"),
(
"pyproject.toml",
"./project/__init__.py",
"./project/__version__.py",
"my_project/__init__.py",
"my_project/__version__.py",
),
),
(
("src/project/__init__.py", "src/project/__version__.py"),
("src/my-project/__init__.py", "src/my-project/__version__.py"),
(
"pyproject.toml",
"./src/project/__init__.py",
"./src/project/__version__.py",
"src/my-project/__init__.py",
"src/my-project/__version__.py",
),
),
),
Expand All @@ -95,7 +95,7 @@ def test_guess_python_version_files(tmpdir, files, expected):

(path / "pyproject.toml").write_text(
"""[tool.poetry]
name = "project"
name = "my-project"
version = "1.0.0"
"""
)
Expand All @@ -107,9 +107,7 @@ def test_guess_python_version_files(tmpdir, files, expected):

assert (
guess_version_files(
ProjectConfig(
path=Path(tmpdir), project_type=ProjectTypeEnum.python
)
ProjectConfig(path=path, project_type=ProjectTypeEnum.python)
)
== expected
)
Expand Down

0 comments on commit 8a4c1e8

Please sign in to comment.