Skip to content

Commit

Permalink
Use TemporaryDirectory for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed May 12, 2024
1 parent 1dc64e9 commit 0354439
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
18 changes: 12 additions & 6 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
12 changes: 6 additions & 6 deletions tests/test_download.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0354439

Please sign in to comment.