Skip to content

Commit f7fc608

Browse files
authored
small filesystem fixes (#34956)
* Fix typehint for get_filesystem * Fix bug with creating files in cwd with LocalFileSystem * lint * revert precommit config change * isort * add file to start PR builder * Revert "add file to start PR builder" This reverts commit f80b361. * fix isort again
1 parent b4e5e39 commit f7fc608

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

sdks/python/apache_beam/io/filesystems.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def get_scheme(path):
125125

126126
@staticmethod
127127
def get_filesystem(path):
128-
# type: (str) -> FileSystems
128+
# type: (str) -> FileSystem
129129

130130
"""Get the correct filesystem for the specified path
131131
"""

sdks/python/apache_beam/io/localfilesystem.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ def create(
157157
158158
Returns: file handle with a close function for the user to use
159159
"""
160-
os.makedirs(os.path.dirname(path), exist_ok=True)
160+
dirname = os.path.dirname(path)
161+
if dirname:
162+
os.makedirs(os.path.dirname(path), exist_ok=True)
161163
return self._path_open(path, 'wb', mime_type, compression_type)
162164

163165
def open(

sdks/python/apache_beam/io/localfilesystem_test.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
#
1818

1919
"""Unit tests for LocalFileSystem."""
20-
21-
# pytype: skip-file
22-
20+
import contextlib
2321
import filecmp
2422
import logging
2523
import os
@@ -35,6 +33,8 @@
3533
from apache_beam.io.filesystem import BeamIOError
3634
from apache_beam.options.pipeline_options import PipelineOptions
3735

36+
# pytype: skip-file
37+
3838

3939
def _gen_fake_join(separator):
4040
"""Returns a callable that joins paths with the given separator."""
@@ -65,10 +65,27 @@ def setUp(self):
6565
def tearDown(self):
6666
shutil.rmtree(self.tmpdir)
6767

68+
@contextlib.contextmanager
69+
def tmpdir_as_cwd(self):
70+
"""Context manager that sets the current working directory to a temp dir."""
71+
old_cwd = os.getcwd()
72+
os.chdir(self.tmpdir)
73+
try:
74+
yield
75+
finally:
76+
os.chdir(old_cwd)
77+
6878
def test_scheme(self):
6979
self.assertIsNone(self.fs.scheme())
7080
self.assertIsNone(localfilesystem.LocalFileSystem.scheme())
7181

82+
def test_create_cwd_file(self):
83+
with self.tmpdir_as_cwd():
84+
with self.fs.create("blah.txt") as f:
85+
f.write(b"blah")
86+
with open("blah.txt", "rb") as f:
87+
assert f.read() == b"blah"
88+
7289
@mock.patch('apache_beam.io.localfilesystem.os')
7390
def test_unix_path_join(self, *unused_mocks):
7491
# Test joining of Unix paths.

0 commit comments

Comments
 (0)