Skip to content

Commit 88ff197

Browse files
committed
Fix error code printing for segfault.
1 parent 34d1532 commit 88ff197

File tree

6 files changed

+66
-36
lines changed

6 files changed

+66
-36
lines changed

data/languages/cpp/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,9 @@ target_link_libraries(solution_expected_cpp PRIVATE nlohmann_json::nlohmann_json
7676
set(CMAKE_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bin)
7777

7878
install(TARGETS solution_cpp
79-
CONFIGURATIONS Release
8079
RUNTIME DESTINATION ${CMAKE_INSTALL_DIR}
8180
)
8281

8382
install(TARGETS solution_expected_cpp
84-
CONFIGURATIONS Release
8583
RUNTIME DESTINATION ${CMAKE_INSTALL_DIR}
8684
)

src/app/logger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def log(message: str):
1717
print(message)
1818

1919
def logResults(results):
20-
#TODO: Print indivitual test status for a failed run?
20+
duration = ("Unknown" if "duration_ms" not in results
21+
else str(results["duration_ms"]) + "ms")
2122
print("Status: " + str(results["status"]) + "\n" +
22-
"Duration: " + str(results["duration_ms"]) + "ms")
23+
"Duration: " + duration)

src/app/openleetcode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,17 @@ def main():
213213

214214
if not os.path.isfile(os.path.join(build_dir, "CMakeCache.txt")):
215215
print("CMakeCache.txt does not exist. Running CMake to configure the ")
216-
if run(f"cmake -B {build_dir}", src_dir) != 0:
216+
if run(f"cmake -B {build_dir} -DCMAKE_BUILD_TYPE=Debug", src_dir) != 0:
217217
print(logger.red(f"CMake failed!"))
218218
sys.exit(1)
219219
else:
220220
print("CMakeCache.txt exists. Skipping CMake configuration.")
221221

222-
if run(f"cmake --build . --config Release -j", build_dir) != 0:
222+
if run(f"cmake --build . --config Debug -j", build_dir) != 0:
223223
print(logger.red("Build failed!"))
224224
sys.exit(1)
225225

226-
if run(f"cmake --install .", build_dir) != 0:
226+
if run(f"cmake --install . --config Debug -v", build_dir) != 0:
227227
print(logger.red("Cmake install failed!"))
228228
sys.exit(1)
229229

src/app/testrunner.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,20 @@ def runTests(exec_filename,
6060
timeout=PROBLEM_LTE_S,
6161
stdout=subprocess.PIPE,
6262
stderr=subprocess.PIPE)
63-
6463
except subprocess.TimeoutExpired:
6564
return 1, f"Time Limit Exceeded"
6665
except Exception as e:
6766
return 1, f"Error running the test, error={e}"
6867

6968
if not os.path.isfile(test_restults_filename):
70-
return 1, f"The file {test_restults_filename} does not exist."
69+
jsonResultsObj = {
70+
"status": "Failed",
71+
"stderr": subprocess_obj.stderr.decode('utf-8'),
72+
"stdout": subprocess_obj.stdout.decode('utf-8'),
73+
"errorcode": hex(subprocess_obj.returncode)
74+
}
75+
with open(test_restults_filename, 'w') as f:
76+
json.dump(jsonResultsObj, f, indent=4)
7177

7278
# read test_restults_filename into a json object
7379
with open(test_restults_filename, 'r') as f:

src/schema/results_validation_schema.json

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,31 @@
9696
"$ref": "#/definitions/testcase_filter_name_schema"
9797
}
9898
},
99-
"required": ["duration_ms", "status", "tests", "stderr"],
100-
"if": {
101-
"not": {
102-
"properties": {
103-
"testcase_filter_name": { "pattern": "^All$", "flags": "i" }
99+
"required": ["status"],
100+
"allOf": [
101+
{
102+
"if": {
103+
"properties": {
104+
"errorcode": {}
105+
},
106+
"required": ["errorcode"]
107+
},
108+
"then": {
109+
"required": ["stdout", "stderr"]
110+
},
111+
"else": {
112+
"required": ["duration_ms", "tests", "stderr"],
113+
"if": {
114+
"not": {
115+
"properties": {
116+
"testcase_filter_name": { "pattern": "^All$", "flags": "i" }
117+
}
118+
}
119+
},
120+
"then": {
121+
"required": ["stdout"]
122+
}
104123
}
105124
}
106-
},
107-
"then": {
108-
"required": ["stdout"]
109-
}
125+
]
110126
}

src/ui/index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ function saveSolution(language, content) {
4141
file.writeFileSync(userSolutionFilename, content);
4242
}
4343

44-
function parseStdout(stdout) {
44+
function parseResultsFileFromStdout(stdout) {
4545
// stdout:
4646
// LongestSubstringWithoutRepeatingCharacters for testcase All in language cpp
4747
// Results written to /path\to/openleetcode/src/ui/testcase_output/<testname><datetime>.results
4848
// Status: <status>
4949
// Duration: <duration>ms
50-
return stdout.match(/Results written to (.*\.results)/)[1];
50+
match = stdout.match(/Results written to (.*\.results)/);
51+
if (!match || match.length === 0) {
52+
return null;
53+
}
54+
return match[1];
5155
}
5256

5357
function parseBuildError(stdout) {
@@ -156,31 +160,36 @@ function run(callback, testcase = 'All', expected = false) {
156160

157161
var resultsFilename;
158162
exec(command, (error, stdout, stderr) => {
159-
if (error) {
163+
var element = document.getElementById("compilation-content");
164+
165+
element.textContent = "";
166+
resultsFilename = parseResultsFileFromStdout(stdout);
167+
if (!resultsFilename || !file.existsSync(resultsFilename)) {
168+
console.log("Setting error");
160169
console.log("Error running the command, error: " + error +
161170
", stderr: " + stderr + ", stdout: " + stdout);
162-
var element = document.getElementById("compilation-content");
163-
element.textContent = parseBuildError(stdout + "\n" + error);
171+
element.textContent = parseBuildError(stdout);
164172
document.getElementById('tab-compilation-button').click();
165173
return;
166174
}
167-
var element = document.getElementById("compilation-content");
168-
element.textContent = "";
169-
170-
resultsFilename = parseStdout(stdout);
171-
if (!resultsFilename) {
172-
throw new Error("Could not parse results filename from stdout: " +
173-
"${stdout}");
174-
}
175175

176-
if (!file.existsSync(resultsFilename)) {
177-
throw new Error(`Results file does not exist: ${resultsFilename}`);
178-
}
179-
180176
const results = file.readFileSync(resultsFilename, 'utf8');
181177
console.log(results);
182178
const resultsJson = JSON.parse(results);
183-
callback(resultsJson);
179+
errorcode = resultsJson["errorcode"];
180+
console.log("errorcode: " + errorcode);
181+
if (errorcode != undefined && errorcode !== 0) {
182+
html = "<p>Errorcode: " + resultsJson.errorcode + "</p>";
183+
html += "<p>Stdout: " + resultsJson.stdout + "</p>";
184+
html += "<p>Stderr: " + resultsJson.stderr + "</p>";
185+
186+
element.innerHTML = html;
187+
document.getElementById('tab-compilation-button').click();
188+
return;
189+
} else {
190+
console.log("Setting results");
191+
callback(resultsJson);
192+
}
184193
});
185194
}
186195

0 commit comments

Comments
 (0)