Skip to content

Commit

Permalink
Fix #126 Support --exclude flag for ADD and COPY
Browse files Browse the repository at this point in the history
--exclude is a new flag for specifying files to not be included in the
built image. We will no longer consider this as an unrecognized flag.

Signed-off-by: Remy Suen <remy.suen@gmail.com>
  • Loading branch information
rcjsuen committed Mar 25, 2024
1 parent f522faf commit 69280a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

## [Unreleased]
### Added
- support parsing the new `--exclude` flag for ADD and COPY instructions ([#124](https://github.com/rcjsuen/dockerfile-utils/issues/124))

## [0.15.0] - 2023-09-10
### Added
- ignore predefined platform ARG variables if they are used as a base image ([#119](https://github.com/rcjsuen/dockerfile-utils/issues/119))
Expand Down
8 changes: 4 additions & 4 deletions src/dockerValidator.ts
Expand Up @@ -700,7 +700,7 @@ export class Validator {
if (problem !== null) {
problems.push(problem);
}
} else if (name !== "chmod" && name !== "chown" && name !== "checksum") {
} else if (name !== "chmod" && name !== "chown" && name !== "checksum" && name !== "exclude") {
let range = flag.getNameRange();
problems.push(Validator.createUnknownAddFlag(addInstructionRange.start.line, flagRange.start, range.end, name));
}
Expand All @@ -709,7 +709,7 @@ export class Validator {
if (addDestinationDiagnostic !== null) {
problems.push(addDestinationDiagnostic);
}
this.checkFlagValue(addInstructionRange.start.line, addFlags, ["chmod", "chown", "checksum"], problems);
this.checkFlagValue(addInstructionRange.start.line, addFlags, ["chmod", "chown", "checksum", "exclude"], problems);
this.checkDuplicateFlags(addInstructionRange.start.line, addFlags, ["chmod", "chown", "checksum", "keep-git-dir", "link"], problems);
this.checkJSONQuotes(instruction, problems);
break;
Expand All @@ -728,7 +728,7 @@ export class Validator {
if (problem !== null) {
problems.push(problem);
}
} else if (name !== "chmod" && name !== "chown" && name !== "from") {
} else if (name !== "chmod" && name !== "chown" && name !== "from" && name !== "exclude") {
let range = flag.getNameRange();
problems.push(Validator.createUnknownCopyFlag(copyInstructionRange.start.line, flagRange.start, range.end, name));
}
Expand All @@ -750,7 +750,7 @@ export class Validator {
if (copyDestinationDiagnostic !== null) {
problems.push(copyDestinationDiagnostic);
}
this.checkFlagValue(copyInstructionRange.start.line, flags, ["chmod", "chown", "from"], problems);
this.checkFlagValue(copyInstructionRange.start.line, flags, ["chmod", "chown", "from", "exclude"], problems);
this.checkDuplicateFlags(copyInstructionRange.start.line, flags, ["chmod", "chown", "from", "link"], problems);
this.checkJSONQuotes(instruction, problems);
break;
Expand Down
46 changes: 46 additions & 0 deletions test/dockerValidator.test.ts
Expand Up @@ -2241,6 +2241,29 @@ describe("Docker Validator Tests", function() {
});
});

describe("exclude", () => {
it("ok", () => {
const diagnostics = validateDockerfile("FROM alpine\nADD --exclude=*.txt . .");
assert.strictEqual(diagnostics.length, 0);
});

it("duplicate flag is allowed", () => {
const diagnostics = validateDockerfile("FROM alpine\nADD --exclude=*.txt --exclude=*.md . .");
assert.strictEqual(diagnostics.length, 0);
});

it("no value", () => {
const diagnostics = validateDockerfile("FROM alpine\nADD --exclude . .");
assert.strictEqual(diagnostics.length, 1);
assertFlagMissingValue(diagnostics[0], 1, "exclude", 1, 6, 1, 13);
});

it("empty value", () => {
const diagnostics = validateDockerfile("FROM alpine\nADD --exclude= . .");
assert.strictEqual(diagnostics.length, 0);
});
});

describe("checksum", () => {
it("ok", () => {
const diagnostics = validateDockerfile("FROM alpine\nADD --checksum=sha256:24454f830cdb571e2c4ad15481119c43b3cafd48dd869a9b2945d1036d1dc68d https://mirrors.edge.kernel.org/pub/linux/kernel/Historic/linux-0.01.tar.gz /");
Expand Down Expand Up @@ -2755,6 +2778,29 @@ describe("Docker Validator Tests", function() {
});
});

describe("exclude", () => {
it("ok", () => {
const diagnostics = validateDockerfile("FROM alpine\nCOPY --exclude=*.txt . .");
assert.strictEqual(diagnostics.length, 0);
});

it("duplicate flag is allowed", () => {
const diagnostics = validateDockerfile("FROM alpine\nCOPY --exclude=*.txt --exclude=*.md . .");
assert.strictEqual(diagnostics.length, 0);
});

it("no value", () => {
const diagnostics = validateDockerfile("FROM alpine\nCOPY --exclude . .");
assert.strictEqual(diagnostics.length, 1);
assertFlagMissingValue(diagnostics[0], 1, "exclude", 1, 7, 1, 14);
});

it("empty value", () => {
const diagnostics = validateDockerfile("FROM alpine\nCOPY --exclude= . .");
assert.strictEqual(diagnostics.length, 0);
});
});

describe("from", function() {
it("ok", function() {
let diagnostics = validateDockerfile("FROM alpine\nFROM busybox AS bb\nCOPY --from=bb . .");
Expand Down

0 comments on commit 69280a6

Please sign in to comment.