Skip to content

Commit 7100f22

Browse files
author
Simon Engledew
committed
Add a bunch of tests cases and harden the function aganst malformed workflows
1 parent 107fe84 commit 7100f22

6 files changed

Lines changed: 179 additions & 36 deletions

File tree

lib/actions-util.js

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

lib/actions-util.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/actions-util.test.js

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

lib/actions-util.test.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions-util.test.ts

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ test("validateWorkflow() when on.push is valid", (t) => {
113113
on: ["push", "pull_request"],
114114
});
115115

116-
t.deepEqual(errors.length, 0);
116+
t.deepEqual(errors, []);
117117
});
118118

119119
test("validateWorkflow() when on.push is a valid superset", (t) => {
120120
const errors = actionsutil.validateWorkflow({
121121
on: ["push", "pull_request", "schedule"],
122122
});
123123

124-
t.deepEqual(errors.length, 0);
124+
t.deepEqual(errors, []);
125125
});
126126

127127
test("validateWorkflow() when on.push should not have a path", (t) => {
@@ -140,7 +140,7 @@ test("validateWorkflow() when on.push is a correct object", (t) => {
140140
on: { push: { branches: ["main"] }, pull_request: { branches: ["main"] } },
141141
});
142142

143-
t.deepEqual(errors.length, 0);
143+
t.deepEqual(errors, []);
144144
});
145145

146146
test("validateWorkflow() when on.pull_requests is a string", (t) => {
@@ -164,9 +164,7 @@ test("validateWorkflow() when on.push is correct with empty objects", (t) => {
164164
on: { push: undefined, pull_request: undefined },
165165
});
166166

167-
console.log(errors);
168-
169-
t.deepEqual(errors.length, 0);
167+
t.deepEqual(errors, []);
170168
});
171169

172170
test("validateWorkflow() when on.push is mismatched", (t) => {
@@ -188,7 +186,7 @@ test("validateWorkflow() when on.push is not mismatched", (t) => {
188186
},
189187
});
190188

191-
t.deepEqual(errors.length, 0);
189+
t.deepEqual(errors, []);
192190
});
193191

194192
test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
@@ -202,6 +200,93 @@ test("validateWorkflow() when on.push is mismatched for pull_request", (t) => {
202200
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
203201
});
204202

203+
test("validateWorkflow() for a range of malformed workflows", (t) => {
204+
t.deepEqual(
205+
actionsutil.validateWorkflow({
206+
on: {
207+
push: 1,
208+
pull_request: 1,
209+
},
210+
} as any),
211+
[]
212+
);
213+
214+
t.deepEqual(
215+
actionsutil.validateWorkflow({
216+
on: 1,
217+
} as any),
218+
[actionsutil.WorkflowErrors.MissingHooks]
219+
);
220+
221+
t.deepEqual(
222+
actionsutil.validateWorkflow({
223+
on: 1,
224+
jobs: 1,
225+
} as any),
226+
[actionsutil.WorkflowErrors.MissingHooks]
227+
);
228+
229+
t.deepEqual(
230+
actionsutil.validateWorkflow({
231+
on: 1,
232+
jobs: [1],
233+
} as any),
234+
[actionsutil.WorkflowErrors.MissingHooks]
235+
);
236+
237+
t.deepEqual(
238+
actionsutil.validateWorkflow({
239+
on: 1,
240+
jobs: { 1: 1 },
241+
} as any),
242+
[actionsutil.WorkflowErrors.MissingHooks]
243+
);
244+
245+
t.deepEqual(
246+
actionsutil.validateWorkflow({
247+
on: 1,
248+
jobs: { test: 1 },
249+
} as any),
250+
[actionsutil.WorkflowErrors.MissingHooks]
251+
);
252+
253+
t.deepEqual(
254+
actionsutil.validateWorkflow({
255+
on: 1,
256+
jobs: { test: [1] },
257+
} as any),
258+
[actionsutil.WorkflowErrors.MissingHooks]
259+
);
260+
261+
t.deepEqual(
262+
actionsutil.validateWorkflow({
263+
on: 1,
264+
jobs: { test: { steps: 1 } },
265+
} as any),
266+
[actionsutil.WorkflowErrors.MissingHooks]
267+
);
268+
269+
t.deepEqual(
270+
actionsutil.validateWorkflow({
271+
on: 1,
272+
jobs: { test: { steps: [{ notrun: "git checkout HEAD^2" }] } },
273+
} as any),
274+
[actionsutil.WorkflowErrors.MissingHooks]
275+
);
276+
277+
t.deepEqual(
278+
actionsutil.validateWorkflow({
279+
on: 1,
280+
jobs: { test: [undefined] },
281+
} as any),
282+
[actionsutil.WorkflowErrors.MissingHooks]
283+
);
284+
285+
t.deepEqual(actionsutil.validateWorkflow(1 as any), [
286+
actionsutil.WorkflowErrors.MissingHooks,
287+
]);
288+
});
289+
205290
test("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
206291
const errors = actionsutil.validateWorkflow({
207292
on: {

src/actions-util.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,16 @@ export function validateWorkflow(doc: Workflow): CodedError[] {
209209

210210
// .jobs[key].steps[].run
211211
for (const job of Object.values(doc?.jobs || {})) {
212-
for (const step of job?.steps || []) {
213-
// this was advice that we used to give in the README
214-
// we actually want to run the analysis on the merge commit
215-
// to produce results that are more inline with expectations
216-
// (i.e: this is what will happen if you merge this PR)
217-
// and avoid some race conditions
218-
if (step?.run === "git checkout HEAD^2") {
219-
errors.push(WorkflowErrors.CheckoutWrongHead);
212+
if (Array.isArray(job?.steps)) {
213+
for (const step of job?.steps) {
214+
// this was advice that we used to give in the README
215+
// we actually want to run the analysis on the merge commit
216+
// to produce results that are more inline with expectations
217+
// (i.e: this is what will happen if you merge this PR)
218+
// and avoid some race conditions
219+
if (step?.run === "git checkout HEAD^2") {
220+
errors.push(WorkflowErrors.CheckoutWrongHead);
221+
}
220222
}
221223
}
222224
}
@@ -284,6 +286,10 @@ export function validateWorkflow(doc: Workflow): CodedError[] {
284286
errors.push(WorkflowErrors.MismatchedBranches);
285287
}
286288
}
289+
} else {
290+
// on is not a known type
291+
// this workflow is likely malformed
292+
missing = MissingTriggers.Push | MissingTriggers.PullRequest;
287293
}
288294

289295
switch (missing) {

0 commit comments

Comments
 (0)