Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_get_file: fix makedirs call when the lpath is relative and in the working directory #618

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gcsfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,8 @@ async def _get_file_request(
callback.set_size(size)

checker = get_consistency_checker(consistency)
os.makedirs(os.path.dirname(lpath), exist_ok=True)
lparent = os.path.dirname(lpath) or os.curdir
martindurant marked this conversation as resolved.
Show resolved Hide resolved
os.makedirs(lparent, exist_ok=True)
with open(lpath, "wb") as f2:
while True:
data = await r.content.read(4096 * 32)
Expand Down
13 changes: 13 additions & 0 deletions gcsfs/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,19 @@ def test_get_put_file_in_dir(protocol, gcs):
assert gcs.cat(protocol + TEST_BUCKET + "/temp_dir/accounts.1.json") == data1


@pytest.mark.parametrize("protocol", ["", "gs://", "gcs://"])
def test_get_file_to_current_working_directory(monkeypatch, protocol, gcs):
fn = protocol + TEST_BUCKET + "/temp"
gcs.pipe(fn, b"hello world")

with tempdir() as dn:
os.makedirs(dn)
monkeypatch.chdir(dn)
Comment on lines +523 to +525
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used tempdir to be consistent with the other tests, but we could simplify this using tmp_path fixture and use monkeypatch.chdir() on it.

Suggested change
with tempdir() as dn:
os.makedirs(dn)
monkeypatch.chdir(dn)
monkeypatch.chdir(tmp_path)

gcs.get_file(fn, "temp")
with open("temp", mode="rb") as f:
assert f.read() == b"hello world"


def test_special_characters_filename(gcs: GCSFileSystem):
special_filename = """'!"`#$%&'()+,-.<=>?@[]^_{}~/'"""
full_path = TEST_BUCKET + "/" + special_filename
Expand Down
Loading