From 4c6556865d24147866126dcecaab2f4b3a905ecd Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Sun, 16 Jul 2017 19:29:11 +0900 Subject: [PATCH] Fix #74 Warn the user if ENV is missing an equals sign Signed-off-by: Remy Suen --- src/dockerValidator.ts | 18 ++++++++++++++++++ test/dockerValidator.test.ts | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/dockerValidator.ts b/src/dockerValidator.ts index ca3e789..13f4cf9 100644 --- a/src/dockerValidator.ts +++ b/src/dockerValidator.ts @@ -25,6 +25,7 @@ export enum ValidationCode { INVALID_AS, INVALID_PORT, INVALID_SIGNAL, + SYNTAX_MISSING_EQUALS, UNKNOWN_INSTRUCTION, DEPRECATED_MAINTAINER } @@ -192,6 +193,13 @@ export class Validator { if (properties.length === 1 && properties[0].getValue() === null) { let range = properties[0].getNameRange(); problems.push(Validator.createENVRequiresTwoArguments(range.start, range.end)); + } else if (properties.length !== 0) { + for (let property of properties) { + if (property.getValue() === null) { + let range = property.getNameRange(); + problems.push(Validator.createSyntaxMissingEquals(range.start, range.end, property.getName())); + } + } } break; case "FROM": @@ -251,6 +259,8 @@ export class Validator { "invalidPort": "Invalid containerPort: ${0}", "invalidStopSignal": "Invalid signal: ${0}", + "syntaxMissingEquals": "Syntax error - can't find = in \"${0}\". Must be of the form: name=value", + "instructionExtraArgument": "Instruction has an extra argument", "instructionMissingArgument": "Instruction has no arguments", "instructionRequiresOneArgument": "${0} requires exactly one argument", @@ -313,6 +323,10 @@ export class Validator { return Validator.formatMessage(Validator.dockerProblems["instructionUnknown"], instruction); } + public static getDiagnosticMessage_SyntaxMissingEquals(argument: string) { + return Validator.formatMessage(Validator.dockerProblems["syntaxMissingEquals"], argument); + } + public static getDiagnosticMessage_InstructionCasing() { return Validator.dockerProblems["instructionCasing"]; } @@ -361,6 +375,10 @@ export class Validator { return Validator.createError(start, end, Validator.getDiagnosticMessage_NoSourceImage(), ValidationCode.NO_SOURCE_IMAGE); } + static createSyntaxMissingEquals(start: Position, end: Position, argument: string): Diagnostic { + return Validator.createError(start, end, Validator.getDiagnosticMessage_SyntaxMissingEquals(argument), ValidationCode.SYNTAX_MISSING_EQUALS); + } + static createUnknownInstruction(start: Position, end: Position, instruction: string): Diagnostic { return Validator.createError(start, end, Validator.getDiagnosticMessage_InstructionUnknown(instruction), ValidationCode.UNKNOWN_INSTRUCTION); } diff --git a/test/dockerValidator.test.ts b/test/dockerValidator.test.ts index 6b26155..be4a4a3 100644 --- a/test/dockerValidator.test.ts +++ b/test/dockerValidator.test.ts @@ -195,6 +195,17 @@ function assertDeprecatedMaintainer(diagnostic: Diagnostic, severity: Diagnostic assert.equal(diagnostic.range.end.character, endCharacter); } +function assertSyntaxMissingEquals(diagnostic: Diagnostic, argument: string, startLine: number, startCharacter: number, endLine: number, endCharacter: number) { + assert.equal(diagnostic.code, ValidationCode.SYNTAX_MISSING_EQUALS); + assert.equal(diagnostic.severity, DiagnosticSeverity.Error); + assert.equal(diagnostic.source, source); + assert.equal(diagnostic.message, Validator.getDiagnosticMessage_SyntaxMissingEquals(argument)); + assert.equal(diagnostic.range.start.line, startLine); + assert.equal(diagnostic.range.start.character, startCharacter); + assert.equal(diagnostic.range.end.line, endLine); + assert.equal(diagnostic.range.end.character, endCharacter); +} + function testValidArgument(instruction: string, argument: string) { let gaps = [ " ", "\t", " \\\n", " \\\r", " \\\r\n" ]; for (let gap of gaps) { @@ -1019,6 +1030,16 @@ describe("Docker Validator Tests", function() { assert.equal(diagnostics.length, 1); assertENVRequiresTwoArguments(diagnostics[0], 1, 4, 1, 5); }); + + it("syntax missing equals", function() { + let diagnostics = validate("FROM node\nENV a=b c"); + assert.equal(diagnostics.length, 1); + assertSyntaxMissingEquals(diagnostics[0], "c", 1, 8, 1, 9); + + diagnostics = validate("FROM node\nENV a=b c d=e"); + assert.equal(diagnostics.length, 1); + assertSyntaxMissingEquals(diagnostics[0], "c", 1, 8, 1, 9); + }); }); });