Skip to content

Commit d2b887d

Browse files
authored
Merge pull request #661 from adborden/bugfix/mod-include
Fix handling of include patterns in kpm pkg
2 parents 3f95c1f + 31c243e commit d2b887d

40 files changed

+139
-11
lines changed

pkg/utils/utils.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func HashDir(dir string) (string, error) {
4848

4949
// files in the ".git "directory will cause the same repository, cloned at different times,
5050
// has different checksum.
51-
for _, ignore := range ignores {
51+
for _, ignore := range defaultIgnores {
5252
if strings.Contains(path, ignore) {
5353
return nil
5454
}
@@ -120,7 +120,7 @@ func Exists(path string) (bool, error) {
120120
}
121121

122122
// todo: Consider using the OCI tarball as the standard tar format.
123-
var ignores = []string{".git", ".tar"}
123+
var defaultIgnores = []string{".git", ".tar"}
124124

125125
func TarDir(srcDir string, tarPath string, include []string, exclude []string) error {
126126
fw, err := os.Create(tarPath)
@@ -132,11 +132,19 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
132132
tw := tar.NewWriter(fw)
133133
defer tw.Close()
134134

135+
// In case the tarPath is within the current working directory, exclude it from the tar itself.
136+
ignores := append(defaultIgnores, filepath.Join(srcDir, filepath.Dir(tarPath)))
137+
135138
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
136139
if err != nil {
137140
return err
138141
}
139142

143+
// Ignore the current directory root "."
144+
if path == srcDir {
145+
return nil
146+
}
147+
140148
for _, ignore := range ignores {
141149
if strings.Contains(path, ignore) {
142150
return nil
@@ -145,7 +153,8 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
145153

146154
getNewPattern := func(ex string) string {
147155
newPath := ex
148-
if !strings.HasPrefix(ex, srcDir+string(filepath.Separator)) {
156+
prefix := srcDir + string(filepath.Separator)
157+
if !strings.HasPrefix(ex, prefix) {
149158
newPath = filepath.Join(srcDir, ex)
150159
}
151160
return newPath
@@ -157,12 +166,20 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e
157166
}
158167
}
159168

169+
// If the include list is empty, all files are included by default.
170+
matchedInclude := len(include) == 0
160171
for _, inc := range include {
161-
if matched, _ := filepath.Match(getNewPattern(inc), path); !matched {
162-
return nil
172+
if matched, _ := filepath.Match(getNewPattern(inc), path); matched {
173+
matchedInclude = true
174+
break
163175
}
164176
}
165177

178+
// As long as _one_ of the include patterns matches, the file will be included.
179+
if !matchedInclude {
180+
return nil
181+
}
182+
166183
relPath, _ := filepath.Rel(srcDir, path)
167184
relPath = filepath.ToSlash(relPath)
168185

pkg/utils/utils_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"archive/tar"
5+
"fmt"
56
"io"
67
"os"
78
"path"
@@ -126,19 +127,22 @@ func TestTarDir(t *testing.T) {
126127

127128
_ = TarDir(testSrcDir, tarPath, []string{}, []string{"*.mod"})
128129
fileNames, _ := getTarFileNames(tarPath)
130+
assert.Greater(t, len(fileNames), 0, "expected tar to have greater than 0 files")
129131
for _, fileName := range fileNames {
130132
flag, _ := filepath.Match(getNewPattern("*.mod"), fileName)
131-
assert.Equal(t, flag, false)
133+
assert.Equal(t, flag, false, "expected *.mod to be excluded")
132134
}
133135
_, err = os.Stat(tarPath)
134136
assert.Equal(t, err, nil)
135137
os.Remove(tarPath)
136138

137139
_ = TarDir(testSrcDir, tarPath, []string{"*/*.lock", "*.mod"}, []string{})
138140
fileNames, _ = getTarFileNames(tarPath)
141+
assert.Greater(t, len(fileNames), 0, "expected tar to have greater than 0 files")
139142
for _, fileName := range fileNames {
140-
flag, _ := filepath.Match(getNewPattern("*/*.lock"), fileName)
141-
assert.Equal(t, flag, true)
143+
matchedLockPattern, _ := filepath.Match(getNewPattern("*/*.lock"), fileName)
144+
matchedModPattern, _ := filepath.Match(getNewPattern("*.mod"), fileName)
145+
assert.True(t, matchedLockPattern || matchedModPattern, fmt.Sprintf("expected \"%s\" to match one of */*.lock or *.mod", fileName))
142146
}
143147
_, err = os.Stat(tarPath)
144148
assert.Equal(t, err, nil)

test/e2e/kpm_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package e2e
22

33
import (
4+
"archive/tar"
45
"context"
56
"encoding/json"
67
"fmt"
8+
"io"
79
"os"
810
"path/filepath"
11+
"sort"
912
"strings"
1013

1114
"github.com/onsi/ginkgo/v2"
@@ -22,6 +25,59 @@ import (
2225
)
2326

2427
var _ = ginkgo.Describe("Kpm CLI Testing", func() {
28+
ginkgo.Context("testing 'kpm pkg'", func() {
29+
testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "kpm_pkg")
30+
testSuites := LoadAllTestSuites(testSuitesRoot)
31+
testDataRoot := filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "test_data")
32+
for _, ts := range testSuites {
33+
ts := ts
34+
ginkgo.It(ts.GetTestSuiteInfo(), func() {
35+
workspace := GetWorkspace()
36+
37+
// Copy the test data to the workspace
38+
CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name))
39+
40+
// Run the command from test_suite.input
41+
stdout, stderr, _ := ExecKpmWithWorkDir(ts.Input, filepath.Join(workspace, ts.Name))
42+
43+
// kpm pkg always produces no output
44+
gomega.Expect(stdout).To(gomega.BeEmpty(), "unexpected stdout from kpm pkg")
45+
gomega.Expect(stderr).To(gomega.BeEmpty(), "unexpected stderr from kpm pkg")
46+
//time.Sleep(300 * time.Second)
47+
48+
// Assert the contents of the tarball without extracting
49+
expectedTarContentFilepath := filepath.Join(testDataRoot, ts.Name, "expected_tar_content.txt")
50+
exist, _ := utils.Exists(expectedTarContentFilepath)
51+
gomega.Expect(exist).To(gomega.BeTrue(), "expected_tar_content.txt not found in test data")
52+
expectedTarContentBytes, err := os.ReadFile(expectedTarContentFilepath)
53+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "failed to read from expected_tar_content.txt")
54+
expectedFiles := strings.Split(strings.TrimSpace(string(expectedTarContentBytes)), "\n")
55+
sort.Strings(expectedFiles)
56+
57+
tarballPath := filepath.Join(workspace, ts.Name, "dist", fmt.Sprintf("%s_0.0.1.tar", ts.Name))
58+
fmt.Printf("tarballPath: %s\n", tarballPath)
59+
exist, _ = utils.Exists(tarballPath)
60+
gomega.Expect(exist).To(gomega.BeTrue(), "pkg tarball not found, check kcl.mod name should match test suite")
61+
f, err := os.Open(tarballPath)
62+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "failed to open pkg tarball")
63+
defer f.Close()
64+
65+
tr := tar.NewReader(f)
66+
var actualFiles []string
67+
for {
68+
hdr, err := tr.Next()
69+
if err == io.EOF {
70+
break
71+
}
72+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred(), "error reading pkg tarball")
73+
actualFiles = append(actualFiles, hdr.Name)
74+
}
75+
sort.Strings(actualFiles)
76+
gomega.Expect(actualFiles).To(gomega.Equal(expectedFiles), "unexpected pkg tarball content")
77+
})
78+
}
79+
})
80+
2581
ginkgo.Context("testing 'exec kpm outside a kcl package'", func() {
2682
testSuites := LoadAllTestSuites(filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "exec_outside_pkg"))
2783
testDataRoot := filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "test_data")

test/e2e/test_suite.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ func LoadTestSuite(testSuitePath, name string) TestSuite {
6363
reporter.Report("kpm_e2e: loading '", name, "' from ", testSuitePath)
6464
CheckTestSuite(testSuitePath, name)
6565
return TestSuite{
66-
Name: name,
66+
Name: name,
6767
ExpectStdout: LoadFirstFileWithExt(testSuitePath, STDOUT_EXT),
6868
ExpectStderr: LoadFirstFileWithExt(testSuitePath, STDERR_EXT),
69-
Input: LoadFirstFileWithExt(testSuitePath, INPUT_EXT),
70-
Envs: LoadFirstFileWithExt(testSuitePath, ENV_EXT),
69+
70+
// Strip whitespace to ignore the leading and trailing new lines.
71+
Input: strings.TrimSpace(LoadFirstFileWithExt(testSuitePath, INPUT_EXT)),
72+
Envs: LoadFirstFileWithExt(testSuitePath, ENV_EXT),
7173
}
7274
}
7375

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm pkg --target=dist

test/e2e/test_suites/kpm/kpm_pkg/pkg_default/test_suite.stderr

Whitespace-only changes.

test/e2e/test_suites/kpm/kpm_pkg/pkg_default/test_suite.stdout

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm pkg --target=dist

0 commit comments

Comments
 (0)