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