Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a bunch of tests cases and harden the function aganst malformed w…
…orkflows
Simon Engledew committed Dec 3, 2020

Unverified

No user is associated with the committer email.
1 parent 107fe84 commit 7100f22
Showing 6 changed files with 179 additions and 36 deletions.
33 changes: 20 additions & 13 deletions lib/actions-util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.js.map

Large diffs are not rendered by default.

57 changes: 51 additions & 6 deletions lib/actions-util.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/actions-util.test.js.map

Large diffs are not rendered by default.

99 changes: 92 additions & 7 deletions src/actions-util.test.ts
@@ -113,15 +113,15 @@ test("validateWorkflow() when on.push is valid", (t) => {
on: ["push", "pull_request"],
});

t.deepEqual(errors.length, 0);
t.deepEqual(errors, []);
});

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

t.deepEqual(errors.length, 0);
t.deepEqual(errors, []);
});

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) => {
on: { push: { branches: ["main"] }, pull_request: { branches: ["main"] } },
});

t.deepEqual(errors.length, 0);
t.deepEqual(errors, []);
});

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) => {
on: { push: undefined, pull_request: undefined },
});

console.log(errors);

t.deepEqual(errors.length, 0);
t.deepEqual(errors, []);
});

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

t.deepEqual(errors.length, 0);
t.deepEqual(errors, []);
});

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) => {
t.deepEqual(errors, [actionsutil.WorkflowErrors.MismatchedBranches]);
});

test("validateWorkflow() for a range of malformed workflows", (t) => {
t.deepEqual(
actionsutil.validateWorkflow({
on: {
push: 1,
pull_request: 1,
},
} as any),
[]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: 1,
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: [1],
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { 1: 1 },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { test: 1 },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { test: [1] },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { test: { steps: 1 } },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { test: { steps: [{ notrun: "git checkout HEAD^2" }] } },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(
actionsutil.validateWorkflow({
on: 1,
jobs: { test: [undefined] },
} as any),
[actionsutil.WorkflowErrors.MissingHooks]
);

t.deepEqual(actionsutil.validateWorkflow(1 as any), [
actionsutil.WorkflowErrors.MissingHooks,
]);
});

test("validateWorkflow() when on.pull_request for every branch but push specifies branches", (t) => {
const errors = actionsutil.validateWorkflow({
on: {
22 changes: 14 additions & 8 deletions src/actions-util.ts
@@ -209,14 +209,16 @@ export function validateWorkflow(doc: Workflow): CodedError[] {

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

switch (missing) {

0 comments on commit 7100f22

Please sign in to comment.