diff --git a/src/autoimport/services.py b/src/autoimport/services.py index bedd249..8ea9e11 100644 --- a/src/autoimport/services.py +++ b/src/autoimport/services.py @@ -28,6 +28,9 @@ def fix_files( source = file_wrapper.read() fixed_source = fix_code(source, config) + if fixed_source == source: + continue + try: # Click testing runner doesn't simulate correctly the reading from stdin # instead of setting the name attribute to `` it gives an diff --git a/tests/e2e/test_cli.py b/tests/e2e/test_cli.py index efbe599..e999567 100644 --- a/tests/e2e/test_cli.py +++ b/tests/e2e/test_cli.py @@ -1,5 +1,6 @@ """Test the command line interface.""" +import os import re from pathlib import Path from textwrap import dedent @@ -154,3 +155,21 @@ def test_config_path_argument(runner: CliRunner, tmpdir: LocalPath) -> None: assert result.exit_code == 0 assert test_file.read() == PYPROJECT_CONFIG_FIXED_SOURCE + + +def test_fix_files_doesnt_touch_the_file_if_its_not_going_to_change_it( + runner: CliRunner, tmpdir: LocalPath +) -> None: + """ + Given: A file that doesn't need any change + When: fix files is run + Then: The file is untouched + """ + test_file = tmpdir / "source.py" + test_file.write("a = 1") + modified_time = os.path.getmtime(test_file) + + result = runner.invoke(cli, [str(test_file)]) + + assert result.exit_code == 0 + assert os.path.getmtime(test_file) == modified_time