From 63764b37e5bcb7670ea6bdf60368923d2460a958 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Sun, 18 Feb 2024 10:50:31 +0100 Subject: [PATCH 1/2] fix: Prevent .dockerignore entry from matching everything if it ends with globstart --- src/Testcontainers/Images/IgnoreFile.cs | 19 +++++++++++-------- .../Fixtures/Images/IgnoreFileFixture.cs | 14 +++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/Testcontainers/Images/IgnoreFile.cs b/src/Testcontainers/Images/IgnoreFile.cs index d5d3ec336..7da514c4f 100644 --- a/src/Testcontainers/Images/IgnoreFile.cs +++ b/src/Testcontainers/Images/IgnoreFile.cs @@ -73,14 +73,17 @@ public IgnoreFile(IEnumerable patterns, ILogger logger) // Prepare exact and partial patterns. .Aggregate(new List>(), (lines, line) => { - var key = line.Key; - var value = line.Value; - - lines.AddRange(key - .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) - .Skip(1) - .Prepend(key) - .Select(ignorePattern => new KeyValuePair(ignorePattern, value))); + const string globstar = "**/"; + + if (line.Key.StartsWith(globstar)) + { + lines.Add(line); + lines.Add(new KeyValuePair(line.Key.Substring(globstar.Length), line.Value)); + } + else + { + lines.Add(line); + } return lines; }) diff --git a/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs b/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs index fdb1a3870..e91f2617b 100644 --- a/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs +++ b/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs @@ -13,7 +13,7 @@ public IgnoreFileFixture() var ignoreAllFilesAndDirectories = new IgnoreFile(new[] { "*", "!README*.md" }, logger); var ignoreNonRecursiveFiles = new IgnoreFile(new[] { "*/temp*" }, logger); var ignoreNonRecursiveNestedFiles = new IgnoreFile(new[] { "*/*/temp*" }, logger); - var ignoreRecursiveFiles = new IgnoreFile(new[] { "**/*.txt" }, logger); + var ignoreRecursiveFiles = new IgnoreFile(new[] { "**/*.txt", "**/.idea", "**/.vs", "**/.git", "!**/.gitignore", "!.git/HEAD", "!.git/refs/heads/**", "src/**/lipsum.config" }, logger); var ignoreSingleCharacterFiles = new IgnoreFile(new[] { "temp?" }, logger); var ignoreExceptionFiles = new IgnoreFile(new[] { "*.md", "!README*.md", "README-secret.md" }, logger); Add(ignoreFilesAndDirectories, "bin/Debug", false); @@ -35,6 +35,18 @@ public IgnoreFileFixture() Add(ignoreRecursiveFiles, "lorem/lipsum.txt", false); Add(ignoreRecursiveFiles, "lorem/lipsum/lipsum.config", true); Add(ignoreRecursiveFiles, "lorem/lipsum.config", true); + Add(ignoreRecursiveFiles, "src/.idea/../v17/../lipsum.log", false); + Add(ignoreRecursiveFiles, "src/.vs/../v17/../lipsum.log", false); + Add(ignoreRecursiveFiles, ".git/logs", false); + Add(ignoreRecursiveFiles, "src/lorem/lipsum/lipsum.config", false); + Add(ignoreRecursiveFiles, "src/lorem/lipsum.config", false); + Add(ignoreRecursiveFiles, ".gitignore", true); + Add(ignoreRecursiveFiles, ".git/HEAD", true); + Add(ignoreRecursiveFiles, ".git/refs/heads/main", true); + Add(ignoreRecursiveFiles, ".git/refs/heads/bugfix/gh-1119", true); + Add(ignoreRecursiveFiles, "src/lorem/temp", true); + Add(ignoreRecursiveFiles, "src/lipsum.config", true); + Add(ignoreRecursiveFiles, "lipsum.config", true); Add(ignoreSingleCharacterFiles, "temp", false); Add(ignoreSingleCharacterFiles, "temp1", false); Add(ignoreSingleCharacterFiles, "temp12", true); From 1202c4ead393b10a4878b789d6820f8efb1b08fd Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Sun, 18 Feb 2024 20:45:14 +0100 Subject: [PATCH 2/2] fix: Ignore file in root of globstar too --- src/Testcontainers/Images/IgnoreFile.cs | 4 ++-- .../Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Testcontainers/Images/IgnoreFile.cs b/src/Testcontainers/Images/IgnoreFile.cs index 7da514c4f..ae135a6be 100644 --- a/src/Testcontainers/Images/IgnoreFile.cs +++ b/src/Testcontainers/Images/IgnoreFile.cs @@ -75,10 +75,10 @@ public IgnoreFile(IEnumerable patterns, ILogger logger) { const string globstar = "**/"; - if (line.Key.StartsWith(globstar)) + if (line.Key.Contains(globstar)) { lines.Add(line); - lines.Add(new KeyValuePair(line.Key.Substring(globstar.Length), line.Value)); + lines.Add(new KeyValuePair(line.Key.Replace(globstar, string.Empty), line.Value)); } else { diff --git a/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs b/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs index e91f2617b..48ca59d4b 100644 --- a/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs +++ b/tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs @@ -40,12 +40,12 @@ public IgnoreFileFixture() Add(ignoreRecursiveFiles, ".git/logs", false); Add(ignoreRecursiveFiles, "src/lorem/lipsum/lipsum.config", false); Add(ignoreRecursiveFiles, "src/lorem/lipsum.config", false); + Add(ignoreRecursiveFiles, "src/lipsum.config", false); Add(ignoreRecursiveFiles, ".gitignore", true); Add(ignoreRecursiveFiles, ".git/HEAD", true); Add(ignoreRecursiveFiles, ".git/refs/heads/main", true); Add(ignoreRecursiveFiles, ".git/refs/heads/bugfix/gh-1119", true); Add(ignoreRecursiveFiles, "src/lorem/temp", true); - Add(ignoreRecursiveFiles, "src/lipsum.config", true); Add(ignoreRecursiveFiles, "lipsum.config", true); Add(ignoreSingleCharacterFiles, "temp", false); Add(ignoreSingleCharacterFiles, "temp1", false);