1- from functools import wraps
2- from collections import defaultdict
3- from pathlib import Path
4- from typing import Any , Callable , List
1+ import os
52import typing as ty
3+ import warnings
4+ from dataclasses import dataclass
5+ from functools import wraps
6+ from typing import Any , Callable
67
78from ref_utils .error import RefUtilsError
8- from .utils import print_ok , print_err
9- from dataclasses import dataclass , asdict
10- import warnings
11- import json
12- import os
139
14- TEST_RESULT_PATH = Path ("/var/test_result" )
10+ from .utils import print_err , print_ok
11+
1512DEFAULT_TASK_NAME = 'default'
1613__registered_tasks : ty .Dict [str , '_Task' ] = {}
1714
@@ -29,9 +26,10 @@ class TestResult():
2926
3027
3128@dataclass
32- class _TestResult ():
29+ class TaskTestResult ():
3330 """
34- Class used to serialize data before sending it to the webserver.
31+ The result of a task's test, including the task name.
32+ Used for serialization when sending results to the webserver.
3533 """
3634 task_name : str
3735 success : bool
@@ -111,30 +109,38 @@ def wrapper(*args: str, **kwargs: Any) -> Any:
111109
112110
113111
114- def run_tests () -> None :
112+ def run_tests (
113+ * ,
114+ result_will_be_submitted : bool = False ,
115+ only_run_these_tasks : ty .Optional [ty .Sequence [str ]] = None ,
116+ ) -> ty .List [TaskTestResult ]:
115117 """
116- Must be called by the test script to execute all tests.
118+ Execute all registered tests.
119+
120+ Args:
121+ result_will_be_submitted: If True, indicates this is a final submission.
122+ only_run_these_tasks: Optional list of task names to run. If None, runs all tasks.
123+
124+ Returns:
125+ A list of TaskTestResult objects containing the results of each task.
117126 """
127+ # Set env var so test_result_will_be_submitted() works within tests
128+ if result_will_be_submitted :
129+ os .environ ["RESULT_WILL_BE_SUBMITTED" ] = "1"
130+
118131 print_ok ('[+] Running tests..' )
119132 all_tests_passed = True
120133 has_multiple_tasks = len (__registered_tasks ) > 1
121- task_test_results : ty .List [_TestResult ] = []
122-
123- # This is set by task.py if the user only wants to run a subset of tests.
124- only_run_these_tasks = os .environ .get ("ONLY_RUN_THESE_TASKS" )
125- if only_run_these_tasks :
126- only_run_these_tasks = only_run_these_tasks .split (":" )
134+ task_test_results : ty .List [TaskTestResult ] = []
127135
128136 # Run all sub-tasks one after another.
129137 for task_name , tests in __registered_tasks .items ():
130138 task_passed = True
131139
132- TEST_RESULT_PATH .unlink (missing_ok = True )
133-
134140 if has_multiple_tasks :
135141 print_ok (f'[+] *** Running tests for task \" { task_name } \" ***' )
136142 if only_run_these_tasks and task_name not in only_run_these_tasks :
137- print_ok (f "[+] User requested to exclude task, skipping..." )
143+ print_ok ("[+] User requested to exclude task, skipping..." )
138144 continue
139145
140146 if tests .env_tests and not tests .submission_test and not tests .extended_submission_test :
@@ -150,7 +156,7 @@ def run_tests() -> None:
150156
151157 #Do not run submission tests if the environ is invalid
152158 if not task_passed :
153- task_test_results .append (_TestResult (task_name , False , None ))
159+ task_test_results .append (TaskTestResult (task_name , False , None ))
154160 if has_multiple_tasks :
155161 # Only print this if we have multiple tasks. If we only have one,
156162 # the would just duplicate the error printed at the end.
@@ -170,9 +176,9 @@ def run_tests() -> None:
170176 ret = False
171177
172178 if isinstance (ret , bool ):
173- ret = _TestResult (task_name , ret , None )
179+ ret = TaskTestResult (task_name , ret , None )
174180 elif isinstance (ret , TestResult ):
175- ret = _TestResult (task_name , ret .success , ret .score )
181+ ret = TaskTestResult (task_name , ret .success , ret .score )
176182 else :
177183 raise RefUtilsError (f"Submission test returned unexpected type: { type (ret )} " )
178184
@@ -181,7 +187,7 @@ def run_tests() -> None:
181187 all_tests_passed &= ret .success
182188 else :
183189 # If there is no test, we consider this to be an success.
184- task_test_results .append (_TestResult (task_name , True , None ))
190+ task_test_results .append (TaskTestResult (task_name , True , None ))
185191 print_ok ("[+] No test found" )
186192
187193 if not task_passed and has_multiple_tasks :
@@ -197,5 +203,4 @@ def run_tests() -> None:
197203 else :
198204 print_ok ('[+] All tests passed! Good job. Ready to submit!' )
199205
200- results = json .dumps ([asdict (e ) for e in task_test_results ])
201- TEST_RESULT_PATH .write_text (results )
206+ return task_test_results
0 commit comments