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

Support Windows and macOS in tests #359

Merged
merged 4 commits into from
May 12, 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: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
python-version: ["3.8", "3.12"]
python-version: ["3.8"]

steps:
- uses: actions/setup-python@v4
Expand All @@ -36,7 +36,6 @@ jobs:
- name: Test
run: |
make test
if: matrix.os == 'ubuntu-latest'

- name: Install from dist
shell: bash
Expand Down
64 changes: 43 additions & 21 deletions tests/test___main__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import hashlib
import os
import shlex
import subprocess
import sys
import tempfile

from gdown.cached_download import _assert_filehash
from gdown.cached_download import _compute_filehash

here = os.path.dirname(os.path.abspath(__file__))


def _test_cli_with_md5(url_or_id, md5, options=None):
with tempfile.NamedTemporaryFile() as f:
cmd = f"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 = f"{cmd} {options}"
subprocess.call(shlex.split(cmd))
_assert_filehash(path=f.name, hash=f"md5:{md5}")
cmd.extend(options)
subprocess.call(cmd)
_assert_filehash(path=file_path, hash=f"md5:{md5}")


def _test_cli_with_content(url_or_id, content):
with tempfile.NamedTemporaryFile() as f:
subprocess.call(shlex.split(f"gdown --no-cookies {url_or_id} -O {f.name}"))
with open(f.name) as f:
# 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(file_path) as f:
assert f.read() == content


Expand Down Expand Up @@ -77,13 +85,20 @@ def test_download_folder_from_gdrive():

for folder_id, md5 in folder_id_and_md5s:
with tempfile.TemporaryDirectory() as d:
cmd = f"gdown --no-cookies {folder_id} -O {d} --folder"
subprocess.call(shlex.split(cmd))

cmd = "find . -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum | awk '{print $1}'" # noqa: E501
md5_actual = (
subprocess.check_output(cmd, shell=True, cwd=d).decode().strip()
)
cmd = ["gdown", "--no-cookies", folder_id, "-O", d, "--folder"]
subprocess.call(cmd)

md5s_actual = []
for dirpath, dirnames, filenames in os.walk(d):
for filename in filenames:
md5_actual = _compute_filehash(
path=os.path.join(dirpath, filename), algorithm="md5"
)[len("md5:") :]
md5s_actual.append(md5_actual)

md5_actual = hashlib.md5(
("".join(x + "\n" for x in sorted(md5s_actual))).encode()
).hexdigest()
try:
assert md5_actual == md5
break
Expand All @@ -96,8 +111,15 @@ def test_download_folder_from_gdrive():

def test_download_a_folder_with_remining_ok_false():
with tempfile.TemporaryDirectory() as d:
cmd = f"gdown --no-cookies https://drive.google.com/drive/folders/1gd3xLkmjT8IckN6WtMbyFZvLR4exRIkn -O {d} --folder" # noqa: E501
assert subprocess.call(shlex.split(cmd)) == 1
cmd = [
"gdown",
"--no-cookies",
"https://drive.google.com/drive/folders/1gd3xLkmjT8IckN6WtMbyFZvLR4exRIkn",
"-O",
d,
"--folder",
]
assert subprocess.call(cmd) == 1


# def test_download_docs_from_gdrive():
Expand All @@ -115,15 +137,15 @@ def test_download_a_folder_with_remining_ok_false():
def test_download_slides_from_gdrive():
file_id = "13AhW1Z1GYGaiTpJ0Pr2TTXoQivb6jx-a"
md5 = "96704c6c40e308a68d3842e83a0136b9"
_test_cli_with_md5(url_or_id=file_id, md5=md5, options="--format pdf")
_test_cli_with_md5(url_or_id=file_id, md5=md5, options=["--format", "pdf"])


def test_download_a_folder_with_file_content_more_than_the_limit():
url = "https://drive.google.com/drive/folders/1gd3xLkmjT8IckN6WtMbyFZvLR4exRIkn"

with tempfile.TemporaryDirectory() as d:
cmd = f"gdown --no-cookies {url} -O {d} --folder --remaining-ok"
subprocess.check_call(shlex.split(cmd))
cmd = ["gdown", "--no-cookies", url, "-O", d, "--folder", "--remaining-ok"]
subprocess.check_call(cmd)

filenames = sorted(os.listdir(d))
for i in range(50):
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
Loading