/
test_cli.py
192 lines (134 loc) · 6.39 KB
/
test_cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import contextlib
import json
import os
import stat
import subprocess
import sys
import tempfile
from pathlib import Path
import pytest
from click.testing import CliRunner
from shiv.cli import _interpreter_path, find_entry_point, main
from shiv.constants import DISALLOWED_ARGS, DISALLOWED_PIP_ARGS, NO_OUTFILE, NO_PIP_ARGS_OR_SITE_PACKAGES
from shiv.info import main as info_main
from shiv.pip import install
UGOX = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
@contextlib.contextmanager
def mocked_sys_prefix():
attribute_to_mock = "real_prefix" if hasattr(sys, "real_prefix") else "base_prefix"
original = getattr(sys, attribute_to_mock)
setattr(sys, attribute_to_mock, "/fake/dir")
yield
setattr(sys, attribute_to_mock, original)
class TestCLI:
@pytest.fixture
def shiv_root(self, monkeypatch, tmpdir):
with tempfile.TemporaryDirectory(dir=tmpdir) as tmpdir:
os.environ["SHIV_ROOT"] = tmpdir
yield tmpdir
os.environ.pop("SHIV_ROOT")
@pytest.fixture
def runner(self):
"""Returns a click test runner."""
return lambda args: CliRunner().invoke(main, args)
@pytest.fixture
def info_runner(self):
"""Returns a click test runner (for shiv-info)."""
return lambda args: CliRunner().invoke(info_main, args)
def test_find_entry_point(self, tmpdir, package_location):
"""Test that we can find console_script metadata."""
install(["-t", str(tmpdir), str(package_location)])
assert find_entry_point(Path(tmpdir), "hello") == "hello:main"
def test_no_args(self, runner):
"""This should fail with a warning about supplying pip arguments"""
result = runner([])
assert result.exit_code == 1
assert NO_PIP_ARGS_OR_SITE_PACKAGES in result.output
def test_no_outfile(self, runner):
"""This should fail with a warning about not providing an outfile"""
result = runner(["-e", "test", "flask"])
assert result.exit_code == 1
assert NO_OUTFILE in result.output
def test_find_interpreter(self):
interpreter = _interpreter_path()
assert Path(interpreter).exists()
assert Path(interpreter).is_file()
def test_find_interpreter_false(self):
with mocked_sys_prefix():
interpreter = _interpreter_path()
# should fall back on the current sys.executable
assert interpreter == sys.executable
@pytest.mark.parametrize("arg", [arg for tup in DISALLOWED_ARGS.keys() for arg in tup])
def test_disallowed_args(self, runner, arg):
"""This method tests that all the potential disallowed arguments match their error messages."""
# run shiv with a disallowed argument
result = runner(["-o", "tmp", arg])
# get the 'reason' message:
for disallowed in DISALLOWED_ARGS:
if arg in disallowed:
reason = DISALLOWED_ARGS[disallowed]
assert result.exit_code == 1
# assert we got the correct reason
assert DISALLOWED_PIP_ARGS.format(arg=arg, reason=reason) in result.output
@pytest.mark.parametrize("compile_option", ["--compile-pyc", "--no-compile-pyc"])
@pytest.mark.parametrize("force", ["yes", "no"])
def test_hello_world(self, runner, info_runner, shiv_root, package_location, compile_option, force):
output_file = Path(shiv_root, "test.pyz")
result = runner(["-e", "hello:main", "-o", str(output_file), str(package_location), compile_option])
# check that the command successfully completed
assert result.exit_code == 0
# ensure the created file actually exists
assert output_file.exists()
# build env
env = {**os.environ, "SHIV_FORCE_EXTRACT": force}
# now run the produced zipapp
proc = subprocess.run([str(output_file)], stdout=subprocess.PIPE, shell=True, env=env)
assert proc.stdout.decode() == "hello world" + os.linesep
# now run shiv-info on the produced zipapp
result = info_runner([str(output_file)])
# check the rc and output
assert result.exit_code == 0
assert f"pyz file: {str(output_file)}" in result.output
# ensure that executable permissions were retained (skip test on windows)
if os.name != "nt":
build_id = json.loads(info_runner([str(output_file), "--json"]).output)["build_id"]
assert (
Path(shiv_root, f"{output_file.stem}_{build_id}", "site-packages", "hello", "script.sh").stat().st_mode
& UGOX
== UGOX
)
@pytest.mark.parametrize("extend_path", ["--extend-pythonpath", "--no-extend-pythonpath", "-E"])
def test_extend_pythonpath(self, shiv_root, runner, extend_path):
output_file = Path(shiv_root, "test_pythonpath.pyz")
package_dir = Path(shiv_root, "package")
main_script = Path(package_dir, "env.py")
MAIN_PROG = "\n".join(["import os", "def main():", " print(os.environ.get('PYTHONPATH', ''))"])
package_dir.mkdir()
main_script.write_text(MAIN_PROG)
result = runner(["-e", "env:main", "-o", str(output_file), "--site-packages", str(package_dir), extend_path])
# check that the command successfully completed
assert result.exit_code == 0
# ensure the created file actually exists
assert output_file.exists()
# now run the produced zipapp and confirm shiv_root is in PYTHONPATH
proc = subprocess.run([str(output_file)], stdout=subprocess.PIPE, shell=True, env=os.environ)
pythonpath_has_root = str(shiv_root) in proc.stdout.decode()
assert extend_path.startswith("--no") != pythonpath_has_root
def test_no_entrypoint(self, shiv_root, runner, package_location, monkeypatch):
output_file = Path(shiv_root, "test.pyz")
result = runner(["-o", str(output_file), str(package_location)])
# check that the command successfully completed
assert result.exit_code == 0
# ensure the created file actually exists
assert output_file.exists()
# now run the produced zipapp
proc = subprocess.run(
[str(output_file)],
input=b"import hello;print(hello)",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
env=os.environ,
)
assert proc.returncode == 0
assert "hello" in proc.stdout.decode()