Skip to content

Commit

Permalink
add --check (#65)
Browse files Browse the repository at this point in the history
fixes undefined
  • Loading branch information
bollwyvl authored Feb 7, 2023
1 parent a42c726 commit 94a0d12
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/pyproject_fmt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _handle_one(config: Config, opts: PyProjectFmtNamespace) -> bool:
print(formatted, end="")
return changed

if before != formatted:
if before != formatted and not opts.check:
config.pyproject_toml.write_text(formatted, encoding="utf-8")
try:
name = str(config.pyproject_toml.relative_to(Path.cwd()))
Expand Down
6 changes: 5 additions & 1 deletion src/pyproject_fmt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PyProjectFmtNamespace(Namespace):
inputs: list[Path]
stdout: bool
indent: int
check: bool

@property
def configs(self) -> list[Config]:
Expand All @@ -45,8 +46,11 @@ def pyproject_toml_path_creator(argument: str) -> Path:

def _build_cli() -> ArgumentParser:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
group = parser.add_mutually_exclusive_group()
msg = "print the formatted text to the stdout (instead of update in-place)"
parser.add_argument("-s", "--stdout", action="store_true", help=msg)
group.add_argument("-s", "--stdout", action="store_true", help=msg)
msg = "check and fail if any input would be formatted, printing any diffs"
group.add_argument("--check", action="store_true", help=msg)
parser.add_argument("--indent", type=int, default=DEFAULT_INDENT, help="number of spaces to indent")
msg = "pyproject.toml file(s) to format"
parser.add_argument("inputs", nargs="+", type=pyproject_toml_path_creator, help=msg)
Expand Down
23 changes: 21 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def no_color(diff: Any) -> Any:
False,
],
)
@pytest.mark.parametrize(
"check",
[
True,
False,
],
)
@pytest.mark.parametrize(
"cwd",
[
Expand Down Expand Up @@ -87,7 +94,8 @@ def test_main(
outcome: str,
output: str,
monkeypatch: pytest.MonkeyPatch,
cwd: Path,
cwd: bool,
check: bool,
) -> None:
monkeypatch.setattr(pyproject_fmt.__main__, "color_diff", no_color)
if cwd:
Expand All @@ -98,13 +106,24 @@ def test_main(
if not in_place:
args.append("--stdout")

if check:
args.append("--check")

if not in_place:
with pytest.raises(SystemExit):
run(args)
assert pyproject_toml.read_text() == start
return

result = run(args)
assert result == (0 if start == outcome else 1)

out, err = capsys.readouterr()
assert not err

if in_place:
if check:
assert pyproject_toml.read_text() == start
elif in_place:
name = "pyproject.toml" if cwd else str(tmp_path / "pyproject.toml")
output = output.format(name)
assert pyproject_toml.read_text() == outcome
Expand Down

0 comments on commit 94a0d12

Please sign in to comment.