diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index a7717e36..ac183b82 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -2253,45 +2253,3 @@ func testPushWithInsecureSkipTLSverify(t *testing.T) { assert.Equal(t, buf.String(), "Called Success\n") } - -func TestIssues(t *testing.T) { - // "kcl-lang/kcl/issue/1760" is the repo where the issue was actually raised and the issue id. - // "testIssue1760" is the test case cover the issue. - RunTestWithGlobalLockAndKpmCli(t, "kcl-lang/kcl/issue/1760", testIssue1760) -} - -func testIssue1760(t *testing.T, kpmcli *KpmClient) { - rootPath := getTestDir("issues") - mainKFilePath := filepath.Join(rootPath, "kcl-lang/kcl/issue/1760", "a", "main.k") - var buf bytes.Buffer - kpmcli.SetLogWriter(&buf) - - res, err := kpmcli.Run( - WithRunSource( - &downloader.Source{ - Local: &downloader.Local{ - Path: mainKFilePath, - }, - }, - ), - ) - - if err != nil { - t.Fatal(err) - } - - assert.Contains(t, - utils.RmNewline(buf.String()), - "downloading 'kcl-lang/fluxcd-source-controller:v1.3.2' from 'ghcr.io/kcl-lang/fluxcd-source-controller:v1.3.2'", - ) - assert.Contains(t, - utils.RmNewline(buf.String()), - "downloading 'kcl-lang/k8s:1.31.2' from 'ghcr.io/kcl-lang/k8s:1.31.2'", - ) - - assert.Contains(t, - utils.RmNewline(buf.String()), - "downloading 'kcl-lang/fluxcd-helm-controller:v1.0.3' from 'ghcr.io/kcl-lang/fluxcd-helm-controller:v1.0.3'", - ) - assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") -} diff --git a/pkg/client/issues_test.go b/pkg/client/issues_test.go new file mode 100644 index 00000000..962c6514 --- /dev/null +++ b/pkg/client/issues_test.go @@ -0,0 +1,184 @@ +package client + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/features" + pkg "kcl-lang.io/kpm/pkg/package" + "kcl-lang.io/kpm/pkg/utils" +) + +func TestKclIssue1760(t *testing.T) { + testPath := "github.com/kcl-lang/kcl/issues/1760" + testCases := []struct { + name string + setup func() + }{ + { + name: "Default", + setup: func() { + features.Disable(features.SupportNewStorage) + features.Disable(features.SupportMVS) + }, + }, + { + name: "SupportNewStorage", + setup: func() { + features.Enable(features.SupportNewStorage) + features.Disable(features.SupportMVS) + }, + }, + { + name: "SupportMVS", + setup: func() { + features.Disable(features.SupportNewStorage) + features.Enable(features.SupportMVS) + }, + }, + { + name: "SupportNewStorageAndMVS", + setup: func() { + features.Enable(features.SupportNewStorage) + features.Enable(features.SupportMVS) + }, + }, + } + + for _, tc := range testCases { + tc := tc // capture range variable + t.Run(tc.name, func(t *testing.T) { + tc.setup() + + testFunc := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + mainKFilePath := filepath.Join(rootPath, testPath, "a", "main.k") + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + res, err := kpmcli.Run( + WithRunSource( + &downloader.Source{ + Local: &downloader.Local{ + Path: mainKFilePath, + }, + }, + ), + ) + + if err != nil { + t.Fatal(err) + } + + assert.Contains(t, + utils.RmNewline(buf.String()), + "downloading 'kcl-lang/fluxcd-source-controller:v1.3.2' from 'ghcr.io/kcl-lang/fluxcd-source-controller:v1.3.2'", + ) + assert.Contains(t, + utils.RmNewline(buf.String()), + "downloading 'kcl-lang/k8s:1.31.2' from 'ghcr.io/kcl-lang/k8s:1.31.2'", + ) + + assert.Contains(t, + utils.RmNewline(buf.String()), + "downloading 'kcl-lang/fluxcd-helm-controller:v1.0.3' from 'ghcr.io/kcl-lang/fluxcd-helm-controller:v1.0.3'", + ) + assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") + } + + RunTestWithGlobalLockAndKpmCli(t, testPath, testFunc) + }) + } +} + +func TestKpmIssue550(t *testing.T) { + testPath := "github.com/kcl-lang/kpm/issues/550" + testCases := []struct { + name string + setup func() + expected string + }{ + { + name: "Default", + setup: func() { + features.Disable(features.SupportNewStorage) + features.Disable(features.SupportMVS) + }, + expected: filepath.Join("flask-demo-kcl-manifests_test-branch-without-modfile", "aa", "cc"), + }, + { + name: "SupportNewStorage", + setup: func() { + features.Enable(features.SupportNewStorage) + features.Disable(features.SupportMVS) + }, + expected: filepath.Join("git", "src", "200297ed26e4aeb7", "flask-demo-kcl-manifests", "test-branch-without-modfile", "aa", "cc"), + }, + { + name: "SupportMVS", + setup: func() { + features.Disable(features.SupportNewStorage) + features.Enable(features.SupportMVS) + }, + expected: filepath.Join("flask-demo-kcl-manifests_test-branch-without-modfile", "aa", "cc"), + }, + { + name: "SupportNewStorageAndMVS", + setup: func() { + features.Enable(features.SupportNewStorage) + features.Enable(features.SupportMVS) + }, + expected: filepath.Join("git", "src", "200297ed26e4aeb7", "flask-demo-kcl-manifests", "test-branch-without-modfile", "aa", "cc"), + }, + } + + for _, tc := range testCases { + tc := tc // capture range variable + t.Run(tc.name, func(t *testing.T) { + tc.setup() + + testFunc := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + modPath := filepath.Join(rootPath, testPath, "pkg") + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + tmpKpmHome, err := os.MkdirTemp("", "") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmpKpmHome) + + kpmcli.homePath = tmpKpmHome + + kMod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + if err != nil { + t.Fatal(err) + } + + res, err := kpmcli.ResolveDepsIntoMap(kMod) + + if err != nil { + t.Fatal(err) + } + fmt.Printf("buf.String(): %v\n", buf.String()) + assert.Contains(t, + utils.RmNewline(buf.String()), + "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with branch 'test-branch-without-modfile'", + ) + assert.Equal(t, len(res), 1) + assert.Equal(t, res["cc"], filepath.Join(tmpKpmHome, tc.expected)) + } + + RunTestWithGlobalLockAndKpmCli(t, testPath, testFunc) + }) + } +} diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/README.md b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/README.md similarity index 100% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/README.md rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/README.md diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/kcl.mod similarity index 100% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/kcl.mod diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod.lock b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/kcl.mod.lock similarity index 76% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod.lock rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/kcl.mod.lock index 6a6d847e..252a3083 100644 --- a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod.lock +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/kcl.mod.lock @@ -19,11 +19,3 @@ reg = "ghcr.io" repo = "kcl-lang/fluxcd-source-controller" oci_tag = "v1.3.2" - [dependencies.k8s] - name = "k8s" - full_name = "k8s_1.31.2" - version = "1.31.2" - sum = "xBZgPsnpVVyWBpahuPQHReeRx28eUHGFoaPeqbct+vs=" - reg = "ghcr.io" - repo = "kcl-lang/k8s" - oci_tag = "1.31.2" diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/main.k similarity index 100% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/main.k rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/a/main.k diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/kcl.mod similarity index 100% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/kcl.mod diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod.lock b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/kcl.mod.lock similarity index 61% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod.lock rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/kcl.mod.lock index b67bef5c..7fbf5d48 100644 --- a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod.lock +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/kcl.mod.lock @@ -6,10 +6,3 @@ reg = "ghcr.io" repo = "kcl-lang/fluxcd-source-controller" oci_tag = "v1.3.2" - [dependencies.k8s] - name = "k8s" - full_name = "k8s_1.31.2" - version = "1.31.2" - reg = "ghcr.io" - repo = "kcl-lang/k8s" - oci_tag = "1.31.2" diff --git a/pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/main.k similarity index 100% rename from pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/main.k rename to pkg/client/test_data/issues/github.com/kcl-lang/kcl/issues/1760/b/main.k diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/README.md b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/README.md new file mode 100644 index 00000000..85a177d3 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/README.md @@ -0,0 +1,33 @@ +# bug: path information is lost in kcl mod metadata + + +## 1. Minimal reproduce step (Required) + +```shell +kcl mod init demo +cd demo +kcl mod add k8s --git https://github.com/kcl-lang/modules +kcl mod add k8s --rename k8soci +kcl mod add json_merge_patch --git https://github.com/kcl-lang/modules --tag v0.1.0 +kcl mod metadata +``` + +## 2. What did you expect to see? (Required) + +Show the metadata message with local storage path. + +```shell +{"packages":{"json_merge_patch":{"name":"json_merge_patch","manifest_path":""},"k8s":{"name":"k8s","manifest_path":""},"k8soci":{"name":"k8soci","manifest_path":""}}} +``` + +## 3. What did you see instead (Required) + +The real path is empty. + +```shell +{"packages":{"json_merge_patch":{"name":"json_merge_patch","manifest_path":""},"k8s":{"name":"k8s","manifest_path":""},"k8soci":{"name":"k8soci","manifest_path":""}}} +``` + +## 4. What is your KCL components version? (Required) + +v0.11.0-alpha.1 diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod new file mode 100644 index 00000000..e499e150 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "pkg" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "test-branch-without-modfile", version = "0.0.1" } diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod.lock b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod.lock new file mode 100644 index 00000000..a69a0636 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/kcl.mod.lock @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.cc] + name = "cc" + full_name = "cc_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + branch = "test-branch-without-modfile" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/550/pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/update.go b/pkg/client/update.go index dd1f3bc5..ee7c11e2 100644 --- a/pkg/client/update.go +++ b/pkg/client/update.go @@ -100,6 +100,7 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { err := depResolver.Resolve( resolver.WithResolveKclMod(kMod), resolver.WithEnableCache(true), + resolver.WithCachePath(c.homePath), ) if err != nil { diff --git a/pkg/features/features.go b/pkg/features/features.go index 0116544b..0e69fc49 100644 --- a/pkg/features/features.go +++ b/pkg/features/features.go @@ -5,6 +5,7 @@ package features import ( "os" "strings" + "sync" ) const ( @@ -14,10 +15,13 @@ const ( SupportNewStorage = "SupportNewStorage" ) -var features = map[string]bool{ - SupportMVS: false, - SupportNewStorage: false, -} +var ( + features = map[string]bool{ + SupportMVS: false, + SupportNewStorage: false, + } + mu sync.Mutex +) func init() { // supports the env: export KPM_FEATURE_GATES="SupportMVS=true" @@ -39,6 +43,8 @@ func FeatureGates() map[string]bool { // pkg/runtime/features, so callers won't need to import // both packages for checking whether a feature is enabled. func Enabled(feature string) (bool, error) { + mu.Lock() + defer mu.Unlock() if enabled, ok := features[feature]; ok { return enabled, nil } @@ -48,6 +54,8 @@ func Enabled(feature string) (bool, error) { // Enable enables the specified feature. If the feature is not // present, it's a no-op. func Enable(feature string) { + mu.Lock() + defer mu.Unlock() if _, ok := features[feature]; ok { features[feature] = true } @@ -56,6 +64,8 @@ func Enable(feature string) { // Disable disables the specified feature. If the feature is not // present, it's a no-op. func Disable(feature string) { + mu.Lock() + defer mu.Unlock() if _, ok := features[feature]; ok { features[feature] = false }