Skip to content

Commit e32d995

Browse files
authored
Replace the stdout extraction idiom with an EXTRACT command (#817)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 09a0cc2 commit e32d995

36 files changed

Lines changed: 62 additions & 116 deletions

scripts/clitest.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
COMPARE <actual> AGAINST <expected>
1313
REPLACE <pattern> WITH <replacement> IN <path>
1414
DROP LINES MATCHING <pattern> IN <path>
15-
KEEP LINES MATCHING <pattern> IN <path>
15+
EXTRACT STDOUT FROM <observation> INTO <destination>
1616
SORT <path>
1717
COPY <source> TO <destination>
1818
TREE <directory> INTO <destination>
@@ -266,7 +266,7 @@ def command_filter(self, keyword, operands, line_number):
266266
if len(operands) != 5 or operands[0] != "LINES" \
267267
or operands[1] != "MATCHING" or operands[3] != "IN":
268268
raise TestFailure(
269-
line_number, f"usage: {keyword} LINES MATCHING <regex> IN <file>")
269+
line_number, "usage: DROP LINES MATCHING <regex> IN <file>")
270270
pattern, replacement, name = operands[2], None, operands[4]
271271

272272
path = self.resolve(name, line_number)
@@ -278,10 +278,9 @@ def command_filter(self, keyword, operands, line_number):
278278
content = expression.sub(
279279
self.expand(replacement, line_number).replace("\\", "\\\\"), content)
280280
else:
281-
keep = keyword == "KEEP"
282281
content = "".join(
283282
line for line in content.splitlines(keepends=True)
284-
if bool(expression.search(line)) == keep)
283+
if not expression.search(line))
285284
self.write(path, content)
286285

287286
def command_tree(self, operands, line_number):
@@ -330,6 +329,22 @@ def command_checksum(self, operands, line_number):
330329
with open(self.resolve(operands[1], line_number), "rb") as handle:
331330
self.environment[name] = hashlib.sha256(handle.read()).hexdigest()
332331

332+
def command_extract(self, operands, line_number):
333+
if len(operands) != 5 or operands[0] != "STDOUT" \
334+
or operands[1] != "FROM" or operands[3] != "INTO":
335+
raise TestFailure(
336+
line_number,
337+
"usage: EXTRACT STDOUT FROM <observation> INTO <destination>")
338+
content = self.read(self.resolve(operands[2], line_number), line_number)
339+
extracted = []
340+
for line in content.splitlines():
341+
if line == "1>":
342+
extracted.append("")
343+
elif line.startswith("1> "):
344+
extracted.append(line[len("1> "):])
345+
self.write(self.resolve(operands[4], line_number),
346+
"".join(line + "\n" for line in extracted))
347+
333348
def command_sort(self, operands, line_number):
334349
if len(operands) != 1:
335350
raise TestFailure(line_number, "usage: SORT <file>")
@@ -482,6 +497,9 @@ def collect(destination, tokens):
482497
elif statement.keyword == "CHECKSUM" and len(operands) == 4:
483498
definitions.append((position, operands[3], statement.number))
484499
collect(consumed, [operands[1]])
500+
elif statement.keyword == "EXTRACT" and len(operands) == 5:
501+
record(artifacts, operands[4], (statement.number, "EXTRACT"))
502+
collect(consumed, [operands[2]])
485503
elif statement.keyword == "STAT" and len(operands) == 4:
486504
record(artifacts, operands[3], (statement.number, "STAT"))
487505
collect(consumed, [operands[1]])
@@ -557,7 +575,7 @@ def run_script(path, binary, environment):
557575
interpreter.command_compare(rest, number)
558576
elif keyword == "ENV":
559577
interpreter.command_env(rest, number)
560-
elif keyword in ("REPLACE", "DROP", "KEEP"):
578+
elif keyword in ("REPLACE", "DROP"):
561579
interpreter.command_filter(keyword, rest, number)
562580
elif keyword == "TREE":
563581
interpreter.command_tree(rest, number)
@@ -567,6 +585,8 @@ def run_script(path, binary, environment):
567585
interpreter.command_compress(rest, number)
568586
elif keyword == "CHECKSUM":
569587
interpreter.command_checksum(rest, number)
588+
elif keyword == "EXTRACT":
589+
interpreter.command_extract(rest, number)
570590
elif keyword == "SORT":
571591
interpreter.command_sort(rest, number)
572592
elif keyword == "STAT":

test/bundle/pass_bigint.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ EOF
2424

2525
RUN bundle schema.json --resolve nested.json STDIN /dev/null IN . INTO result.txt EXPECTING 0
2626

27-
COPY result.txt TO bundled.json
28-
KEEP LINES MATCHING '^1>' IN bundled.json
29-
REPLACE '^1> ?' WITH '' IN bundled.json
27+
EXTRACT STDOUT FROM result.txt INTO bundled.json
3028

3129
WRITE expected_0.txt UNTIL EOF
3230
1> {

test/bundle/pass_boolean_schema_default_dialect.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ EOF
77

88
RUN bundle schema.json --default-dialect https://json-schema.org/draft/2020-12/schema STDIN /dev/null IN . INTO result.txt EXPECTING 0
99

10-
COPY result.txt TO bundled.json
11-
KEEP LINES MATCHING '^1>' IN bundled.json
12-
REPLACE '^1> ?' WITH '' IN bundled.json
10+
EXTRACT STDOUT FROM result.txt INTO bundled.json
1311

1412
WRITE expected_0.txt UNTIL EOF
1513
1> true

test/bundle/pass_bundled_metaschema.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ EOF
2222

2323
RUN bundle schema.json STDIN /dev/null IN . INTO result.txt EXPECTING 0
2424

25-
COPY result.txt TO bundled.json
26-
KEEP LINES MATCHING '^1>' IN bundled.json
27-
REPLACE '^1> ?' WITH '' IN bundled.json
25+
EXTRACT STDOUT FROM result.txt INTO bundled.json
2826

2927
WRITE expected_0.txt UNTIL EOF
3028
1> {

test/bundle/pass_config_ignore.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ EOF
4141

4242
RUN bundle schema.json --resolve schemas STDIN /dev/null IN . INTO result.txt EXPECTING 0
4343

44-
COPY result.txt TO bundled.json
45-
KEEP LINES MATCHING '^1>' IN bundled.json
46-
REPLACE '^1> ?' WITH '' IN bundled.json
44+
EXTRACT STDOUT FROM result.txt INTO bundled.json
4745

4846
WRITE expected_0.txt UNTIL EOF
4947
1> {

test/bundle/pass_into_resolve_directory.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ EOF
2828

2929
RUN bundle schema.json --resolve schemas STDIN /dev/null IN . INTO result.txt EXPECTING 0
3030

31-
COPY result.txt TO bundled.json
32-
KEEP LINES MATCHING '^1>' IN bundled.json
33-
REPLACE '^1> ?' WITH '' IN bundled.json
31+
EXTRACT STDOUT FROM result.txt INTO bundled.json
3432

3533
WRITE expected_0.txt UNTIL EOF
3634
1> {

test/bundle/pass_resolve_directory.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ EOF
2323

2424
RUN bundle schema.json --resolve schemas STDIN /dev/null IN . INTO result.txt EXPECTING 0
2525

26-
COPY result.txt TO bundled.json
27-
KEEP LINES MATCHING '^1>' IN bundled.json
28-
REPLACE '^1> ?' WITH '' IN bundled.json
26+
EXTRACT STDOUT FROM result.txt INTO bundled.json
2927

3028
WRITE expected_0.txt UNTIL EOF
3129
1> {

test/bundle/pass_resolve_metaschema.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ EOF
4040

4141
RUN bundle schema.json --resolve schemas STDIN /dev/null IN . INTO result.txt EXPECTING 0
4242

43-
COPY result.txt TO bundled.json
44-
KEEP LINES MATCHING '^1>' IN bundled.json
45-
REPLACE '^1> ?' WITH '' IN bundled.json
43+
EXTRACT STDOUT FROM result.txt INTO bundled.json
4644

4745
WRITE expected_0.txt UNTIL EOF
4846
1> {

test/bundle/pass_resolve_no_identifier.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ EOF
1919

2020
RUN bundle schema.json --resolve nested.json STDIN /dev/null IN . INTO result.txt EXPECTING 0
2121

22-
COPY result.txt TO bundled.json
23-
KEEP LINES MATCHING '^1>' IN bundled.json
24-
REPLACE '^1> ?' WITH '' IN bundled.json
22+
EXTRACT STDOUT FROM result.txt INTO bundled.json
2523

2624
REPLACE $CWD_URI WITH '[CWD_URI]' IN result.txt
2725
REPLACE $CWD WITH '[CWD]' IN result.txt

test/bundle/pass_resolve_single.clitest

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ EOF
2121

2222
RUN bundle schema.json --resolve remote.json STDIN /dev/null IN . INTO result.txt EXPECTING 0
2323

24-
COPY result.txt TO bundled.json
25-
KEEP LINES MATCHING '^1>' IN bundled.json
26-
REPLACE '^1> ?' WITH '' IN bundled.json
24+
EXTRACT STDOUT FROM result.txt INTO bundled.json
2725

2826
WRITE expected_0.txt UNTIL EOF
2927
1> {

0 commit comments

Comments
 (0)