-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest_list.py
More file actions
225 lines (186 loc) · 6.95 KB
/
test_list.py
File metadata and controls
225 lines (186 loc) · 6.95 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
import pytest
import re
from itertools import zip_longest
from manage import list_command
from manage.indexutils import Index
FAKE_INSTALLS = [
{"company": "Company2", "tag": "1.0", "sort-version": "1.0"},
{"company": "Company1", "tag": "2.0", "sort-version": "2.0"},
{"company": "Company1", "tag": "1.0", "sort-version": "1.0", "default": True},
]
FAKE_INDEX = {
"versions": [
{
**{k: v for k, v in i.items() if k not in ("default",)},
"schema": 1,
"id": f"{i['company']}-{i['tag']}",
"install-for": [i["tag"]],
} for i in FAKE_INSTALLS
]
}
class ListCapture:
def __init__(self):
self.args = []
# Out of order on purpose - list_command should not modify order
self.installs = FAKE_INSTALLS
self.captured = []
self.source = None
self.install_dir = "<none>"
self.format = "test"
self.one = False
self.unmanaged = True
list_command.FORMATTERS["test"] = lambda c, i: self.captured.extend(i)
def __call__(self, *filters, **kwargs):
self.args = filters
for k, v in kwargs.items():
if not hasattr(self, k):
raise TypeError(f"command has no option {k!r}")
setattr(self, k, v)
self.captured.clear()
list_command.execute(self)
return [f"{i['company']}/{i['tag']}" for i in self.captured]
def get_installs(self, include_unmanaged=False):
assert include_unmanaged == self.unmanaged
return self.installs
@pytest.fixture
def list_cmd():
try:
yield ListCapture()
finally:
list_command.FORMATTERS.pop("test", None)
def test_list(list_cmd):
# list_command does not sort its entries - get_installs() does that
assert list_cmd() == [
"Company2/1.0",
"Company1/2.0",
"Company1/1.0",
]
# unmanaged doesn't affect our result (because we shim the function that
# does the check), but it'll at least ensure it gets passed through.
assert list_cmd(unmanaged=True)
def test_list_filter(list_cmd):
assert list_cmd("2.0") == ["Company1/2.0"]
assert list_cmd("1.0") == ["Company2/1.0", "Company1/1.0"]
assert list_cmd("Company1/") == ["Company1/2.0", "Company1/1.0"]
assert list_cmd("Company1\\") == ["Company1/2.0", "Company1/1.0"]
assert list_cmd("Company\\") == ["Company2/1.0", "Company1/2.0", "Company1/1.0"]
assert list_cmd(">1") == ["Company1/2.0"]
assert list_cmd(">=1") == ["Company2/1.0", "Company1/2.0", "Company1/1.0"]
assert list_cmd("<=2") == ["Company2/1.0", "Company1/2.0", "Company1/1.0"]
assert list_cmd("<2") == ["Company2/1.0", "Company1/1.0"]
assert list_cmd("1", "2") == ["Company2/1.0", "Company1/2.0", "Company1/1.0"]
assert list_cmd("Company1\\1", "Company2\\1") == ["Company2/1.0", "Company1/1.0"]
def test_list_one(list_cmd):
assert list_cmd("2", one=True) == ["Company1/2.0"]
# Gets Company1/1.0 because it's marked as default
assert list_cmd("1", one=True) == ["Company1/1.0"]
def from_index(*filters):
return [
f"{i['company']}/{i['tag']}"
for i in list_command._get_installs_from_index([Index("./index.json", FAKE_INDEX)], filters)
]
def test_list_online():
assert from_index("Company1\\2.0") == ["Company1/2.0"]
assert from_index("Company2\\", "Company1\\1") == ["Company2/1.0", "Company1/1.0"]
assert from_index() == ["Company1/2.0", "Company2/1.0", "Company1/1.0"]
def test_format_table(assert_log):
list_command.format_table(None, FAKE_INSTALLS)
assert_log(
(r"!B!Tag\s+Name\s+Managed By\s+Version\s+Alias\s*!W!", ()),
(r"Company2\\1\.0\s+Company2\s+1\.0", ()),
(r"Company1\\2\.0\s+Company1\s+2\.0", ()),
(r"!G!Company1\\1\.0\s+\*\s+Company1\s+1\.0\s*!W!", ()),
)
def test_format_table_aliases(assert_log):
list_command.format_table(None, [
{
"company": "COMPANY",
"tag": "TAG",
"display-name": "DISPLAY",
"sort-version": "VER",
"alias": [
{"name": "python.exe"},
{"name": "pythonw.exe"},
{"name": "python3.10.exe"},
{"name": "pythonw3.10.exe"},
],
}
])
assert_log(
(r"!B!Tag\s+Name\s+Managed By\s+Version\s+Alias\s*!W!", ()),
(r"COMPANY\\TAG\s+DISPLAY\s+COMPANY\s+VER\s+" + re.escape("python[w].exe, python[w]3.10.exe"), ()),
)
def test_format_table_truncated(assert_log):
list_command.format_table(None, [
{
"company": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 4,
"tag": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 4,
"display-name": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 4,
"sort-version": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 4,
"alias": [
{"name": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 4},
],
}
])
assert_log(
(r"!B!Tag\s+Name\s+Managed By\s+Version\s+Alias\s*!W!", ()),
(r"\w{27}\.\.\.\s+\w{57}\.\.\.\s+\w{27}\.\.\.\s+\w{12}\.\.\.\s+\w{47}\.\.\.", ()),
(r"", ()),
(r".+columns were truncated.+", ()),
)
def test_format_table_empty(assert_log):
list_command.format_table(None, [])
assert_log(
(r"!B!Tag\s+Name\s+Managed By\s+Version\s+Alias\s*!W!", ()),
(r".+No runtimes.+", ()),
)
def test_format_csv(assert_log):
list_command.format_csv(None, FAKE_INSTALLS)
# CSV format only contains columns that are present, so this doesn't look
# as complete as for normal installs, but it's fine for the test.
assert_log(
"company,tag,sort-version,default",
"Company2,1.0,1.0,",
"Company1,2.0,2.0,",
"Company1,1.0,1.0,True",
)
def test_format_csv_complex(assert_log):
data = [
{
**d,
"alias": [dict(name=f"n{i}.{j}", target=f"t{i}.{j}") for j in range(i + 1)]
}
for i, d in enumerate(FAKE_INSTALLS)
]
list_command.format_csv(None, data)
assert_log(
"company,tag,sort-version,alias.name,alias.target.default",
"Company2,1.0,1.0,n0.0,t0.0,",
"Company1,2.0,2.0,n1.0,t1.0,",
"Company1,2.0,2.0,n1.1,t1.1,",
"Company1,1.0,1.0,n2.0,t2.0,True",
"Company1,1.0,1.0,n2.1,t2.1,True",
"Company1,1.0,1.0,n2.2,t2.2,True",
)
def test_format_csv_empty(assert_log):
list_command.format_csv(None, [])
assert_log(assert_log.end_of_log())
def test_csv_exclude():
result = list(list_command._csv_filter_and_expand([
dict(a=1, b=2),
dict(a=3, c=4),
dict(a=5, b=6, c=7),
], exclude={"b"}))
assert result == [dict(a=1), dict(a=3, c=4), dict(a=5, c=7)]
def test_csv_expand():
result = list(list_command._csv_filter_and_expand([
dict(a=[1, 2], b=[3, 4]),
dict(a=[5], b=[6]),
dict(a=7, b=8),
], expand={"a"}))
assert result == [
dict(a=1, b=[3, 4]),
dict(a=2, b=[3, 4]),
dict(a=5, b=[6]),
dict(a=7, b=8),
]