Skip to content

Commit b044eef

Browse files
committed
Support test patterns in 'gen' and 'invoke'
1 parent a19fb71 commit b044eef

File tree

5 files changed

+42
-24
lines changed

5 files changed

+42
-24
lines changed

scripts/gen.sh

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ function usage {
1313
errcho -e " -s, --sensitive"
1414
errcho -e "\tTerminates on the first error."
1515
errcho -e " -m, --model-solution=<model-solution-path>"
16-
errcho -e " -t, --test=<test-name>"
16+
errcho -e " -t, --test=<test-name-pattern>"
17+
errcho -e "\tGenerates only tests matching the given pattern. Examples: 1-01, '1-*', '1-0?'"
1718
errcho -e " -d, --gen-data=<gen-data-file>"
1819
errcho -e " --no-gen"
1920
errcho -e " --no-sol"
@@ -25,8 +26,8 @@ function usage {
2526
model_solution=""
2627
gen_data_file="${GEN_DIR}/data"
2728
SENSITIVE_RUN="false"
28-
SINGULAR_TEST="false"
29-
SOLE_TEST_NAME=""
29+
SPECIFIC_TESTS="false"
30+
SPECIFIED_TESTS_PATTERN=""
3031
SKIP_GEN="false"
3132
SKIP_SOL="false"
3233
SKIP_VAL="false"
@@ -40,8 +41,8 @@ function handle_option {
4041
exit 0
4142
;;
4243
-t|--test=*)
43-
fetch_arg_value "SOLE_TEST_NAME" "-t" "--test" "test name"
44-
SINGULAR_TEST="true"
44+
fetch_arg_value "SPECIFIED_TESTS_PATTERN" "-t" "--test" "test name"
45+
SPECIFIC_TESTS="true"
4546
;;
4647
-m|--model-solution=*)
4748
fetch_arg_value "model_solution" "-m" "--model-solution" "solution path"
@@ -88,7 +89,7 @@ sensitive check_file_exists "Generation data file" "${gen_data_file}"
8889

8990
command_exists dos2unix || cecho yellow "WARNING: dos2unix is not available. Line endings might be incorrect."
9091

91-
export SENSITIVE_RUN SINGULAR_TEST SOLE_TEST_NAME SKIP_GEN SKIP_SOL SKIP_VAL
92+
export SENSITIVE_RUN SPECIFIC_TESTS SPECIFIED_TESTS_PATTERN SKIP_GEN SKIP_SOL SKIP_VAL
9293

9394

9495
recreate_dir "${LOGS_DIR}"

scripts/internal/gen.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import os
22
import sys
33
from collections import defaultdict
4-
from gen_data_parser import DataVisitor, parse_data, data_parse_error, check_test_exists
4+
from gen_data_parser import DataVisitor, parse_data, data_parse_error, check_test_pattern_exists, test_name_matches_pattern
55
from util import load_json, run_bash_command
66

77

88
PROBLEM_JSON = os.environ.get('PROBLEM_JSON')
99
SUBTASKS_JSON = os.environ.get('SUBTASKS_JSON')
1010
INTERNALS_DIR = os.environ.get('INTERNALS')
11-
SINGULAR_TEST = os.environ.get('SINGULAR_TEST')
12-
SOLE_TEST_NAME = os.environ.get('SOLE_TEST_NAME')
11+
SPECIFIC_TESTS = os.environ.get('SPECIFIC_TESTS')
12+
SPECIFIED_TESTS_PATTERN = os.environ.get('SPECIFIED_TESTS_PATTERN')
1313

1414

1515
class SummaryVisitor(DataVisitor):
@@ -61,7 +61,7 @@ def print_mapping(self, stream):
6161

6262
class GeneratingVisitor(DataVisitor):
6363
def on_test(self, testset_name, test_name, line, line_number):
64-
if SINGULAR_TEST == "false" or test_name == SOLE_TEST_NAME:
64+
if SPECIFIC_TESTS == "false" or test_name_matches_pattern(test_name, SPECIFIED_TESTS_PATTERN):
6565
command = [
6666
'bash',
6767
os.path.join(INTERNALS_DIR, 'gen_test.sh'),
@@ -75,8 +75,8 @@ def on_test(self, testset_name, test_name, line, line_number):
7575
task_data = load_json(PROBLEM_JSON)
7676
gen_data = sys.stdin.readlines()
7777

78-
if SINGULAR_TEST == "true":
79-
check_test_exists(gen_data, task_data, SOLE_TEST_NAME)
78+
if SPECIFIC_TESTS == "true":
79+
check_test_pattern_exists(gen_data, task_data, SPECIFIED_TESTS_PATTERN)
8080

8181
summary_visitor = SummaryVisitor()
8282
parse_data(gen_data, task_data, summary_visitor)

scripts/internal/gen_data_parser.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import fnmatch
34
from test_name import get_test_name
45

56
line_number = 0
@@ -113,3 +114,18 @@ def check_test_exists(gen_data, task_data, test_name):
113114
if not tests_visitor.has_test(test_name):
114115
sys.stderr.write("Invalid test name '%s'\n" % test_name)
115116
exit(2)
117+
118+
119+
120+
def test_name_matches_pattern(test_name, pattern):
121+
return fnmatch.fnmatchcase(test_name, pattern)
122+
123+
124+
125+
def check_test_pattern_exists(gen_data, task_data, test_pattern):
126+
tests_visitor = TestsVisitor()
127+
parse_data(gen_data, task_data, tests_visitor)
128+
if not [test_name for test_name in tests_visitor.tests if test_name_matches_pattern(test_name, test_pattern)]:
129+
sys.stderr.write("No test name matches the pattern '%s'\n" % test_pattern)
130+
exit(2)
131+

scripts/internal/invoke.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import os
22
import sys
33

4-
from gen_data_parser import DataVisitor, parse_data, check_test_exists
4+
from gen_data_parser import DataVisitor, parse_data, check_test_pattern_exists, test_name_matches_pattern
55
from util import load_json, run_bash_command
66

77

88
PROBLEM_JSON = os.environ.get('PROBLEM_JSON')
99
INTERNALS_DIR = os.environ.get('INTERNALS')
10-
SINGULAR_TEST = os.environ.get('SINGULAR_TEST')
11-
SOLE_TEST_NAME = os.environ.get('SOLE_TEST_NAME')
10+
SPECIFIC_TESTS = os.environ.get('SPECIFIC_TESTS')
11+
SPECIFIED_TESTS_PATTERN = os.environ.get('SPECIFIED_TESTS_PATTERN')
1212

1313

1414
class InvokingVisitor(DataVisitor):
1515
def on_test(self, testset_name, test_name, line, line_number):
16-
if SINGULAR_TEST == "false" or test_name == SOLE_TEST_NAME:
16+
if SPECIFIC_TESTS == "false" or test_name_matches_pattern(test_name, SPECIFIED_TESTS_PATTERN):
1717
command = [
1818
'bash',
1919
os.path.join(INTERNALS_DIR, 'invoke_test.sh'),
@@ -25,7 +25,7 @@ def on_test(self, testset_name, test_name, line, line_number):
2525
task_data = load_json(PROBLEM_JSON)
2626
gen_data = sys.stdin.readlines()
2727

28-
if SINGULAR_TEST == "true":
29-
check_test_exists(gen_data, task_data, SOLE_TEST_NAME)
28+
if SPECIFIC_TESTS == "true":
29+
check_test_pattern_exists(gen_data, task_data, SPECIFIED_TESTS_PATTERN)
3030

3131
parse_data(gen_data, task_data, InvokingVisitor())

scripts/invoke.sh

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ function usage {
1414
errcho -e "\tTerminates on the first error."
1515
errcho -e " -r, --show-reason"
1616
errcho -e "\tDisplays the reason for not being accepted, e.g. checker output"
17-
errcho -e " -t, --test=<test-name>"
17+
errcho -e " -t, --test=<test-name-pattern>"
18+
errcho -e "\tInvokes only tests matching the given pattern. Examples: 1-01, '1-*', '1-0?'"
1819
errcho -e " -d, --gen-data=<gen-data-file>"
1920
errcho -e " --no-check"
2021
errcho -e " --no-sol-compile"
@@ -30,8 +31,8 @@ function usage {
3031
gen_data_file="${GEN_DIR}/data"
3132
SHOW_REASON="false"
3233
SENSITIVE_RUN="false"
33-
SINGULAR_TEST="false"
34-
SOLE_TEST_NAME=""
34+
SPECIFIC_TESTS="false"
35+
SPECIFIED_TESTS_PATTERN=""
3536
SKIP_CHECK="false"
3637
skip_compile_sol="false"
3738

@@ -44,8 +45,8 @@ function handle_option {
4445
exit 0
4546
;;
4647
-t|--test=*)
47-
fetch_arg_value "SOLE_TEST_NAME" "-t" "--test" "test name"
48-
SINGULAR_TEST="true"
48+
fetch_arg_value "SPECIFIED_TESTS_PATTERN" "-t" "--test" "test name"
49+
SPECIFIC_TESTS="true"
4950
;;
5051
-s|--sensitive)
5152
SENSITIVE_RUN="true"
@@ -123,7 +124,7 @@ sensitive check_file_exists "Solution file" "${solution}"
123124

124125
sensitive check_file_exists "Generation data file" "${gen_data_file}"
125126

126-
export SHOW_REASON SENSITIVE_RUN SINGULAR_TEST SOLE_TEST_NAME SKIP_CHECK SOFT_TL HARD_TL
127+
export SHOW_REASON SENSITIVE_RUN SPECIFIC_TESTS SPECIFIED_TESTS_PATTERN SKIP_CHECK SOFT_TL HARD_TL
127128

128129

129130
recreate_dir "${LOGS_DIR}"

0 commit comments

Comments
 (0)