|
| 1 | +import pytest |
| 2 | + |
| 3 | +from thefuck.rules.python_module_error import get_new_command, match |
| 4 | +from thefuck.types import Command |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def module_error_output(filename, module_name): |
| 9 | + return """Traceback (most recent call last): |
| 10 | + File "{0}", line 1, in <module> |
| 11 | + import {1} |
| 12 | +ModuleNotFoundError: No module named '{1}'""".format( |
| 13 | + filename, module_name |
| 14 | + ) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "test", |
| 19 | + [ |
| 20 | + Command("python hello_world.py", "Hello World"), |
| 21 | + Command( |
| 22 | + "./hello_world.py", |
| 23 | + """Traceback (most recent call last): |
| 24 | + File "hello_world.py", line 1, in <module> |
| 25 | + pritn("Hello World") |
| 26 | +NameError: name 'pritn' is not defined""", |
| 27 | + ), |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_not_match(test): |
| 31 | + assert not match(test) |
| 32 | + |
| 33 | + |
| 34 | +positive_tests = [ |
| 35 | + ( |
| 36 | + "python some_script.py", |
| 37 | + "some_script.py", |
| 38 | + "more_itertools", |
| 39 | + "pip install more_itertools && python some_script.py", |
| 40 | + ), |
| 41 | + ( |
| 42 | + "./some_other_script.py", |
| 43 | + "some_other_script.py", |
| 44 | + "a_module", |
| 45 | + "pip install a_module && ./some_other_script.py", |
| 46 | + ), |
| 47 | +] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "script, filename, module_name, corrected_script", positive_tests |
| 52 | +) |
| 53 | +def test_match(script, filename, module_name, corrected_script, module_error_output): |
| 54 | + assert match(Command(script, module_error_output)) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "script, filename, module_name, corrected_script", positive_tests |
| 59 | +) |
| 60 | +def test_get_new_command( |
| 61 | + script, filename, module_name, corrected_script, module_error_output |
| 62 | +): |
| 63 | + assert get_new_command(Command(script, module_error_output)) == corrected_script |
0 commit comments