Use conditional case for diff reports (#1226)

When --diff flag is used, black will now use the
conditional case in the Report output: eg "would
be reformatted"
This commit is contained in:
kyle hausmann 2020-01-18 10:13:15 -05:00 committed by Jelle Zijlstra
parent 9ef8e6c9b0
commit a02829bea1
2 changed files with 25 additions and 3 deletions

View File

@ -442,7 +442,7 @@ def main(
except re.error: except re.error:
err(f"Invalid regular expression for exclude given: {exclude!r}") err(f"Invalid regular expression for exclude given: {exclude!r}")
ctx.exit(2) ctx.exit(2)
report = Report(check=check, quiet=quiet, verbose=verbose) report = Report(check=check, diff=diff, quiet=quiet, verbose=verbose)
root = find_project_root(src) root = find_project_root(src)
sources: Set[Path] = set() sources: Set[Path] = set()
path_empty(src, quiet, verbose, ctx) path_empty(src, quiet, verbose, ctx)
@ -3614,6 +3614,7 @@ class Report:
"""Provides a reformatting counter. Can be rendered with `str(report)`.""" """Provides a reformatting counter. Can be rendered with `str(report)`."""
check: bool = False check: bool = False
diff: bool = False
quiet: bool = False quiet: bool = False
verbose: bool = False verbose: bool = False
change_count: int = 0 change_count: int = 0
@ -3623,7 +3624,7 @@ class Report:
def done(self, src: Path, changed: Changed) -> None: def done(self, src: Path, changed: Changed) -> None:
"""Increment the counter for successful reformatting. Write out a message.""" """Increment the counter for successful reformatting. Write out a message."""
if changed is Changed.YES: if changed is Changed.YES:
reformatted = "would reformat" if self.check else "reformatted" reformatted = "would reformat" if self.check or self.diff else "reformatted"
if self.verbose or not self.quiet: if self.verbose or not self.quiet:
out(f"{reformatted} {src}") out(f"{reformatted} {src}")
self.change_count += 1 self.change_count += 1
@ -3669,7 +3670,7 @@ def __str__(self) -> str:
Use `click.unstyle` to remove colors. Use `click.unstyle` to remove colors.
""" """
if self.check: if self.check or self.diff:
reformatted = "would be reformatted" reformatted = "would be reformatted"
unchanged = "would be left unchanged" unchanged = "would be left unchanged"
failed = "would fail to reformat" failed = "would fail to reformat"

View File

@ -778,6 +778,13 @@ def err(msg: str, **kwargs: Any) -> None:
"2 files would be reformatted, 3 files would be left unchanged, " "2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.", "2 files would fail to reformat.",
) )
report.check = False
report.diff = True
self.assertEqual(
unstyle(str(report)),
"2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.",
)
def test_report_quiet(self) -> None: def test_report_quiet(self) -> None:
report = black.Report(quiet=True) report = black.Report(quiet=True)
@ -865,6 +872,13 @@ def err(msg: str, **kwargs: Any) -> None:
"2 files would be reformatted, 3 files would be left unchanged, " "2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.", "2 files would fail to reformat.",
) )
report.check = False
report.diff = True
self.assertEqual(
unstyle(str(report)),
"2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.",
)
def test_report_normal(self) -> None: def test_report_normal(self) -> None:
report = black.Report() report = black.Report()
@ -955,6 +969,13 @@ def err(msg: str, **kwargs: Any) -> None:
"2 files would be reformatted, 3 files would be left unchanged, " "2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.", "2 files would fail to reformat.",
) )
report.check = False
report.diff = True
self.assertEqual(
unstyle(str(report)),
"2 files would be reformatted, 3 files would be left unchanged, "
"2 files would fail to reformat.",
)
def test_lib2to3_parse(self) -> None: def test_lib2to3_parse(self) -> None:
with self.assertRaises(black.InvalidInput): with self.assertRaises(black.InvalidInput):