forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_console_script_gen_test.py
More file actions
231 lines (201 loc) · 7.06 KB
/
py_console_script_gen_test.py
File metadata and controls
231 lines (201 loc) · 7.06 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import tempfile
import textwrap
import unittest
from python.private.py_console_script_gen import run
class RunTest(unittest.TestCase):
def setUp(self):
self.maxDiff = None
def test_no_console_scripts_error(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
outfile = tmpdir / "out.py"
given_contents = (
textwrap.dedent(
"""
[non_console_scripts]
foo = foo.bar:fizz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
with self.assertRaises(RuntimeError) as cm:
run(
entry_points=entry_points,
out=outfile,
console_script=None,
console_script_guess="",
shebang="",
)
self.assertEqual(
"The package does not provide any console_scripts in its entry_points.txt",
cm.exception.args[0],
)
def test_no_entry_point_selected_error(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
outfile = tmpdir / "out.py"
given_contents = (
textwrap.dedent(
"""
[console_scripts]
foo = foo.bar:fizz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
with self.assertRaises(RuntimeError) as cm:
run(
entry_points=entry_points,
out=outfile,
console_script=None,
console_script_guess="bar-baz",
shebang="",
)
self.assertEqual(
"Tried to guess that you wanted 'bar-baz', but could not find it. Please select one of the following console scripts: foo",
cm.exception.args[0],
)
def test_incorrect_entry_point(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
outfile = tmpdir / "out.py"
given_contents = (
textwrap.dedent(
"""
[console_scripts]
foo = foo.bar:fizz
bar = foo.bar:buzz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
with self.assertRaises(RuntimeError) as cm:
run(
entry_points=entry_points,
out=outfile,
console_script="baz",
console_script_guess="",
shebang="",
)
self.assertEqual(
"The console_script 'baz' was not found, only the following are available: bar, foo",
cm.exception.args[0],
)
def test_a_single_entry_point(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
given_contents = (
textwrap.dedent(
"""
[console_scripts]
foo = foo.bar:baz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
out = tmpdir / "foo.py"
run(
entry_points=entry_points,
out=out,
console_script=None,
console_script_guess="foo",
shebang="",
)
got = out.read_text()
want = textwrap.dedent(
"""\
import sys
# See @rules_python//python/private:py_console_script_gen.py for explanation
if getattr(sys.flags, "safe_path", False):
# We are running on Python 3.11 and we don't need this workaround
pass
elif ".runfiles" not in sys.path[0]:
sys.path = sys.path[1:]
try:
from foo.bar import baz
except ImportError:
entries = "\\n".join(sys.path)
print("Printing sys.path entries for easier debugging:", file=sys.stderr)
print(f"sys.path is:\\n{entries}", file=sys.stderr)
raise
if __name__ == "__main__":
sys.exit(baz()) # type: ignore
"""
)
self.assertEqual(want, got)
def test_a_second_entry_point_class_method(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
given_contents = (
textwrap.dedent(
"""
[console_scripts]
foo = foo.bar:Bar.baz
bar = foo.baz:Bar.baz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
out = tmpdir / "out.py"
run(
entry_points=entry_points,
out=out,
console_script="bar",
console_script_guess="",
shebang="",
)
got = out.read_text()
self.assertRegex(got, "from foo\.baz import Bar")
self.assertRegex(got, "sys\.exit\(Bar\.baz\(\)\)")
def test_shebang_included(self):
with tempfile.TemporaryDirectory() as tmpdir:
tmpdir = pathlib.Path(tmpdir)
given_contents = (
textwrap.dedent(
"""
[console_scripts]
foo = foo.bar:baz
"""
).strip()
+ "\n"
)
entry_points = tmpdir / "entry_points.txt"
entry_points.write_text(given_contents)
out = tmpdir / "foo.py"
shebang = "#!/usr/bin/env python3"
run(
entry_points=entry_points,
out=out,
console_script=None,
console_script_guess="foo",
shebang=shebang,
)
got = out.read_text()
self.assertTrue(got.startswith(shebang + "\n"))
if __name__ == "__main__":
unittest.main()