Permalink
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/lib/fingerprints.test.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
179 lines (179 sloc)
7.42 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var __importStar = (this && this.__importStar) || function (mod) { | |
if (mod && mod.__esModule) return mod; | |
var result = {}; | |
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | |
result["default"] = mod; | |
return result; | |
}; | |
var __importDefault = (this && this.__importDefault) || function (mod) { | |
return (mod && mod.__esModule) ? mod : { "default": mod }; | |
}; | |
Object.defineProperty(exports, "__esModule", { value: true }); | |
const fs = __importStar(require("fs")); | |
const path = __importStar(require("path")); | |
const ava_1 = __importDefault(require("ava")); | |
const fingerprints = __importStar(require("./fingerprints")); | |
const logging_1 = require("./logging"); | |
const testing_utils_1 = require("./testing-utils"); | |
testing_utils_1.setupTests(ava_1.default); | |
function testHash(t, input, expectedHashes) { | |
let index = 0; | |
const callback = function (lineNumber, hash) { | |
t.is(lineNumber, index + 1); | |
t.is(hash, expectedHashes[index]); | |
index++; | |
}; | |
fingerprints.hash(callback, input); | |
t.is(index, input.split(/\r\n|\r|\n/).length); | |
} | |
ava_1.default("hash", (t) => { | |
// Try empty file | |
testHash(t, "", ["c129715d7a2bc9a3:1"]); | |
// Try various combinations of newline characters | |
testHash(t, " a\nb\n \t\tc\n d", [ | |
"271789c17abda88f:1", | |
"54703d4cd895b18:1", | |
"180aee12dab6264:1", | |
"a23a3dc5e078b07b:1", | |
]); | |
testHash(t, " hello; \t\nworld!!!\n\n\n \t\tGreetings\n End", [ | |
"8b7cf3e952e7aeb2:1", | |
"b1ae1287ec4718d9:1", | |
"bff680108adb0fcc:1", | |
"c6805c5e1288b612:1", | |
"b86d3392aea1be30:1", | |
"e6ceba753e1a442:1", | |
]); | |
testHash(t, " hello; \t\nworld!!!\n\n\n \t\tGreetings\n End\n", [ | |
"e9496ae3ebfced30:1", | |
"fb7c023a8b9ccb3f:1", | |
"ce8ba1a563dcdaca:1", | |
"e20e36e16fcb0cc8:1", | |
"b3edc88f2938467e:1", | |
"c8e28b0b4002a3a0:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
testHash(t, " hello; \t\nworld!!!\r\r\r \t\tGreetings\r End\r", [ | |
"e9496ae3ebfced30:1", | |
"fb7c023a8b9ccb3f:1", | |
"ce8ba1a563dcdaca:1", | |
"e20e36e16fcb0cc8:1", | |
"b3edc88f2938467e:1", | |
"c8e28b0b4002a3a0:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
testHash(t, " hello; \t\r\nworld!!!\r\n\r\n\r\n \t\tGreetings\r\n End\r\n", [ | |
"e9496ae3ebfced30:1", | |
"fb7c023a8b9ccb3f:1", | |
"ce8ba1a563dcdaca:1", | |
"e20e36e16fcb0cc8:1", | |
"b3edc88f2938467e:1", | |
"c8e28b0b4002a3a0:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
testHash(t, " hello; \t\nworld!!!\r\n\n\r \t\tGreetings\r End\r\n", [ | |
"e9496ae3ebfced30:1", | |
"fb7c023a8b9ccb3f:1", | |
"ce8ba1a563dcdaca:1", | |
"e20e36e16fcb0cc8:1", | |
"b3edc88f2938467e:1", | |
"c8e28b0b4002a3a0:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
// Try repeating line that will generate identical hashes | |
testHash(t, "Lorem ipsum dolor sit amet.\n".repeat(10), [ | |
"a7f2ff13bc495cf2:1", | |
"a7f2ff13bc495cf2:2", | |
"a7f2ff13bc495cf2:3", | |
"a7f2ff13bc495cf2:4", | |
"a7f2ff13bc495cf2:5", | |
"a7f2ff13bc495cf2:6", | |
"a7f2ff1481e87703:1", | |
"a9cf91f7bbf1862b:1", | |
"55ec222b86bcae53:1", | |
"cc97dc7b1d7d8f7b:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
testHash(t, "x = 2\nx = 1\nprint(x)\nx = 3\nprint(x)\nx = 4\nprint(x)\n", [ | |
"e54938cc54b302f1:1", | |
"bb609acbe9138d60:1", | |
"1131fd5871777f34:1", | |
"5c482a0f8b35ea28:1", | |
"54517377da7028d2:1", | |
"2c644846cb18d53e:1", | |
"f1b89f20de0d133:1", | |
"c129715d7a2bc9a3:1", | |
]); | |
}); | |
function testResolveUriToFile(uri, index, artifactsURIs) { | |
const location = { uri, index }; | |
const artifacts = artifactsURIs.map((artifactURI) => ({ | |
location: { uri: artifactURI }, | |
})); | |
return fingerprints.resolveUriToFile(location, artifacts, process.cwd(), logging_1.getRunnerLogger(true)); | |
} | |
ava_1.default("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; | |
t.true(filepath.startsWith(`${cwd}/`)); | |
const relativeFilepath = filepath.substring(cwd.length + 1); | |
// Absolute paths are unmodified | |
t.is(testResolveUriToFile(filepath, undefined, []), filepath); | |
t.is(testResolveUriToFile(`file://${filepath}`, undefined, []), filepath); | |
// Relative paths are made absolute | |
t.is(testResolveUriToFile(relativeFilepath, undefined, []), filepath); | |
t.is(testResolveUriToFile(`file://${relativeFilepath}`, undefined, []), filepath); | |
// Absolute paths outside the src root are discarded | |
t.is(testResolveUriToFile("/src/foo/bar.js", undefined, []), undefined); | |
t.is(testResolveUriToFile("file:///src/foo/bar.js", undefined, []), undefined); | |
// Other schemes are discarded | |
t.is(testResolveUriToFile(`https://${filepath}`, undefined, []), undefined); | |
t.is(testResolveUriToFile(`ftp://${filepath}`, undefined, []), undefined); | |
// Invalid URIs are discarded | |
t.is(testResolveUriToFile(1, undefined, []), undefined); | |
t.is(testResolveUriToFile(undefined, undefined, []), undefined); | |
// Non-existent files are discarded | |
t.is(testResolveUriToFile(`${filepath}2`, undefined, []), undefined); | |
// Index is resolved | |
t.is(testResolveUriToFile(undefined, 0, [filepath]), filepath); | |
t.is(testResolveUriToFile(undefined, 1, ["foo", filepath]), filepath); | |
// Invalid indexes are discarded | |
t.is(testResolveUriToFile(undefined, 1, [filepath]), undefined); | |
t.is(testResolveUriToFile(undefined, "0", [filepath]), undefined); | |
}); | |
ava_1.default("addFingerprints", (t) => { | |
// Run an end-to-end test on a test file | |
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 | |
const checkoutPath = path.normalize(`${__dirname}/../src/testdata`); | |
t.deepEqual(fingerprints.addFingerprints(input, checkoutPath, logging_1.getRunnerLogger(true)), expected); | |
}); | |
ava_1.default("missingRegions", (t) => { | |
// Run an end-to-end test on a test file | |
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 | |
const checkoutPath = path.normalize(`${__dirname}/../src/testdata`); | |
t.deepEqual(fingerprints.addFingerprints(input, checkoutPath, logging_1.getRunnerLogger(true)), expected); | |
}); | |
//# sourceMappingURL=fingerprints.test.js.map |