Skip to content

Commit af8e0ac

Browse files
authored
Update test_dirfs.py - DirFileSystem transaction context propagation
This test verifies that the fix for issue fsspec#1823 works correctly by ensuring that DirFileSystem properly propagates its transaction context to the underlying filesystem. When a transaction is started on a DirFileSystem, both the wrapper and the wrapped filesystem should have their _intrans flags set, allowing writes to be deferred until the transaction is committed. Test confirms that: - Both filesystems have _intrans=True inside transaction - Files do not appear on disk until transaction commits - Files appear correctly after transaction commit
1 parent 96df6ff commit af8e0ac

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

fsspec/implementations/tests/test_dirfs.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import os
12
import pytest
23

4+
from fsspec.implementations.local import LocalFileSystem
35
from fsspec.asyn import AsyncFileSystem
46
from fsspec.implementations.dirfs import DirFileSystem
57
from fsspec.spec import AbstractFileSystem
@@ -615,3 +617,38 @@ def test_from_url(m):
615617
assert fs.ls("", False) == ["file"]
616618
assert fs.ls("", True)[0]["name"] == "file"
617619
assert fs.cat("file") == b"data"
620+
621+
622+
def test_dirfs_transaction_propagation(tmpdir):
623+
# Setup
624+
path = str(tmpdir)
625+
base = LocalFileSystem()
626+
fs = DirFileSystem(path=path, fs=base)
627+
628+
# Test file path
629+
test_file = "transaction_test.txt"
630+
full_path = os.path.join(path, test_file)
631+
632+
# Before transaction, both filesystems should have _intrans=False
633+
assert not base._intrans
634+
assert not fs._intrans
635+
636+
# Enter transaction and write file
637+
with fs.transaction:
638+
# After fix, both filesystems should have _intrans=True
639+
assert base._intrans, "Base filesystem transaction flag not set"
640+
assert fs._intrans, "DirFileSystem transaction flag not set"
641+
642+
# Write to file
643+
with fs.open(test_file, "wb") as f:
644+
f.write(b"hello world")
645+
646+
# Check if file exists on disk - it should not until transaction commits
647+
assert not os.path.exists(full_path), "File exists during transaction, not deferred to temp file"
648+
649+
# After transaction commits, file should exist
650+
assert os.path.exists(full_path), "File not created after transaction commit"
651+
652+
# Verify content
653+
with open(full_path, "rb") as f:
654+
assert f.read() == b"hello world"

0 commit comments

Comments
 (0)