diff --git a/oci/client/build.go b/oci/client/build.go index 7ad33036..ba2cb74c 100644 --- a/oci/client/build.go +++ b/oci/client/build.go @@ -49,7 +49,10 @@ func (c *Client) Build(artifactPath, sourceDir string, ignorePaths []string) (er }() ignore := strings.Join(ignorePaths, "\n") - domain := strings.Split(filepath.Clean(sourceDir), string(filepath.Separator)) + var domain []string + if sourceDir != "." { + domain = strings.Split(filepath.Clean(sourceDir), string(filepath.Separator)) + } ps := sourceignore.ReadPatterns(strings.NewReader(ignore), domain) matcher := sourceignore.NewMatcher(ps) filter := func(p string, fi os.FileInfo) bool { diff --git a/oci/client/build_test.go b/oci/client/build_test.go index 153f9be9..6e093160 100644 --- a/oci/client/build_test.go +++ b/oci/client/build_test.go @@ -29,43 +29,84 @@ import ( ) func TestBuild(t *testing.T) { - g := NewWithT(t) - testDir := "./testdata/artifact" c := NewLocalClient() - tmpDir := t.TempDir() - artifactPath := filepath.Join(tmpDir, "files.tar.gz") + tests := []struct { + name string + path string + ignorePath []string + expectErr bool + checkPaths []string + }{ + { + name: "non-existent path", + path: "testdata/non-existent", + expectErr: true, + }, + { + name: "existing path", + path: "testdata/artifact", + ignorePath: []string{"ignore.txt", "ignore-dir/", "!/deploy", "somedir/git"}, + checkPaths: []string{"ignore.txt", "ignore-dir/", "!/deploy", "somedir/git"}, + }, + { + name: "existing path with leading slash", + path: "./testdata/artifact", + ignorePath: []string{"ignore.txt", "ignore-dir/", "!/deploy", "somedir/git"}, + checkPaths: []string{"ignore.txt", "ignore-dir/", "!/deploy", "somedir/git"}, + }, + { + name: "current directory", + path: ".", + ignorePath: []string{"/*", "!/internal"}, + checkPaths: []string{"/testdata", "!internal/", "build.go", "meta.go"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + tmpDir := t.TempDir() + artifactPath := filepath.Join(tmpDir, "files.tar.gz") - // test with non-existent path - err := c.Build(artifactPath, "testdata/non-existent", nil) - g.Expect(err).To(HaveOccurred()) + err := c.Build(artifactPath, tt.path, tt.ignorePath) + if tt.expectErr { + g.Expect(err).To(HaveOccurred()) + return + } - ignorePaths := []string{"ignore.txt", "ignore-dir/", "!/deploy"} - err = c.Build(artifactPath, testDir, ignorePaths) - g.Expect(err).ToNot(HaveOccurred()) + g.Expect(err).To(Not(HaveOccurred())) - _, err = os.Stat(artifactPath) - g.Expect(err).ToNot(HaveOccurred()) + _, err = os.Stat(artifactPath) + g.Expect(err).ToNot(HaveOccurred()) - b, err := os.ReadFile(artifactPath) - g.Expect(err).ToNot(HaveOccurred()) + b, err := os.ReadFile(artifactPath) + g.Expect(err).ToNot(HaveOccurred()) + + untarDir := t.TempDir() + _, err = untar.Untar(bytes.NewReader(b), untarDir) + g.Expect(err).To(BeNil()) - untarDir := t.TempDir() - _, err = untar.Untar(bytes.NewReader(b), untarDir) - g.Expect(err).To(BeNil()) + checkPathExists(t, untarDir, tt.path, tt.checkPaths) + }) + } +} + +func checkPathExists(t *testing.T, dir, testDir string, paths []string) { + g := NewWithT(t) - for _, path := range ignorePaths { + for _, path := range paths { var shouldExist bool if strings.HasPrefix(path, "!") { shouldExist = true path = path[1:] } - fullPath := filepath.Join(untarDir, testDir, path) + fullPath := filepath.Join(dir, testDir, path) _, err := os.Stat(fullPath) if shouldExist { g.Expect(err).To(BeNil()) - return + continue } g.Expect(err).ToNot(BeNil()) g.Expect(os.IsNotExist(err)).To(BeTrue()) diff --git a/oci/client/testdata/artifact/somedir/git/repo.yaml b/oci/client/testdata/artifact/somedir/git/repo.yaml new file mode 100644 index 00000000..253b7632 --- /dev/null +++ b/oci/client/testdata/artifact/somedir/git/repo.yaml @@ -0,0 +1,8 @@ +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: HelmRepository +metadata: + name: podinfo + namespace: flux-system +spec: + interval: 2m + url: https://stefanprodan.github.io/podinfo