forked from BetaHuhn/action-input-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.test.ts
201 lines (161 loc) · 4.92 KB
/
README.test.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
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
192
193
194
195
196
197
198
199
200
201
import * as parser from "../src";
describe('README tests', () => {
const ENV = { ...process.env };
beforeEach(() => {
process.env = { ...ENV };
});
afterEach(() => {
process.env = ENV;
});
it('Example', () => {
process.env.INPUT_NAMES = 'Maximilian\nRichard';
const value = parser.getInput({
input: 'names',
type: <const>[String],
});
const check: TypeCheck<typeof value, (undefined | string)[] | undefined> = value;
expect(value).toEqual(['Maximilian', 'Richard']);
});
it('Configuration', () => {
process.env.INPUT_NAMES = '\nRichard';
const value = parser.getInput({
input: 'names',
type: <const>[String],
default: <const>['maximilian'],
});
const check: TypeCheck<typeof value, [string, ...(undefined | string)[]]> = value;
expect(value).toEqual(['maximilian', 'Richard']);
});
it('Basic Example', () => {
process.env.INPUT_NAME = 'Maximilian';
const value1 = parser.getInput('name');
const check1: TypeCheck<typeof value1, string | undefined> = value1;
const value2 = parser.getInput({
input: 'name',
});
const check2: TypeCheck<typeof value2, string | undefined> = value2;
expect(value1).toEqual('Maximilian');
expect(value2).toEqual(value1);
});
describe('Specifiy a type', () => {
it('simple boolean', () => {
process.env.INPUT_DRY_RUN = 'true';
const value = parser.getInput({
input: 'dry_run',
type: Boolean,
});
const check: TypeCheck<typeof value, boolean | undefined> = value;
expect(value).toEqual(true);
});
it('string array', () => {
process.env.INPUT_STAGES = 'dev\nprod';
const value = parser.getInput({
input: 'stages',
type: <const>[String],
});
const check: TypeCheck<typeof value, (string | undefined)[] | undefined> = value;
expect(value).toEqual(['dev', 'prod']);
});
it('number string tuple', () => {
process.env.INPUT_FRUITS = '10\napples';
const value = parser.getInput({
input: 'fruits',
type: <const>[Number, String],
});
const check: TypeCheck<typeof value, [number | undefined, string | undefined] | undefined> = value;
expect(value).toEqual([10, 'apples']);
});
});
describe('Specify a default value', () => {
it('string with default value', () => {
const value = parser.getInput({
input: 'name',
default: 'Maximilian',
});
const check: TypeCheck<typeof value, string> = value;
expect(value).toEqual('Maximilian');
});
it('number string tuple', () => {
process.env.INPUT_COMMAND = 'bring me\n\napples';
const value = parser.getInput({
input: 'command',
type: <const>[String, Number, String],
default: <const>[undefined, 10],
});
const check: TypeCheck<typeof value, [string | undefined, number, string | undefined]> = value;
expect(value).toEqual(['bring me', 10, 'apples']);
});
});
it('Set an input to be required', () => {
expect(() => {
const value = parser.getInput({
input: 'name',
required: true,
});
}).toThrow('input `name` is required but was not provided');
});
it('Pick from multiple inputs', () => {
process.env.INPUT_GH_PAT = 'abcdefghijklmnopqrstuvwxyz';
const value = parser.getInput({
input: ['GITHUB_TOKEN', 'GH_PAT']
});
const check: TypeCheck<typeof value, string | undefined> = value;
expect(value).toEqual('abcdefghijklmnopqrstuvwxyz');
});
it('Pick from multiple inputs', () => {
process.env.INPUT_GREETING = 'Hello world!';
process.env.INPUT_GH_PAT = 'abcdefghijklmnopqrstuvwxyz';
const { value1, value2, value3 } = parser.getInputs({
value1: 'greeting',
value2: {
input: ['GITHUB_TOKEN', 'GH_PAT'],
required: true,
},
value3: {
input: 'max retires',
type: Number,
default: 3,
},
});
const check1: TypeCheck<typeof value1, string | undefined> = value1;
const check2: TypeCheck<typeof value2, string> = value2;
const check3: TypeCheck<typeof value3, number> = value3;
expect(value1).toEqual('Hello world!');
expect(value2).toEqual('abcdefghijklmnopqrstuvwxyz');
expect(value3).toEqual(3);
});
it('Advanced example', () => {
process.env.INPUT_GITHUB_TOKEN = 'TOKEN';
process.env.INPUT_REPOSITORY = 'username/reponame';
process.env.INPUT_LABELS = 'merged\nready';
const config = parser.getInputs({
githubToken: {
input: 'github_token',
required: true,
},
repository: {
input: 'repository',
type: (val: string) => {
const [user, repo] = val.split('/')
return { user, repo }
},
},
labels: {
input: 'labels',
type: <const>[String],
},
dryRun: {
input: 'dry_run',
type: Boolean,
default: false,
},
});
const check: TypeCheck<typeof config, { githubToken: string, repository: { user: string, repo: string } | undefined, labels: (string | undefined)[] | undefined, dryRun: boolean }> = config;
expect(config).toEqual({
githubToken: 'TOKEN',
repository: { user: 'username', repo: 'reponame' },
labels: ['merged', 'ready'],
dryRun: false,
});
});
});