Files
codeql-action/src/codeql.test.ts
T

97 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-06-18 16:40:02 +02:00
import * as toolcache from '@actions/tool-cache';
2020-06-18 18:29:25 +02:00
import test from 'ava';
2020-06-18 16:40:02 +02:00
import nock from 'nock';
2020-06-18 18:29:25 +02:00
import * as path from 'path';
2020-06-18 16:40:02 +02:00
2020-06-26 17:22:19 +01:00
import * as codeql from './codeql';
2020-08-25 16:19:15 +01:00
import { getRunnerLogger } from './logging';
2020-07-06 16:04:02 +01:00
import {setupTests} from './testing-utils';
2020-06-18 18:29:25 +02:00
import * as util from './util';
2020-06-18 16:40:02 +02:00
2020-07-06 16:04:02 +01:00
setupTests(test);
2020-06-23 13:59:58 +01:00
2020-06-18 16:40:02 +02:00
test('download codeql bundle cache', async t => {
await util.withTmpDir(async tmpDir => {
const versions = ['20200601', '20200610'];
2020-06-18 16:40:02 +02:00
for (let i = 0; i < versions.length; i++) {
const version = versions[i];
2020-06-18 16:40:02 +02:00
nock('https://example.com')
.get(`/download/codeql-bundle-${version}/codeql-bundle.tar.gz`)
.replyWithFile(200, path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`));
2020-06-18 16:40:02 +02:00
2020-08-25 16:19:15 +01:00
await codeql.setupCodeQL(
`https://example.com/download/codeql-bundle-${version}/codeql-bundle.tar.gz`,
'token',
'https://github.example.com',
tmpDir,
tmpDir,
'runner',
getRunnerLogger());
2020-06-18 16:40:02 +02:00
t.assert(toolcache.find('CodeQL', `0.0.0-${version}`));
}
2020-06-18 16:40:02 +02:00
const cachedVersions = toolcache.findAllVersions('CodeQL');
2020-06-18 16:40:02 +02:00
t.is(cachedVersions.length, 2);
});
2020-06-18 18:29:25 +02:00
});
2020-06-18 16:40:02 +02:00
test('parse codeql bundle url version', t => {
const tests = {
'20200601': '0.0.0-20200601',
'20200601.0': '0.0.0-20200601.0',
'20200601.0.0': '20200601.0.0',
'1.2.3': '1.2.3',
'1.2.3-alpha': '1.2.3-alpha',
'1.2.3-beta.1': '1.2.3-beta.1',
};
for (const [version, expectedVersion] of Object.entries(tests)) {
const url = `https://github.com/.../codeql-bundle-${version}/...`;
try {
2020-08-25 16:19:15 +01:00
const parsedVersion = codeql.getCodeQLURLVersion(url, getRunnerLogger());
t.deepEqual(parsedVersion, expectedVersion);
} catch (e) {
t.fail(e.message);
2020-06-18 16:40:02 +02:00
}
}
2020-06-18 18:29:25 +02:00
});
2020-08-10 09:12:36 +02:00
test('getExtraOptions works for explicit paths', t => {
t.deepEqual(codeql.getExtraOptions({}, ['foo'], []), []);
t.deepEqual(codeql.getExtraOptions({foo: [42]}, ['foo'], []), ['42']);
t.deepEqual(codeql.getExtraOptions({foo: {bar: [42]}}, ['foo', 'bar'], []), ['42']);
});
test('getExtraOptions works for wildcards', t => {
t.deepEqual(codeql.getExtraOptions({'*': [42]}, ['foo'], []), ['42']);
});
test('getExtraOptions works for wildcards and explicit paths', t => {
let o1 = {'*': [42], foo: [87]};
t.deepEqual(codeql.getExtraOptions(o1, ['foo'], []), ['42', '87']);
let o2 = {'*': [42], foo: [87]};
t.deepEqual(codeql.getExtraOptions(o2, ['foo', 'bar'], []), ['42']);
let o3 = {'*': [42], foo: { '*': [87], bar: [99]}};
let p = ['foo', 'bar'];
t.deepEqual(codeql.getExtraOptions(o3, p, []), ['42', '87', '99']);
});
test('getExtraOptions throws for bad content', t => {
t.throws(() => codeql.getExtraOptions({'*': 42}, ['foo'], []));
t.throws(() => codeql.getExtraOptions({foo: 87}, ['foo'], []));
t.throws(() => codeql.getExtraOptions({'*': [42], foo: { '*': 87, bar: [99]}}, ['foo', 'bar'], []));
});