Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishkeshan committed Feb 29, 2024
1 parent 56cea1b commit be42034
Showing 1 changed file with 100 additions and 80 deletions.
180 changes: 100 additions & 80 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,103 @@
const process = require("process");
const cp = require("child_process");
const path = require("path");
const process = require('process')
const cp = require('child_process')
const path = require('path')

const node = process.execPath;
const ip = path.join(__dirname, "..", "src", "main.js");
const node = process.execPath
const ip = path.join(__dirname, '..', 'src', 'main.js')
const options = {
env: process.env,
encoding: "utf-8",
};

const { parseRunnerResults } = require("../src/main");

test("test runs", () => {
process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString("base64");
process.env.INPUT_RUNNERS = "result1";

const child = cp.spawnSync(node, [ip], options);
const stdout = child.stdout.toString();
expect(stdout).toContain(`✅ Test 1`);
});

test("test fails", () => {
process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "fail", "message": "Test failed with non-zero exit code." }] }').toString("base64");
process.env.INPUT_RUNNERS = "result1";

const child = cp.spawnSync(node, [ip], options);
const stdout = child.stdout.toString();
expect(stdout).toContain(`❌ Test 1`);
});

test("test errors out", () => {
process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "error", "message": "Test failed to execute." }] }').toString("base64");
process.env.INPUT_RUNNERS = "result1";

const child = cp.spawnSync(node, [ip], options);
const stdout = child.stdout.toString();
expect(stdout).toContain(`Error: Test failed to execute.`);
});

test("fails to run if input not in the right format", () => {
process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString("base64");
process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString("base64");
process.env.INPUT_RUNNERS = "[result1,result2]";

const child = cp.spawnSync(node, [ip], options);
const stderr = child.stderr.toString();
expect(stderr).toContain(`The runners input must be a comma-separated list of strings.`);
});

// test('test runs with multiple runners', () => {
// process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }').toString('base64')
// process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString('base64')
// process.env.INPUT_RUNNERS = 'result1,result2'

// const child = cp.spawnSync(node, [ip], options)
// const stdout = child.stdout.toString()
// console.log(stdout)
// expect(stdout).toBe('pass')
// })

// test('test fails with multiple runners', () => {
// process.env.RESULT1_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 1", "status": "fail", "message": null }] }').toString('base64')
// process.env.RESULT2_RESULTS = Buffer.from('{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }').toString('base64')
// process.env.INPUT_RUNNERS = 'result1,result2'

// const child = cp.spawnSync(node, [ip], options)
// const stdout = child.stdout.toString()
// console.log(stdout)
// expect(stdout).toBe('fail')
// })

test("test autograding-output.parseRunnerResults", function () {
process.env.INPUT_RUNNERS = "result1,result2";

let testResults1 = parseRunnerResults(process.env.INPUT_RUNNERS);
expect(testResults1.length).toBe(2);
expect(testResults1[0].runner).toBe("result1");
expect(testResults1[1].runner).toBe("result2");

process.env.INPUT_RUNNERS = "[result1,result2]";
expect(() => parseRunnerResults(process.env.INPUT_RUNNERS)).toThrow("The runners input must be a comma-separated list of strings.");
});
encoding: 'utf-8',
}

const {parseRunnerResults} = require('../src/main')

test('test runs', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }',
).toString('base64')
process.env.INPUT_RUNNERS = 'result1'

const child = cp.spawnSync(node, [ip], options)
const stdout = child.stdout.toString()
expect(stdout).toContain(`✅ Test 1`)
})

test('test fails', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "fail", "message": "Test failed with non-zero exit code." }] }',
).toString('base64')
process.env.INPUT_RUNNERS = 'result1'

const child = cp.spawnSync(node, [ip], options)
const stdout = child.stdout.toString()
expect(stdout).toContain(`❌ Test 1`)
})

test('test errors out', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "error", "message": "Test failed to execute." }] }',
).toString('base64')
process.env.INPUT_RUNNERS = 'result1'

const child = cp.spawnSync(node, [ip], options)
const stdout = child.stdout.toString()
expect(stdout).toContain(`Error: Test failed to execute.`)
})

test('fails to run if input not in the right format', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }',
).toString('base64')
process.env.RESULT2_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }',
).toString('base64')
process.env.INPUT_RUNNERS = '[result1,result2]'

const child = cp.spawnSync(node, [ip], options)
const stderr = child.stderr.toString()
expect(stderr).toContain(`The runners input must be a comma-separated list of strings.`)
})

test('test runs with multiple runners', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "pass", "message": null }] }',
).toString('base64')
process.env.RESULT2_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }',
).toString('base64')
process.env.INPUT_RUNNERS = 'result1,result2'

const child = cp.spawnSync(node, [ip], options)
const stdout = child.stdout.toString()
console.log(stdout)
expect(stdout).toContain('✅')
})

test('test fails with multiple runners', () => {
process.env.RESULT1_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 1", "status": "fail", "message": null }] }',
).toString('base64')
process.env.RESULT2_RESULTS = Buffer.from(
'{ "tests": [{ "name": "Test 2", "status": "pass", "message": null }] }',
).toString('base64')
process.env.INPUT_RUNNERS = 'result1,result2'

const child = cp.spawnSync(node, [ip], options)
const stdout = child.stdout.toString()
console.log(stdout)
expect(stdout).toContain('❌')
})

test('test autograding-output.parseRunnerResults', function () {
process.env.INPUT_RUNNERS = 'result1,result2'

const testResults1 = parseRunnerResults(process.env.INPUT_RUNNERS)
expect(testResults1.length).toBe(2)
expect(testResults1[0].runner).toBe('result1')
expect(testResults1[1].runner).toBe('result2')

process.env.INPUT_RUNNERS = '[result1,result2]'
expect(() => parseRunnerResults(process.env.INPUT_RUNNERS)).toThrow(
'The runners input must be a comma-separated list of strings.',
)
})

0 comments on commit be42034

Please sign in to comment.