diff --git a/src/autocommand/autoparse.py b/src/autocommand/autoparse.py index 11c1e02..0276a3f 100644 --- a/src/autocommand/autoparse.py +++ b/src/autocommand/autoparse.py @@ -327,7 +327,7 @@ def work_with_file(name=sys.stdout): # If it was a filename, f is closed at the end here. ''' if isinstance(filename_or_file, (str, bytes, int)): - with open(filename_or_file) as file: + with open(filename_or_file, *args, **kwargs) as file: yield file else: yield filename_or_file diff --git a/test/test_autoparse/test_smart_open.py b/test/test_autoparse/test_smart_open.py index a101390..224c293 100644 --- a/test/test_autoparse/test_smart_open.py +++ b/test/test_autoparse/test_smart_open.py @@ -7,12 +7,35 @@ def test_smart_open_is_exported(): assert autocommand.smart_open is smart_open -def test_smart_open_path(tmpdir): - path = str(tmpdir.join('file.txt').ensure(file=True)) +def test_smart_open_path_read(tmpdir): + target = tmpdir.join('file.txt') + target.write("Hello") + + with smart_open(str(target)) as file: + assert not file.closed + assert file.read() == "Hello" + + assert file.closed + - with smart_open(path, 'w') as file: +def test_smart_open_path_write(tmpdir): + target = tmpdir.join('file.txt').ensure(file=True) + + with smart_open(str(target), 'w') as file: assert not file.closed + file.write("Test Content") # Tests that the file is writable + assert file.closed + assert target.read() == "Test Content" + + +def test_smart_open_path_create(tmpdir): + target = tmpdir.join("file.txt") + + with smart_open(str(target), 'w') as file: + file.write("Test Content") + + assert target.read() == "Test Content" def test_smart_open_file(tmpdir):