-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleanup.test.js
136 lines (118 loc) · 4.08 KB
/
cleanup.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { cleanup } = require("./cleanup.js");
const {
DeviceFarmClient,
ListUploadsCommand,
DeleteUploadCommand
} = require("@aws-sdk/client-device-farm");
const core = require("@actions/core");
const github = require("@actions/github");
const fs = require("fs/promises");
const { UPLOAD } = require("./constants");
const { mockClient } = require("aws-sdk-client-mock");
require("aws-sdk-client-mock-jest");
const mock = require("mock-fs");
jest.mock("@actions/core");
jest.mock("@actions/github", () => ({
context: {
runId: 1
}
}));
jest.mock("fs/promises");
function mockGetState(requestResponse) {
return function (name, options) { // eslint-disable-line no-unused-vars
return requestResponse[name]
}
}
const STATES = {
projectArn: "fake-project-arn",
artifactFolder: "folder"
};
const mockDeviceFarm = mockClient(DeviceFarmClient);
describe("Cleanup", () => {
beforeEach(() => {
jest.clearAllMocks();
core.getState = jest.fn().mockImplementation(mockGetState(STATES));
mockDeviceFarm.reset();
});
afterEach(() => {
mock.restore();
});
it("should delete matching upload(s) only", async () => {
mockDeviceFarm
.on(ListUploadsCommand, {
arn: "fake-project-arn"
})
.resolves({
uploads: [
{ // Matches all criteria
arn: "fake-upload-arn-1",
name: "1_fake-upload-name",
status: UPLOAD.STATUS.INITIALIZED,
type: "ANDROID_APP",
category: UPLOAD.CATEGORY.PRIVATE
},
{ // Fails match on category
arn: "fake-upload-arn-2",
name: "1_fake-upload-name",
status: UPLOAD.STATUS.INITIALIZED,
type: "ANDROID_APP",
category: UPLOAD.CATEGORY.CURATED
},
{ // Fails match on name
arn: "fake-upload-arn-3",
name: "2_fake-upload-name",
status: UPLOAD.STATUS.INITIALIZED,
type: "ANDROID_APP",
category: UPLOAD.CATEGORY.PRIVATE
},
{ // Fails match on status
arn: "fake-upload-arn-4",
name: "1_fake-upload-name",
status: UPLOAD.STATUS.SUCCEEDED,
type: "ANDROID_APP",
category: UPLOAD.CATEGORY.PRIVATE
}
]
});
await cleanup();
expect(github.context.runId).toBe(1);
expect(mockDeviceFarm).toHaveReceivedNthSpecificCommandWith(1, ListUploadsCommand, {
arn: "fake-project-arn"
});
expect(mockDeviceFarm).toHaveReceivedNthSpecificCommandWith(1, DeleteUploadCommand, {
arn: "fake-upload-arn-1"
});
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
it("should delete artifact folder", async () => {
mockDeviceFarm
.on(ListUploadsCommand, {
arn: "fake-project-arn"
})
.resolves({
uploads: []
});
mock({
'folder': {/** empty directory */}
});
await cleanup();
expect(fs.rm).toBeCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
it("should not delete any folder", async () => {
mockDeviceFarm
.on(ListUploadsCommand, {
arn: "fake-project-arn"
})
.resolves({
uploads: []
});
await cleanup();
expect(fs.rm).toBeCalledTimes(0);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
it("should handle exceptions gracefully", async () => {
await cleanup();
expect(core.setFailed).toHaveBeenCalledWith("Cannot read properties of undefined (reading 'uploads')");
});
});