33import importlib
44import runpy
55from dataclasses import dataclass
6+ from typing import Optional
67
78# local imports
89from constants import *
@@ -33,24 +34,29 @@ def __init__(self, mod, properlyImported):
3334
3435@dataclass
3536class RunSetup :
36- def __init__ (self , pathDir : str , args : list [str ]):
37+ def __init__ (self , pathDir : str , args : Optional [ list [str ]] = None , installProfile : bool = True ):
3738 self .pathDir = os .path .abspath (pathDir )
3839 self .args = args
3940 self .sysPathInserted = False
4041 self .oldArgs = sys .argv
42+ self .installProfile = installProfile
4143 def __enter__ (self ):
4244 if self .pathDir not in sys .path :
4345 sys .path .insert (0 , self .pathDir )
4446 self .sysPathInserted = True
45- sys .argv = self .args
46- self .originalProfile = sys .getprofile ()
47- stacktrace .installProfileHook ()
47+ if self .args is not None :
48+ sys .argv = self .args
49+ if self .installProfile :
50+ self .originalProfile = sys .getprofile ()
51+ stacktrace .installProfileHook ()
4852 def __exit__ (self , exc_type , value , traceback ):
49- sys .setprofile (self .originalProfile )
53+ if self .installProfile :
54+ sys .setprofile (self .originalProfile )
5055 if self .sysPathInserted :
5156 sys .path .remove (self .pathDir )
5257 self .sysPathInserted = False
53- sys .argv = self .oldArgs
58+ if self .args is not None :
59+ sys .argv = self .oldArgs
5460
5561def prepareLib (onlyCheckRunnable , enableTypeChecking ):
5662 libDefs = None
@@ -108,6 +114,7 @@ def runTestsInFile(testFile, globals, libDefs, doTypecheck=True, extraDirs=[]):
108114 printStderr ()
109115 printStderr (f"Running tutor's tests in { testFile } " )
110116 libDefs .resetTestCount ()
117+ runCode (testFile , globals , doTypecheck = doTypecheck , extraDirs = extraDirs )
111118 try :
112119 runCode (testFile , globals , doTypecheck = doTypecheck , extraDirs = extraDirs )
113120 except :
@@ -123,7 +130,9 @@ def performChecks(check, testFile, globals, libDefs, doTypecheck=True, extraDirs
123130 if check :
124131 testResultsInstr = {'total' : 0 , 'failing' : 0 }
125132 if testFile :
126- testResultsInstr = runTestsInFile (testFile , globals , libDefs , doTypecheck = doTypecheck ,
127- extraDirs = extraDirs )
133+ testDir = os .path .dirname (testFile )
134+ with RunSetup (testDir ):
135+ testResultsInstr = runTestsInFile (testFile , globals , libDefs , doTypecheck = doTypecheck ,
136+ extraDirs = extraDirs )
128137 failingSum = testResultsStudent ['failing' ] + testResultsInstr ['failing' ]
129138 utils .die (0 if failingSum < 1 else 1 )
0 commit comments