Skip to content

Commit

Permalink
check for . path
Browse files Browse the repository at this point in the history
Signed-off-by: Somtochi Onyekwere <[email protected]>
  • Loading branch information
somtochiama committed Aug 17, 2022
1 parent 4d3acc8 commit fb3355b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
5 changes: 4 additions & 1 deletion oci/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
81 changes: 61 additions & 20 deletions oci/client/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
8 changes: 8 additions & 0 deletions oci/client/testdata/artifact/somedir/git/repo.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fb3355b

Please sign in to comment.