Skip to content

Commit 90ea144

Browse files
henrymercerCopilot
andcommitted
Strengthen download status report tests
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent f342ca9 commit 90ea144

2 files changed

Lines changed: 87 additions & 24 deletions

File tree

src/codeql.test.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ test.serial(
156156
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
157157
t.is(result.toolsVersion, `0.0.0-${version}`);
158158
t.is(result.toolsSource, ToolsSource.Download);
159+
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
159160
}
160161

161162
t.is(toolcache.findAllVersions("CodeQL").length, 2);
@@ -191,9 +192,7 @@ test.serial(
191192
t.assert(toolcache.find("CodeQL", `2.15.0`));
192193
t.is(result.toolsVersion, `2.15.0`);
193194
t.is(result.toolsSource, ToolsSource.Download);
194-
if (result.toolsDownloadStatusReport) {
195-
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
196-
}
195+
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
197196
});
198197
},
199198
);
@@ -230,9 +229,7 @@ test.serial(
230229
t.assert(toolcache.find("CodeQL", "0.0.0-20200610"));
231230
t.deepEqual(result.toolsVersion, "0.0.0-20200610");
232231
t.is(result.toolsSource, ToolsSource.Download);
233-
if (result.toolsDownloadStatusReport) {
234-
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
235-
}
232+
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
236233
});
237234
},
238235
);
@@ -282,11 +279,7 @@ for (const {
282279
t.assert(toolcache.find("CodeQL", expectedToolcacheVersion));
283280
t.deepEqual(result.toolsVersion, expectedToolcacheVersion);
284281
t.is(result.toolsSource, ToolsSource.Download);
285-
t.assert(
286-
Number.isInteger(
287-
result.toolsDownloadStatusReport?.downloadDurationMs,
288-
),
289-
);
282+
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
290283
});
291284
},
292285
);
@@ -417,9 +410,7 @@ test.serial(
417410
);
418411
t.deepEqual(result.toolsVersion, defaults.cliVersion);
419412
t.is(result.toolsSource, ToolsSource.Download);
420-
if (result.toolsDownloadStatusReport) {
421-
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
422-
}
413+
t.truthy(result.toolsDownloadStatusReport);
423414

424415
const cachedVersions = toolcache.findAllVersions("CodeQL");
425416
t.is(cachedVersions.length, 2);
@@ -458,9 +449,7 @@ test.serial(
458449
);
459450
t.deepEqual(result.toolsVersion, defaults.cliVersion);
460451
t.is(result.toolsSource, ToolsSource.Download);
461-
if (result.toolsDownloadStatusReport) {
462-
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
463-
}
452+
t.truthy(result.toolsDownloadStatusReport);
464453

465454
const cachedVersions = toolcache.findAllVersions("CodeQL");
466455
t.is(cachedVersions.length, 2);
@@ -502,9 +491,7 @@ test.serial(
502491

503492
t.is(result.toolsVersion, "0.0.0-20230203");
504493
t.is(result.toolsSource, ToolsSource.Download);
505-
if (result.toolsDownloadStatusReport) {
506-
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
507-
}
494+
assertDownloadDurationInteger(t, result.toolsDownloadStatusReport);
508495

509496
const cachedVersions = toolcache.findAllVersions("CodeQL");
510497
t.is(cachedVersions.length, 1);
@@ -517,11 +504,9 @@ test.serial(
517504

518505
function assertDownloadDurationInteger(
519506
t: ExecutionContext<unknown>,
520-
statusReport: ToolsDownloadStatusReport,
507+
statusReport: ToolsDownloadStatusReport | undefined,
521508
) {
522-
if (statusReport.downloadDurationMs !== undefined) {
523-
t.assert(Number.isInteger(statusReport.downloadDurationMs));
524-
}
509+
t.assert(Number.isInteger(statusReport?.downloadDurationMs));
525510
}
526511

527512
test.serial("getExtraOptions works for explicit paths", (t) => {

src/tools-download.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { once } from "events";
2+
import * as path from "path";
3+
4+
import * as toolcache from "@actions/tool-cache";
5+
import test from "ava";
6+
import nock from "nock";
7+
import * as sinon from "sinon";
8+
9+
import { getRunnerLogger } from "./logging";
10+
import * as tar from "./tar";
11+
import { setupTests } from "./testing-utils";
12+
import { downloadAndExtract } from "./tools-download";
13+
import { withTmpDir } from "./util";
14+
15+
setupTests(test);
16+
17+
test.serial(
18+
"downloadAndExtract reports the duration when downloading before extracting",
19+
async (t) => {
20+
await withTmpDir(async (tmpDir) => {
21+
const archivePath = path.join(tmpDir, "codeql-bundle.tar.gz");
22+
const destination = path.join(tmpDir, "codeql");
23+
sinon.stub(toolcache, "downloadTool").resolves(archivePath);
24+
sinon.stub(tar, "extract").resolves(destination);
25+
26+
const statusReport = await downloadAndExtract(
27+
"https://example.com/codeql-bundle.tar.gz",
28+
"gzip",
29+
destination,
30+
undefined,
31+
{},
32+
undefined,
33+
getRunnerLogger(true),
34+
);
35+
36+
t.assert(Number.isInteger(statusReport.downloadDurationMs));
37+
});
38+
},
39+
);
40+
41+
test.serial(
42+
"downloadAndExtract omits the download duration when streaming extraction",
43+
async (t) => {
44+
await withTmpDir(async (tmpDir) => {
45+
sinon.stub(process, "platform").value("linux");
46+
const downloadTool = sinon.stub(toolcache, "downloadTool");
47+
const extractTarZst = sinon
48+
.stub(tar, "extractTarZst")
49+
.callsFake(async (archive) => {
50+
if (typeof archive === "string") {
51+
t.fail("Expected the Zstandard archive to be streamed.");
52+
return;
53+
}
54+
const end = once(archive, "end");
55+
archive.resume();
56+
await end;
57+
});
58+
const request = nock("https://example.com")
59+
.get("/codeql-bundle.tar.zst")
60+
.reply(200, "archive");
61+
62+
const statusReport = await downloadAndExtract(
63+
"https://example.com/codeql-bundle.tar.zst",
64+
"zstd",
65+
path.join(tmpDir, "codeql"),
66+
undefined,
67+
{},
68+
{ type: "gnu", version: "1.34" },
69+
getRunnerLogger(true),
70+
);
71+
72+
t.deepEqual(statusReport, {});
73+
t.false(downloadTool.called);
74+
t.true(extractTarZst.calledOnce);
75+
t.true(request.isDone());
76+
});
77+
},
78+
);

0 commit comments

Comments
 (0)