From 43140b5e968f905e99cb161d0ad11dae1a084f8e Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Sun, 12 May 2024 13:27:42 +0900 Subject: [PATCH] Use TemporaryDirectory for Windows --- tests/test___main__.py | 18 ++++++++++++------ tests/test_download.py | 12 ++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/test___main__.py b/tests/test___main__.py index ebd48bf5..1cb564e4 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -11,19 +11,25 @@ def _test_cli_with_md5(url_or_id, md5, options=None): - with tempfile.NamedTemporaryFile() as f: - cmd = ["gdown", "--no-cookies", url_or_id, "-O", f.name] + # We can't use NamedTemporaryFile because Windows doesn't allow the subprocess + # to write the file created by the parent process. + with tempfile.TemporaryDirectory() as d: + file_path = os.path.join(d, "file") + cmd = ["gdown", "--no-cookies", url_or_id, "-O", file_path] if options is not None: cmd.extend(options) subprocess.call(cmd) - _assert_filehash(path=f.name, hash=f"md5:{md5}") + _assert_filehash(path=file_path, hash=f"md5:{md5}") def _test_cli_with_content(url_or_id, content): - with tempfile.NamedTemporaryFile() as f: - cmd = ["gdown", "--no-cookies", url_or_id, "-O", f.name] + # We can't use NamedTemporaryFile because Windows doesn't allow the subprocess + # to write the file created by the parent process. + with tempfile.TemporaryDirectory() as d: + file_path = os.path.join(d, "file") + cmd = ["gdown", "--no-cookies", url_or_id, "-O", file_path] subprocess.call(cmd) - with open(f.name) as f: + with open(file_path) as f: assert f.read() == content diff --git a/tests/test_download.py b/tests/test_download.py index 34d08e86..98a6e783 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -1,12 +1,12 @@ import os +import tempfile from gdown.download import download def test_download(): - url = "https://raw.githubusercontent.com/wkentaro/gdown/3.1.0/gdown/__init__.py" # NOQA - output = "/tmp/gdown_r" - - # Usage before https://github.com/wkentaro/gdown/pull/32 - assert download(url, output, quiet=False) == output - os.remove(output) + with tempfile.TemporaryDirectory() as d: + file_path = os.path.join(d, "file") + url = "https://raw.githubusercontent.com/wkentaro/gdown/3.1.0/gdown/__init__.py" # NOQA + # Usage before https://github.com/wkentaro/gdown/pull/32 + assert download(url=url, output=file_path, quiet=False) == file_path