Skip to content

Commit

Permalink
fix: Prevent .dockerignore entry from matching everything if it ends …
Browse files Browse the repository at this point in the history
…with globstart (testcontainers#1122)
  • Loading branch information
HofmeisterAn authored Feb 19, 2024
1 parent a21b6e1 commit 2766fec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/Testcontainers/Images/IgnoreFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ public IgnoreFile(IEnumerable<string> patterns, ILogger logger)
// Prepare exact and partial patterns.
.Aggregate(new List<KeyValuePair<string, bool>>(), (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<string, bool>(ignorePattern, value)));
const string globstar = "**/";

if (line.Key.Contains(globstar))
{
lines.Add(line);
lines.Add(new KeyValuePair<string, bool>(line.Key.Replace(globstar, string.Empty), line.Value));
}
else
{
lines.Add(line);
}

return lines;
})
Expand Down
14 changes: 13 additions & 1 deletion tests/Testcontainers.Tests/Fixtures/Images/IgnoreFileFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, "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, "lipsum.config", true);
Add(ignoreSingleCharacterFiles, "temp", false);
Add(ignoreSingleCharacterFiles, "temp1", false);
Add(ignoreSingleCharacterFiles, "temp12", true);
Expand Down

0 comments on commit 2766fec

Please sign in to comment.