Skip to content

Commit

Permalink
Fix #104 Add hovers for ONBUILD HEALTHCHECK flags
Browse files Browse the repository at this point in the history
If an ONBUILD's trigger instruction is a HEALTHCHECK, the HEALTHCHECK
trigger's flags should have hovers enabled.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Jul 25, 2017
1 parent 1cce8fc commit 32ab40f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 62 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ All notable changes to this project will be documented in this file.
- default Docker ARG variables
- add '$' as a trigger character
- textDocument/hover
- HEALTHCHECK CMD flags ([#82](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/82))
- HEALTHCHECK CMD flags ([#82](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/82), [#104](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/104))
- textDocument/publishDiagnostics
- check the spelling of instruction flags ([#75](https://github.com/rcjsuen/dockerfile-language-server-nodejs/issues/75))
- COPY's from
Expand Down
61 changes: 46 additions & 15 deletions src/dockerHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,54 @@ export class DockerHover {
}
}

if (instruction.getKeyword() === "HEALTHCHECK") {
let flags = (instruction as ModifiableInstruction).getFlags();
for (let flag of flags) {
if (Util.isInsideRange(textDocumentPosition.position, flag.getNameRange())) {
switch (flag.getName()) {
case "interval":
return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval");
case "retries":
return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries");
case "start-period":
return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod");
case "timeout":
return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout");
switch (instruction.getKeyword()) {
case "HEALTHCHECK":
let flags = (instruction as ModifiableInstruction).getFlags();
for (let flag of flags) {
if (Util.isInsideRange(textDocumentPosition.position, flag.getNameRange())) {
switch (flag.getName()) {
case "interval":
return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval");
case "retries":
return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries");
case "start-period":
return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod");
case "timeout":
return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout");
}
return null;
}
return null;
}
}
break;
case "ONBUILD":
if ((instruction as Onbuild).getTriggerInstruction() === "HEALTHCHECK") {
for (let arg of instruction.getArguments()) {
let value = arg.getValue().toUpperCase();
if ((value === "CMD" || value === "NONE") && arg.isBefore(textDocumentPosition.position)) {
return null;
}
}

for (let arg of instruction.getArguments()) {
if (Util.isInsideRange(textDocumentPosition.position, arg.getRange())) {
let value = arg.getValue();
if (value.indexOf("--interval") === 0) {
return this.markdown.getMarkdown("HEALTHCHECK_FlagInterval");
}
if (value.indexOf("--retries") === 0) {
return this.markdown.getMarkdown("HEALTHCHECK_FlagRetries");
}
if (value.indexOf("--start-period") === 0) {
return this.markdown.getMarkdown("HEALTHCHECK_FlagStartPeriod");
}
if (value.indexOf("--timeout") === 0) {
return this.markdown.getMarkdown("HEALTHCHECK_FlagTimeout");
}
}
}
}
break;

}
}

Expand Down
103 changes: 57 additions & 46 deletions test/dockerHover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,59 +731,70 @@ describe("Dockerfile hover", function() {
});
});

describe("HEALTHCHECK", function() {
it("--interval", function() {
let document = createDocument("HEALTHCHECK --interval");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});
function createHealthcheckTest(trigger: boolean) {
let onbuild = trigger ? "ONBUILD " : "";
let triggerOffset = onbuild.length;

describe("HEALTHCHECK", function() {
it("--interval", function() {
let document = createDocument(onbuild + "HEALTHCHECK --interval");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});

it("--interval=\\$x", function() {
let document = createDocument("HEALTHCHECK --interval=\\$x");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});
it("--interval=\\$x", function() {
let document = createDocument(onbuild + "HEALTHCHECK --interval=\\$x");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});

it("--interval=\\a", function() {
let document = createDocument("HEALTHCHECK --interval=\\a");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});
it("--interval=\\a", function() {
let document = createDocument(onbuild + "HEALTHCHECK --interval=\\a");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagInterval"));
});

it("--retries", function() {
let document = createDocument("HEALTHCHECK --retries");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagRetries"));
});
it("--retries", function() {
let document = createDocument(onbuild + "HEALTHCHECK --retries");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagRetries"));
});

it("--start-period", function() {
let document = createDocument("HEALTHCHECK --start-period");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagStartPeriod"));
});
it("--start-period", function() {
let document = createDocument(onbuild + "HEALTHCHECK --start-period");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagStartPeriod"));
});

it("--timeout", function() {
let document = createDocument("HEALTHCHECK --timeout");
let hover = onHover(document, 0, 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagTimeout"));
it("--timeout", function() {
let document = createDocument(onbuild + "HEALTHCHECK --timeout");
let hover = onHover(document, 0, triggerOffset + 17);
assert.equal(hover, markdownDocumentation.getMarkdown("HEALTHCHECK_FlagTimeout"));
});

function createFlagsAfterTest(subcommand: string) {
it("flags after " + subcommand, function() {
let document = createDocument(onbuild + "HEALTHCHECK " + subcommand + " \\\n--interval=30s\\\n--retries=3\\\n--start-period=30s\\\n--timeout=30s");
let hover = onHover(document, 1, 4);
assert.equal(hover, null);
hover = onHover(document, 2, 4);
assert.equal(hover, null);
hover = onHover(document, 3, 4);
assert.equal(hover, null);
hover = onHover(document, 4, 4);
assert.equal(hover, null);
});
}

createFlagsAfterTest("CMD");
createFlagsAfterTest("NONE");
});
}

createHealthcheckTest(false);

function createFlagsAfterTest(subcommand: string) {
it("flags after " + subcommand, function() {
let document = createDocument("HEALTHCHECK " + subcommand + " \\\n--interval=30s\\\n--retries=3\\\n--start-period=30s\\\n--timeout=30s");
let hover = onHover(document, 1, 4);
assert.equal(hover, null);
hover = onHover(document, 2, 4);
assert.equal(hover, null);
hover = onHover(document, 3, 4);
assert.equal(hover, null);
hover = onHover(document, 4, 4);
assert.equal(hover, null);
});
}

createFlagsAfterTest("CMD");
createFlagsAfterTest("NONE");
describe("ONBUILD", function() {
createHealthcheckTest(true);
});
});

Expand Down

0 comments on commit 32ab40f

Please sign in to comment.