]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Only return exit code 1 when --check is used
authorŁukasz Langa <lukasz@langa.pl>
Fri, 23 Mar 2018 00:33:44 +0000 (17:33 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Fri, 23 Mar 2018 00:33:44 +0000 (17:33 -0700)
Also, output less confusing messages in --check.

Fixes #50

README.md
black.py
tests/test_black.py

index 6ec8dd8d79de45dfe6acb38e6489b86054aa90e8..951948fe89c397100e208ad398c34a42c4955a0e 100644 (file)
--- a/README.md
+++ b/README.md
@@ -44,10 +44,10 @@ black [OPTIONS] [SRC]...
 Options:
   -l, --line-length INTEGER   Where to wrap around.  [default: 88]
   --check                     Don't write back the files, just return the
 Options:
   -l, --line-length INTEGER   Where to wrap around.  [default: 88]
   --check                     Don't write back the files, just return the
-                              status.  Return code 0 means nothing changed.
-                              Return code 1 means some files were reformatted.
-                              Return code 123 means there was an internal
-                              error.
+                              status.  Return code 0 means nothing would
+                              change.  Return code 1 means some files would be
+                              reformatted.  Return code 123 means there was an
+                              internal error.
   --fast / --safe             If --fast given, skip temporary sanity checks.
                               [default: --safe]
   --version                   Show the version and exit.
   --fast / --safe             If --fast given, skip temporary sanity checks.
                               [default: --safe]
   --version                   Show the version and exit.
@@ -58,7 +58,9 @@ Options:
 * it does nothing if no sources are passed to it;
 * it will read from standard input and write to standard output if `-`
   is used as the filename;
 * it does nothing if no sources are passed to it;
 * it will read from standard input and write to standard output if `-`
   is used as the filename;
-* it only outputs messages to users on standard error.
+* it only outputs messages to users on standard error;
+* exits with code 0 unless an internal error occured (or `--check` was
+  used).
 
 
 ## The philosophy behind *Black*
 
 
 ## The philosophy behind *Black*
@@ -307,6 +309,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 
 ### 18.3a4 (unreleased)
 
 
 ### 18.3a4 (unreleased)
 
+* only return exit code 1 when --check is used (#50)
+
 * don't remove single trailing commas from square bracket indexing
   (#59)
 
 * don't remove single trailing commas from square bracket indexing
   (#59)
 
index 950eac79e8c8f41c6d672183cfbf717ff10fccdc..bb8ec2e57a074938bff931076db5f0b80dd49304 100644 (file)
--- a/black.py
+++ b/black.py
@@ -62,7 +62,7 @@ class CannotSplit(Exception):
     is_flag=True,
     help=(
         "Don't write back the files, just return the status.  Return code 0 "
     is_flag=True,
     help=(
         "Don't write back the files, just return the status.  Return code 0 "
-        "means nothing changed.  Return code 1 means some files were "
+        "means nothing would change.  Return code 1 means some files would be "
         "reformatted.  Return code 123 means there was an internal error."
     ),
 )
         "reformatted.  Return code 123 means there was an internal error."
     ),
 )
@@ -100,7 +100,7 @@ def main(
         ctx.exit(0)
     elif len(sources) == 1:
         p = sources[0]
         ctx.exit(0)
     elif len(sources) == 1:
         p = sources[0]
-        report = Report()
+        report = Report(check=check)
         try:
             if not p.is_file() and str(p) == '-':
                 changed = format_stdin_to_stdout(
         try:
             if not p.is_file() and str(p) == '-':
                 changed = format_stdin_to_stdout(
@@ -1438,6 +1438,7 @@ def gen_python_files_in_dir(path: Path) -> Iterator[Path]:
 @dataclass
 class Report:
     """Provides a reformatting counter."""
 @dataclass
 class Report:
     """Provides a reformatting counter."""
+    check: bool = False
     change_count: int = 0
     same_count: int = 0
     failure_count: int = 0
     change_count: int = 0
     same_count: int = 0
     failure_count: int = 0
@@ -1445,7 +1446,8 @@ class Report:
     def done(self, src: Path, changed: bool) -> None:
         """Increment the counter for successful reformatting. Write out a message."""
         if changed:
     def done(self, src: Path, changed: bool) -> None:
         """Increment the counter for successful reformatting. Write out a message."""
         if changed:
-            out(f'reformatted {src}')
+            reformatted = 'would reformat' if self.check else 'reformatted'
+            out(f'{reformatted} {src}')
             self.change_count += 1
         else:
             out(f'{src} already well formatted, good job.', bold=False)
             self.change_count += 1
         else:
             out(f'{src} already well formatted, good job.', bold=False)
@@ -1464,7 +1466,7 @@ class Report:
         if self.failure_count:
             return 123
 
         if self.failure_count:
             return 123
 
-        elif self.change_count:
+        elif self.change_count and self.check:
             return 1
 
         return 0
             return 1
 
         return 0
@@ -1474,21 +1476,27 @@ class Report:
 
         Use `click.unstyle` to remove colors.
         """
 
         Use `click.unstyle` to remove colors.
         """
+        if self.check:
+            reformatted = "would be reformatted"
+            unchanged = "would be left unchanged"
+            failed = "would fail to reformat"
+        else:
+            reformatted = "reformatted"
+            unchanged = "left unchanged"
+            failed = "failed to reformat"
         report = []
         if self.change_count:
             s = 's' if self.change_count > 1 else ''
             report.append(
         report = []
         if self.change_count:
             s = 's' if self.change_count > 1 else ''
             report.append(
-                click.style(f'{self.change_count} file{s} reformatted', bold=True)
+                click.style(f'{self.change_count} file{s} {reformatted}', bold=True)
             )
         if self.same_count:
             s = 's' if self.same_count > 1 else ''
             )
         if self.same_count:
             s = 's' if self.same_count > 1 else ''
-            report.append(f'{self.same_count} file{s} left unchanged')
+            report.append(f'{self.same_count} file{s} {unchanged}')
         if self.failure_count:
             s = 's' if self.failure_count > 1 else ''
             report.append(
         if self.failure_count:
             s = 's' if self.failure_count > 1 else ''
             report.append(
-                click.style(
-                    f'{self.failure_count} file{s} failed to reformat', fg='red'
-                )
+                click.style(f'{self.failure_count} file{s} {failed}', fg='red')
             )
         return ', '.join(report) + '.'
 
             )
         return ', '.join(report) + '.'
 
index ec7a883b5b3291054b500f6f938c5595c68c3910..69746d12f0c117def22796f4050eef55d47304f7 100644 (file)
@@ -205,7 +205,10 @@ class BlackTestCase(unittest.TestCase):
             self.assertEqual(
                 unstyle(str(report)), '1 file reformatted, 1 file left unchanged.'
             )
             self.assertEqual(
                 unstyle(str(report)), '1 file reformatted, 1 file left unchanged.'
             )
+            self.assertEqual(report.return_code, 0)
+            report.check = True
             self.assertEqual(report.return_code, 1)
             self.assertEqual(report.return_code, 1)
+            report.check = False
             report.failed(Path('e1'), 'boom')
             self.assertEqual(len(out_lines), 2)
             self.assertEqual(len(err_lines), 1)
             report.failed(Path('e1'), 'boom')
             self.assertEqual(len(out_lines), 2)
             self.assertEqual(len(err_lines), 1)
@@ -246,6 +249,12 @@ class BlackTestCase(unittest.TestCase):
                 '2 files failed to reformat.',
             )
             self.assertEqual(report.return_code, 123)
                 '2 files failed to reformat.',
             )
             self.assertEqual(report.return_code, 123)
+            report.check = True
+            self.assertEqual(
+                unstyle(str(report)),
+                '2 files would be reformatted, 2 files would be left unchanged, '
+                '2 files would fail to reformat.',
+            )
 
     def test_is_python36(self) -> None:
         node = black.lib2to3_parse("def f(*, arg): ...\n")
 
     def test_is_python36(self) -> None:
         node = black.lib2to3_parse("def f(*, arg): ...\n")