Skip to content

Commit 2669a49

Browse files
authored
Allow multiple input test cases (#85)
1 parent c12f106 commit 2669a49

File tree

5 files changed

+53
-23
lines changed

5 files changed

+53
-23
lines changed

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@ You will get output like so:
2929
```bash
3030
$ invoke test python any
3131

32-
> 🟢 script "./src/python/sort_builtin.py" succeeded
33-
> 🟢 script "./src/python/sort_bubble_sort.py" succeeded
34-
> 🟢 script "./src/python/sort_selection_sort.py" succeeded
35-
> 🟢 script "./src/python/sort_insertion_sort.py" succeeded
36-
> ✨ script run success ✨
32+
docker run ...
33+
⏱ ./src/python/sort_builtin.py on ./data/sort_input_0.txt ran for 0.5 seconds
34+
🟢 ./src/python/sort_builtin.py on ./data/sort_input_0.txt succeeded
35+
docker run ...
36+
⏱ ./src/python/sort_bubble_sort.py on ./data/sort_input_0.txt ran for 0.51 seconds
37+
🟢 ./src/python/sort_bubble_sort.py on ./data/sort_input_0.txt succeeded
38+
docker run ...
39+
⏱ ./src/python/sort_selection_sort.py on ./data/sort_input_0.txt ran for 0.58 seconds
40+
🟢 ./src/python/sort_selection_sort.py on ./data/sort_input_0.txt succeeded
41+
docker run ...
42+
⏱ ./src/python/sort_insertion_sort.py on ./data/sort_input_0.txt ran for 0.52 seconds
43+
🟢 ./src/python/sort_insertion_sort.py on ./data/sort_input_0.txt succeeded
44+
45+
✨ script run success ✨
3746
```
3847

3948
_(note: I will likely not bother to update the above example when the output changes in minor ways)_
File renamed without changes.
File renamed without changes.

src/readme.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

tasks.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ class TestRunnerContext:
4242
script_name: str
4343
docker_pull: str
4444
docker_run_test: str
45+
script_type: str
4546
script_relative_path: str
4647
script_name_with_file_type: str
4748
script_output_file_path: str
4849
script_output_file_name: str
50+
input_file_path: str
4951
prepared_file_path: str
5052
snippet_start_line: int
5153
snippet_end_line: int
@@ -73,23 +75,31 @@ def __init__(self, language) -> None:
7375
# generate the contexts
7476
self.ctxs = []
7577
for script_path in glob.glob(f"{self.base_directory}/src/{language}/*"):
78+
# given "src/python/sort_builtin.py" => return "sort"
79+
script_type = script_path.split("/")[-1].split("_")[0]
80+
7681
# ignore helpers, metadata files, etc
7782
if config.get("ignoreFiles") and script_path.split("/")[-1] in config.get(
7883
"ignoreFiles"
7984
):
8085
continue
86+
8187
# ignore directories, generally compiled code
8288
if not os.path.isfile(script_path):
8389
continue
84-
# generate a context for this particular script
85-
if ctx := self.generate(language, config, script_path):
86-
self.ctxs.append(ctx)
90+
91+
for input_file_path in glob.glob(
92+
f"{self.data_folder_path}/{script_type}_input_*.txt"
93+
):
94+
# generate a context for this particular script
95+
if ctx := self.generate(language, config, script_path, input_file_path):
96+
self.ctxs.append(ctx)
8797

8898
@property
8999
def data(self):
90100
return [ctx.data for ctx in self.ctxs]
91101

92-
def generate(self, language, config, script_path):
102+
def generate(self, language, config, script_path, input_file_path):
93103
# given "src/python/sort_builtin.py" => split on "/" and return "sort_builtin.py"
94104
script_path_split_on_slash = script_path.split("/")
95105
script_name_with_file_type = script_path_split_on_slash[-1]
@@ -106,11 +116,14 @@ def generate(self, language, config, script_path):
106116

107117
# get the path of the file that's been prepared in advance
108118
# and has the output we would be expecting from out script
109-
prepared_file_path = f"{self.data_folder_path}/{script_type}_output.txt"
119+
prepared_file_path = input_file_path.replace("input", "output")
120+
121+
# given "data/sort_input_1.txt" => return "1"
122+
prepared_file_index = prepared_file_path.split("_")[-1].split(".")[0]
110123

111124
# our scripts write their output files to this path
112125
script_output_file_name = (
113-
f"output_{script_type}_via_{language}_{script_name}.txt"
126+
f"output_{language}_{script_name}_{prepared_file_index}.txt"
114127
)
115128
script_output_file_path = f"{self.data_folder_path}/{script_output_file_name}"
116129

@@ -142,7 +155,7 @@ def generate(self, language, config, script_path):
142155

143156
# construct ending call args
144157
docker_run_test_list += [
145-
f"-e=INPUT_PATH={self.data_folder_path}/{script_type}_input.txt",
158+
f"-e=INPUT_PATH={input_file_path}",
146159
f"-e=OUTPUT_PATH={script_output_file_path}",
147160
config["dockerImage"],
148161
*script_invoker,
@@ -170,14 +183,14 @@ def generate(self, language, config, script_path):
170183
if snippet_start_line != 0:
171184
raise Exception(
172185
f'Found multiple "{self.snippet_start_text}" lines in {script_relative_path}.\n'
173-
f"The text with found on lines {snippet_start_line - snippet_start_line_offset + 1} and {idx + 1}."
186+
f"The lines were {snippet_start_line - snippet_start_line_offset + 1} and {idx + 1}."
174187
)
175188
snippet_start_line = idx + 3
176189
if self.snippet_end_text in line:
177190
if snippet_end_line != 0:
178191
raise Exception(
179192
f'Found multiple "{self.snippet_end_text}" lines in {script_relative_path}.\n'
180-
f"The text with found on lines {snippet_end_line + snippet_end_line_offset + 1} and {idx + 1}."
193+
f"The lines were {snippet_end_line + snippet_end_line_offset + 1} and {idx + 1}."
181194
)
182195
snippet_end_line = idx - snippet_end_line_offset
183196
if snippet_start_line == 0:
@@ -195,10 +208,12 @@ def generate(self, language, config, script_path):
195208
script_name=script_name,
196209
docker_pull=docker_pull,
197210
docker_run_test=docker_run_test,
211+
script_type=script_type,
198212
script_relative_path=script_relative_path,
199213
script_name_with_file_type=script_name_with_file_type,
200214
script_output_file_path=script_output_file_path,
201215
script_output_file_name=script_output_file_name,
216+
input_file_path=input_file_path,
202217
prepared_file_path=prepared_file_path,
203218
snippet_start_line=snippet_start_line,
204219
snippet_end_line=snippet_end_line,
@@ -244,19 +259,24 @@ def run_tests(self, input_script):
244259
# report timing
245260
# we round the number so humans dont over-index on small differences
246261
print(
247-
f'\t⏱ script "{ctx.script_relative_path}" run for {round(end_time - start_time, 2)} seconds'
262+
f"\t{ctx.script_relative_path} on {ctx.input_file_path} "
263+
f"ran for {round(end_time - start_time, 2)} seconds"
248264
)
249265

250266
# check if the script invoke failed
251267
if output.exited != 0:
252268
self.set_success_status(False)
253-
print(f'\t🔴 script "{ctx.script_relative_path}" failed, reason:')
269+
print(
270+
f"\t🔴 {ctx.script_relative_path} on {ctx.input_file_path} failed, reason:"
271+
)
254272
print(f'\t\t the exit code "{output.exited}" was not 0')
255273

256274
# check if the output file was created
257275
if not os.path.exists(ctx.script_output_file_path):
258276
self.set_success_status(False)
259-
print(f'\t🔴 script "{ctx.script_relative_path}" failed, reason:')
277+
print(
278+
f"\t🔴 {ctx.script_relative_path} on {ctx.input_file_path} failed, reason:"
279+
)
260280
print(
261281
f"\t\t the output {ctx.script_output_file_name} file was not created"
262282
)
@@ -266,14 +286,18 @@ def run_tests(self, input_script):
266286
ctx.prepared_file_path, ctx.script_output_file_path
267287
):
268288
self.set_success_status(True)
269-
print(f'\t🟢 script "{ctx.script_relative_path}" succeeded')
289+
print(
290+
f"\t🟢 {ctx.script_relative_path} on {ctx.input_file_path} succeeded"
291+
)
270292

271293
# check if the output file does not match the prepared file
272294
if os.path.exists(ctx.script_output_file_path) and not filecmp.cmp(
273295
ctx.prepared_file_path, ctx.script_output_file_path
274296
):
275297
self.set_success_status(False)
276-
print(f'\t🔴 script "{ctx.script_relative_path}" failed, reason:')
298+
print(
299+
f"\t🔴 {ctx.script_relative_path} on {ctx.input_file_path} failed, reason:"
300+
)
277301
print(
278302
f"\t\t output file {ctx.script_output_file_name} has does not match the prepared file"
279303
)
@@ -370,4 +394,4 @@ def test(ctx: invoke.Context, language, input_script):
370394

371395
@invoke.task
372396
def clean(ctx: invoke.Context):
373-
ctx.run("git clean -fdx ./data/*")
397+
ctx.run("git clean -fdx ./data/output_*")

0 commit comments

Comments
 (0)