diff --git a/AUTHORS b/AUTHORS index 95e6b13f11e..731f912178b 100644 --- a/AUTHORS +++ b/AUTHORS @@ -488,3 +488,4 @@ Zachary OBrien Zhouxin Qiu Zoltán Máté Zsolt Cserna +Johnny Huang diff --git a/changelog/13292.bugfix.rst b/changelog/13292.bugfix.rst new file mode 100644 index 00000000000..c60cfd53ac9 --- /dev/null +++ b/changelog/13292.bugfix.rst @@ -0,0 +1 @@ +Fixed stale pyc cache loading after rapid test file updates. diff --git a/src/_pytest/assertion/rewrite.py b/src/_pytest/assertion/rewrite.py index 2e606d1903a..c4f86300bd1 100644 --- a/src/_pytest/assertion/rewrite.py +++ b/src/_pytest/assertion/rewrite.py @@ -317,7 +317,7 @@ def _write_pyc_fp( flags = b"\x00\x00\x00\x00" fp.write(flags) # as of now, bytecode header expects 32-bit numbers for size and mtime (#4903) - mtime = int(source_stat.st_mtime) & 0xFFFFFFFF + mtime = source_stat.st_mtime_ns & 0xFFFFFFFF size = source_stat.st_size & 0xFFFFFFFF # " None: flags = b"\x00\x00\x00\x00" - mtime = b"\x58\x3c\xb0\x5f" + mtime = b"\x98\xf34\x10\x91\x8b,\x18" mtime_int = int.from_bytes(mtime, "little") - os.utime(source, (mtime_int, mtime_int)) + # set mtime_ns for win requires integer nanoseconds + os.utime(source, ns=(mtime_int, mtime_int)) + + mtime = (mtime_int & 0xFFFFFFFF).to_bytes(4, "little") size = len(source_bytes).to_bytes(4, "little") @@ -2374,3 +2377,30 @@ def test_saferepr_unbounded(self): _saferepr(self.Help) == f"" ) + + +class TestIssue13292: + """ + Check the pyc cache generated in the 1st test execution + is not loaded in the 2nd test execution. + """ + + def test_stale_pyc_cache_is_not_loaded(self, pytester: Pytester) -> None: + pytester.makepyfile(""" + def test_dummy1(): + def func(): + pass + print(func.__qualname__) + """) + r = pytester.runpytest("-s") + assert r.ret == 0 + assert "test_dummy1..func" in r.stdout.str() + pytester.makepyfile(""" + def test_dummy2(): + def func(): + pass + print(func.__qualname__) + """) + r = pytester.runpytest("-s") + assert r.ret == 0 + assert "test_dummy2..func" in r.stdout.str()