Skip to content

Commit

Permalink
Only write file if there have been changes.
Browse files Browse the repository at this point in the history
This avoids unnecessary changes to the mtime
  • Loading branch information
domdfcoding committed Jun 22, 2021
1 parent 642c6d6 commit 6501551
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 1 addition & 2 deletions formate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ def reformat_file(

if ret:
click.echo(r.get_diff(), color=resolve_color_default(colour))

r.to_file()
r.to_file()

return ret
20 changes: 20 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,26 @@ def test_integration(

config = load_toml(tmp_pathplus / "formate.toml")

st = (tmp_pathplus / "code.py").stat()
assert st == st

assert reformat_file(tmp_pathplus / "code.py", config) == 1
advanced_file_regression.check_file(tmp_pathplus / "code.py")
check_out(capsys.readouterr(), advanced_data_regression)

# mtime should have changed
new_st = (tmp_pathplus / "code.py").stat()
assert new_st.st_mtime != st.st_mtime
assert new_st != st

# Calling a second time shouldn't change anything
assert reformat_file(tmp_pathplus / "code.py", config) == 0
advanced_file_regression.check_file(tmp_pathplus / "code.py")

# mtime should be the same
assert (tmp_pathplus / "code.py").stat().st_mtime == new_st.st_mtime
assert (tmp_pathplus / "code.py").stat() == new_st


def test_integration_pyproject(
tmp_pathplus: PathPlus,
Expand Down Expand Up @@ -128,6 +140,9 @@ def test_reformatter_class(
with pytest.raises(ValueError, match=r"'Reformatter.run\(\)' must be called first!"):
r.get_diff()

st = (tmp_pathplus / "code.py").stat()
assert st == st

assert r.run() == 1
r.to_file()

Expand All @@ -139,6 +154,11 @@ def test_reformatter_class(
assert not captured.out
assert not captured.err

# mtime should have changed
new_st = (tmp_pathplus / "code.py").stat()
assert new_st.st_mtime != st.st_mtime
assert new_st != st

# Calling a second time shouldn't change anything
r = Reformatter(tmp_pathplus / "code.py", config)
assert r.run() == 0
Expand Down

0 comments on commit 6501551

Please sign in to comment.