Skip to content

Commit 0e617c9

Browse files
committed
fix bug when running tutor tests
1 parent 24edd25 commit 0e617c9

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

python/code/wypp/instrument.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ def find_spec(
140140
target: types.ModuleType | None = None,
141141
) -> ModuleSpec | None:
142142

143-
debug(f'Consulting InstrumentingFinder.find_spec for fullname={fullname}')
143+
debug(f'Consulting InstrumentingFinder.find_spec for fullname={fullname}, path={path}, target={target}')
144144
# 1) The fullname is the name of the main module. This might be a dotted name such as x.y.z.py
145145
# so we have special logic here
146146
fp = os.path.join(self.modDir, f"{fullname}.py")
147+
debug(f'fullPath: {fp}')
147148
if self.mainModName == fullname and os.path.isfile(fp):
148149
loader = InstrumentingLoader(fullname, fp)
149150
spec = spec_from_file_location(fullname, fp, loader=loader)

python/code/wypp/runCode.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import importlib
44
import runpy
55
from dataclasses import dataclass
6+
from typing import Optional
67

78
# local imports
89
from constants import *
@@ -33,24 +34,29 @@ def __init__(self, mod, properlyImported):
3334

3435
@dataclass
3536
class 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

5561
def 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

Comments
 (0)