Repository navigation
Expand file tree
/
Copy pathindex.test.js
More file actions
177 lines (156 loc) · 7.9 KB
/
Copy pathindex.test.js
File metadata and controls
177 lines (156 loc) · 7.9 KB
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
const { runAction } = require('./index');
function makeCore(inputs = {}) {
return {
getInput: jest.fn((name) => (name in inputs ? inputs[name] : '')),
setOutput: jest.fn(),
setFailed: jest.fn(),
};
}
function makeExec() {
return { exec: jest.fn().mockResolvedValue(0) };
}
function makeGithub({ repo = 'my-repo', owner = 'my-org', ref = 'refs/heads/main' } = {}) {
return { context: { repo: { owner, repo }, ref } };
}
beforeEach(() => {
delete process.env.YONTRACK_PROJECT_NAME;
delete process.env.YONTRACK_BRANCH_NAME;
delete process.env.YONTRACK_BUILD_NAME;
});
describe('runAction — required inputs', () => {
test('throws when validation input is missing', async () => {
const core = makeCore({ build: 'b1' });
const execDep = makeExec();
const github = makeGithub();
await expect(runAction({ core, exec: execDep, github })).rejects.toThrow('validation input is required');
});
test('throws when build is missing and YONTRACK_BUILD_NAME is unset', async () => {
const core = makeCore({ validation: 'BUILD' });
const execDep = makeExec();
const github = makeGithub();
await expect(runAction({ core, exec: execDep, github })).rejects.toThrow('build input is required');
});
});
describe('runAction — fallbacks', () => {
test('uses YONTRACK_BUILD_NAME env when build input is empty', async () => {
process.env.YONTRACK_BUILD_NAME = '99';
const core = makeCore({ validation: 'BUILD' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--build', '99']));
});
test('uses YONTRACK_PROJECT_NAME env when project input is empty', async () => {
process.env.YONTRACK_PROJECT_NAME = 'env-project';
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub({ repo: 'context-repo' });
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--project', 'env-project']));
});
test('falls back to repo name when neither input nor env var sets project', async () => {
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub({ repo: 'context-repo' });
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--project', 'context-repo']));
});
test('uses YONTRACK_BRANCH_NAME env when branch input is empty', async () => {
process.env.YONTRACK_BRANCH_NAME = 'env-branch';
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub({ ref: 'refs/heads/somewhere' });
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--branch', 'env-branch']));
});
test('parses branch from ref when neither input nor env var sets branch', async () => {
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub({ ref: 'refs/heads/feature/foo' });
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--branch', 'feature/foo']));
});
test('throws on unsupported ref when no branch override and no env var', async () => {
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub({ ref: 'refs/something-else/foo' });
await expect(runAction({ core, exec: execDep, github })).rejects.toThrow('Ref not supported');
});
test('input project takes precedence over env var', async () => {
process.env.YONTRACK_PROJECT_NAME = 'env-project';
const core = makeCore({ validation: 'BUILD', build: '1', project: 'input-project' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--project', 'input-project']));
});
});
describe('runAction — status pass-through', () => {
test('appends --status when status is provided', async () => {
const core = makeCore({ validation: 'BUILD', build: '1', status: 'PASSED' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['--status', 'PASSED']));
});
test('does not append --status when input is empty', async () => {
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
const callArgs = execDep.exec.mock.calls[0][1];
expect(callArgs).not.toContain('--status');
});
});
describe('runAction — validation data', () => {
test('appends type as positional arg when provided', async () => {
const core = makeCore({ validation: 'BUILD', build: '1', type: 'tests' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
const callArgs = execDep.exec.mock.calls[0][1];
expect(callArgs).toContain('tests');
});
test('splits flags on spaces and appends them after type', async () => {
const core = makeCore({ validation: 'BUILD', build: '1', type: 'tests', flags: '--passed 10 --failed 1' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining(['tests', '--passed', '10', '--failed', '1']));
});
test('throws when flags are provided without type', async () => {
const core = makeCore({ validation: 'BUILD', build: '1', flags: '--passed 10' });
const execDep = makeExec();
const github = makeGithub();
await expect(runAction({ core, exec: execDep, github })).rejects.toThrow('Flags are provided without a data type');
});
});
describe('runAction — invocation contract', () => {
test('uses the executable input', async () => {
const core = makeCore({ validation: 'BUILD', build: '1', executable: 'custom-cli' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('custom-cli', expect.any(Array));
});
test('defaults executable to ontrack-cli when input is empty', async () => {
const core = makeCore({ validation: 'BUILD', build: '1' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.any(Array));
});
test('always includes validate, --project, --branch, --build, --validation in args', async () => {
const core = makeCore({ validation: 'STAMP1', build: '7', project: 'p', branch: 'b' });
const execDep = makeExec();
const github = makeGithub();
await runAction({ core, exec: execDep, github });
expect(execDep.exec).toHaveBeenCalledWith('ontrack-cli', expect.arrayContaining([
'validate',
'--project', 'p',
'--branch', 'b',
'--build', '7',
'--validation', 'STAMP1',
]));
});
});