Debian Codegraph @

tests/test_cli.py

raw ◀ ▶
1 Debian patch touch this file
1 import os
2 from pathlib import Path
3
4 import pytest
5
6 import conda_package_handling.cli as cli
7
8 from .test_api import data_dir, test_package_name
9
10
11 def test_cli(tmpdir, mocker):
12 """
13 Code coverage for the cli.
14 """
15 for command in [
16 [
17 "x",
18 str(Path(data_dir, test_package_name + ".tar.bz2")),
19 f"--prefix={tmpdir}",
20 ],
21 [
22 "x",
23 str(Path(data_dir, test_package_name + ".conda")),
24 "--info",
25 f"--prefix={tmpdir}",
26 ],
27 [
28 "c",
29 str(Path(tmpdir, test_package_name)),
30 ".tar.bz2",
31 f"--out-folder={tmpdir}",
32 ],
33 ]:
34 cli.main(args=command)
35
36 # XXX difficult to get to this error handling code through the actual CLI;
37 # for example, a .tar.bz2 that can't be extracted raises OSError instead of
38 # returning errors. Designed for .tar.bz2 -> .conda conversions that somehow
39 # omit files?
40 mocker.patch(
41 "conda_package_handling.api.transmute",
42 return_value=set("that is why you fail".split()),
43 )
44 with pytest.raises(SystemExit):
45 command = [
46 "t",
47 str(Path(data_dir, test_package_name + ".tar.bz2")),
48 ".conda",
49 f"--out-folder={tmpdir}",
50 ]
51 cli.main(args=command)
52
53
54 def test_import_main():
55 """
56 e.g. python -m conda_package_handling
57 """
58 with pytest.raises(SystemExit):
59 import conda_package_handling.__main__ # noqa
60
61
62 @pytest.mark.parametrize(
63 "artifact,n_files",
64 [("mock-2.0.0-py37_1000.conda", 43), ("mock-2.0.0-py37_1000.tar.bz2", 43)],
65 )
66 def test_list(artifact, n_files, capsys):
67 "Integration test to ensure `cph list` works correctly."
68 cli.main(["list", os.path.relpath(os.path.join(data_dir, artifact), os.getcwd())])
69 stdout, stderr = capsys.readouterr()
70 assert n_files == sum(bool(line.strip()) for line in stdout.splitlines())
71
72 # test verbose flag
73 cli.main(
74 [
75 "list",
76 "--verbose",
77 os.path.join(data_dir, artifact),
78 ]
79 )
80 stdout, stderr = capsys.readouterr()
81 assert n_files == sum(bool(line.strip()) for line in stdout.splitlines())
82
83 with pytest.raises(ValueError):
84 cli.main(["list", "setup.py"])
85
86 cli.main(["list", os.path.join(data_dir, artifact), "--components=pkg"])
87 stdout, stderr = capsys.readouterr()
88 listed_files = sum(bool(line.strip()) for line in stdout.splitlines())
89 if artifact.endswith(".conda"):
90 assert listed_files < n_files # info folder filtered out
91 else:
92 assert listed_files == n_files # no info filtering in tar.bz2
93
94
•95 @pytest.mark.skip(reason="Requires network access")
96 @pytest.mark.parametrize(
97 "fn,n_files",
98 [
99 ("mock-2.0.0-py37_1000.conda", 43),
100 ("mock-2.0.0-py37_1000.tar.bz2", -1),
101 ],
102 )
103 def test_list_remote(capsys, localserver, fn, n_files):
104 "Integration test to ensure `cph list <URL>` works correctly."
105 url = "/".join([localserver, fn])
106 if url.endswith(".tar.bz2"):
107 # This is not supported in streaming mode
108 with pytest.raises(ValueError):
109 cli.main(["list", url])
110 return
111
112 cli.main(["list", url])
113 stdout, _ = capsys.readouterr()
114 assert n_files == sum(bool(line.strip()) for line in stdout.splitlines())
115
116 # Local list should be the same as a 'remote' list
117 cli.main(["list", str(Path(__file__).parent / "data" / fn)])
118 stdout_local, _ = capsys.readouterr()
119 assert list(map(str.strip, stdout_local.splitlines())) == list(
120 map(str.strip, stdout.splitlines())
121 )
122
123 cli.main(["list", url, "-v"])
124 stdout_verbose, _ = capsys.readouterr()
125 assert n_files == sum(bool(line.strip()) for line in stdout_verbose.splitlines())