-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.test.js
191 lines (160 loc) · 7.11 KB
/
action.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const fs = require('fs')
const nock = require('nock')
const path = require('path')
const os = require('os')
const core = require('@actions/core')
const mockRelease = {
assets: [
{
id: 1,
name: 'copywrite_0.1.3_darwin_x86_64.tar.gz',
url: 'https://api.github.com/repos/hashicorp/copywrite/releases/assets/1',
browser_download_url: 'https://github.com/hashicorp/copywrite/releases/download/v0.1.3/copywrite_0.1.3_darwin_x86_64.tar.gz'
},
{
id: 2,
name: 'copywrite_0.1.3_linux_x86_64.tar.gz',
url: 'https://api.github.com/repos/hashicorp/copywrite/releases/assets/2',
browser_download_url: 'https://github.com/hashicorp/copywrite/releases/download/v0.1.3/copywrite_0.1.3_linux_x86_64.tar.gz'
},
{
id: 3,
name: 'copywrite_0.1.3_windows_x86_64.zip',
url: 'https://api.github.com/repos/hashicorp/copywrite/releases/assets/3',
browser_download_url: 'https://github.com/hashicorp/copywrite/releases/download/v0.1.3/copywrite_0.1.3_windows_x86_64.zip'
}
],
id: '1',
name: 'v0.1.3',
tag_name: 'v0.1.3'
}
beforeAll(() => {
nock.disableNetConnect()
})
beforeEach(() => {
process.env.INPUT_TOKEN = 'testtoken'
process.env.INPUT_VERSION = 'latest'
process.env['INPUT_VERSION-CHECKSUM'] = '5663389ef1a8ec48af6ca622e66bf0f54ba8f22c127f14cb8a3f429e40868582'
const spyOsArch = jest.spyOn(os, 'arch')
spyOsArch.mockReturnValue('x64')
const spyOsPlatform = jest.spyOn(os, 'platform')
spyOsPlatform.mockReturnValue('win32')
})
describe('action', () => {
test('installs latest version', (done) => {
const scopeAPI = nock('https://api.github.com')
.get('/repos/hashicorp/copywrite/releases/latest')
.reply(200, mockRelease)
.get('/repos/hashicorp/copywrite/releases/assets/3')
.replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
// const scopeWeb = nock('https://github.com')
// .get('/hashicorp/copywrite/releases/download/v0.1.3/copywrite_0.1.3_windows_x86_64.zip')
// .replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
const spyCoreAddPath = jest.spyOn(core, 'addPath')
const spyCoreSetOutput = jest.spyOn(core, 'setOutput')
fs.mkdtemp(path.join(os.tmpdir(), 'setup-copywrite-'), async (err, directory) => {
if (err) throw err
process.env.RUNNER_TEMP = directory
const spyOsHomedir = jest.spyOn(os, 'homedir')
spyOsHomedir.mockReturnValue(directory)
const action = require('./action')
await expect(await action()).resolves
expect(scopeAPI.isDone()).toBeTruthy()
expect(spyCoreAddPath).toHaveBeenCalled()
expect(spyCoreSetOutput).toHaveBeenCalledWith('version', 'v0.1.3')
done()
})
})
test('installs configured version', (done) => {
const scopeAPI = nock('https://api.github.com')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.reply(200, mockRelease)
.get('/repos/hashicorp/copywrite/releases/assets/3')
.replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
// const scopeWeb = nock('https://github.com')
// .get('/hashicorp/copywrite/releases/download/v0.1.3/copywrite_0.1.3_windows_x86_64.zip')
// .replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
const spyCoreAddPath = jest.spyOn(core, 'addPath')
const spyCoreSetOutput = jest.spyOn(core, 'setOutput')
fs.mkdtemp(path.join(os.tmpdir(), 'setup-copywrite-'), async (err, directory) => {
if (err) throw err
process.env.INPUT_VERSION = 'v0.1.3'
process.env.RUNNER_TEMP = directory
const spyOsHomedir = jest.spyOn(os, 'homedir')
spyOsHomedir.mockReturnValue(directory)
const action = require('./action')
await expect(await action()).resolves
expect(scopeAPI.isDone()).toBeTruthy()
expect(spyCoreAddPath).toHaveBeenCalled()
expect(spyCoreSetOutput).toHaveBeenCalledWith('version', 'v0.1.3')
done()
})
})
test('retries transient errors', (done) => {
const scope = nock('https://api.github.com')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.replyWithError(500, 'expected transient error')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.reply(200, mockRelease)
.get('/repos/hashicorp/copywrite/releases/assets/3')
.replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
fs.mkdtemp(path.join(os.tmpdir(), 'setup-copywrite-'), async (err, directory) => {
if (err) throw err
process.env.INPUT_VERSION = 'v0.1.3'
process.env.RUNNER_TEMP = directory
const spyOsHomedir = jest.spyOn(os, 'homedir')
spyOsHomedir.mockReturnValue(directory)
const action = require('./action')
await expect(await action()).resolves
expect(scope.isDone()).toBeTruthy()
done()
})
})
test('retries abuse limit errors', (done) => {
const scope = nock('https://api.github.com')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.replyWithError(403, {
message: 'You have triggered an abuse detection mechanism and have been temporarily blocked from content creation. Please retry your request again later.',
documentation_url: 'https://docs.github.com/rest/overview/resources-in-the-rest-api#abuse-rate-limits'
})
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.reply(200, mockRelease)
.get('/repos/hashicorp/copywrite/releases/assets/3')
.replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
fs.mkdtemp(path.join(os.tmpdir(), 'setup-copywrite-'), async (err, directory) => {
if (err) throw err
process.env.INPUT_VERSION = 'v0.1.3'
process.env.RUNNER_TEMP = directory
const spyOsHomedir = jest.spyOn(os, 'homedir')
spyOsHomedir.mockReturnValue(directory)
const action = require('./action')
await expect(await action()).resolves
expect(scope.isDone()).toBeTruthy()
done()
})
})
test('retries rate limit errors', (done) => {
const scope = nock('https://api.github.com')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.replyWithError(429, 'expected rate limit error')
.get('/repos/hashicorp/copywrite/releases/tags/v0.1.3')
.reply(200, mockRelease)
.get('/repos/hashicorp/copywrite/releases/assets/3')
.replyWithFile(200, path.resolve(__dirname, 'test.zip'), { 'content-type': 'application/octet-stream' })
fs.mkdtemp(path.join(os.tmpdir(), 'setup-copywrite-'), async (err, directory) => {
if (err) throw err
process.env.INPUT_VERSION = 'v0.1.3'
process.env.RUNNER_TEMP = directory
const spyOsHomedir = jest.spyOn(os, 'homedir')
spyOsHomedir.mockReturnValue(directory)
const action = require('./action')
await expect(await action()).resolves
expect(scope.isDone()).toBeTruthy()
done()
})
})
})