Skip to content

Commit

Permalink
Preserve file permissions also during reset.
Browse files Browse the repository at this point in the history
Fixes #6.
  • Loading branch information
udifuchs committed Apr 29, 2024
1 parent 6dd9b3c commit e2ab449
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions pylint_silent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def reset(py_filename: str, signature: str) -> None:
out_file.write(line)

if something_changed:
shutil.copymode(py_filename, out_filename)
os.rename(out_filename, py_filename)
else:
os.remove(out_filename)
Expand Down
Empty file modified tests/sAmple_1_after_apply.py
100644 → 100755
Empty file.
Empty file modified tests/sAmple_1_after_apply_w_signature.py
100644 → 100755
Empty file.
26 changes: 14 additions & 12 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def run_pylint_to_file(self, out_file: str) -> Optional[int]:
return self.run_pylint("--max-module-lines=10")


def assert_files_equal(file_1: str, file_2: str) -> None:
"""Check that both file are the same and have the same permissions."""
assert filecmp.cmp(file_1, file_2), f"diff {file_1} {file_2}"
f1_mode = os.stat(file_1).st_mode
f2_mode = os.stat(file_2).st_mode
assert f1_mode == f2_mode, f"diff mode {file_1}({f1_mode:o}) {file_2}({f2_mode:o})"


@pytest.fixture(name="ctx")
def fixture_ctx(tmpdir: str) -> Context:
"""Create context fixture for running tests."""
Expand Down Expand Up @@ -111,8 +119,7 @@ def test_apply(ctx: Context) -> None:
run_pylint_silent("apply", "--max-line-length=70", pylint_output)

# Test that the expected python file was generated.
assert filecmp.cmp(ctx.temp_sample_filename, ctx.sample_after_apply), \
f"diff {ctx.temp_sample_filename} {ctx.sample_after_apply}"
assert_files_equal(ctx.temp_sample_filename, ctx.sample_after_apply)

# Test that pylint is indeed silent now.
exitcode = ctx.run_pylint("--disable=duplicate-code")
Expand All @@ -129,8 +136,7 @@ def test_apply_signature(ctx: Context) -> None:
run_pylint_silent("apply", "--signature", pylint_output)

# Test that the expected python file was generated.
assert filecmp.cmp(ctx.temp_sample_filename, ctx.sample_after_apply_w_sig), \
f"diff {ctx.temp_sample_filename} {ctx.sample_after_apply_w_sig}"
assert_files_equal(ctx.temp_sample_filename, ctx.sample_after_apply_w_sig)

# Test that pylint is indeed silent now.
exitcode = ctx.run_pylint("--disable=duplicate-code")
Expand Down Expand Up @@ -176,20 +182,17 @@ def test_reset(ctx: Context) -> None:
"""
run_pylint_silent("reset", ctx.temp_sample_after_apply)

assert filecmp.cmp(ctx.temp_sample_after_apply, ctx.sample_filename), \
f"diff {ctx.temp_sample_after_apply} {ctx.sample_filename}"
assert_files_equal(ctx.temp_sample_after_apply, ctx.sample_filename)

# Test resetting a clean file.
run_pylint_silent("reset", ctx.temp_sample_after_apply)

assert filecmp.cmp(ctx.temp_sample_after_apply, ctx.sample_filename), \
f"diff {ctx.temp_sample_filename} {ctx.sample_filename}"
assert_files_equal(ctx.temp_sample_after_apply, ctx.sample_filename)

# Test resetting a file with signature.
run_pylint_silent("reset", "--signature", ctx.temp_sample_after_apply_w_sig)

assert filecmp.cmp(ctx.temp_sample_after_apply_w_sig, ctx.sample_filename), \
f"diff {ctx.temp_sample_filename} {ctx.sample_filename}"
assert_files_equal(ctx.temp_sample_after_apply_w_sig, ctx.sample_filename)


def test_reset_sample2(ctx: Context) -> None:
Expand All @@ -199,8 +202,7 @@ def test_reset_sample2(ctx: Context) -> None:
"""
run_pylint_silent("reset", ctx.temp_sample2_filename)

assert filecmp.cmp(ctx.sample2_after_reset, ctx.temp_sample2_filename), \
f"diff {ctx.sample2_after_reset} {ctx.temp_sample2_filename}"
assert_files_equal(ctx.sample2_after_reset, ctx.temp_sample2_filename)

# Test resetting a file without signatures but with --signature (should fail)
run_pylint_silent("reset", "--signature", ctx.temp_sample2_again_filename)
Expand Down

0 comments on commit e2ab449

Please sign in to comment.