]> 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:

Fix handling of empty files
authorŁukasz Langa <lukasz@langa.pl>
Tue, 5 Jun 2018 02:04:29 +0000 (19:04 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Tue, 5 Jun 2018 02:04:29 +0000 (19:04 -0700)
black.py
tests/test_black.py

index 46c0907894d8a9b713f1899eb4dbdb818439f13b..c6b018ffd050431a4592ab5b15820cb610c986f3 100644 (file)
--- a/black.py
+++ b/black.py
@@ -607,6 +607,9 @@ def decode_bytes(src: bytes) -> Tuple[FileContent, Encoding, NewLine]:
     """
     srcbuf = io.BytesIO(src)
     encoding, lines = tokenize.detect_encoding(srcbuf.readline)
+    if not lines:
+        return "", encoding, "\n"
+
     newline = "\r\n" if b"\r\n" == lines[0][-2:] else "\n"
     srcbuf.seek(0)
     with io.TextIOWrapper(srcbuf, encoding) as tiow:
@@ -623,7 +626,7 @@ GRAMMARS = [
 def lib2to3_parse(src_txt: str) -> Node:
     """Given a string with source, return the lib2to3 Node."""
     grammar = pygram.python_grammar_no_print_statement
-    if src_txt[-1] != "\n":
+    if src_txt[-1:] != "\n":
         src_txt += "\n"
     for grammar in GRAMMARS:
         drv = driver.Driver(grammar, pytree.convert)
index f6952c721ccd0ba64f8c921e3dd10d2a75c1d6c1..e98f019e6808335e5176040e19cd8fcb945b64c0 100644 (file)
@@ -100,6 +100,25 @@ class BlackTestCase(unittest.TestCase):
                 black.err(str(ve))
         self.assertEqual(expected, actual)
 
+    @patch("black.dump_to_file", dump_to_stderr)
+    def test_empty(self) -> None:
+        source = expected = ""
+        actual = fs(source)
+        self.assertFormatEqual(expected, actual)
+        black.assert_equivalent(source, actual)
+        black.assert_stable(source, actual, line_length=ll)
+
+    def test_empty_ff(self) -> None:
+        expected = ""
+        tmp_file = Path(black.dump_to_file())
+        try:
+            self.assertFalse(ff(tmp_file, write_back=black.WriteBack.YES))
+            with open(tmp_file, encoding="utf8") as f:
+                actual = f.read()
+        finally:
+            os.unlink(tmp_file)
+        self.assertFormatEqual(expected, actual)
+
     @patch("black.dump_to_file", dump_to_stderr)
     def test_self(self) -> None:
         source, expected = read_data("test_black")