Skip to content

Commit d38386c

Browse files
committed
support case-insensitive command names, add comment parsing, more tests
1 parent e8c9b75 commit d38386c

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

parser.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ function splitCommand(line) {
271271
// Make sure we get the same results irrespective of leading/trailing spaces
272272
var match = line.match(TOKEN_WHITESPACE);
273273
if (!match) {
274-
return { name: line, rest: '' };
274+
return { name: line.toUpperCase(), rest: '' };
275275
}
276-
var name = line.substr(0, match.index);
276+
var name = line.substr(0, match.index).toUpperCase();
277277
var rest = line.substr(match.index + match[0].length);
278278

279279
return { name: name, rest: rest };
@@ -348,6 +348,7 @@ function parse(contents, options) {
348348
var lines = contents.split(/[\r?\n]/);
349349
var parseResult;
350350
var remainder = '';
351+
var includeComments = options && options['includeComments'];
351352

352353
for (i = 0; i < lines.length; i++) {
353354
lineno = i + 1;
@@ -359,9 +360,7 @@ function parse(contents, options) {
359360

360361
parseResult = parseLine(line, lineno);
361362
if (parseResult.command) {
362-
if (parseResult.command.name !== 'COMMENT' ||
363-
options.includeComments)
364-
{
363+
if (parseResult.command.name !== 'COMMENT' || includeComments) {
365364
commands.push(parseResult.command);
366365
}
367366
}

test/test_parser.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,53 @@ tape('should parse a Dockerfile', function (t) {
99
var commands = dockerfileParser.parse(dockerFile);
1010

1111
var numCommands = 15;
12-
t.assert(commands.length === numCommands, 'Check number of commands');
12+
t.equal(commands.length, numCommands, 'Check number of commands');
1313

1414
var from = commands[0];
15-
t.assert(from.name === 'FROM', 'First command should be FROM');
15+
t.equal(from.name, 'FROM', 'First command should be FROM');
1616

1717
var add = commands[1];
18-
t.assert(add.name === 'ADD', 'Check ADD command');
19-
t.assert(add.args[0] === '.', 'Check ADD first arg');
20-
t.assert(add.args[1] === '/srv/app', 'Check ADD second arg');
18+
t.equal(add.name, 'ADD', 'Check ADD command');
19+
t.equal(add.args[0], '.', 'Check ADD first arg');
20+
t.equal(add.args[1], '/srv/app', 'Check ADD second arg');
2121

2222
var env = commands[7];
23-
t.assert(env.name === 'ENV', '8th command is ENV');
24-
t.assert(Object.keys(env.args).length === 2, 'ENV command has 2 keys');
25-
t.assert(env.args['VAR2'] === '20', 'ENV VAR2 check');
26-
t.assert(env.args['VAR3'] === '30', 'ENV VAR3 check');
23+
t.equal(env.name, 'ENV', '8th command is ENV');
24+
t.equal(Object.keys(env.args).length, 2, 'ENV command has 2 keys');
25+
t.equal(env.args['VAR2'], '20', 'ENV VAR2 check');
26+
t.equal(env.args['VAR3'], '30', 'ENV VAR3 check');
2727

28-
t.assert(commands[numCommands-1].name === 'ENTRYPOINT',
28+
t.equal(commands[numCommands-1].name, 'ENTRYPOINT',
2929
'Last command should be ENTRYPOINT');
3030

3131
t.end();
3232
});
33+
34+
35+
tape('mesos Dockerfile', function (t) {
36+
37+
var dname = __dirname;
38+
var dockerFile = fs.readFileSync(dname + '/mesos/Dockerfile', 'utf8');
39+
var commands = dockerfileParser.parse(dockerFile);
40+
41+
t.equal(commands.length, 18, 'Check number of commands');
42+
t.equal(commands[0].name, 'FROM', 'First command should be FROM');
43+
t.equal(commands[17].name, 'WORKDIR', 'Last command should be WORKDIR');
44+
45+
t.end();
46+
});
47+
48+
49+
tape('case insensitive', function (t) {
50+
51+
var dname = __dirname;
52+
var dockerFile = fs.readFileSync(dname + '/caseTest', 'utf8');
53+
var commands = dockerfileParser.parse(dockerFile);
54+
55+
t.equal(commands.length, 3, 'Check number of commands');
56+
t.equal(commands[0].name, 'FROM', 'Command should be FROM');
57+
t.equal(commands[1].name, 'ADD', 'Command should be ADD');
58+
t.equal(commands[2].name, 'COPY', 'Command should be COPY');
59+
60+
t.end();
61+
});

0 commit comments

Comments
 (0)