Refactor code style and improve documentation
CI / Quality Checks (push) Failing after 12s
CI / Test Suite (push) Successful in 10s

- Standardized string quotes to single quotes across all files.
- Added docstrings to several functions and classes for better clarity.
- Updated mypy configuration in pyproject.toml for enhanced type checking.
- Ignored specific linting rules for test files in ruff configuration.
- Improved error messages in exception handling for better user feedback.
- Cleaned up code formatting and structure for consistency.
This commit is contained in:
ВяткинАртём
2026-05-27 17:24:30 +03:00
parent 1c3228e039
commit d7cbd876ea
23 changed files with 214 additions and 203 deletions
+19 -21
View File
@@ -14,50 +14,48 @@ def _build_valid_session_validation(path: Path) -> SessionValidation:
def test_download_returns_binary_mp4(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
expected_content = b"mp4-binary-content"
expected_content = b'mp4-binary-content'
def fake_validate_session_file(path: Path) -> SessionValidation:
return _build_valid_session_validation(path)
def fake_download_video(*, url: str, output_dir: Path, session_path: Path) -> Path:
del url, session_path
downloaded_file = output_dir / "video.mp4"
downloaded_file = output_dir / 'video.mp4'
downloaded_file.write_bytes(expected_content)
return downloaded_file
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
monkeypatch.setattr(api, "download_video", fake_download_video)
monkeypatch.setattr(api, 'validate_session_file', fake_validate_session_file)
monkeypatch.setattr(api, 'download_video', fake_download_video)
downloaded_video = api.download(
"https://youtube.com/shorts/example",
session_path="cookies.txt",
'https://youtube.com/shorts/example',
session_path='cookies.txt',
)
assert downloaded_video == DownloadedVideo(
filename="video.mp4",
filename='video.mp4',
content=expected_content,
)
def test_download_to_path_returns_path_from_downloader(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
expected_path = tmp_path / "video.mp4"
def test_download_to_path_returns_path_from_downloader(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
expected_path = tmp_path / 'video.mp4'
def fake_validate_session_file(path: Path) -> SessionValidation:
return _build_valid_session_validation(path)
def fake_download_video(*, url: str, output_dir: Path, session_path: Path) -> Path:
del url, session_path
expected_path.write_bytes(b"mp4")
expected_path.write_bytes(b'mp4')
return output_dir / expected_path.name
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
monkeypatch.setattr(api, "download_video", fake_download_video)
monkeypatch.setattr(api, 'validate_session_file', fake_validate_session_file)
monkeypatch.setattr(api, 'download_video', fake_download_video)
downloaded_path = api.download_to_path(
"https://youtube.com/shorts/example",
session_path="cookies.txt",
'https://youtube.com/shorts/example',
session_path='cookies.txt',
output_dir=tmp_path,
)
@@ -66,15 +64,15 @@ def test_download_to_path_returns_path_from_downloader(
def test_download_rejects_invalid_url() -> None:
with pytest.raises(InvalidUrlError):
api.download("https://example.com/watch?v=1", session_path="cookies.txt")
api.download('https://example.com/watch?v=1', session_path='cookies.txt')
def test_download_rejects_invalid_session(monkeypatch: pytest.MonkeyPatch) -> None:
def fake_validate_session_file(path: Path) -> SessionValidation:
del path
return SessionValidation(False, False, False, False, "bad session")
return SessionValidation(False, False, False, False, 'bad session')
monkeypatch.setattr(api, "validate_session_file", fake_validate_session_file)
monkeypatch.setattr(api, 'validate_session_file', fake_validate_session_file)
with pytest.raises(InvalidSessionError, match="bad session"):
api.download("https://youtube.com/shorts/example", session_path="cookies.txt")
with pytest.raises(InvalidSessionError, match='bad session'):
api.download('https://youtube.com/shorts/example', session_path='cookies.txt')
+8 -8
View File
@@ -13,19 +13,19 @@ def test_cli_prints_downloaded_path(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
expected_path = tmp_path / "video.mp4"
expected_path = tmp_path / 'video.mp4'
def fake_download_to_path(**kwargs: object) -> Path:
del kwargs
return expected_path
monkeypatch.setattr(cli, "download_to_path", fake_download_to_path)
monkeypatch.setattr(cli, 'download_to_path', fake_download_to_path)
exit_code = cli.main(
[
"https://youtube.com/shorts/example",
"cookies.txt",
"--output-dir",
'https://youtube.com/shorts/example',
'cookies.txt',
'--output-dir',
str(tmp_path),
]
)
@@ -39,11 +39,11 @@ def test_cli_prints_downloaded_path(
def test_cli_exits_with_error(monkeypatch: pytest.MonkeyPatch) -> None:
def raise_error(**kwargs: object) -> Path:
del kwargs
raise InvalidSessionError("session error")
raise InvalidSessionError('session error')
monkeypatch.setattr(cli, "download_to_path", raise_error)
monkeypatch.setattr(cli, 'download_to_path', raise_error)
with pytest.raises(SystemExit) as exc_info:
cli.main(["https://youtube.com/shorts/example", "cookies.txt"])
cli.main(['https://youtube.com/shorts/example', 'cookies.txt'])
assert exc_info.value.code == 1
+17 -14
View File
@@ -12,39 +12,42 @@ def test_find_supported_js_runtimes_prefers_deno(
) -> None:
monkeypatch.setattr(
runtime,
"which",
lambda executable: "/usr/bin/deno" if executable == "deno" else None,
'which',
lambda executable: '/usr/bin/deno' if executable == 'deno' else None,
)
runtimes = runtime.find_supported_js_runtimes()
assert runtimes == {"deno": {"path": "/usr/bin/deno"}}
assert runtimes == {'deno': {'path': '/usr/bin/deno'}}
def test_find_supported_js_runtimes_uses_best_supported_node(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
first_node = tmp_path / ".nvm" / "versions" / "node" / "v22.1.0" / "bin" / "node"
second_node = tmp_path / ".nvm" / "versions" / "node" / "v22.9.0" / "bin" / "node"
first_node = tmp_path / '.nvm' / 'versions' / 'node' / 'v22.1.0' / 'bin' / 'node'
second_node = tmp_path / '.nvm' / 'versions' / 'node' / 'v22.9.0' / 'bin' / 'node'
second_node.parent.mkdir(parents=True)
first_node.parent.mkdir(parents=True)
first_node.write_text("", encoding="utf-8")
second_node.write_text("", encoding="utf-8")
first_node.write_text('', encoding='utf-8')
second_node.write_text('', encoding='utf-8')
monkeypatch.setattr(runtime, "which", lambda executable: None)
monkeypatch.setattr(Path, "home", lambda: tmp_path)
def fake_which(_executable: str) -> None:
return None
monkeypatch.setattr(runtime, 'which', fake_which)
monkeypatch.setattr(Path, 'home', lambda: tmp_path)
monkeypatch.setattr(
runtime,
"_probe_executable_version",
lambda executable: (22, 9, 0) if executable.endswith("v22.9.0/bin/node") else (22, 1, 0),
'_probe_executable_version',
lambda executable: (22, 9, 0) if executable.endswith('v22.9.0/bin/node') else (22, 1, 0),
)
runtimes = runtime.find_supported_js_runtimes()
assert runtimes == {"node": {"path": str(second_node.resolve())}}
assert runtimes == {'node': {'path': str(second_node.resolve())}}
def test_parse_semver_handles_prefixed_versions() -> None:
assert runtime._parse_semver("v22.12.1") == (22, 12, 1)
assert runtime._parse_semver("node") is None
assert runtime._parse_semver('v22.12.1') == (22, 12, 1)
assert runtime._parse_semver('node') is None
+9 -9
View File
@@ -6,17 +6,17 @@ from yt_shorts_downloader.session import validate_session_file
def test_validate_session_file_accepts_valid_youtube_session(tmp_path: Path) -> None:
session_file = tmp_path / "cookies.txt"
session_file = tmp_path / 'cookies.txt'
session_file.write_text(
"\n".join(
'\n'.join(
[
"# Netscape HTTP Cookie File",
".youtube.com\tTRUE\t/\tFALSE\t9999999999\tSID\tvalue1",
".youtube.com\tTRUE\t/\tTRUE\t9999999999\tSAPISID\tvalue2",
".youtube.com\tTRUE\t/\tTRUE\t9999999999\tLOGIN_INFO\tvalue3",
'# Netscape HTTP Cookie File',
'.youtube.com\tTRUE\t/\tFALSE\t9999999999\tSID\tvalue1',
'.youtube.com\tTRUE\t/\tTRUE\t9999999999\tSAPISID\tvalue2',
'.youtube.com\tTRUE\t/\tTRUE\t9999999999\tLOGIN_INFO\tvalue3',
]
),
encoding="utf-8",
encoding='utf-8',
)
validation = validate_session_file(session_file)
@@ -28,8 +28,8 @@ def test_validate_session_file_accepts_valid_youtube_session(tmp_path: Path) ->
def test_validate_session_file_rejects_invalid_format(tmp_path: Path) -> None:
session_file = tmp_path / "cookies.txt"
session_file.write_text("broken\trow", encoding="utf-8")
session_file = tmp_path / 'cookies.txt'
session_file.write_text('broken\trow', encoding='utf-8')
validation = validate_session_file(session_file)