|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +import * as actionsCache from "@actions/cache"; |
1 | 5 | import test from "ava"; |
| 6 | +import * as sinon from "sinon"; |
2 | 7 |
|
3 | | -import { mockCodeQLVersion, setupTests } from "../testing-utils"; |
4 | | -import { DiskUsage } from "../util"; |
| 8 | +import { |
| 9 | + getRecordingLogger, |
| 10 | + LoggedMessage, |
| 11 | + mockCodeQLVersion, |
| 12 | + setupTests, |
| 13 | +} from "../testing-utils"; |
| 14 | +import { DiskUsage, withTmpDir } from "../util"; |
5 | 15 |
|
6 | | -import { getCacheKey } from "./status"; |
| 16 | +import { getCacheKey, shouldSkipOverlayAnalysis } from "./status"; |
7 | 17 |
|
8 | 18 | setupTests(test); |
9 | 19 |
|
@@ -61,3 +71,102 @@ test("getCacheKey rounds disk space down to nearest 10 GiB", async (t) => { |
61 | 71 | "codeql-overlay-status-javascript-2.20.0-runner-10GB", |
62 | 72 | ); |
63 | 73 | }); |
| 74 | + |
| 75 | +test("shouldSkipOverlayAnalysis returns false when no cached status exists", async (t) => { |
| 76 | + await withTmpDir(async (tmpDir) => { |
| 77 | + process.env["RUNNER_TEMP"] = tmpDir; |
| 78 | + const codeql = mockCodeQLVersion("2.20.0"); |
| 79 | + const messages: LoggedMessage[] = []; |
| 80 | + const logger = getRecordingLogger(messages); |
| 81 | + |
| 82 | + sinon.stub(actionsCache, "restoreCache").resolves(undefined); |
| 83 | + |
| 84 | + const result = await shouldSkipOverlayAnalysis( |
| 85 | + codeql, |
| 86 | + ["javascript"], |
| 87 | + makeDiskUsage(50), |
| 88 | + logger, |
| 89 | + ); |
| 90 | + |
| 91 | + t.false(result); |
| 92 | + t.true( |
| 93 | + messages.some( |
| 94 | + (m) => |
| 95 | + m.type === "debug" && |
| 96 | + typeof m.message === "string" && |
| 97 | + m.message.includes("No cached overlay status found."), |
| 98 | + ), |
| 99 | + ); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +test("shouldSkipOverlayAnalysis returns true when cached status indicates failed build", async (t) => { |
| 104 | + await withTmpDir(async (tmpDir) => { |
| 105 | + process.env["RUNNER_TEMP"] = tmpDir; |
| 106 | + const codeql = mockCodeQLVersion("2.20.0"); |
| 107 | + const messages: LoggedMessage[] = []; |
| 108 | + const logger = getRecordingLogger(messages); |
| 109 | + |
| 110 | + const status = { |
| 111 | + attemptedToBuildOverlayBaseDatabase: true, |
| 112 | + builtOverlayBaseDatabase: false, |
| 113 | + }; |
| 114 | + |
| 115 | + // Stub restoreCache to write the status file and return a key |
| 116 | + sinon.stub(actionsCache, "restoreCache").callsFake(async (paths) => { |
| 117 | + const statusFile = paths[0]; |
| 118 | + await fs.promises.mkdir(path.dirname(statusFile), { recursive: true }); |
| 119 | + await fs.promises.writeFile(statusFile, JSON.stringify(status)); |
| 120 | + return "found-key"; |
| 121 | + }); |
| 122 | + |
| 123 | + const result = await shouldSkipOverlayAnalysis( |
| 124 | + codeql, |
| 125 | + ["javascript"], |
| 126 | + makeDiskUsage(50), |
| 127 | + logger, |
| 128 | + ); |
| 129 | + |
| 130 | + t.true(result); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +test("shouldSkipOverlayAnalysis returns false when cached status indicates successful build", async (t) => { |
| 135 | + await withTmpDir(async (tmpDir) => { |
| 136 | + process.env["RUNNER_TEMP"] = tmpDir; |
| 137 | + const codeql = mockCodeQLVersion("2.20.0"); |
| 138 | + const messages: LoggedMessage[] = []; |
| 139 | + const logger = getRecordingLogger(messages); |
| 140 | + |
| 141 | + const status = { |
| 142 | + attemptedToBuildOverlayBaseDatabase: true, |
| 143 | + builtOverlayBaseDatabase: true, |
| 144 | + }; |
| 145 | + |
| 146 | + sinon.stub(actionsCache, "restoreCache").callsFake(async (paths) => { |
| 147 | + const statusFile = paths[0]; |
| 148 | + await fs.promises.mkdir(path.dirname(statusFile), { recursive: true }); |
| 149 | + await fs.promises.writeFile(statusFile, JSON.stringify(status)); |
| 150 | + return "found-key"; |
| 151 | + }); |
| 152 | + |
| 153 | + const result = await shouldSkipOverlayAnalysis( |
| 154 | + codeql, |
| 155 | + ["javascript"], |
| 156 | + makeDiskUsage(50), |
| 157 | + logger, |
| 158 | + ); |
| 159 | + |
| 160 | + t.false(result); |
| 161 | + t.true( |
| 162 | + messages.some( |
| 163 | + (m) => |
| 164 | + m.type === "debug" && |
| 165 | + typeof m.message === "string" && |
| 166 | + m.message.includes( |
| 167 | + "Cached overlay status does not indicate a previous unsuccessful attempt", |
| 168 | + ), |
| 169 | + ), |
| 170 | + ); |
| 171 | + }); |
| 172 | +}); |
0 commit comments