scdl/tests/utils.py
7x11x13 9fd4814e1f
Typecheck with mypy, lint and format with ruff (#499)
* Lint and format with ruff, check types with mypy

* Fix mypy checks

* Update CI

* Remove shebangs

* Ignore EXE rules

* Fix EOL

* Fix mypy for 3.7

* Fix CI

* Fix CI

* Ensure filelock path exists

* ci: various ci fixes (#500)

---------

Co-authored-by: Arsenii es3n1n <me@es3n.in>
2024-07-09 10:51:23 -04:00

65 lines
2.0 KiB
Python

import os
import subprocess
from pathlib import Path
from typing import Optional
import music_tag # type: ignore[import]
from soundcloud import SoundCloud
client_id = SoundCloud().client_id
def call_scdl_with_auth(
*args: str,
encoding: Optional[str] = "utf-8",
) -> subprocess.CompletedProcess:
auth_token = os.getenv("AUTH_TOKEN")
assert auth_token
args = ("scdl", *args, f"--auth-token={auth_token}", f"--client-id={client_id}")
return subprocess.run(
args,
capture_output=True,
encoding=encoding,
errors="ignore" if encoding is not None else None,
check=False,
)
def assert_track(
tmp_path: Path,
expected_name: str,
expected_title: str = "testing - test track",
expected_artist: str = "7x11x13-testing",
expected_genre: Optional[str] = "Testing",
expected_artwork_len: Optional[int] = 16136,
expected_album: Optional[str] = None,
expected_albumartist: Optional[str] = None,
expected_tracknumber: Optional[int] = None,
check_metadata: bool = True,
) -> None:
file = tmp_path / expected_name
assert file.exists()
if check_metadata:
f = music_tag.load_file(file)
assert f["title"].value == expected_title
assert f["artist"].value == expected_artist
if expected_genre:
assert f["genre"].value == expected_genre
if expected_artwork_len is not None:
if expected_artwork_len > 0:
assert len(f["artwork"].value.data) == expected_artwork_len
else:
assert not f["artwork"]
if expected_album:
assert f["album"].value == expected_album
if expected_albumartist:
assert f["albumartist"].value == expected_albumartist
if expected_tracknumber is not None:
assert f["tracknumber"].value == expected_tracknumber
def assert_not_track(tmp_path: Path, expected_name: str) -> None:
file = tmp_path / expected_name
assert not file.exists()