Skip to content

Commit

Permalink
Fix: outputting diff when both check and diff options are used. (#147)
Browse files Browse the repository at this point in the history
* Fix: outputting diff when both check and diff options are used

* Add: test case.

* Add: checklog
  • Loading branch information
hadialqattan authored Jul 3, 2022
1 parent 63b79e4 commit 542147d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Fixed

- [outputting code diff when both `--check` and `--diff` options are used by @hadialqattan](https://github.com/hadialqattan/pycln/pull/147)

## [1.3.5] - 2022-06-23

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions pycln/utils/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _output(
self.reporter.unchanged_file(self._path)
else:
self.reporter.changed_file(self._path)
if not self.configs.check:
if not self.configs.check or self.configs.diff:
fixed_lines = Refactor.remove_useless_passes(fixed_lines)
if self.configs.diff:
self.reporter.colored_unified_diff(
Expand Down Expand Up @@ -294,7 +294,7 @@ def _refactor(self, original_lines: List[str]) -> str:
continue

# Depends on `--check, -c` option.
if self.configs.check:
if self.configs.check and not self.configs.diff:
fixed_lines.append(CHANGE_MARK)
continue

Expand Down
18 changes: 10 additions & 8 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,42 +344,44 @@ def test_code_session(
assert fixed_code == expec_fixed_code

@pytest.mark.parametrize(
"fixed_lines, original_lines, mode, expec_output",
"fixed_lines, original_lines, modes, expec_output",
[
pytest.param(
["code..\n"], ["code..\n"], "verbose", "looks good!", id="unchanged"
["code..\n"], ["code..\n"], ["verbose"], "looks good!", id="unchanged"
),
pytest.param(
["fixed.code..\n"],
["original.code..\n"],
"check",
["check"],
"🚀",
id="changed-check",
),
pytest.param(
["import x\n"],
["import x, y\n"],
"diff",
["diff"],
"-import x, y\n+import x\n",
id="changed-diff",
),
pytest.param(
["import x\n"],
["import x, y\n"],
"diff",
["diff", "check"],
"-import x, y\n+import x\n",
id="changed-diff",
id="changed-[diff & check]",
),
],
)
@mock.patch(MOCK % "Refactor.remove_useless_passes")
def test_output(
self, remove_useless_passes, fixed_lines, original_lines, mode, expec_output
self, remove_useless_passes, fixed_lines, original_lines, modes, expec_output
):
remove_useless_passes.return_value = fixed_lines
setattr(self.configs, mode, True)
for mode in modes:
setattr(self.configs, mode, True)
with sysu.std_redirect(sysu.STD.OUT) as stdout:
self.session_maker._output(fixed_lines, original_lines, "utf-8", "\n")
assert expec_output, "Expected output mustn't be empty str."
assert expec_output in stdout.getvalue()

@mock.patch(MOCK % "Refactor.remove_useless_passes")
Expand Down

0 comments on commit 542147d

Please sign in to comment.