From 69280a6b83c4a4cf6ed700d55f7de197aac4cb92 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Mon, 25 Mar 2024 18:09:22 -0400 Subject: [PATCH] Fix #126 Support --exclude flag for ADD and COPY --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 --- CHANGELOG.md | 4 ++++ src/dockerValidator.ts | 8 +++---- test/dockerValidator.test.ts | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b39254..520aa49 100644 --- a/CHANGELOG.md +++ b/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)) diff --git a/src/dockerValidator.ts b/src/dockerValidator.ts index 479639c..1ac3b9f 100644 --- a/src/dockerValidator.ts +++ b/src/dockerValidator.ts @@ -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)); } @@ -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; @@ -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)); } @@ -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; diff --git a/test/dockerValidator.test.ts b/test/dockerValidator.test.ts index 46b0084..78ede34 100644 --- a/test/dockerValidator.test.ts +++ b/test/dockerValidator.test.ts @@ -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 /"); @@ -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 . .");