Skip to content

Commit 2af4858

Browse files
committed
test: add e2e test suite for 'kpm pkg'
Add e2e coverage for 'kpm pkg'. We validate that the tarball produced contains the expected files. Rather than modify TestSuite struct with expected tar content, which is not relevant to other tests, we store the expected_tar_content.txt within test data. e2e tests valdate different include/exclude scenarios. The tests are failing to start and will be fixed in the next few commits. Signed-off-by: Aaron D Borden <adborden@a14n.net>
1 parent cca5480 commit 2af4858

38 files changed

+110
-3
lines changed

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

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

Whitespace-only changes.

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

Whitespace-only changes.

0 commit comments

Comments
 (0)