@@ -95,15 +95,15 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
95
95
parser .add_argument (
96
96
"--operator" ,
97
97
help = "Patch operator version in release.yaml. Format <operator>=<version>" ,
98
- nargs = "* " ,
98
+ action = "append " ,
99
99
type = cli_parse_operator_args ,
100
100
default = [],
101
101
)
102
102
103
103
parser .add_argument (
104
104
"--skip-operator" ,
105
105
help = "Skip given operator(s) when installing a release." ,
106
- nargs = "* " ,
106
+ action = "append " ,
107
107
default = [],
108
108
)
109
109
@@ -145,7 +145,7 @@ def cli_parse_operator_args(args: str) -> tuple[str, str]:
145
145
f"Invalid operator argument: { args } . Must be in format <operator>=<version>"
146
146
)
147
147
op , version = args .split ("=" , maxsplit = 1 )
148
- return ( op , version )
148
+ return op , version
149
149
150
150
151
151
def cli_log_level (cli_arg : str ) -> int :
@@ -186,12 +186,13 @@ def have_requirements() -> None:
186
186
187
187
@contextlib .contextmanager
188
188
def release_file (
189
- operators : list [tuple [str , str ]] = [],
190
- skip_ops : list [str ] = [],
189
+ operators : list [tuple [str , str ]], skip_ops : list [str ]
191
190
) -> collections .abc .Generator [str , None , None ]:
192
- """Patch release.yaml with operator versions if needed .
191
+ """Generate a (possibly modified) copy of the release.yaml file .
193
192
194
- If no --operator is set, the default release file is used.
193
+ Operator versions passed as --operator take precedence over the release.yaml contents.
194
+
195
+ Operators passed as --skip-operator are excluded from the resulting release.yaml contents.
195
196
196
197
If an invalid operator name is provided (i.e. one that doesn't exist in the
197
198
original release file), a TestRunnerException is raised.
@@ -202,51 +203,60 @@ def release_file(
202
203
203
204
def _patch ():
204
205
release_file = os .path .join ("tests" , "release.yaml" )
205
- # Make a copy so we can mutate it without affecting the original
206
- ops_copy = operators .copy ()
206
+ # A marker to validate that all ops were patched
207
207
patched_release = []
208
208
with open (release_file , "r" ) as f :
209
+ patched_ops = []
209
210
patch_version = ""
210
211
for line in f :
211
212
if patch_version :
212
213
line = re .sub (":.+$" , f": { patch_version } " , line )
213
214
patch_version = ""
214
215
else :
215
- for op , version in ops_copy :
216
+ for op , version in operators :
216
217
if op in line :
217
218
patch_version = version
218
- ops_copy . remove (( op , version )) # found an operator to patch
219
+ patched_ops . append ( op )
219
220
break
220
221
patched_release .append (line .rstrip ("\n " ))
221
222
223
+ # Sanity test that cli didn't contain garbage that is silently discarded
224
+ ops_not_patched = set ([op for op , _ in operators ]) - set (patched_ops )
225
+ if ops_not_patched :
226
+ logging .error (
227
+ f"Patched operators [{ ', ' .join (ops_not_patched )} ] not found in { release_file } "
228
+ )
229
+ raise TestRunnerException ()
230
+
222
231
# Filter out skip operators
223
- skipped_release = []
232
+ release_contents = []
233
+ skip = False
234
+ valid_skip_ops = []
224
235
for line in patched_release :
225
- skip = False
226
236
if skip :
227
237
skip = False
228
- else :
229
- if any ((op in line for op in skip_ops )):
238
+ continue
239
+ for op in skip_ops :
240
+ if op in line :
230
241
skip = True
242
+ valid_skip_ops .append (op )
231
243
break
232
- else :
233
- skipped_release .append (line )
234
- patched_release = skipped_release
235
-
236
- if ops_copy :
237
- # Some --operator args were not found in the release file. This is
238
- # most likely a typo and CI pipelines should terminate early in such
239
- # cases.
244
+ else :
245
+ release_contents .append (line )
246
+ # Sanity test that cli didn't contain garbage that is silently discarded
247
+ ops_not_skipped = set (skip_ops ) - set (valid_skip_ops )
248
+ if ops_not_skipped :
240
249
logging .error (
241
- f"Operators { ', ' .join ([ op for op , _ in ops_copy ]) } not found in { release_file } "
250
+ f"Skipped operators [ { ', ' .join (ops_not_skipped ) } ] not found in { release_file } "
242
251
)
243
252
raise TestRunnerException ()
253
+
244
254
with tempfile .NamedTemporaryFile (
245
255
mode = "w" ,
246
256
delete = False ,
247
257
prefix = "patched" ,
248
258
) as f :
249
- pcontents = "\n " .join (patched_release )
259
+ pcontents = "\n " .join (release_contents )
250
260
logging .debug (f"Writing patched release to { f .name } : { pcontents } \n " )
251
261
f .write (pcontents )
252
262
return f .name
0 commit comments