Skip to content

Commit

Permalink
Use TemporaryDirectory instead of NamedTemporaryFile in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed May 12, 2024
1 parent 4c1b22d commit 7edf77f
Showing 1 changed file with 12 additions and 6 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

0 comments on commit 7edf77f

Please sign in to comment.