Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't touch files that are not going to be changed #181

Merged
merged 1 commit into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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