| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import subprocess |
| 12 |
|
| 13 |
BEFORE = """ |
| 14 |
from hypothesis.strategies import complex_numbers, complex_numbers as cn |
| 15 |
|
| 16 |
complex_numbers(min_magnitude=None) # simple call to fix |
| 17 |
complex_numbers(min_magnitude=None, max_magnitude=1) # plus arg after |
| 18 |
complex_numbers(allow_infinity=False, min_magnitude=None) # plus arg before |
| 19 |
cn(min_magnitude=None) # imported as alias |
| 20 |
""" |
| 21 |
AFTER = BEFORE.replace("None", "0") |
| 22 |
_unchanged = """ |
| 23 |
complex_numbers(min_magnitude=1) # value OK |
| 24 |
|
| 25 |
class Foo: |
| 26 |
def complex_numbers(self, **kw): pass |
| 27 |
|
| 28 |
complex_numbers(min_magnitude=None) # defined in a different scope |
| 29 |
""" |
| 30 |
BEFORE += _unchanged |
| 31 |
AFTER += _unchanged |
| 32 |
del _unchanged |
| 33 |
|
| 34 |
|
| 35 |
def run(command, *, cwd=None, input=None): |
| 36 |
return subprocess.run( |
| 37 |
command, |
| 38 |
input=input, |
| 39 |
capture_output=True, |
| 40 |
shell=True, |
| 41 |
text=True, |
| 42 |
cwd=cwd, |
| 43 |
encoding="utf-8", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def test_codemod_single_file(tmp_path): |
| 48 |
fname = tmp_path / "mycode.py" |
| 49 |
fname.write_text(BEFORE, encoding="utf-8") |
| 50 |
result = run("hypothesis codemod mycode.py", cwd=tmp_path) |
| 51 |
assert result.returncode == 0 |
| 52 |
assert fname.read_text(encoding="utf-8") == AFTER |
| 53 |
|
| 54 |
|
| 55 |
def test_codemod_multiple_files(tmp_path): |
| 56 |
# LibCST had some trouble with multiprocessing on Windows |
| 57 |
files = [tmp_path / "mycode1.py", tmp_path / "mycode2.py"] |
| 58 |
for f in files: |
| 59 |
f.write_text(BEFORE, encoding="utf-8") |
| 60 |
result = run("hypothesis codemod mycode1.py mycode2.py", cwd=tmp_path) |
| 61 |
assert result.returncode == 0 |
| 62 |
for f in files: |
| 63 |
assert f.read_text(encoding="utf-8") == AFTER |
| 64 |
|
| 65 |
|
| 66 |
def test_codemod_from_stdin(): |
| 67 |
result = run("hypothesis codemod -", input=BEFORE) |
| 68 |
assert result.returncode == 0 |
| 69 |
assert result.stdout.rstrip() == AFTER.rstrip() |