-
Notifications
You must be signed in to change notification settings - Fork 41
/
gh-task-config.spec.ts
146 lines (127 loc) · 8.12 KB
/
gh-task-config.spec.ts
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import 'reflect-metadata';
import * as actionCore from '@actions/core';
import normalizePath from 'normalize-path';
import path from 'path';
import { Mock, Times, IMock } from 'typemoq';
import { GHTaskConfig } from './gh-task-config';
describe(GHTaskConfig, () => {
let processStub: any;
let actionCoreMock: IMock<typeof actionCore>;
let taskConfig: GHTaskConfig;
let markdownSummaryMock: IMock<typeof actionCore.summary>;
beforeEach(() => {
processStub = {
env: {},
} as any;
actionCoreMock = Mock.ofType<typeof actionCore>();
markdownSummaryMock = Mock.ofType<typeof actionCore.summary>();
taskConfig = new GHTaskConfig(processStub, actionCoreMock.object);
});
afterEach(() => {
actionCoreMock.verifyAll();
markdownSummaryMock.verifyAll();
});
function getPlatformAgnosticPath(inputPath: string): string {
return normalizePath(path.resolve(inputPath));
}
it.each`
inputOption | inputValue | expectedValue | getInputFunc
${'repo-token'} | ${'token'} | ${'token'} | ${() => taskConfig.getToken()}
${'scan-url-relative-path'} | ${'path'} | ${'path'} | ${() => taskConfig.getStaticSiteUrlRelativePath()}
${'scan-url-relative-path'} | ${''} | ${undefined} | ${() => taskConfig.getStaticSiteUrlRelativePath()}
${'chrome-path'} | ${'./chrome-path'} | ${getPlatformAgnosticPath(__dirname + '/chrome-path')} | ${() => taskConfig.getChromePath()}
${'input-file'} | ${'./input-file'} | ${getPlatformAgnosticPath(__dirname + '/input-file')} | ${() => taskConfig.getInputFile()}
${'input-file'} | ${''} | ${undefined} | ${() => taskConfig.getInputFile()}
${'output-dir'} | ${'./output-dir'} | ${getPlatformAgnosticPath(__dirname + '/output-dir')} | ${() => taskConfig.getReportOutDir()}
${'site-dir'} | ${'./site-dir'} | ${getPlatformAgnosticPath(__dirname + '/site-dir')} | ${() => taskConfig.getStaticSiteDir()}
${'site-dir'} | ${''} | ${undefined} | ${() => taskConfig.getStaticSiteDir()}
${'url'} | ${'url'} | ${'url'} | ${() => taskConfig.getUrl()}
${'url'} | ${''} | ${undefined} | ${() => taskConfig.getUrl()}
${'discovery-patterns'} | ${'abc'} | ${'abc'} | ${() => taskConfig.getDiscoveryPatterns()}
${'discovery-patterns'} | ${''} | ${undefined} | ${() => taskConfig.getDiscoveryPatterns()}
${'input-urls'} | ${'abc'} | ${'abc'} | ${() => taskConfig.getInputUrls()}
${'input-urls'} | ${''} | ${undefined} | ${() => taskConfig.getInputUrls()}
${'max-urls'} | ${'20'} | ${20} | ${() => taskConfig.getMaxUrls()}
${'scan-timeout'} | ${'100000'} | ${100000} | ${() => taskConfig.getScanTimeout()}
${'localhost-port'} | ${'8080'} | ${8080} | ${() => taskConfig.getStaticSitePort()}
${'localhost-port'} | ${''} | ${undefined} | ${() => taskConfig.getStaticSitePort()}
${'baseline-file'} | ${'./baseline-file'} | ${getPlatformAgnosticPath(__dirname + '/baseline-file')} | ${() => taskConfig.getBaselineFile()}
${'baseline-file'} | ${''} | ${undefined} | ${() => taskConfig.getBaselineFile()}
${'single-worker'} | ${'true'} | ${true} | ${() => taskConfig.getSingleWorker()}
${'single-worker'} | ${''} | ${true} | ${() => taskConfig.getSingleWorker()}
${'single-worker'} | ${'false'} | ${false} | ${() => taskConfig.getSingleWorker()}
${'hosting-mode'} | ${'staticSite'} | ${'staticSite'} | ${() => taskConfig.getHostingMode()}
${'hosting-mode'} | ${'dynamicSite'} | ${'dynamicSite'} | ${() => taskConfig.getHostingMode()}
${'hosting-mode'} | ${''} | ${undefined} | ${() => taskConfig.getHostingMode()}
`(
`input value '$inputValue' returned as '$expectedValue' for '$inputOption' parameter`,
({ inputOption, getInputFunc, inputValue, expectedValue }) => {
if (typeof inputValue == 'boolean') {
actionCoreMock
.setup((am) => am.getBooleanInput(inputOption))
.returns(() => inputValue)
.verifiable(Times.once());
} else {
actionCoreMock
.setup((am) => am.getInput(inputOption))
.returns(() => inputValue)
.verifiable(Times.once());
}
const retrievedOption = getInputFunc();
expect(retrievedOption).toStrictEqual(expectedValue);
},
);
it('should use environment value when chrome-path input is undefined', () => {
const chromePath = 'some path';
processStub = {
env: {
CHROME_BIN: chromePath,
},
} as any;
actionCoreMock
.setup((o) => o.getInput('chrome-path'))
.returns(() => '')
.verifiable(Times.once());
taskConfig = new GHTaskConfig(processStub, actionCoreMock.object);
const absolutePath = taskConfig.getChromePath();
expect(absolutePath).toBe(chromePath);
});
it('should use workspace to build absolute file path', () => {
const workspace = '/home/user/workspace';
processStub = {
env: {
GITHUB_WORKSPACE: workspace,
},
} as any;
actionCoreMock
.setup((o) => o.getInput('input-file'))
.returns(() => './file.txt')
.verifiable(Times.once());
taskConfig = new GHTaskConfig(processStub, actionCoreMock.object);
const absolutePath = taskConfig.getInputFile();
expect(absolutePath).toBe(getPlatformAgnosticPath(`${workspace}/file.txt`));
});
it('should return run id from environment', () => {
const runId = 789;
processStub = {
env: {
GITHUB_RUN_ID: `${runId}`,
},
} as any;
taskConfig = new GHTaskConfig(processStub, actionCoreMock.object);
const actualRunId = taskConfig.getRunId();
expect(actualRunId).toBe(runId);
});
it('should write job summary', async () => {
const markdownStub = 'markdownStub';
markdownSummaryMock
.setup((o) => o.addRaw(markdownStub))
.returns(() => markdownSummaryMock.object)
.verifiable();
markdownSummaryMock.setup((o) => o.write()).verifiable();
actionCoreMock.setup((o) => o.summary).returns(() => markdownSummaryMock.object);
await taskConfig.writeJobSummary(markdownStub);
});
});