|
| 1 | +import logging |
| 2 | + |
| 3 | +from codemodder.codemods.regex_transformer import RegexTransformerPipeline |
| 4 | +from codemodder.context import CodemodExecutionContext |
| 5 | +from codemodder.file_context import FileContext |
| 6 | + |
| 7 | + |
| 8 | +def test_transformer_no_change(mocker, caplog, tmp_path_factory): |
| 9 | + caplog.set_level(logging.DEBUG) |
| 10 | + base_dir = tmp_path_factory.mktemp("foo") |
| 11 | + code = base_dir / "code.py" |
| 12 | + code.write_text("# Something that won't match") |
| 13 | + |
| 14 | + file_context = FileContext( |
| 15 | + base_dir, |
| 16 | + code, |
| 17 | + ) |
| 18 | + execution_context = CodemodExecutionContext( |
| 19 | + directory=base_dir, |
| 20 | + dry_run=True, |
| 21 | + verbose=False, |
| 22 | + registry=mocker.MagicMock(), |
| 23 | + providers=None, |
| 24 | + repo_manager=mocker.MagicMock(), |
| 25 | + path_include=[], |
| 26 | + path_exclude=[], |
| 27 | + ) |
| 28 | + pipeline = RegexTransformerPipeline( |
| 29 | + pattern=r"hello", replacement="bye", change_description="testing" |
| 30 | + ) |
| 31 | + |
| 32 | + changeset = pipeline.apply( |
| 33 | + context=execution_context, |
| 34 | + file_context=file_context, |
| 35 | + results=None, |
| 36 | + ) |
| 37 | + assert changeset is None |
| 38 | + assert "No changes produced for" in caplog.text |
| 39 | + |
| 40 | + |
| 41 | +def test_transformer(mocker, tmp_path_factory): |
| 42 | + base_dir = tmp_path_factory.mktemp("foo") |
| 43 | + code = base_dir / "code.py" |
| 44 | + text = "# Something that will match pattern hello" |
| 45 | + code.write_text(text) |
| 46 | + |
| 47 | + file_context = FileContext( |
| 48 | + base_dir, |
| 49 | + code, |
| 50 | + ) |
| 51 | + execution_context = CodemodExecutionContext( |
| 52 | + directory=base_dir, |
| 53 | + dry_run=False, |
| 54 | + verbose=False, |
| 55 | + registry=mocker.MagicMock(), |
| 56 | + providers=None, |
| 57 | + repo_manager=mocker.MagicMock(), |
| 58 | + path_include=[], |
| 59 | + path_exclude=[], |
| 60 | + ) |
| 61 | + pipeline = RegexTransformerPipeline( |
| 62 | + pattern=r"hello", replacement="bye", change_description="testing" |
| 63 | + ) |
| 64 | + |
| 65 | + changeset = pipeline.apply( |
| 66 | + context=execution_context, |
| 67 | + file_context=file_context, |
| 68 | + results=None, |
| 69 | + ) |
| 70 | + assert changeset is not None |
| 71 | + assert code.read_text() == text.replace("hello", "bye") |
0 commit comments