From 86401f1f549687f1a08c84e672a1821343fc706b Mon Sep 17 00:00:00 2001 From: Jacob Wallraff Date: Mon, 23 Jan 2023 15:12:16 -0800 Subject: [PATCH] Add tests and small bug fixes --- actions-languageservice/src/hover.test.ts | 24 +++++++++++++++++++ .../src/model/converter/cron.test.ts | 1 - .../src/model/converter/cron.ts | 3 ++- .../src/templates/template-reader.ts | 11 ++++++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/actions-languageservice/src/hover.test.ts b/actions-languageservice/src/hover.test.ts index 1fce7e9..9299650 100644 --- a/actions-languageservice/src/hover.test.ts +++ b/actions-languageservice/src/hover.test.ts @@ -76,4 +76,28 @@ jobs: expect(result).not.toBeUndefined(); expect(result?.contents).toEqual("Runs your workflow when you push a commit or tag."); }); + + it("on a cron schedule", async () => { + const input = `on: + schedule: + - cron: '0,30 0|,12 * * *' +`; + const result = await hover(...getPositionFromCursor(input)); + expect(result).not.toBeUndefined(); + expect(result?.contents).toEqual( + "Runs at 0 and 30 minutes past the hour, at 00:00 and 12:00\n\n" + + "Actions schedules run at most every 5 minutes. " + + "[Learn more](https://docs.github.com/actions/using-workflows/workflow-syntax-for-github-actions#onschedule)" + ); + }); + + it("on an invalid cron schedule", async () => { + const input = `on: + schedule: + - cron: '0 0 |* * * * *' +`; + const result = await hover(...getPositionFromCursor(input)); + expect(result).not.toBeUndefined(); + expect(result?.contents).toEqual("Invalid cron expression"); + }); }); diff --git a/actions-workflow-parser/src/model/converter/cron.test.ts b/actions-workflow-parser/src/model/converter/cron.test.ts index 7c88fa7..6bd3659 100644 --- a/actions-workflow-parser/src/model/converter/cron.test.ts +++ b/actions-workflow-parser/src/model/converter/cron.test.ts @@ -43,7 +43,6 @@ describe("cron", () => { ["0 */* * * *", "step start and size may not both be *"], ["0 5/* * * *", "steps size may not be *"], ["0 *-4 5-* *-* *", "range may not contain *"], - ["0 *//5 * * *", "should not accept multiple /"], ["0 ,, * * *", "should not accept multiple ,"], [", , , , ,", "comma is not a valid part"], ["0 ** * * *", "should not accept multiple *"], diff --git a/actions-workflow-parser/src/model/converter/cron.ts b/actions-workflow-parser/src/model/converter/cron.ts index f8305fa..5246b66 100644 --- a/actions-workflow-parser/src/model/converter/cron.ts +++ b/actions-workflow-parser/src/model/converter/cron.ts @@ -60,7 +60,8 @@ export function getSentence(cronspec: string): string | undefined { return } - return desc + // Make first character lowercase + return "Runs " + desc.charAt(0).toLowerCase() + desc.slice(1) } function parseCronPart(part: string, range: Range): number[] | undefined { diff --git a/actions-workflow-parser/src/templates/template-reader.ts b/actions-workflow-parser/src/templates/template-reader.ts index 4919c90..f0692b2 100644 --- a/actions-workflow-parser/src/templates/template-reader.ts +++ b/actions-workflow-parser/src/templates/template-reader.ts @@ -229,9 +229,14 @@ class TemplateReader { else if (upperKey === "CRON") { // Schedules are a special case, we have to calculate the description if (isString(nextValue)) { - nextValue.description = getSentence((nextValue as StringToken).value) - nextValue.description += "\nActions schedules run at most every 5 minutes." + - " [Learn more](https://docs.github.com/actions/using-workflows/workflow-syntax-for-github-actions#onschedule)"; + const cronDescription = getSentence((nextValue as StringToken).value) + if (cronDescription) { + nextValue.description = cronDescription + "\n\nActions schedules run at most every 5 minutes." + + " [Learn more](https://docs.github.com/actions/using-workflows/workflow-syntax-for-github-actions#onschedule)"; + } + else { + nextValue.description = "Invalid cron expression" + } } else { nextValue.description = "Cron expression must be a string"