/** * Corrector para la práctica de cmd */ // IMPORTS const should = require('chai').should(); const path = require('path'); const fs = require('fs-extra'); const Utils = require('./utils'); const to = require('./to'); const child_process = require("child_process"); const spawn = require("child_process").spawn; const path_assignment = path.resolve(path.join(__dirname, "../")); // CRITICAL ERRORS let error_critical = null; // CONSTANTS const T_WAIT = 2; // Time between commands const T_TEST = 2 * 60; // Time between tests (seconds) // HELPERS const timeout = ms => new Promise(res => setTimeout(res, ms)); //TESTS describe("mooc_node-mod5_quiz_cmd", function () { this.timeout(T_TEST * 1000); it(`1: Checking that the assignment directory exists...`, async function () { this.score = 0.34; this.msg_ok = `Found the directory '${path_assignment}'`; this.msg_err = `Couldn't find the directory '${path_assignment}'`; const [error_path, path_ok] = await to(fs.pathExists(path_assignment)); if (error_path) { error_critical = this.msg_err; } path_ok.should.be.equal(true); }); it(`2: Installing dependencies...`, async function () { this.score = 0.33; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { this.msg_ok = "Dependencies installed successfully"; this.msg_err = "Error installing dependencies"; // check that package.json exists const path_json = path.join(path_assignment, 'package.json'); const [json_nok, json_ok] = await to(fs.pathExists(path_json)); if (json_nok || !json_ok) { this.msg_err = `The file '${path_json}' has not been found`; error_critical = this.msg_err; } json_ok.should.be.equal(true); // check package.json format const [error_json, contenido] = await to(fs.readFile(path_json, 'utf8')); if (error_json) { this.msg_err = `The file '${path_json}' doesn't have the right format`; error_critical = this.msg_err; } should.not.exist(error_json); const es_json = Utils.isJSON(contenido); if (!es_json) { error_critical = this.msg_err; } es_json.should.be.equal(true); // run npm install let error_deps; try { child_process.execSync("npm install", {cwd: path_assignment}).toString(); } catch (error_deps) { this.msg_err = "Error running 'npm install': " + error_deps; error_critical = this.msg_err; console.log(error_deps); } should.not.exist(error_deps); // inject local figlet try { const figdata = "module.exports.textSync = function(text){return text};"; fs.removeSync(path.join(path_assignment, 'node_modules', 'figlet')); fs.mkdirSync(path.join(path_assignment, 'node_modules', 'figlet')); fs.writeFileSync(path.join(path_assignment, 'node_modules', 'figlet', 'index.js'), figdata, { encoding: 'utf8', flag: 'w' }); } catch (error) { debug("Error wrapping figlet"); } // replace answers file [_, path_ok] = await to(fs.move(path.join(path_assignment, 'quizzes.json'), path.join(path_assignment, 'quizzes.original.json'), {"overwrite": true})); [error_deps, path_ok] = await to(fs.copy(path.join(path_assignment, 'tests', 'quizzes.json'), path.join(path_assignment, 'quizzes.json'), {"overwrite": true})); if (error_deps) { this.msg_err = "Error copying the answers file: " + error_deps; error_critical = this.msg_err; } should.not.exist(error_deps); } }); it(`3: Checking that the file 'quizzes.json' is read. Running 'list'...`, async function () { this.score = 0.33; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["list"]; const expected = "Answer Number 1"; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); if (client) { client.stdin.write(input[0] + "\n"); } await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`4: Checking that invalid input parameters are detected. Running 'test'...`, async function () { this.score = 1; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["test"]; const expected = /error/img; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`5: Checking that right answers are detected. Running 'test 1'...`, async function () { this.score = 1; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["test 1", "OK"]; const expected = /\bcorrect/img; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`6: Checking that wrong answers are detected. Running 'test 1'...`, async function () { this.score = 1; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["test 1", "NOK"]; const expected = /incorrect/img; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`7: Checking that right answers are detected. Running 'play'...`, async function () { this.score = 1; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["play", "OK"]; const expected = /correct/img; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`8: Checking that answers are correctly scored. Running 'play'...`, async function () { this.score = 1; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["play", "OK"]; const expected = /1/; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; console.log(expected, "OUT", output); error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`9: Checking that wrong answers are detected. Running 'play'...`, async function () { this.score = 2; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["play", "NOK"]; const expected = /incorrect/img; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); it(`10: Checking that the wrong answer ends the game. Running 'play'...`, async function () { this.score = 2; if (error_critical) { this.msg_err = error_critical; should.not.exist(error_critical); } else { const input = ["play", "NOK"]; const expected = "fin"; let output = ""; let error_std = ""; const client = spawn("node", ["main.js"], {cwd: path_assignment}); client.on('error', function (data) { error_std += data }); client.stdin.on('data', function (data) { output += data }); client.stdout.on('data', function (data) { output += data }); client.stderr.on('data', function (data) { error_std += data }); await timeout(T_WAIT * 1000); client.stdin.write(input[0] + "\n"); await timeout(T_WAIT * 1000); client.stdin.write(input[1] + "\n"); await timeout(T_WAIT * 1000); if (client) { client.kill(); } this.msg_ok = `Found '${expected}' in ${path_assignment}`; this.msg_err = `Couldn't find '${expected}' in ${path_assignment}\nError:${error_std}\nReceived:${output}`; error_std.should.be.equal(""); Utils.search(expected, output).should.be.equal(true); } }); after("Restoring the original file", async function () { [error_copy, path_ok] = await to(fs.move(path.join(path_assignment, 'quizzes.original.json'), path.join(path_assignment, 'quizzes.json'), {"overwrite": true})); }); });