Debian Codegraph @

tests/test_interface.py

raw
1 """
2 Test format classes.
3
4 (Some of their code is unreachable through api.py)
5 """
6
7 import os
8 from pathlib import Path
9
10 import pytest
11
12 import conda_package_handling.conda_fmt as conda_fmt
13 from conda_package_handling.conda_fmt import CondaFormat_v2
14 from conda_package_handling.tarball import CondaTarBZ2
15
16 from .test_api import data_dir, test_package_name
17
18 TEST_CONDA = Path(data_dir, test_package_name + ".conda")
19 TEST_TARBZ = Path(data_dir, test_package_name + ".tar.bz2")
20
21
22 def test_extract_create(tmpdir):
23 for format, infile, outfile in (
24 (CondaFormat_v2, TEST_CONDA, "newmock.conda"),
25 (CondaTarBZ2, TEST_TARBZ, "newmock.tar.bz2"),
26 ):
27 both_path = Path(tmpdir, f"mkdirs-{outfile.split('.', 1)[-1]}")
28
29 # these old APIs don't guarantee Path-like's
30 format.extract(infile, str(both_path))
31 assert sorted(os.listdir(both_path)) == sorted(["lib", "info"])
32
33 if format == CondaFormat_v2:
34 info_path = Path(tmpdir, "info-only")
35 format.extract_info(TEST_CONDA, str(info_path)) # type: ignore
36 assert os.listdir(info_path) == ["info"]
37
38 filelist = [str(p.relative_to(both_path)) for p in both_path.rglob("*")]
39 format.create(
40 both_path,
41 filelist,
42 tmpdir / outfile,
43 # compression_tuple is for libarchive compatibility. Instead, pass
44 # compressor=(compressor factory function)
45 compression_tuple=(".tar.zst", "zstd", "zstd:compression-level=1"),
46 )
47
48 assert (tmpdir / outfile).exists()
49
50 with pytest.raises(ValueError):
51 CondaFormat_v2.create(
52 "",
53 [],
54 "",
55 compressor=True,
56 compression_tuple=("1", "2", "3"), # type: ignore
57 )
58
59
60 @pytest.mark.parametrize(
61 ("level", "threads", "cpu_count", "expected"),
62 [
63 (None, None, 7, (19, 1)),
64 (1, 1, 7, (1, 1)),
65 (None, -1, 7, (19, 7)),
66 (None, -1, None, (19, 1)),
67 ],
68 )
69 def test_zstd_level_threads(monkeypatch, level, threads, cpu_count, expected):
70 monkeypatch.setattr(conda_fmt.os, "cpu_count", lambda: cpu_count)
71 assert conda_fmt._translate_zstd_level_threads(level, threads) == expected
72
73
74 def test_list_contents_dispatches_to_subclass(monkeypatch):
75 class CustomCondaFormat(CondaFormat_v2):
76 @staticmethod
77 def _list_remote_contents(url, verbose=False, components=("info", "pkg")):
78 return url, verbose, components
79
80 monkeypatch.setattr(
81 CondaFormat_v2,
82 "_list_remote_contents",
83 staticmethod(lambda *args, **kwargs: "base"),
84 )
85 url = "https://example.invalid/package.conda"
86 assert CustomCondaFormat.list_contents(url) == (url, False, ("info", "pkg"))