Skip to content

Commit

Permalink
Showing 10 changed files with 304 additions and 48 deletions.
29 changes: 29 additions & 0 deletions lib/analysis-paths.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions lib/external-queries.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 156 additions & 0 deletions lib/fingerprints.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions lib/util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion package.json
@@ -5,10 +5,17 @@
"description": "CodeQL action",
"scripts": {
"build": "tsc",
"test": "jest",
"test": "ava src/**",
"lint": "tslint -p . -c tslint.json 'src/**/*.ts'",
"removeNPMAbsolutePaths": "removeNPMAbsolutePaths . --force"
},
"ava": {
"typescript": {
"rewritePaths": {
"src/": "lib/"
}
}
},
"license": "MIT",
"dependencies": {
"@actions/core": "^1.0.0",
16 changes: 9 additions & 7 deletions src/analysis-paths.test.ts
@@ -1,18 +1,20 @@
import test from 'ava';

import * as analysisPaths from './analysis-paths';
import * as configUtils from './config-utils';

test("emptyPaths", async () => {
test("emptyPaths", async t => {
let config = new configUtils.Config();
analysisPaths.includeAndExcludeAnalysisPaths(config, []);
expect(process.env['LGTM_INDEX_INCLUDE']).toBeUndefined();
expect(process.env['LGTM_INDEX_EXCLUDE']).toBeUndefined();
t.is(process.env['LGTM_INDEX_INCLUDE'], undefined);
t.is(process.env['LGTM_INDEX_EXCLUDE'], undefined);
});

test("nonEmptyPaths", async () => {
test("nonEmptyPaths", async t => {
let config = new configUtils.Config();
config.paths.push('path1', 'path2');
config.pathsIgnore.push('path3', 'path4');
analysisPaths.includeAndExcludeAnalysisPaths(config, []);
expect(process.env['LGTM_INDEX_INCLUDE']).toEqual('path1\npath2');
expect(process.env['LGTM_INDEX_EXCLUDE']).toEqual('path3\npath4');
});
t.is(process.env['LGTM_INDEX_INCLUDE'], 'path1\npath2');
t.is(process.env['LGTM_INDEX_EXCLUDE'], 'path3\npath4');
});
5 changes: 3 additions & 2 deletions src/external-queries.test.ts
@@ -1,11 +1,12 @@
import test from 'ava';
import * as fs from "fs";
import * as path from "path";

import * as configUtils from "./config-utils";
import * as externalQueries from "./external-queries";
import * as util from "./util";

test("checkoutExternalQueries", async () => {
test("checkoutExternalQueries", async t => {
let config = new configUtils.Config();
config.externalQueries = [
new configUtils.ExternalQuery("github/codeql-go", "df4c6869212341b601005567381944ed90906b6b"),
@@ -16,6 +17,6 @@ test("checkoutExternalQueries", async () => {
await externalQueries.checkoutExternalQueries(config);

// COPYRIGHT file existed in df4c6869212341b601005567381944ed90906b6b but not in master
expect(fs.existsSync(path.join(tmpDir, "github", "codeql-go", "COPYRIGHT"))).toBeTruthy();
t.true(fs.existsSync(path.join(tmpDir, "github", "codeql-go", "COPYRIGHT")));
});
});
76 changes: 43 additions & 33 deletions src/fingerprints.test.ts
@@ -1,24 +1,28 @@
import test from 'ava';
import * as ava from "ava";
import * as fs from 'fs';
import * as path from 'path';

import * as fingerprints from './fingerprints';

function testHash(input: string, expectedHashes: string[]) {
function testHash(t: ava.Assertions, input: string, expectedHashes: string[]) {
let index = 0;
let callback = function (lineNumber: number, hash: string) {
expect(lineNumber).toBe(index + 1);
expect(hash).toBe(expectedHashes[index]);
t.is(lineNumber, index + 1);
t.is(hash, expectedHashes[index]);
index++;
};
fingerprints.hash(callback, input);
expect(index).toBe(input.split(/\r\n|\r|\n/).length);
t.is(index, input.split(/\r\n|\r|\n/).length);
}

test('hash', () => {
test('hash', (t: ava.Assertions) => {
// Try empty file
testHash("", ["c129715d7a2bc9a3:1"]);
testHash(t, "", ["c129715d7a2bc9a3:1"]);

// Try various combinations of newline characters
testHash(
t,
" a\nb\n \t\tc\n d",
[
"271789c17abda88f:1",
@@ -27,6 +31,7 @@ test('hash', () => {
"a23a3dc5e078b07b:1"
]);
testHash(
t,
" hello; \t\nworld!!!\n\n\n \t\tGreetings\n End",
[
"8b7cf3e952e7aeb2:1",
@@ -37,6 +42,7 @@ test('hash', () => {
"e6ceba753e1a442:1",
]);
testHash(
t,
" hello; \t\nworld!!!\n\n\n \t\tGreetings\n End\n",
[
"e9496ae3ebfced30:1",
@@ -48,6 +54,7 @@ test('hash', () => {
"c129715d7a2bc9a3:1",
]);
testHash(
t,
" hello; \t\nworld!!!\r\r\r \t\tGreetings\r End\r",
[
"e9496ae3ebfced30:1",
@@ -59,6 +66,7 @@ test('hash', () => {
"c129715d7a2bc9a3:1",
]);
testHash(
t,
" hello; \t\r\nworld!!!\r\n\r\n\r\n \t\tGreetings\r\n End\r\n",
[
"e9496ae3ebfced30:1",
@@ -70,6 +78,7 @@ test('hash', () => {
"c129715d7a2bc9a3:1",
]);
testHash(
t,
" hello; \t\nworld!!!\r\n\n\r \t\tGreetings\r End\r\n",
[
"e9496ae3ebfced30:1",
@@ -83,6 +92,7 @@ test('hash', () => {

// Try repeating line that will generate identical hashes
testHash(
t,
"Lorem ipsum dolor sit amet.\n".repeat(10),
[
"a7f2ff13bc495cf2:1",
@@ -105,76 +115,76 @@ function testResolveUriToFile(uri: any, index: any, artifactsURIs: any[]) {
return fingerprints.resolveUriToFile(location, artifacts);
}

test('resolveUriToFile', () => {
test('resolveUriToFile', t => {
// The resolveUriToFile method checks that the file exists and is in the right directory
// so we need to give it real files to look at. We will use this file as an example.
// For this to work we require the current working directory to be a parent, but this
// should generally always be the case so this is fine.
const cwd = process.cwd();
const filepath = __filename;
expect(filepath.startsWith(cwd + '/')).toBeTruthy();
t.true(filepath.startsWith(cwd + '/'));
const relativeFilepaht = filepath.substring(cwd.length + 1);

process.env['GITHUB_WORKSPACE'] = cwd;

// Absolute paths are unmodified
expect(testResolveUriToFile(filepath, undefined, [])).toBe(filepath);
expect(testResolveUriToFile('file://' + filepath, undefined, [])).toBe(filepath);
t.is(testResolveUriToFile(filepath, undefined, []), filepath);
t.is(testResolveUriToFile('file://' + filepath, undefined, []), filepath);

// Relative paths are made absolute
expect(testResolveUriToFile(relativeFilepaht, undefined, [])).toBe(filepath);
expect(testResolveUriToFile('file://' + relativeFilepaht, undefined, [])).toBe(filepath);
t.is(testResolveUriToFile(relativeFilepaht, undefined, []), filepath);
t.is(testResolveUriToFile('file://' + relativeFilepaht, undefined, []), filepath);

// Absolute paths outside the src root are discarded
expect(testResolveUriToFile('/src/foo/bar.js', undefined, [])).toBe(undefined);
expect(testResolveUriToFile('file:///src/foo/bar.js', undefined, [])).toBe(undefined);
t.is(testResolveUriToFile('/src/foo/bar.js', undefined, []), undefined);
t.is(testResolveUriToFile('file:///src/foo/bar.js', undefined, []), undefined);

// Other schemes are discarded
expect(testResolveUriToFile('https://' + filepath, undefined, [])).toBe(undefined);
expect(testResolveUriToFile('ftp://' + filepath, undefined, [])).toBe(undefined);
t.is(testResolveUriToFile('https://' + filepath, undefined, []), undefined);
t.is(testResolveUriToFile('ftp://' + filepath, undefined, []), undefined);

// Invalid URIs are discarded
expect(testResolveUriToFile(1, undefined, [])).toBe(undefined);
expect(testResolveUriToFile(undefined, undefined, [])).toBe(undefined);
t.is(testResolveUriToFile(1, undefined, []), undefined);
t.is(testResolveUriToFile(undefined, undefined, []), undefined);

// Non-existant files are discarded
expect(testResolveUriToFile(filepath + '2', undefined, [])).toBe(undefined);
t.is(testResolveUriToFile(filepath + '2', undefined, []), undefined);

// Index is resolved
expect(testResolveUriToFile(undefined, 0, [filepath])).toBe(filepath);
expect(testResolveUriToFile(undefined, 1, ['foo', filepath])).toBe(filepath);
t.is(testResolveUriToFile(undefined, 0, [filepath]), filepath);
t.is(testResolveUriToFile(undefined, 1, ['foo', filepath]), filepath);

// Invalid indexes are discarded
expect(testResolveUriToFile(undefined, 1, [filepath])).toBe(undefined);
expect(testResolveUriToFile(undefined, '0', [filepath])).toBe(undefined);
t.is(testResolveUriToFile(undefined, 1, [filepath]), undefined);
t.is(testResolveUriToFile(undefined, '0', [filepath]), undefined);
});

test('addFingerprints', () => {
test('addFingerprints', t => {
// Run an end-to-end test on a test file
let input = fs.readFileSync(__dirname + '/testdata/fingerprinting.input.sarif').toString();
let expected = fs.readFileSync(__dirname + '/testdata/fingerprinting.expected.sarif').toString();
let input = fs.readFileSync(__dirname + '/../src/testdata/fingerprinting.input.sarif').toString();
let expected = fs.readFileSync(__dirname + '/../src/testdata/fingerprinting.expected.sarif').toString();

// The test files are stored prettified, but addFingerprints outputs condensed JSON
input = JSON.stringify(JSON.parse(input));
expected = JSON.stringify(JSON.parse(expected));

// The URIs in the SARIF files resolve to files in the testdata directory
process.env['GITHUB_WORKSPACE'] = __dirname + '/testdata';
process.env['GITHUB_WORKSPACE'] = path.normalize(__dirname + '/../src/testdata');

expect(fingerprints.addFingerprints(input)).toBe(expected);
t.deepEqual(fingerprints.addFingerprints(input), expected);
});

test('missingRegions', () => {
test('missingRegions', t => {
// Run an end-to-end test on a test file
let input = fs.readFileSync(__dirname + '/testdata/fingerprinting2.input.sarif').toString();
let expected = fs.readFileSync(__dirname + '/testdata/fingerprinting2.expected.sarif').toString();
let input = fs.readFileSync(__dirname + '/../src/testdata/fingerprinting2.input.sarif').toString();
let expected = fs.readFileSync(__dirname + '/../src/testdata/fingerprinting2.expected.sarif').toString();

// The test files are stored prettified, but addFingerprints outputs condensed JSON
input = JSON.stringify(JSON.parse(input));
expected = JSON.stringify(JSON.parse(expected));

// The URIs in the SARIF files resolve to files in the testdata directory
process.env['GITHUB_WORKSPACE'] = __dirname + '/testdata';
process.env['GITHUB_WORKSPACE'] = path.normalize(__dirname + '/../src/testdata');

expect(fingerprints.addFingerprints(input)).toBe(expected);
t.deepEqual(fingerprints.addFingerprints(input), expected);
});
9 changes: 5 additions & 4 deletions src/util.test.ts
@@ -1,9 +1,10 @@
import test from 'ava';
import * as fs from 'fs';

import * as util from './util';

test('getToolNames', () => {
const input = fs.readFileSync(__dirname + '/testdata/tool-names.sarif', 'utf8')
test('getToolNames', t => {
const input = fs.readFileSync(__dirname + '/../src/testdata/tool-names.sarif', 'utf8');
const toolNames = util.getToolNames(input);
expect(toolNames).toStrictEqual(["CodeQL command-line toolchain", "ESLint"])
})
t.deepEqual(toolNames, ["CodeQL command-line toolchain", "ESLint"]);
});
2 changes: 1 addition & 1 deletion tsconfig.json
@@ -59,5 +59,5 @@
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"exclude": ["node_modules", "**/*.test.ts"]
"exclude": ["node_modules"]
}

0 comments on commit 572c8bb

Please sign in to comment.