Anthony Sottile | 8f61529 | 2022-01-15 19:24:05 -0500 | [diff] [blame] | 1 | from __future__ import annotations |
| 2 | |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 3 | import textwrap |
| 4 | |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 5 | import pytest |
| 6 | |
| 7 | from pre_commit_hooks.string_fixer import main |
| 8 | |
| 9 | TESTS = ( |
| 10 | # Base cases |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 11 | ("''", "''", 0), |
| 12 | ('""', "''", 1), |
| 13 | (r'"\'"', r'"\'"', 0), |
| 14 | (r'"\""', r'"\""', 0), |
| 15 | (r"'\"\"'", r"'\"\"'", 0), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 16 | # String somewhere in the line |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 17 | ('x = "foo"', "x = 'foo'", 1), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 18 | # Test escaped characters |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 19 | (r'"\'"', r'"\'"', 0), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 20 | # Docstring |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 21 | ('""" Foo """', '""" Foo """', 0), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 22 | ( |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 23 | textwrap.dedent( |
| 24 | """ |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 25 | x = " \\ |
| 26 | foo \\ |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 27 | "\n |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 28 | """, |
| 29 | ), |
| 30 | textwrap.dedent( |
| 31 | """ |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 32 | x = ' \\ |
| 33 | foo \\ |
| 34 | '\n |
Anthony Sottile | e9aea74 | 2017-07-15 12:56:51 -0700 | [diff] [blame] | 35 | """, |
| 36 | ), |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 37 | 1, |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 38 | ), |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 39 | ('"foo""bar"', "'foo''bar'", 1), |
Anthony Sottile | f27ee31 | 2023-10-07 13:50:33 -0400 | [diff] [blame] | 40 | pytest.param( |
| 41 | "f'hello{\"world\"}'", |
| 42 | "f'hello{\"world\"}'", |
| 43 | 0, |
| 44 | id='ignore nested fstrings', |
| 45 | ), |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 46 | ) |
| 47 | |
| 48 | |
Anthony Sottile | 2983d44 | 2015-02-10 08:14:53 -0800 | [diff] [blame] | 49 | @pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS) |
| 50 | def test_rewrite(input_s, output, expected_retval, tmpdir): |
Anthony Sottile | 711b730 | 2019-05-16 09:42:04 -0700 | [diff] [blame] | 51 | path = tmpdir.join('file.py') |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 52 | path.write(input_s) |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 53 | retval = main([str(path)]) |
Anthony Sottile | a99475a | 2016-05-27 14:09:50 -0700 | [diff] [blame] | 54 | assert path.read() == output |
Ken Struys | 12f02df | 2015-02-05 19:58:20 -0800 | [diff] [blame] | 55 | assert retval == expected_retval |
Anthony Sottile | 711b730 | 2019-05-16 09:42:04 -0700 | [diff] [blame] | 56 | |
| 57 | |
| 58 | def test_rewrite_crlf(tmpdir): |
| 59 | f = tmpdir.join('f.py') |
| 60 | f.write_binary(b'"foo"\r\n"bar"\r\n') |
Max Rozentsveyg | f35bfed | 2020-05-20 12:07:45 -0400 | [diff] [blame] | 61 | assert main((str(f),)) |
Anthony Sottile | 711b730 | 2019-05-16 09:42:04 -0700 | [diff] [blame] | 62 | assert f.read_binary() == b"'foo'\r\n'bar'\r\n" |