|
| 1 | +import * as fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +import test from "ava"; |
| 5 | +import * as sinon from "sinon"; |
| 6 | + |
| 7 | +import * as diagnostics from "./diagnostics"; |
| 8 | +import type { FileSystem } from "./fs"; |
| 9 | +import { BuiltInLanguage } from "./languages"; |
| 10 | +import { |
| 11 | + checkExpectedLogMessages, |
| 12 | + createTestConfig, |
| 13 | + initAllState, |
| 14 | + RecordingLogger, |
| 15 | + setupTests, |
| 16 | +} from "./testing-utils"; |
| 17 | + |
| 18 | +setupTests(test); |
| 19 | + |
| 20 | +type TestFS = FileSystem< |
| 21 | + "existsSync" | "mkdirSync" | "writeFileSync" | "readFileSync" | "renameSync" |
| 22 | +>; |
| 23 | + |
| 24 | +function makeStubFS() { |
| 25 | + // The functions in `fs` cannot be stubbed. Create a new, minimal object |
| 26 | + // that we can stub. |
| 27 | + const stubbedFS: TestFS = { |
| 28 | + existsSync: fs.existsSync, |
| 29 | + mkdirSync: fs.mkdirSync, |
| 30 | + writeFileSync: fs.writeFileSync, |
| 31 | + readFileSync: fs.readFileSync, |
| 32 | + renameSync: fs.renameSync, |
| 33 | + }; |
| 34 | + |
| 35 | + const existsSync = sinon.stub(stubbedFS, "existsSync"); |
| 36 | + const mkdirSync = sinon.stub(stubbedFS, "mkdirSync"); |
| 37 | + const writeFileSync = sinon.stub(stubbedFS, "writeFileSync"); |
| 38 | + const readFileSync = sinon.stub(stubbedFS, "readFileSync"); |
| 39 | + const renameSync = sinon.stub(stubbedFS, "renameSync"); |
| 40 | + |
| 41 | + return { |
| 42 | + stubbedFS, |
| 43 | + existsSync, |
| 44 | + mkdirSync, |
| 45 | + writeFileSync, |
| 46 | + readFileSync, |
| 47 | + renameSync, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +const id = "codeql-action/test-diagnostic"; |
| 52 | +const name = "Test title"; |
| 53 | + |
| 54 | +test("makeDiagnostic - adds expected properties", (t) => { |
| 55 | + const diagnostic = diagnostics.makeDiagnostic(id, name, {}); |
| 56 | + |
| 57 | + t.truthy(diagnostic.timestamp); |
| 58 | + t.is(diagnostic.source.id, id); |
| 59 | + t.is(diagnostic.source.name, name); |
| 60 | +}); |
| 61 | + |
| 62 | +test("addDiagnostic writes temporary diagnostics and flushDiagnostics moves them", (t) => { |
| 63 | + const logger = new RecordingLogger(); |
| 64 | + const { stubbedFS, existsSync, mkdirSync, writeFileSync, renameSync } = |
| 65 | + makeStubFS(); |
| 66 | + |
| 67 | + let databasePath: fs.PathLike | undefined; |
| 68 | + existsSync.callsFake((p) => { |
| 69 | + databasePath = p; |
| 70 | + return false; |
| 71 | + }); |
| 72 | + |
| 73 | + const diagnostic = diagnostics.makeDiagnostic(id, name, {}); |
| 74 | + t.notThrows(() => { |
| 75 | + diagnostics.addDiagnostic( |
| 76 | + createTestConfig({ tempDir: path.resolve("/temp/") }), |
| 77 | + BuiltInLanguage.actions, |
| 78 | + diagnostic, |
| 79 | + logger, |
| 80 | + stubbedFS as FileSystem, |
| 81 | + ); |
| 82 | + diagnostics.flushDiagnostics( |
| 83 | + initAllState({ logger, fs: stubbedFS as FileSystem }), |
| 84 | + createTestConfig({ |
| 85 | + tempDir: path.resolve("/temp/"), |
| 86 | + dbLocation: path.resolve("/test/database/"), |
| 87 | + }), |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + // addDiagnostic |
| 92 | + t.is(existsSync.callCount, 1); |
| 93 | + t.is(mkdirSync.callCount, 1); |
| 94 | + t.is(writeFileSync.callCount, 1); |
| 95 | + t.is(writeFileSync.args[0].length, 2); |
| 96 | + t.is(writeFileSync.args[0][1], JSON.stringify(diagnostic)); |
| 97 | + |
| 98 | + // flushDiagnostics |
| 99 | + t.is(renameSync.callCount, 1); |
| 100 | + t.is(renameSync.args[0].length, 2); |
| 101 | + |
| 102 | + const sourcePath = renameSync.args[0][0].toString(); |
| 103 | + const expectedSourcePathPrefix = path.resolve( |
| 104 | + "/temp/codeql-action-temp-diagnostics/actions/diagnostic/codeql-action/", |
| 105 | + ); |
| 106 | + t.true( |
| 107 | + sourcePath.startsWith(expectedSourcePathPrefix), |
| 108 | + `'${sourcePath}' does not start with '${expectedSourcePathPrefix}`, |
| 109 | + ); |
| 110 | + |
| 111 | + const destPath = renameSync.args[0][1].toString(); |
| 112 | + const expectedDestPathPrefix = path.resolve( |
| 113 | + "/test/database/actions/diagnostic/codeql-action/", |
| 114 | + ); |
| 115 | + t.true( |
| 116 | + destPath.startsWith(expectedDestPathPrefix), |
| 117 | + `'${destPath}' does not start with '${expectedDestPathPrefix}`, |
| 118 | + ); |
| 119 | + |
| 120 | + checkExpectedLogMessages(t, logger.messages, [ |
| 121 | + `but the database at ${databasePath} does not exist yet`, |
| 122 | + "Moving 1 diagnostic(s) to their respective databases.", |
| 123 | + ]); |
| 124 | +}); |
0 commit comments