Skip to content

Commit 538cbdd

Browse files
Merge pull request #19 from github/external_queries_tmp_dir
Use tmp dir for external queries test
2 parents 5a800cc + 49575f8 commit 538cbdd

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

lib/external-queries.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/util.js

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/external-queries.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import * as path from "path";
33

44
import * as configUtils from "./config-utils";
55
import * as externalQueries from "./external-queries";
6+
import * as util from "./util";
67

78
test("checkoutExternalQueries", async () => {
89
let config = new configUtils.Config();
910
config.externalQueries = [
1011
new configUtils.ExternalQuery("github/codeql-go", "df4c6869212341b601005567381944ed90906b6b"),
1112
];
12-
await externalQueries.checkoutExternalQueries(config);
1313

14-
let destination = process.env["RUNNER_WORKSPACE"] || "/tmp/codeql-action/";
15-
// COPYRIGHT file existed in df4c6869212341b601005567381944ed90906b6b but not in master
16-
expect(fs.existsSync(path.join(destination, "github", "codeql-go", "COPYRIGHT"))).toBeTruthy();
14+
await util.withTmpDir(async tmpDir => {
15+
process.env["RUNNER_WORKSPACE"] = tmpDir;
16+
await externalQueries.checkoutExternalQueries(config);
17+
18+
// COPYRIGHT file existed in df4c6869212341b601005567381944ed90906b6b but not in master
19+
expect(fs.existsSync(path.join(tmpDir, "github", "codeql-go", "COPYRIGHT"))).toBeTruthy();
20+
});
1721
});

src/external-queries.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import * as fs from 'fs';
44
import * as path from 'path';
55

66
import * as configUtils from './config-utils';
7+
import * as util from './util';
78

89
export async function checkoutExternalQueries(config: configUtils.Config) {
9-
const folder = process.env['RUNNER_WORKSPACE'] || '/tmp/codeql-action';
10+
const folder = util.getRequiredEnvParam('RUNNER_WORKSPACE');
1011

1112
for (const externalQuery of config.externalQueries) {
1213
core.info('Checking out ' + externalQuery.repository);

src/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as http from '@actions/http-client';
33
import * as auth from '@actions/http-client/auth';
44
import * as octokit from '@octokit/rest';
55
import consoleLogLevel from 'console-log-level';
6+
import * as fs from "fs";
7+
import * as os from 'os';
68
import * as path from 'path';
79

810
import * as sharedEnv from './shared-environment';
@@ -313,3 +315,11 @@ export function getToolNames(sarifContents: string): string[] {
313315

314316
return Object.keys(toolNames);
315317
}
318+
319+
// Creates a random temporary directory, runs the given body, and then deletes the directory.
320+
// Mostly intended for use within tests.
321+
export async function withTmpDir(body: (tmpDir: string) => Promise<void>) {
322+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codeql-action-'));
323+
await body(tmpDir);
324+
fs.rmdirSync(tmpDir, { recursive: true });
325+
}

0 commit comments

Comments
 (0)