Skip to content

Commit

Permalink
Merge pull request #181 from lyz-code/fix/dont_touch_unmodified_files
Browse files Browse the repository at this point in the history
fix: don't touch files that are not going to be changed
  • Loading branch information
lyz-code committed Jan 21, 2022
2 parents e1e641d + f8a468e commit 909d89c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/autoimport/services.py
Expand Up @@ -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 `<stdin>` it gives an
Expand Down
19 changes: 19 additions & 0 deletions 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
Expand Down Expand Up @@ -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

0 comments on commit 909d89c

Please sign in to comment.