Skip to content

Commit c08e62d

Browse files
committed
Clean up from review and add nock for test validation
1 parent 3d0361d commit c08e62d

File tree

9 files changed

+139
-10
lines changed

9 files changed

+139
-10
lines changed
211 Bytes
Binary file not shown.
211 Bytes
Binary file not shown.
212 Bytes
Binary file not shown.
213 Bytes
Binary file not shown.

__tests__/installer.test.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import io = require('@actions/io');
22
import fs = require('fs');
3+
import nock = require('nock');
34
import os = require('os');
45
import path = require('path');
56

@@ -36,6 +37,10 @@ describe('installer tests', () => {
3637
await io.rmRF(tempDir);
3738
}, 100000);
3839

40+
beforeEach(() => {
41+
nock.cleanAll();
42+
});
43+
3944
it('Acquires version of node if no matching version is installed', async () => {
4045
await installer.getNode('10.16.0');
4146
const nodeDir = path.join(toolDir, 'node', '10.16.0', os.arch());
@@ -123,9 +128,25 @@ describe('installer tests', () => {
123128

124129
it('Acquires specified x86 version of node if no matching version is installed', async () => {
125130
const arch = 'x86';
126-
await installer.getNode('8.8.1', arch);
127-
const nodeDir = path.join(toolDir, 'node', '8.8.1', arch);
128-
131+
const version = '8.8.0';
132+
const fileExtension = IS_WINDOWS ? '7z' : 'tar.gz';
133+
const platform = {
134+
linux: 'linux',
135+
darwin: 'darwin',
136+
win32: 'win'
137+
}[process.platform];
138+
const fileName = `node-v${version}-${platform}-${arch}.${fileExtension}`;
139+
const pathOnNodeJs = `/dist/v${version}/${fileName}`;
140+
const scope = nock('https://nodejs.org')
141+
.get(pathOnNodeJs)
142+
.replyWithFile(
143+
200,
144+
path.join(__dirname, '__fixtures__', `mock-${fileName}`)
145+
);
146+
await installer.getNode(version, arch);
147+
const nodeDir = path.join(toolDir, 'node', version, arch);
148+
149+
expect(scope.isDone()).toBe(true);
129150
expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
130151
if (IS_WINDOWS) {
131152
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);
@@ -136,9 +157,25 @@ describe('installer tests', () => {
136157

137158
it('Acquires specified x64 version of node if no matching version is installed', async () => {
138159
const arch = 'x64';
139-
await installer.getNode('8.8.1', arch);
140-
const nodeDir = path.join(toolDir, 'node', '8.8.1', arch);
141-
160+
const version = '8.9.1';
161+
const fileExtension = IS_WINDOWS ? '7z' : 'tar.gz';
162+
const platform = {
163+
linux: 'linux',
164+
darwin: 'darwin',
165+
win32: 'win'
166+
}[process.platform];
167+
const fileName = `node-v${version}-${platform}-${arch}.${fileExtension}`;
168+
const pathOnNodeJs = `/dist/v${version}/${fileName}`;
169+
const scope = nock('https://nodejs.org')
170+
.get(pathOnNodeJs)
171+
.replyWithFile(
172+
200,
173+
path.join(__dirname, '__fixtures__', `mock-${fileName}`)
174+
);
175+
await installer.getNode(version, arch);
176+
const nodeDir = path.join(toolDir, 'node', version, arch);
177+
178+
expect(scope.isDone()).toBe(true);
142179
expect(fs.existsSync(`${nodeDir}.complete`)).toBe(true);
143180
if (IS_WINDOWS) {
144181
expect(fs.existsSync(path.join(nodeDir, 'node.exe'))).toBe(true);

lib/setup-node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ function run() {
3131
if (!version) {
3232
version = core.getInput('node-version');
3333
}
34-
const osArch = core.getInput('node-arch') || os.arch();
3534
if (version) {
35+
const osArch = core.getInput('node-arch') || os.arch();
3636
// TODO: installer doesn't support proxy
3737
yield installer.getNode(version, osArch);
3838
}

package-lock.json

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

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"@actions/github": "^1.0.0",
2727
"@actions/io": "^1.0.0",
2828
"@actions/tool-cache": "^1.0.0",
29-
"typed-rest-client": "^1.5.0",
30-
"semver": "^6.1.1"
29+
"semver": "^6.1.1",
30+
"typed-rest-client": "^1.5.0"
3131
},
3232
"devDependencies": {
3333
"@types/jest": "^24.0.13",
@@ -36,6 +36,7 @@
3636
"husky": "^2.3.0",
3737
"jest": "^24.8.0",
3838
"jest-circus": "^24.7.1",
39+
"nock": "^11.3.5",
3940
"prettier": "^1.17.1",
4041
"ts-jest": "^24.0.2",
4142
"typescript": "^3.5.1"

src/setup-node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ async function run() {
1414
if (!version) {
1515
version = core.getInput('node-version');
1616
}
17-
const osArch = core.getInput('node-arch') || os.arch();
17+
1818
if (version) {
19+
const osArch = core.getInput('node-arch') || os.arch();
1920
// TODO: installer doesn't support proxy
2021
await installer.getNode(version, osArch);
2122
}

0 commit comments

Comments
 (0)