Skip to content

Commit aaa619b

Browse files
authored
fix: should not crash when cache:key:files files don't exist (#1292)
1 parent 9bee7ac commit aaa619b

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/job.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ export class Job {
385385
}
386386
return `${cwd}/${path}`;
387387
});
388-
389-
return "md-" + await Utils.checksumFiles(files);
388+
return "md-" + await Utils.checksumFiles(cwd, files);
390389
}
391390

392391
get beforeScripts (): string[] {

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {GitData, GitSchema} from "./git-data";
1010
import globby from "globby";
1111
import micromatch from "micromatch";
1212
import axios from "axios";
13+
import path from "path";
1314

1415
type RuleResultOpt = {
1516
cwd: string;
@@ -261,11 +262,12 @@ export class Utils {
261262
return {hrdeltatime: process.hrtime(time)};
262263
}
263264

264-
static async checksumFiles (files: string[]): Promise<string> {
265+
static async checksumFiles (cwd: string, files: string[]): Promise<string> {
265266
const promises: Promise<string>[] = [];
266267

267268
files.forEach((file) => {
268269
promises.push(new Promise((resolve, reject) => {
270+
if (! fs.pathExistsSync(file)) resolve(path.relative(cwd, file)); // must use relative path here, so that checksum can be deterministic when running the unit tests
269271
checksum.file(file, (err, hash) => {
270272
if (err) {
271273
return reject(err);

tests/test-cases/cache-key-files/.gitlab-ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,15 @@ cache-key-file referencing $CI_PROJECT_DIR:
3232
- fakepackage.json
3333
script:
3434
- echo 1
35+
36+
cache-key-file file dont exist:
37+
stage: test
38+
image: bash
39+
cache:
40+
key:
41+
files:
42+
- no-such-file
43+
paths:
44+
- /tmp
45+
script:
46+
- echo 1

tests/test-cases/cache-key-files/integration.key-files.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,16 @@ test("cache-key-files <cache-key-file referencing $CI_PROJECT_DIR>", async () =>
3535

3636
expect(writeStreams.stdoutLines.join("\n")).toContain(expected.join("\n"));
3737
});
38+
39+
test("cache-key-files <cache-key-file file dont exist>", async () => {
40+
const writeStreams = new WriteStreamsMock();
41+
await handler({
42+
cwd: "tests/test-cases/cache-key-files",
43+
job: ["cache-key-file file dont exist"],
44+
}, writeStreams);
45+
46+
const expected = [
47+
chalk`{blueBright cache-key-file file dont exist} {magentaBright exported cache /tmp 'md-18bbe9d7603e540e28418cf4a072938ac477a2c1'}`,
48+
];
49+
expect(writeStreams.stdoutLines.join("\n")).toContain(expected.join("\n"));
50+
});

0 commit comments

Comments
 (0)