Skip to content

test_write_text_atomic_preserves_existing_mode asserts POSIX mode bits that Windows cannot represent #2622

Closed

Description

@rajarshidattapy

Labels: bug, tests, windows, good first issue
Version: 0.9.38 (commit 10ad921)

Summary

The test sets 0o644 on a file and asserts the atomic write preserves it. Windows does not
implement POSIX permission bits — os.chmod only toggles the read-only attribute, and
os.stat().st_mode reports 0o666 for any writable file. The assertion can never hold.

Steps to reproduce

$ pytest tests/test_atomic_writes.py::test_write_text_atomic_preserves_existing_mode -q

Actual behavior

    def test_write_text_atomic_preserves_existing_mode(tmp_path):
        # An atomic replace must not tighten a 0644 file to mkstemp's 0600 default.
        p = tmp_path / "graph.json"
        p.write_text("{}", encoding="utf-8")
        os.chmod(p, 0o644)
        write_text_atomic(p, '{"x": 1}')
>       assert (os.stat(p).st_mode & 0o777) == 0o644
E       AssertionError: assert (33206 & 511) == 420

33206 is 0o100666, so st_mode & 0o777 is 0o666, not 0o644. The os.chmod(p, 0o644)
on line 4 did not do what the test assumes — on Windows it is a no-op for anything other
than the read-only bit.

Expected behavior

Skipped on Windows. The property under test — "an atomic replace must not tighten a 0644 file
to mkstemp's 0600 default" — is a POSIX property with no Windows equivalent.

Suggested fix

@pytest.mark.skipif(
    os.name == "nt",
    reason="Windows has no POSIX mode bits; chmod only toggles the read-only attribute",
)
def test_write_text_atomic_preserves_existing_mode(tmp_path):
    ...

If Windows coverage of write_text_atomic is wanted, the meaningful analogue is the
read-only attribute surviving a replace:

@pytest.mark.skipif(os.name != "nt", reason="Windows-specific attribute check")
def test_write_text_atomic_preserves_readonly_attribute(tmp_path):
    p = tmp_path / "graph.json"
    p.write_text("{}", encoding="utf-8")
    os.chmod(p, stat.S_IREAD)
    write_text_atomic(p, '{"x": 1}')
    assert not (os.stat(p).st_mode & stat.S_IWRITE)

Activity

  1. safishamsi commented on Aug 13, 2026

    @safishamsi
    Member

    Fixed in v0.9.42 (graphifyy==0.9.42 on PyPI). The POSIX-mode assertion is skipped on Windows with a Windows analogue added, and a failed atomic write no longer leaks a read-only temp. Thanks @rajarshidattapy (PR #2623).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions