Skip to content

Commit 7af49ef

Browse files
ralph-saunderslphiri
authored andcommitted
Adds inline ignore functionality and unit tests. Updates the README.md file to describe how to use the new functionality. (#132)
1 parent 0b5b0f4 commit 7af49ef

4 files changed

Lines changed: 127 additions & 62 deletions

File tree

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ Note the (optional) `inverse_rule` attribute - this is just a convinient way to
121121
## Required Instruction Section
122122
This section includes a list of instructions that must exist in the dockerfile in order for it to be considered valid.
123123

124+
## Inline Ignore Instructions
125+
The user can tell dockerfile_lint to ignore a specific comand line inside a Dockerfile by placing a comment containing the word "dockerfile_lint" followed by the word "ignore", separated by a space, or a space and a dash/equals sign, above the command in the Dockerfile to be ignored.
126+
```
127+
# Add is required <for some previously approved reason documented here>
128+
# dockerfile_lint - ignore
129+
ADD http://example.com/big.tar.xz /usr/src/things/
130+
```
131+
The above inline ignore would cause dockerfile_lint to skip processing the ADD command that follows it. This allows the writing of strict rules in order to catch when best practices are not followed, while still being able to explicitly override the check on a case by case basis if a valid reason exists.
132+
124133
# Library Usage
125134
126135
## Node.js application use

lib/parser.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
var TOKEN_WHITESPACE = RegExp(/[\t\v\f\r ]+/);
1010
var TOKEN_LINE_CONTINUATION = RegExp(/\\[ \t]*$/);
1111
var TOKEN_COMMENT = RegExp(/^#.*$/);
12+
// # dockerfile_lint ignore | # dockerfile_lint - ignore | # dockerfile_lint = ignore
13+
var TOKEN_INLINE_IGNORE = RegExp(/^#.*dockerfile_lint[ ]*\W[ ]*ignore.*$/);
1214
var errDockerfileNotStringArray = new Error('When using JSON array syntax, ' + 'arrays must be comprised of strings only.');
1315

1416

@@ -444,6 +446,7 @@ function parse(contents, options) {
444446
var parseResult;
445447
var partialLine = '';
446448
var includeComments = options && options['includeComments'];
449+
var ignoreNextCommand = false;
447450

448451
for (i = 0; i < lines.length; i++) {
449452
//LDMOD
@@ -470,9 +473,30 @@ function parse(contents, options) {
470473
lineno = i + 1;
471474
}
472475
parseResult = parseLine(line, lineno);
473-
if (parseResult.command) {
474-
if (parseResult.command.name !== 'COMMENT' || includeComments) {
475-
commands.push(parseResult.command);
476+
//
477+
// Implement and inline ignore functionality
478+
// Add a check to see if the comment contains dockerfile_lint and ignore,
479+
// if it does, ignore the next command by setting a flag that we check
480+
// right after calling parseLine().
481+
//
482+
if(parseResult.command) {
483+
if (lines[i].match(TOKEN_INLINE_IGNORE)) {
484+
ignoreNextCommand = true;
485+
}
486+
else {
487+
if(parseResult.command.name !== 'COMMENT' || includeComments) {
488+
//
489+
// If an "ignore the next command comment" (i.e. an inline ignore) was found
490+
// then skip checking this command and reset the flag.
491+
//
492+
if(ignoreNextCommand === false) {
493+
commands.push(parseResult.command);
494+
}
495+
else {
496+
console.log("IGNORING STATEMENT: " + parseResult.command.raw)
497+
ignoreNextCommand = false;
498+
}
499+
}
476500
}
477501
}
478502
partialLine = parseResult.partialLine;

package-lock.json

Lines changed: 59 additions & 59 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/unit/parser.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ describe('parse function', function () {
5050

5151
});
5252

53+
it('should correctly ignore commands preceeded by an inline ignore', function () {
54+
var options = {
55+
includeComments: false
56+
};
57+
var contents = 'FROM ubuntu:latest\n'
58+
+ '#Comment1\n'
59+
+ '# dockerfile_lint - ignore\n'
60+
+ 'RUN echo done\n'
61+
+ "LABEL two=3 'one two'=4 three="
62+
+ '#Comment2\n'
63+
+ '#Comment3 \n';
64+
var commands = parser.parse(contents, options);
65+
commands.length.should.eql(2);
66+
67+
});
68+
69+
it('should not ignore commands preceeded by an comment about the inline ignore functionality', function () {
70+
var options = {
71+
includeComments: false
72+
};
73+
var contents = 'FROM ubuntu:latest\n'
74+
+ '#Comment1\n'
75+
+ '# dockerfile_lint comment about inline ignore\n'
76+
+ 'RUN echo done\n'
77+
+ "LABEL two=3 'one two'=4 three="
78+
+ '#Comment2\n'
79+
+ '#Comment3 \n';
80+
var commands = parser.parse(contents, options);
81+
commands.length.should.eql(3);
82+
83+
});
84+
5385
it('should correctly report errors', function () {
5486
var options = {
5587
includeComments: false

0 commit comments

Comments
 (0)