Skip to content

Commit

Permalink
Fix #74 Warn the user if ENV is missing an equals sign
Browse files Browse the repository at this point in the history
Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Jul 16, 2017
1 parent 3e29b03 commit 4c65568
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/dockerValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum ValidationCode {
INVALID_AS,
INVALID_PORT,
INVALID_SIGNAL,
SYNTAX_MISSING_EQUALS,
UNKNOWN_INSTRUCTION,
DEPRECATED_MAINTAINER
}
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"];
}
Expand Down Expand Up @@ -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);
}
Expand Down
21 changes: 21 additions & 0 deletions test/dockerValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
});
});

Expand Down

0 comments on commit 4c65568

Please sign in to comment.