move functions to util.ts

This commit is contained in:
Alex Kalyvitis 2020-06-22 17:17:25 +02:00
parent 2758bd30c8
commit dcba70915d
12 changed files with 189 additions and 186 deletions

View file

@ -1,5 +1,6 @@
import test from 'ava';
import * as fs from 'fs';
import * as os from "os";
import * as util from './util';
@ -8,3 +9,54 @@ test('getToolNames', t => {
const toolNames = util.getToolNames(input);
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
});
test('getMemoryFlag() should return the correct --ram flag', t => {
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const tests = {
"": `--ram=${totalMem - 256}`,
"512": "--ram=512",
};
for (const [input, expectedFlag] of Object.entries(tests)) {
process.env['INPUT_RAM'] = input;
const flag = util.getMemoryFlag();
t.deepEqual(flag, expectedFlag);
}
});
test('getMemoryFlag() throws if the ram input is < 0 or NaN', t => {
for (const input of ["-1", "hello!"]) {
process.env['INPUT_RAM'] = input;
t.throws(util.getMemoryFlag);
}
});
test('getThreadsFlag() should return the correct --threads flag', t => {
const numCpus = os.cpus().length;
const tests = {
"0": "--threads=0",
"1": "--threads=1",
[`${numCpus + 1}`]: `--threads=${numCpus}`
};
for (const [input, expectedFlag] of Object.entries(tests)) {
process.env['INPUT_THREADS'] = input;
const flag = util.getThreadsFlag();
t.deepEqual(flag, expectedFlag);
}
});
test('getThreadsFlag() throws if the ram input is < 0 or NaN', t => {
for (const input of ["-1", "hello!"]) {
process.env['INPUT_THREADS'] = input;
t.throws(util.getThreadsFlag);
}
});