From c6972a16bb2d3fc08befb12f3b46b3e3b67788bd Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Sat, 9 Sep 2023 17:03:16 -0400 Subject: [PATCH] Fix #117 Suggest --start-interval as a completion item --start-interval will now be included in the list of completion items for HEALTHCHECK instructions. Signed-off-by: Remy Suen --- CHANGELOG.md | 1 + src/dockerAssist.ts | 10 ++++++++ src/dockerMarkdown.ts | 5 ++++ src/dockerPlainText.ts | 3 +++ test/dockerAssist.test.ts | 48 ++++++++++++++++++++++++++++++++------- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ba4968..6cb9c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. - support parsing the new `--keep-git-dir` flag for ADD instructions ([rcjsuen/dockerfile-utils#117](https://github.com/rcjsuen/dockerfile-utils/issues/117)) - support parsing the new `--start-interval` flag for HEALTHCHECK instructions ([rcjsuen/dockerfile-utils#115](https://github.com/rcjsuen/dockerfile-utils/issues/115)) - allow some diagnostics to be ignored if a `# dockerfile-utils: ignore` comment precedes the originating line of the error ([rcjsuen/dockerfile-utils#106](https://github.com/rcjsuen/dockerfile-utils/issues/106)) +- suggest the new `--start-interval` flag for HEALTHCHECK instructions when calculating completion items ([#117](https://github.com/rcjsuen/dockerfile-language-service/issues/117)) ## [0.10.2] - 2023-06-01 ### Fixed diff --git a/src/dockerAssist.ts b/src/dockerAssist.ts index d29598c..1d0c7d2 100644 --- a/src/dockerAssist.ts +++ b/src/dockerAssist.ts @@ -498,6 +498,9 @@ export class DockerAssist { if ("--retries".indexOf(prefix) === 0) { items.push(this.createHEALTHCHECK_FlagRetries(prefix.length, offset)); } + if ("--start-interval".indexOf(prefix) === 0) { + items.push(this.createHEALTHCHECK_FlagStartInterval(prefix.length, offset)); + } if ("--start-period".indexOf(prefix) === 0) { items.push(this.createHEALTHCHECK_FlagStartPeriod(prefix.length, offset)); } @@ -775,6 +778,13 @@ export class DockerAssist { return this.createFlagCompletionItem("--retries=", prefixLength, offset, "--retries=", "HEALTHCHECK_FlagRetries"); } + private createHEALTHCHECK_FlagStartInterval(prefixLength: number, offset: number): CompletionItem { + if (this.snippetSupport) { + return this.createFlagCompletionItem("--start-interval=5s", prefixLength, offset, "--start-interval=${1:5s}", "HEALTHCHECK_FlagStartInterval"); + } + return this.createFlagCompletionItem("--start-interval=", prefixLength, offset, "--start-interval=", "HEALTHCHECK_FlagStartInterval"); + } + private createHEALTHCHECK_FlagStartPeriod(prefixLength: number, offset: number): CompletionItem { if (this.snippetSupport) { return this.createFlagCompletionItem("--start-period=5s", prefixLength, offset, "--start-period=${1:5s}", "HEALTHCHECK_FlagStartPeriod"); diff --git a/src/dockerMarkdown.ts b/src/dockerMarkdown.ts index 18e85a0..169f132 100644 --- a/src/dockerMarkdown.ts +++ b/src/dockerMarkdown.ts @@ -39,6 +39,7 @@ export class MarkdownDocumentation { "hoverFromFlagPlatform": "The platform of the image if referencing a multi-platform image.\n\nSince Docker CE 18.04.", "hoverHealthcheckFlagInterval": "The seconds to wait for the health check to run after the container has started, and then again the number of seconds to wait before running again after the previous check has completed.", "hoverHealthcheckFlagRetries": "The number of consecutive failures of this health check before the container is considered to be `unhealthy`.", + "hoverHealthcheckFlagStartInterval": "The number of seconds to wait between health checks during the start period.", "hoverHealthcheckFlagStartPeriod": "The number of seconds to wait for the container to startup. Failures during this grace period will not count towards the maximum number of retries. However, should a health check succeed during this period then any subsequent failures will count towards the maximum number of retries.\n\nSince Docker 17.05.0-ce.", "hoverHealthcheckFlagTimeout": "The number of seconds to wait for the check to complete before considering it to have failed.", @@ -182,6 +183,10 @@ export class MarkdownDocumentation { contents: this.dockerMessages["hoverHealthcheckFlagRetries"] }, + HEALTHCHECK_FlagStartInterval: { + contents: this.dockerMessages["hoverHealthcheckFlagStartInterval"] + }, + HEALTHCHECK_FlagStartPeriod: { contents: this.dockerMessages["hoverHealthcheckFlagStartPeriod"] }, diff --git a/src/dockerPlainText.ts b/src/dockerPlainText.ts index fd8ad9f..5d7571a 100644 --- a/src/dockerPlainText.ts +++ b/src/dockerPlainText.ts @@ -32,6 +32,7 @@ export class PlainTextDocumentation { "hoverFromFlagPlatform": "The platform of the image if referencing a multi-platform image.\n\nSince Docker CE 18.04.", "hoverHealthcheckFlagInterval": "The seconds to wait for the health check to run after the container has started, and then again the number of seconds to wait before running again after the previous check has completed.", "hoverHealthcheckFlagRetries": "The number of consecutive failures of this health check before the container is considered to be `unhealthy`.", + "hoverHealthcheckFlagStartInterval": "The number of seconds to wait between health checks during the start period.", "hoverHealthcheckFlagStartPeriod": "The number of seconds to wait for the container to startup. Failures during this grace period will not count towards the maximum number of retries. However, should a health check succeed during this period then any subsequent failures will count towards the maximum number of retries.\n\nSince Docker 17.05.0-ce.", "hoverHealthcheckFlagTimeout": "The number of seconds to wait for the check to complete before considering it to have failed.", @@ -185,6 +186,8 @@ export class PlainTextDocumentation { HEALTHCHECK_FlagRetries: this.dockerMessages["hoverHealthcheckFlagRetries"], + HEALTHCHECK_FlagStartInterval: this.dockerMessages["hoverHealthcheckFlagStartInterval"], + HEALTHCHECK_FlagStartPeriod: this.dockerMessages["hoverHealthcheckFlagStartPeriod"], HEALTHCHECK_FlagTimeout: this.dockerMessages["hoverHealthcheckFlagTimeout"], diff --git a/test/dockerAssist.test.ts b/test/dockerAssist.test.ts index cd05143..fe67ee5 100644 --- a/test/dockerAssist.test.ts +++ b/test/dockerAssist.test.ts @@ -537,6 +537,30 @@ function assertHEALTHCHECK_FlagInterval(item: CompletionItem, startLine: number, assertResolvedDocumentation(item); } +function assertHEALTHCHECK_FlagStartInterval(item: CompletionItem, startLine: number, startCharacter: number, endLine: number, endCharacter: number, snippetSupport?: boolean) { + if (snippetSupport === undefined || snippetSupport) { + assert.strictEqual(item.label, "--start-interval=5s"); + } else { + assert.strictEqual(item.label, "--start-interval="); + } + assert.strictEqual(item.kind, CompletionItemKind.Field); + if (snippetSupport === undefined || snippetSupport) { + assert.strictEqual(item.insertTextFormat, InsertTextFormat.Snippet); + assert.strictEqual(item.textEdit.newText, "--start-interval=${1:5s}"); + } else { + assert.strictEqual(item.insertTextFormat, InsertTextFormat.PlainText); + assert.strictEqual(item.textEdit.newText, "--start-interval="); + } + assert.strictEqual(item.data, "HEALTHCHECK_FlagStartInterval"); + assert.strictEqual(item.deprecated, undefined); + assert.strictEqual(item.documentation, undefined); + assert.strictEqual((item.textEdit as TextEdit).range.start.line, startLine); + assert.strictEqual((item.textEdit as TextEdit).range.start.character, startCharacter); + assert.strictEqual((item.textEdit as TextEdit).range.end.line, endLine); + assert.strictEqual((item.textEdit as TextEdit).range.end.character, endCharacter); + assertResolvedDocumentation(item); +} + function assertHEALTHCHECK_FlagTimeout(item: CompletionItem, startLine: number, startCharacter: number, endLine: number, endCharacter: number, snippetSupport?: boolean) { if (snippetSupport === undefined || snippetSupport) { assert.strictEqual(item.label, "--timeout=30s"); @@ -1056,15 +1080,16 @@ function assertFromFlags(items: CompletionItem[], startLine: number, startCharac } function assertHealthcheckItems(items: CompletionItem[], startLine: number, startCharacter: number, endLine: number, endCharacter: number, snippetSupport?: boolean) { - assert.strictEqual(items.length, 6); + assert.strictEqual(items.length, 7); // CMD and NONE first assertHEALTHCHECK_CMD_Subcommand(items[0], startLine, startCharacter, endLine, endCharacter, snippetSupport); assertHEALTHCHECK_NONE_Subcommand(items[1], startLine, startCharacter, endLine, endCharacter); // flags in alphabetical order next assertHEALTHCHECK_FlagInterval(items[2], startLine, startCharacter, endLine, endCharacter, snippetSupport); assertHEALTHCHECK_FlagRetries(items[3], startLine, startCharacter, endLine, endCharacter, snippetSupport); - assertHEALTHCHECK_FlagStartPeriod(items[4], startLine, startCharacter, endLine, endCharacter, snippetSupport); - assertHEALTHCHECK_FlagTimeout(items[5], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagStartInterval(items[4], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagStartPeriod(items[5], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagTimeout(items[6], startLine, startCharacter, endLine, endCharacter, snippetSupport); assert.strictEqual(items[0].sortText, "0"); assert.strictEqual(items[1].sortText, "1"); @@ -1072,20 +1097,23 @@ function assertHealthcheckItems(items: CompletionItem[], startLine: number, star assert.strictEqual(items[3].sortText, "3"); assert.strictEqual(items[4].sortText, "4"); assert.strictEqual(items[5].sortText, "5"); + assert.strictEqual(items[6].sortText, "6"); } function assertHealthcheckFlags(items: CompletionItem[], startLine: number, startCharacter: number, endLine: number, endCharacter: number, snippetSupport?: boolean) { - assert.strictEqual(items.length, 4); + assert.strictEqual(items.length, 5); // alphabetical order assertHEALTHCHECK_FlagInterval(items[0], startLine, startCharacter, endLine, endCharacter, snippetSupport); assertHEALTHCHECK_FlagRetries(items[1], startLine, startCharacter, endLine, endCharacter, snippetSupport); - assertHEALTHCHECK_FlagStartPeriod(items[2], startLine, startCharacter, endLine, endCharacter, snippetSupport); - assertHEALTHCHECK_FlagTimeout(items[3], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagStartInterval(items[2], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagStartPeriod(items[3], startLine, startCharacter, endLine, endCharacter, snippetSupport); + assertHEALTHCHECK_FlagTimeout(items[4], startLine, startCharacter, endLine, endCharacter, snippetSupport); assert.strictEqual(items[0].sortText, "0"); assert.strictEqual(items[1].sortText, "1"); assert.strictEqual(items[2].sortText, "2"); assert.strictEqual(items[3].sortText, "3"); + assert.strictEqual(items[4].sortText, "4"); } function assertONBUILDProposals(proposals: CompletionItem[], offset: number, prefix: number, prefixLength: number) { @@ -2622,9 +2650,13 @@ describe('Docker Content Assist Tests', function () { assert.strictEqual(items.length, 1); assertHEALTHCHECK_FlagRetries(items[0], 1, triggerOffset + 12, 1, triggerOffset + 17, snippetSupport); - items = computePosition("FROM busybox\n" + onbuild + "HEALTHCHECK --start", 1, triggerOffset + 19, snippetSupport); + items = computePosition("FROM busybox\n" + onbuild + "HEALTHCHECK --start-i", 1, triggerOffset + 21, snippetSupport); + assert.strictEqual(items.length, 1); + assertHEALTHCHECK_FlagStartInterval(items[0], 1, triggerOffset + 12, 1, triggerOffset + 21, snippetSupport); + + items = computePosition("FROM busybox\n" + onbuild + "HEALTHCHECK --start-p", 1, triggerOffset + 21, snippetSupport); assert.strictEqual(items.length, 1); - assertHEALTHCHECK_FlagStartPeriod(items[0], 1, triggerOffset + 12, 1, triggerOffset + 19, snippetSupport); + assertHEALTHCHECK_FlagStartPeriod(items[0], 1, triggerOffset + 12, 1, triggerOffset + 21, snippetSupport); items = computePosition("FROM busybox\n" + onbuild + "HEALTHCHECK --time", 1, triggerOffset + 18, snippetSupport); assert.strictEqual(items.length, 1);