@@ -21,6 +21,7 @@ _tests = []
21
21
22
22
def _test_simple (env ):
23
23
calls = []
24
+ warnings_suggestion = []
24
25
25
26
def read_simpleapi (ctx , url , attr , cache , get_auth , block ):
26
27
_ = ctx # buildifier: disable=unused-variable
@@ -31,12 +32,18 @@ def _test_simple(env):
31
32
calls .append (url )
32
33
if "foo" in url and "main" in url :
33
34
return struct (
34
- output = "" ,
35
+ output = struct (
36
+ sdists = {"" : "" },
37
+ whls = {},
38
+ ),
35
39
success = False ,
36
40
)
37
41
else :
38
42
return struct (
39
- output = "data from {}" .format (url ),
43
+ output = struct (
44
+ sdists = {"" : "data from {}" .format (url )},
45
+ whls = {},
46
+ ),
40
47
success = True ,
41
48
)
42
49
@@ -48,12 +55,14 @@ def _test_simple(env):
48
55
index_url_overrides = {},
49
56
index_url = "main" ,
50
57
extra_index_urls = ["extra" ],
58
+ index_strategy = "first-index" ,
51
59
sources = ["foo" , "bar" , "baz" ],
52
60
envsubst = [],
53
61
),
54
62
cache = {},
55
63
parallel_download = True ,
56
64
read_simpleapi = read_simpleapi ,
65
+ _print = warnings_suggestion .append ,
57
66
)
58
67
59
68
env .expect .that_collection (calls ).contains_exactly ([
@@ -63,13 +72,195 @@ def _test_simple(env):
63
72
"main/foo/" ,
64
73
])
65
74
env .expect .that_dict (contents ).contains_exactly ({
66
- "bar" : "data from main/bar/" ,
67
- "baz" : "data from main/baz/" ,
68
- "foo" : "data from extra/foo/" ,
75
+ "bar" : struct (
76
+ sdists = {"" : "data from main/bar/" },
77
+ whls = {},
78
+ ),
79
+ "baz" : struct (
80
+ sdists = {"" : "data from main/baz/" },
81
+ whls = {},
82
+ ),
83
+ "foo" : struct (
84
+ sdists = {"" : "data from extra/foo/" },
85
+ whls = {},
86
+ ),
69
87
})
88
+ env .expect .that_collection (warnings_suggestion ).contains_exactly ([
89
+ """\
90
+ You can use the following `index_url_overrides` to avoid the 404 warnings:
91
+ {
92
+ "foo": "extra",
93
+ "bar": "main",
94
+ "baz": "main",
95
+ }""" ,
96
+ ])
70
97
71
98
_tests .append (_test_simple )
72
99
100
+ def _test_overrides_and_precedence (env ):
101
+ calls = []
102
+
103
+ def read_simpleapi (ctx , url , attr , cache , get_auth , block ):
104
+ _ = ctx # buildifier: disable=unused-variable
105
+ _ = attr
106
+ _ = cache
107
+ _ = get_auth
108
+ env .expect .that_bool (block ).equals (False )
109
+ calls .append (url )
110
+ if "foo" in url and "main" in url :
111
+ return struct (
112
+ output = struct (
113
+ sdists = {"" : "" },
114
+ whls = {},
115
+ ),
116
+ # This will ensure that we fail the test if we go into this
117
+ # branch unexpectedly.
118
+ success = False ,
119
+ )
120
+ else :
121
+ return struct (
122
+ output = struct (
123
+ sdists = {"" : "data from {}" .format (url )},
124
+ whls = {
125
+ url : "whl from {}" .format (url ),
126
+ } if "foo" in url else {},
127
+ ),
128
+ success = True ,
129
+ )
130
+
131
+ contents = simpleapi_download (
132
+ ctx = struct (
133
+ os = struct (environ = {}),
134
+ ),
135
+ attr = struct (
136
+ index_url_overrides = {
137
+ "foo" : "extra1,extra2" ,
138
+ },
139
+ index_url = "main" ,
140
+ extra_index_urls = [],
141
+ # If we pass overrides, then we will get packages from all indexes.
142
+ # However, for packages without index_url_overrides, we will honour
143
+ # the strategy setting.
144
+ index_strategy = "first-index" ,
145
+ sources = ["foo" , "bar" , "baz" ],
146
+ envsubst = [],
147
+ ),
148
+ cache = {},
149
+ parallel_download = True ,
150
+ read_simpleapi = read_simpleapi ,
151
+ _print = fail ,
152
+ )
153
+
154
+ env .expect .that_collection (calls ).contains_exactly ([
155
+ "extra1/foo/" ,
156
+ "extra2/foo/" ,
157
+ "main/bar/" ,
158
+ "main/baz/" ,
159
+ ])
160
+ env .expect .that_dict (contents ).contains_exactly ({
161
+ "bar" : struct (
162
+ sdists = {"" : "data from main/bar/" },
163
+ whls = {},
164
+ ),
165
+ "baz" : struct (
166
+ sdists = {"" : "data from main/baz/" },
167
+ whls = {},
168
+ ),
169
+ "foo" : struct (
170
+ # We prioritize the first index
171
+ sdists = {"" : "data from extra1/foo/" },
172
+ whls = {
173
+ "extra1/foo/" : "whl from extra1/foo/" ,
174
+ "extra2/foo/" : "whl from extra2/foo/" ,
175
+ },
176
+ ),
177
+ })
178
+
179
+ _tests .append (_test_overrides_and_precedence )
180
+
181
+ def _test_unsafe_strategy (env ):
182
+ calls = []
183
+ warnings_suggestion = []
184
+
185
+ def read_simpleapi (ctx , url , attr , cache , get_auth , block ):
186
+ _ = ctx # buildifier: disable=unused-variable
187
+ _ = attr
188
+ _ = cache
189
+ _ = get_auth
190
+ env .expect .that_bool (block ).equals (False )
191
+ calls .append (url )
192
+ return struct (
193
+ output = struct (
194
+ sdists = {"" : "data from {}" .format (url )},
195
+ whls = {
196
+ url : "whl from {}" .format (url ),
197
+ } if "foo" in url else {},
198
+ ),
199
+ success = True ,
200
+ )
201
+
202
+ contents = simpleapi_download (
203
+ ctx = struct (
204
+ os = struct (environ = {}),
205
+ ),
206
+ attr = struct (
207
+ index_url_overrides = {
208
+ "foo" : "extra1,extra2" ,
209
+ },
210
+ index_url = "main" ,
211
+ # This field would be ignored for others
212
+ extra_index_urls = ["extra" ],
213
+ # If we pass overrides, then we will get packages from all indexes.
214
+ # However, for packages without index_url_overrides, we will honour
215
+ # the strategy setting.
216
+ index_strategy = "unsafe" ,
217
+ sources = ["foo" , "bar" , "baz" ],
218
+ envsubst = [],
219
+ ),
220
+ cache = {},
221
+ parallel_download = True ,
222
+ read_simpleapi = read_simpleapi ,
223
+ _print = warnings_suggestion .append ,
224
+ )
225
+
226
+ env .expect .that_collection (calls ).contains_exactly ([
227
+ "extra1/foo/" ,
228
+ "extra2/foo/" ,
229
+ "main/bar/" ,
230
+ "main/baz/" ,
231
+ "extra/bar/" ,
232
+ "extra/baz/" ,
233
+ ])
234
+ env .expect .that_dict (contents ).contains_exactly ({
235
+ "bar" : struct (
236
+ sdists = {"" : "data from main/bar/" },
237
+ whls = {},
238
+ ),
239
+ "baz" : struct (
240
+ sdists = {"" : "data from main/baz/" },
241
+ whls = {},
242
+ ),
243
+ "foo" : struct (
244
+ # We prioritize the first index
245
+ sdists = {"" : "data from extra1/foo/" },
246
+ whls = {
247
+ "extra1/foo/" : "whl from extra1/foo/" ,
248
+ "extra2/foo/" : "whl from extra2/foo/" ,
249
+ },
250
+ ),
251
+ })
252
+ env .expect .that_collection (warnings_suggestion ).contains_exactly ([
253
+ """\
254
+ You can use the following `index_url_overrides` to avoid the 404 warnings:
255
+ {
256
+ "foo": "extra1,extra2",
257
+ "bar": "main,extra",
258
+ "baz": "main,extra",
259
+ }""" ,
260
+ ])
261
+
262
+ _tests .append (_test_unsafe_strategy )
263
+
73
264
def _test_fail (env ):
74
265
calls = []
75
266
fails = []
@@ -83,12 +274,18 @@ def _test_fail(env):
83
274
calls .append (url )
84
275
if "foo" in url :
85
276
return struct (
86
- output = "" ,
277
+ output = struct (
278
+ sdists = {"" : "" },
279
+ whls = {},
280
+ ),
87
281
success = False ,
88
282
)
89
283
else :
90
284
return struct (
91
- output = "data from {}" .format (url ),
285
+ output = struct (
286
+ sdists = {"" : "data from {}" .format (url )},
287
+ whls = {},
288
+ ),
92
289
success = True ,
93
290
)
94
291
@@ -100,17 +297,19 @@ def _test_fail(env):
100
297
index_url_overrides = {},
101
298
index_url = "main" ,
102
299
extra_index_urls = ["extra" ],
300
+ index_strategy = "first-index" ,
103
301
sources = ["foo" , "bar" , "baz" ],
104
302
envsubst = [],
105
303
),
106
304
cache = {},
107
305
parallel_download = True ,
108
306
read_simpleapi = read_simpleapi ,
109
307
_fail = fails .append ,
308
+ _print = fail ,
110
309
)
111
310
112
311
env .expect .that_collection (fails ).contains_exactly ([
113
- """Failed to download metadata for ["foo"] for from urls: ["main", "extra"]""" ,
312
+ """Failed to download metadata for ["foo"] from urls: ["main", "extra"]""" ,
114
313
])
115
314
env .expect .that_collection (calls ).contains_exactly ([
116
315
"extra/foo/" ,
@@ -140,12 +339,14 @@ def _test_download_url(env):
140
339
index_url_overrides = {},
141
340
index_url = "https://example.com/main/simple/" ,
142
341
extra_index_urls = [],
342
+ index_strategy = "first-index" ,
143
343
sources = ["foo" , "bar" , "baz" ],
144
344
envsubst = [],
145
345
),
146
346
cache = {},
147
347
parallel_download = False ,
148
348
get_auth = lambda ctx , urls , ctx_attr : struct (),
349
+ _print = fail ,
149
350
)
150
351
151
352
env .expect .that_dict (downloads ).contains_exactly ({
@@ -175,12 +376,14 @@ def _test_download_url_parallel(env):
175
376
index_url_overrides = {},
176
377
index_url = "https://example.com/main/simple/" ,
177
378
extra_index_urls = [],
379
+ index_strategy = "first-index" ,
178
380
sources = ["foo" , "bar" , "baz" ],
179
381
envsubst = [],
180
382
),
181
383
cache = {},
182
384
parallel_download = True ,
183
385
get_auth = lambda ctx , urls , ctx_attr : struct (),
386
+ _print = fail ,
184
387
)
185
388
186
389
env .expect .that_dict (downloads ).contains_exactly ({
@@ -210,12 +413,14 @@ def _test_download_envsubst_url(env):
210
413
index_url_overrides = {},
211
414
index_url = "$INDEX_URL" ,
212
415
extra_index_urls = [],
416
+ index_strategy = "first-index" ,
213
417
sources = ["foo" , "bar" , "baz" ],
214
418
envsubst = ["INDEX_URL" ],
215
419
),
216
420
cache = {},
217
421
parallel_download = False ,
218
422
get_auth = lambda ctx , urls , ctx_attr : struct (),
423
+ _print = fail ,
219
424
)
220
425
221
426
env .expect .that_dict (downloads ).contains_exactly ({
0 commit comments