package layout import ( "errors" "path" "reflect" "strings" "testing" ) func TestIgnorePatternsCleansLikeDocker(t *testing.T) { body := "# comment\n\n .env \n/secrets\nsrc/../keys\n./build/\n/\n.\n!keep.txt\n" got := IgnorePatterns(body, false) want := []string{".env", "secrets", "keys", "build"} if !reflect.DeepEqual(got, want) { t.Fatalf("IgnorePatterns(no negations) = %q, want %q", got, want) } } func TestIgnorePatternsKeepsNegationsOnlyWhenAsked(t *testing.T) { body := "*\n!src\n! /docs\n!\n" if got, want := IgnorePatterns(body, true), []string{"*", "!src", "!docs"}; !reflect.DeepEqual(got, want) { t.Fatalf("keepNegations=true: got %q, want %q", got, want) } if got, want := IgnorePatterns(body, false), []string{"*"}; !reflect.DeepEqual(got, want) { t.Fatalf("keepNegations=false: got %q, want %q", got, want) } } func TestGeneratedContextFiltersOrderRootThenSidecar(t *testing.T) { got := ContextFilters("*\n!src\n", true, ".env\n!.env\n*.pem\n", true) want := [][]string{{"*", "!src"}, {".env", "*.pem"}} if !reflect.DeepEqual(got, want) { t.Fatalf("ContextFilters = %q, want %q", got, want) } // The sidecar's list never carries a re-include: that is what makes it // impossible for any rule to bring back what it excludes. last := got[len(got)-1] for _, p := range last { if strings.HasPrefix(p, "!") { t.Fatalf("sidecar filter carries a re-include %q", p) } } } func TestContextFiltersWithoutRootOrEmptyLists(t *testing.T) { if got := ContextFilters("ignored\n", false, ".env\n", true); !reflect.DeepEqual(got, [][]string{{".env"}}) { t.Fatalf("no root file: got %q", got) } if got := ContextFilters("# only comments\n", true, "\n", true); len(got) != 0 { t.Fatalf("empty bodies must produce no filter, got %q", got) } } // survives models the module's sequence of Directory.Filter calls: each list // is applied in turn to what the previous one left, and within one list the // LAST matching pattern decides (Docker's rule, `!` = keep). A path matches a // pattern when the pattern matches it or one of its parent directories. It is // a model of the engine, not the engine; what it pins is the ORDERING // property ContextFilters is responsible for. func survives(p string, filters [][]string) bool { for _, list := range filters { excluded := false for _, pat := range list { neg := strings.HasPrefix(pat, "!") if matchesPathOrParent(strings.TrimPrefix(pat, "!"), p) { excluded = !neg } } if excluded { return false } } return true } func matchesPathOrParent(pattern, p string) bool { for cur := p; cur != "." && cur != "/" && cur != ""; cur = path.Dir(cur) { if ok, _ := path.Match(pattern, cur); ok { return true } } return false } func TestGeneratedRootNegationCannotReincludeWhatTheSidecarExcludes(t *testing.T) { root := "*.log\n!.env\n" sidecar := ".env\n*.pem\n" filters := ContextFilters(root, true, sidecar, true) if survives(".env", filters) { t.Fatal("a root `!.env` re-included the .env the sidecar excludes") } if survives("certs/server.pem", filters) { t.Fatal("a sidecar-excluded key survived") } if survives("app.log", filters) { t.Fatal("the root .dockerignore was not applied with the sidecar") } if !survives("src/main.go", filters) { t.Fatal("an ordinary source file was excluded") } } func TestRootAllowlistKeepsWorkingWithASidecar(t *testing.T) { // `*` then `!src`: only src/ reaches the build. Dropping the root's // negations (the sidecar's rule) would exclude everything. filters := ContextFilters("*\n!src\n", true, ".env\n", true) if !survives("src/app.py", filters) { t.Fatal("the root allowlist's re-include was lost") } if survives("README.md", filters) { t.Fatal("the root allowlist excluded nothing") } if survives(".env", filters) { t.Fatal("the sidecar's .env exclusion was not applied") } } func TestSidecarPath(t *testing.T) { if got := SidecarPath(""); got != "Dockerfile.dockerignore" { t.Fatalf("default: %q", got) } if got := SidecarPath(".integraci/Dockerfile"); got != ".integraci/Dockerfile.dockerignore" { t.Fatalf("generated: %q", got) } } func TestIgnoreFileUsableRefusesWhatIsNotARegularFile(t *testing.T) { if ok, err := IgnoreFileUsable("x", Missing); ok || err != nil { t.Fatalf("missing: (%v, %v)", ok, err) } if ok, err := IgnoreFileUsable("x", Regular); !ok || err != nil { t.Fatalf("regular: (%v, %v)", ok, err) } for _, k := range []Kind{Symlink, Directory, Other} { ok, err := IgnoreFileUsable(".integraci/Dockerfile.dockerignore", k) if ok || !errors.Is(err, ErrRefused) { t.Fatalf("%s: (%v, %v), want a refusal", k, ok, err) } } } func TestGeneratedSidecarPresentRefusesOnlyAMissingSidecar(t *testing.T) { err := GeneratedSidecarPresent(".integraci/Dockerfile.dockerignore", Missing) if !errors.Is(err, ErrRefused) { t.Fatalf("missing: %v, want a refusal", err) } // Legitimate: a regular sidecar builds; the other kinds are refused by // IgnoreFileUsable, not here. for _, k := range []Kind{Regular, Symlink, Directory, Other} { if err := GeneratedSidecarPresent(".integraci/Dockerfile.dockerignore", k); err != nil { t.Fatalf("%s: %v, want nil", k, err) } } } func TestIsGeneratedDockerfile(t *testing.T) { for _, p := range []string{".integraci/Dockerfile", "apps/web/.integraci/Dockerfile"} { if !IsGeneratedDockerfile(p) { t.Errorf("%q is the generated Dockerfile", p) } } for _, p := range []string{"Dockerfile", "docker/Dockerfile", ".integraci/Dockerfile.prod", "x.integraci/Dockerfile", ""} { if IsGeneratedDockerfile(p) { t.Errorf("%q is a customer's Dockerfile", p) } } } // A customer's `Dockerfile.dockerignore` written as BuildKit's allowlist idiom // must keep its re-includes: dropping them (the generated-Dockerfile rule) // empties the build context. Review finding F1 on #2168. func TestCustomerSidecarAllowlistKeepsItsReincludes(t *testing.T) { filters := ContextFilters("", false, "*\n!src\n!package.json\n", false) if want := [][]string{{"*", "!src", "!package.json"}}; !reflect.DeepEqual(filters, want) { t.Fatalf("ContextFilters(customer) = %q, want %q", filters, want) } if !survives("src/app.py", filters) || !survives("package.json", filters) { t.Fatal("a customer allowlist sidecar emptied the context") } if survives("README.md", filters) { t.Fatal("the customer allowlist excluded nothing") } } // BuildKit ignores the root .dockerignore when a per-Dockerfile one exists; // for a customer's Dockerfile the module does the same. func TestCustomerSidecarReplacesTheRootIgnoreFile(t *testing.T) { filters := ContextFilters("*.log\nsecrets\n", true, "node_modules\n", false) if want := [][]string{{"node_modules"}}; !reflect.DeepEqual(filters, want) { t.Fatalf("ContextFilters(customer, root present) = %q, want %q", filters, want) } if !survives("app.log", filters) { t.Fatal("the root file was applied beside a customer sidecar") } } // The other direction: the generated pair still cannot be re-opened, even by // an allowlist-shaped sidecar or a root `!.env`. func TestGeneratedSidecarStillCannotReincludeEnv(t *testing.T) { filters := ContextFilters("!.env\n", true, "*\n!src\n.env\n!.env\n", true) if survives(".env", filters) { t.Fatal("the generated sidecar's .env exclusion was undone") } for _, list := range filters[1:] { for _, p := range list { if strings.HasPrefix(p, "!") { t.Fatalf("generated sidecar filter carries a re-include %q", p) } } } }