Skip to content

Commit 9ab9538

Browse files
authored
Merge pull request #313 from actions/aiyan/use-cache-package
Switch cache action to use the cache node package
2 parents 2b83e91 + 6c7d57d commit 9ab9538

17 files changed

+7612
-8474
lines changed

.github/workflows/workflow.yml

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ on:
44
pull_request:
55
branches:
66
- master
7+
- releases/**
78
paths-ignore:
89
- '**.md'
910
push:
1011
branches:
1112
- master
13+
- releases/**
1214
paths-ignore:
1315
- '**.md'
1416

@@ -17,7 +19,7 @@ jobs:
1719
build:
1820
strategy:
1921
matrix:
20-
os: [ubuntu-latest, windows-latest, macOS-latest]
22+
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
2123
fail-fast: false
2224
runs-on: ${{ matrix.os }}
2325
steps:
@@ -61,7 +63,7 @@ jobs:
6163
test-save:
6264
strategy:
6365
matrix:
64-
os: [ubuntu-latest, windows-latest, macOS-latest]
66+
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
6567
fail-fast: false
6668
runs-on: ${{ matrix.os }}
6769
steps:
@@ -84,7 +86,7 @@ jobs:
8486
needs: test-save
8587
strategy:
8688
matrix:
87-
os: [ubuntu-latest, windows-latest, macOS-latest]
89+
os: [ubuntu-latest, ubuntu-16.04, windows-latest, macOS-latest]
8890
fail-fast: false
8991
runs-on: ${{ matrix.os }}
9092
steps:

__tests__/actionUtils.test.ts

+28-219
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,65 @@
11
import * as core from "@actions/core";
2-
import * as io from "@actions/io";
3-
import { promises as fs } from "fs";
4-
import * as os from "os";
5-
import * as path from "path";
62

73
import { Events, Outputs, RefKey, State } from "../src/constants";
8-
import { ArtifactCacheEntry } from "../src/contracts";
94
import * as actionUtils from "../src/utils/actionUtils";
105

11-
import uuid = require("uuid");
12-
136
jest.mock("@actions/core");
14-
jest.mock("os");
15-
16-
function getTempDir(): string {
17-
return path.join(__dirname, "_temp", "actionUtils");
18-
}
197

208
afterEach(() => {
219
delete process.env[Events.Key];
2210
delete process.env[RefKey];
2311
});
2412

25-
afterAll(async () => {
26-
delete process.env["GITHUB_WORKSPACE"];
27-
await io.rmRF(getTempDir());
28-
});
29-
30-
test("getArchiveFileSize returns file size", () => {
31-
const filePath = path.join(__dirname, "__fixtures__", "helloWorld.txt");
32-
33-
const size = actionUtils.getArchiveFileSize(filePath);
34-
35-
expect(size).toBe(11);
36-
});
37-
38-
test("isExactKeyMatch with undefined cache entry returns false", () => {
13+
test("isExactKeyMatch with undefined cache key returns false", () => {
3914
const key = "linux-rust";
40-
const cacheEntry = undefined;
15+
const cacheKey = undefined;
4116

42-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
17+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
4318
});
4419

45-
test("isExactKeyMatch with empty cache entry returns false", () => {
20+
test("isExactKeyMatch with empty cache key returns false", () => {
4621
const key = "linux-rust";
47-
const cacheEntry: ArtifactCacheEntry = {};
22+
const cacheKey = "";
4823

49-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
24+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
5025
});
5126

5227
test("isExactKeyMatch with different keys returns false", () => {
5328
const key = "linux-rust";
54-
const cacheEntry: ArtifactCacheEntry = {
55-
cacheKey: "linux-"
56-
};
29+
const cacheKey = "linux-";
5730

58-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
31+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
5932
});
6033

6134
test("isExactKeyMatch with different key accents returns false", () => {
6235
const key = "linux-áccent";
63-
const cacheEntry: ArtifactCacheEntry = {
64-
cacheKey: "linux-accent"
65-
};
36+
const cacheKey = "linux-accent";
6637

67-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(false);
38+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(false);
6839
});
6940

7041
test("isExactKeyMatch with same key returns true", () => {
7142
const key = "linux-rust";
72-
const cacheEntry: ArtifactCacheEntry = {
73-
cacheKey: "linux-rust"
74-
};
43+
const cacheKey = "linux-rust";
7544

76-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
45+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
7746
});
7847

7948
test("isExactKeyMatch with same key and different casing returns true", () => {
8049
const key = "linux-rust";
81-
const cacheEntry: ArtifactCacheEntry = {
82-
cacheKey: "LINUX-RUST"
83-
};
50+
const cacheKey = "LINUX-RUST";
8451

85-
expect(actionUtils.isExactKeyMatch(key, cacheEntry)).toBe(true);
52+
expect(actionUtils.isExactKeyMatch(key, cacheKey)).toBe(true);
8653
});
8754

8855
test("setOutputAndState with undefined entry to set cache-hit output", () => {
8956
const key = "linux-rust";
90-
const cacheEntry = undefined;
57+
const cacheKey = undefined;
9158

9259
const setOutputMock = jest.spyOn(core, "setOutput");
9360
const saveStateMock = jest.spyOn(core, "saveState");
9461

95-
actionUtils.setOutputAndState(key, cacheEntry);
62+
actionUtils.setOutputAndState(key, cacheKey);
9663

9764
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
9865
expect(setOutputMock).toHaveBeenCalledTimes(1);
@@ -102,43 +69,33 @@ test("setOutputAndState with undefined entry to set cache-hit output", () => {
10269

10370
test("setOutputAndState with exact match to set cache-hit output and state", () => {
10471
const key = "linux-rust";
105-
const cacheEntry: ArtifactCacheEntry = {
106-
cacheKey: "linux-rust"
107-
};
72+
const cacheKey = "linux-rust";
10873

10974
const setOutputMock = jest.spyOn(core, "setOutput");
11075
const saveStateMock = jest.spyOn(core, "saveState");
11176

112-
actionUtils.setOutputAndState(key, cacheEntry);
77+
actionUtils.setOutputAndState(key, cacheKey);
11378

11479
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "true");
11580
expect(setOutputMock).toHaveBeenCalledTimes(1);
11681

117-
expect(saveStateMock).toHaveBeenCalledWith(
118-
State.CacheResult,
119-
JSON.stringify(cacheEntry)
120-
);
82+
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
12183
expect(saveStateMock).toHaveBeenCalledTimes(1);
12284
});
12385

12486
test("setOutputAndState with no exact match to set cache-hit output and state", () => {
12587
const key = "linux-rust";
126-
const cacheEntry: ArtifactCacheEntry = {
127-
cacheKey: "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43"
128-
};
88+
const cacheKey = "linux-rust-bb828da54c148048dd17899ba9fda624811cfb43";
12989

13090
const setOutputMock = jest.spyOn(core, "setOutput");
13191
const saveStateMock = jest.spyOn(core, "saveState");
13292

133-
actionUtils.setOutputAndState(key, cacheEntry);
93+
actionUtils.setOutputAndState(key, cacheKey);
13494

13595
expect(setOutputMock).toHaveBeenCalledWith(Outputs.CacheHit, "false");
13696
expect(setOutputMock).toHaveBeenCalledTimes(1);
13797

138-
expect(saveStateMock).toHaveBeenCalledWith(
139-
State.CacheResult,
140-
JSON.stringify(cacheEntry)
141-
);
98+
expect(saveStateMock).toHaveBeenCalledWith(State.CacheMatchedKey, cacheKey);
14299
expect(saveStateMock).toHaveBeenCalledTimes(1);
143100
});
144101

@@ -152,27 +109,23 @@ test("getCacheState with no state returns undefined", () => {
152109

153110
expect(state).toBe(undefined);
154111

155-
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
112+
expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey);
156113
expect(getStateMock).toHaveBeenCalledTimes(1);
157114
});
158115

159116
test("getCacheState with valid state", () => {
160-
const cacheEntry: ArtifactCacheEntry = {
161-
cacheKey: "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43",
162-
scope: "refs/heads/master",
163-
creationTime: "2019-11-13T19:18:02+00:00",
164-
archiveLocation: "www.actionscache.test/download"
165-
};
117+
const cacheKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43";
118+
166119
const getStateMock = jest.spyOn(core, "getState");
167120
getStateMock.mockImplementation(() => {
168-
return JSON.stringify(cacheEntry);
121+
return cacheKey;
169122
});
170123

171124
const state = actionUtils.getCacheState();
172125

173-
expect(state).toEqual(cacheEntry);
126+
expect(state).toEqual(cacheKey);
174127

175-
expect(getStateMock).toHaveBeenCalledWith(State.CacheResult);
128+
expect(getStateMock).toHaveBeenCalledWith(State.CacheMatchedKey);
176129
expect(getStateMock).toHaveBeenCalledTimes(1);
177130
});
178131

@@ -195,137 +148,6 @@ test("isValidEvent returns false for event that does not have a branch or tag",
195148
expect(isValidEvent).toBe(false);
196149
});
197150

198-
test("resolvePaths with no ~ in path", async () => {
199-
const filePath = ".cache";
200-
201-
// Create the following layout:
202-
// cwd
203-
// cwd/.cache
204-
// cwd/.cache/file.txt
205-
206-
const root = path.join(getTempDir(), "no-tilde");
207-
// tarball entries will be relative to workspace
208-
process.env["GITHUB_WORKSPACE"] = root;
209-
210-
await fs.mkdir(root, { recursive: true });
211-
const cache = path.join(root, ".cache");
212-
await fs.mkdir(cache, { recursive: true });
213-
await fs.writeFile(path.join(cache, "file.txt"), "cached");
214-
215-
const originalCwd = process.cwd();
216-
217-
try {
218-
process.chdir(root);
219-
220-
const resolvedPath = await actionUtils.resolvePaths([filePath]);
221-
222-
const expectedPath = [filePath];
223-
expect(resolvedPath).toStrictEqual(expectedPath);
224-
} finally {
225-
process.chdir(originalCwd);
226-
}
227-
});
228-
229-
test("resolvePaths with ~ in path", async () => {
230-
const cacheDir = uuid();
231-
const filePath = `~/${cacheDir}`;
232-
// Create the following layout:
233-
// ~/uuid
234-
// ~/uuid/file.txt
235-
236-
const homedir = jest.requireActual("os").homedir();
237-
const homedirMock = jest.spyOn(os, "homedir");
238-
homedirMock.mockImplementation(() => {
239-
return homedir;
240-
});
241-
242-
const target = path.join(homedir, cacheDir);
243-
await fs.mkdir(target, { recursive: true });
244-
await fs.writeFile(path.join(target, "file.txt"), "cached");
245-
246-
const root = getTempDir();
247-
process.env["GITHUB_WORKSPACE"] = root;
248-
249-
try {
250-
const resolvedPath = await actionUtils.resolvePaths([filePath]);
251-
252-
const expectedPath = [path.relative(root, target)];
253-
expect(resolvedPath).toStrictEqual(expectedPath);
254-
} finally {
255-
await io.rmRF(target);
256-
}
257-
});
258-
259-
test("resolvePaths with home not found", async () => {
260-
const filePath = "~/.cache/yarn";
261-
const homedirMock = jest.spyOn(os, "homedir");
262-
homedirMock.mockImplementation(() => {
263-
return "";
264-
});
265-
266-
await expect(actionUtils.resolvePaths([filePath])).rejects.toThrow(
267-
"Unable to determine HOME directory"
268-
);
269-
});
270-
271-
test("resolvePaths inclusion pattern returns found", async () => {
272-
const pattern = "*.ts";
273-
// Create the following layout:
274-
// inclusion-patterns
275-
// inclusion-patterns/miss.txt
276-
// inclusion-patterns/test.ts
277-
278-
const root = path.join(getTempDir(), "inclusion-patterns");
279-
// tarball entries will be relative to workspace
280-
process.env["GITHUB_WORKSPACE"] = root;
281-
282-
await fs.mkdir(root, { recursive: true });
283-
await fs.writeFile(path.join(root, "miss.txt"), "no match");
284-
await fs.writeFile(path.join(root, "test.ts"), "match");
285-
286-
const originalCwd = process.cwd();
287-
288-
try {
289-
process.chdir(root);
290-
291-
const resolvedPath = await actionUtils.resolvePaths([pattern]);
292-
293-
const expectedPath = ["test.ts"];
294-
expect(resolvedPath).toStrictEqual(expectedPath);
295-
} finally {
296-
process.chdir(originalCwd);
297-
}
298-
});
299-
300-
test("resolvePaths exclusion pattern returns not found", async () => {
301-
const patterns = ["*.ts", "!test.ts"];
302-
// Create the following layout:
303-
// exclusion-patterns
304-
// exclusion-patterns/miss.txt
305-
// exclusion-patterns/test.ts
306-
307-
const root = path.join(getTempDir(), "exclusion-patterns");
308-
// tarball entries will be relative to workspace
309-
process.env["GITHUB_WORKSPACE"] = root;
310-
311-
await fs.mkdir(root, { recursive: true });
312-
await fs.writeFile(path.join(root, "miss.txt"), "no match");
313-
await fs.writeFile(path.join(root, "test.ts"), "no match");
314-
315-
const originalCwd = process.cwd();
316-
317-
try {
318-
process.chdir(root);
319-
320-
const resolvedPath = await actionUtils.resolvePaths(patterns);
321-
322-
const expectedPath = [];
323-
expect(resolvedPath).toStrictEqual(expectedPath);
324-
} finally {
325-
process.chdir(originalCwd);
326-
}
327-
});
328-
329151
test("isValidEvent returns true for event that has a ref", () => {
330152
const event = Events.Push;
331153
process.env[Events.Key] = event;
@@ -335,16 +157,3 @@ test("isValidEvent returns true for event that has a ref", () => {
335157

336158
expect(isValidEvent).toBe(true);
337159
});
338-
339-
test("unlinkFile unlinks file", async () => {
340-
const testDirectory = await fs.mkdtemp("unlinkFileTest");
341-
const testFile = path.join(testDirectory, "test.txt");
342-
await fs.writeFile(testFile, "hello world");
343-
344-
await actionUtils.unlinkFile(testFile);
345-
346-
// This should throw as testFile should not exist
347-
await expect(fs.stat(testFile)).rejects.toThrow();
348-
349-
await fs.rmdir(testDirectory);
350-
});

0 commit comments

Comments
 (0)