Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix missing path in metadata #551

Merged
merged 12 commits into from
Nov 22, 2024
42 changes: 0 additions & 42 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
184 changes: 184 additions & 0 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -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":"<real_path>"},"k8s":{"name":"k8s","manifest_path":"<real_path>"},"k8soci":{"name":"k8soci","manifest_path":"<real_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
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
1 change: 1 addition & 0 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 14 additions & 4 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package features
import (
"os"
"strings"
"sync"
)

const (
Expand All @@ -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"
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
Loading