Skip to content

Commit fd7d607

Browse files
authored
fix: AssertionError when outside a VCS dir, improve test fixtures (#24)
2 parents 1904def + c0d2041 commit fd7d607

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

CHANGES

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ $ pipx install --suffix=@next g --pip-args '\--pre' --force
2121

2222
<!-- Maintainers, insert changes / features for the next release here -->
2323

24+
### Fixes
25+
26+
- Fix `g` when running outside of a VCS directory (#24)
27+
28+
### Tests
29+
30+
- Use declarative, typed `NamedTuple`-style for `test_command_line` fixtures
31+
(#24)
32+
2433
### Development
2534

2635
- poetry: 1.8.1 -> 1.8.2
@@ -49,14 +58,17 @@ _Maintenance only, no bug fixes, or new features_
4958
--exec 'poetry run ruff check --select ALL . --fix --unsafe-fixes --preview --show-fixes; poetry run ruff format .; git add src tests; git commit --amend --no-edit' \
5059
origin/master
5160
```
61+
5262
- poetry: 1.7.1 -> 1.8.1
5363

5464
See also: https://github.com/python-poetry/poetry/blob/1.8.1/CHANGELOG.md
65+
5566
- ruff 0.2.2 -> 0.3.0 (#22)
5667

5768
Related formattings. Update CI to use `ruff check .` instead of `ruff .`.
5869

5970
See also: https://github.com/astral-sh/ruff/blob/v0.3.0/CHANGELOG.md
71+
6072
- Strengthen linting (#21)
6173

6274
- Add flake8-commas (COM)

src/g/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
"""Package for g."""
33

4+
import io
5+
import logging
46
import os
57
import pathlib
68
import subprocess
@@ -12,8 +14,11 @@
1214

1315
vcspath_registry = {".git": "git", ".svn": "svn", ".hg": "hg"}
1416

17+
log = logging.getLogger(__name__)
18+
1519

1620
def find_repo_type(path: t.Union[pathlib.Path, str]) -> t.Optional[str]:
21+
"""Detect repo type looking upwards."""
1722
for _path in [*list(pathlib.Path(path).parents), pathlib.Path(path)]:
1823
for p in _path.iterdir():
1924
if p.is_dir() and p.name in vcspath_registry:
@@ -45,6 +50,13 @@ def run(
4550
if cmd_args is DEFAULT:
4651
cmd_args = sys.argv[1:]
4752

53+
logging.basicConfig(level=logging.INFO, format="%(message)s")
54+
55+
if cmd is None:
56+
msg = "No VCS found in current directory."
57+
log.info(msg)
58+
return None
59+
4860
assert isinstance(cmd_args, (tuple, list))
4961
assert isinstance(cmd, (str, bytes, pathlib.Path))
5062

tests/test_cli.py

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Tests for g's CLI package."""
22

3+
import enum
4+
import pathlib
35
import subprocess
46
import typing as t
57
from unittest.mock import patch
@@ -20,25 +22,95 @@ def get_output(
2022
return exc.output
2123

2224

25+
class EnvFlag(enum.Enum):
26+
"""Environmental conditions to simulate in test case."""
27+
28+
Git = "Git" # Inside a git directory (like this repo)
29+
Empty = "Empty" # Empty directory (e.g. `tmp_path`)
30+
31+
2332
@pytest.mark.parametrize(
2433
("argv_args", "expect_cmd"),
2534
[
2635
(["g"], "git"),
2736
(["g", "--help"], "git --help"),
2837
],
2938
)
39+
class CommandLineTestFixture(t.NamedTuple):
40+
"""Test fixture for CLI params, environment, and expected result."""
41+
42+
# pytest internal
43+
test_id: str
44+
45+
# env data
46+
env: EnvFlag
47+
48+
# test data
49+
argv_args: t.List[str]
50+
51+
# results
52+
expect_cmd: t.Optional[str]
53+
54+
55+
TEST_FIXTURES: t.List[CommandLineTestFixture] = [
56+
CommandLineTestFixture(
57+
test_id="g-cmd-inside-git-dir",
58+
env=EnvFlag.Git,
59+
argv_args=["g"],
60+
expect_cmd="git",
61+
),
62+
CommandLineTestFixture(
63+
test_id="g-cmd-help-inside-git-dir",
64+
env=EnvFlag.Git,
65+
argv_args=["g --help"],
66+
expect_cmd="git --help",
67+
),
68+
CommandLineTestFixture(
69+
test_id="g-cmd-inside-empty-dir",
70+
env=EnvFlag.Empty,
71+
argv_args=["g"],
72+
expect_cmd=None,
73+
),
74+
CommandLineTestFixture(
75+
test_id="g-cmd-help-inside-empty-dir",
76+
env=EnvFlag.Empty,
77+
argv_args=["g --help"],
78+
expect_cmd=None,
79+
),
80+
]
81+
82+
83+
@pytest.mark.parametrize(
84+
list(CommandLineTestFixture._fields),
85+
TEST_FIXTURES,
86+
ids=[f.test_id for f in TEST_FIXTURES],
87+
)
3088
def test_command_line(
3189
# capsys: pytest.CaptureFixture[str],
90+
test_id: str,
91+
env: EnvFlag,
3292
argv_args: t.List[str],
33-
expect_cmd: str,
93+
expect_cmd: t.Optional[str],
94+
monkeypatch: pytest.MonkeyPatch,
95+
tmp_path: pathlib.Path,
3496
) -> None:
3597
"""Basic CLI usage."""
3698
from g import sys as gsys
3799

100+
if env == EnvFlag.Git:
101+
pass
102+
elif env == EnvFlag.Empty:
103+
monkeypatch.chdir(str(tmp_path))
104+
38105
with patch.object(gsys, "argv", argv_args):
39106
proc = run(wait=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
40-
assert proc is not None
41-
assert proc.stdout is not None
42-
captured = proc.stdout.read()
107+
if expect_cmd is None:
108+
assert proc is None
109+
else:
110+
assert proc is not None
111+
assert proc.stdout is not None
112+
captured = proc.stdout.read()
43113

44-
assert captured == get_output(expect_cmd, shell=True, stderr=subprocess.STDOUT)
114+
assert captured == get_output(
115+
expect_cmd, shell=True, stderr=subprocess.STDOUT
116+
)

0 commit comments

Comments
 (0)