UNPKG

grading

Version:

Grading of student submissions, in particular programming tests.

83 lines 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadCoverageReport = exports.loadUnitReport = void 0; const cliUtil_1 = require("../cli/cliUtil"); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const xml2js_1 = __importDefault(require("xml2js")); const processors_1 = require("xml2js/lib/processors"); /** * Loads JUnit-Report. * It returns a Testuites object. */ async function loadUnitReport(reportsDir, submissionID, type) { const reportFile = path_1.default.join(reportsDir, `_${submissionID}_junit.${type}.xml`); try { if (fs_1.default.existsSync(reportFile)) { const xmlParser = new xml2js_1.default.Parser({ mergeAttrs: true, explicitArray: false, trim: true, charkey: "message", attrValueProcessors: [processors_1.parseNumbers] }); const fileContent = await fs_1.default.promises.readFile(reportFile, "utf-8"); const unitReport = await xmlParser.parseStringPromise(fileContent); const testsuites = unitReport.testsuites; if (!Array.isArray(testsuites.testsuite)) { // enforce array testsuites.testsuite = [testsuites.testsuite]; } testsuites.testsuite.forEach(testsuite => { if (typeof testsuite.name !== 'string') { // enforce string testsuite.name = String(testsuite.name); } else { testsuite.name = testsuite.name.trim(); } if (!Array.isArray(testsuite.testcase)) { // enforce array testsuite.testcase = [testsuite.testcase]; } testsuite.testcase.forEach(testcase => { if (typeof testcase.name !== 'string') { // enforce string testcase.name = String(testcase.name); } else { testcase.name = testcase.name.trim(); } }); }); return testsuites; } else { (0, cliUtil_1.warn)(`Warning, unit report "${reportFile}" not found.`); } } catch (err) { (0, cliUtil_1.warn)("Error loading " + reportFile + ": " + err); } return null; } exports.loadUnitReport = loadUnitReport; /** * Reads coverage report in clover format */ async function loadCoverageReport(reportsDir, submissionid, type) { const coverageFile = path_1.default.join(reportsDir, `_${submissionid}_coverage.${type}.xml`); try { if (fs_1.default.existsSync(coverageFile)) { const xmlParser = new xml2js_1.default.Parser({ mergeAttrs: true, explicitArray: false, trim: true, charkey: "message", attrValueProcessors: [processors_1.parseNumbers] }); const fileContent = await fs_1.default.promises.readFile(coverageFile, "utf-8"); const coverageReport = await xmlParser.parseStringPromise(fileContent); return coverageReport.coverage; } } catch (err) { (0, cliUtil_1.warn)("Error loading " + coverageFile + ": " + err); } return null; } exports.loadCoverageReport = loadCoverageReport; //# sourceMappingURL=reportLoader.js.map