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
29 changes: 9 additions & 20 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,7 @@ func (c *KpmClient) AcquireDepSum(dep pkg.Dependency) (string, error) {

// ResolveDepsIntoMap will calculate the map of kcl package name and local storage path of the external packages.
func (c *KpmClient) ResolveDepsIntoMap(kclPkg *pkg.KclPkg) (map[string]string, error) {
var err error
if kclPkg.IsVendorMode() {
err = c.VendorDeps(kclPkg)
} else {
kclPkg, err = c.Update(
WithUpdatedKclPkg(kclPkg),
)
}
err := c.ResolvePkgDepsMetadata(kclPkg, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -262,21 +255,16 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen
// Since redownloads are not triggered if local dependencies exists,
// indirect dependencies are also synchronized to the lock file by `lockDeps`.
func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) error {
var err error
if kclPkg.IsVendorMode() {
// In the vendor mode, the search path is the vendor subdirectory of the current package.
err := c.VendorDeps(kclPkg)
if err != nil {
return err
}
err = c.VendorDeps(kclPkg)
} else {
// In the non-vendor mode, the search path is the KCL_PKG_PATH.
err := c.resolvePkgDeps(kclPkg, &kclPkg.Dependencies, update)
if err != nil {
return err
}

_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
WithOffline(!update),
)
}
return nil
return err
}

func (c *KpmClient) resolvePkgDeps(kclPkg *pkg.KclPkg, lockDeps *pkg.Dependencies, update bool) error {
Expand Down Expand Up @@ -426,6 +414,7 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error {
if ok, err := features.Enabled(features.SupportMVS); err != nil && ok {
_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
WithOffline(false),
)
if err != nil {
return err
Expand Down
56 changes: 12 additions & 44 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,17 @@ func testResolveMetadataInJsonStr(t *testing.T) {
assert.Equal(t, utils.DirExists(vendorDir), false)
assert.Equal(t, utils.DirExists(filepath.Join(vendorDir, "flask-demo-kcl-manifests_ade147b")), false)
assert.Equal(t, err, nil)
expectedStr := "{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"\"}}}"
expectedPath := filepath.Join("not_exist", "flask-demo-kcl-manifests_ade147b")
if runtime.GOOS == "windows" {
expectedPath = strings.ReplaceAll(expectedPath, "\\", "\\\\")
}
expectedStr := fmt.Sprintf(
"{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"%s\"}}}",
expectedPath,
)
assert.Equal(t, res, expectedStr)
defer func() {
if r := os.RemoveAll(filepath.Join("not_exist", "flask-demo-kcl-manifests_ade147b")); r != nil {
if r := os.RemoveAll(expectedPath); r != nil {
err = fmt.Errorf("panic: %v", r)
}
}()
Expand Down Expand Up @@ -1146,6 +1153,9 @@ func testMetadataOffline(t *testing.T) {
assert.Equal(t, res, "{\"packages\":{}}")
content_after_metadata, err := os.ReadFile(kclMod)
assert.Equal(t, err, nil)
if runtime.GOOS == "windows" {
uglyContent = []byte(strings.ReplaceAll(string(uglyContent), "\r\n", "\n"))
}
assert.Equal(t, string(content_after_metadata), string(uglyContent))

res, err = kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, true)
Expand Down Expand Up @@ -2253,45 +2263,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!")
}
209 changes: 209 additions & 0 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package client

import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"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
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, tc.name, testFunc)
}
}

func TestKpmIssue550(t *testing.T) {
testPath := "github.com/kcl-lang/kpm/issues/550"
testCases := []struct {
name string
setup func()
expected string
winExpected 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"),
winExpected: 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"),
winExpected: filepath.Join("git", "src", "3523a44a55384201", "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"),
winExpected: 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"),
winExpected: filepath.Join("git", "src", "3523a44a55384201", "flask-demo-kcl-manifests", "test-branch-without-modfile", "aa", "cc"),
},
}

for _, tc := range testCases {
tc := tc // capture range variable

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.ResolveDepsMetadataInJsonStr(kMod, true)

if err != nil {
t.Fatal(err)
}

expectedPath := filepath.Join(tmpKpmHome, tc.expected)
if runtime.GOOS == "windows" {
expectedPath = filepath.Join(tmpKpmHome, tc.winExpected)
expectedPath = strings.ReplaceAll(expectedPath, "\\", "\\\\")
}

assert.Equal(t, res, fmt.Sprintf(
`{"packages":{"cc":{"name":"cc","manifest_path":"%s"}}}`,
expectedPath,
))

resMap, 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(resMap), 1)
if runtime.GOOS == "windows" {
assert.Equal(t, resMap["cc"], filepath.Join(tmpKpmHome, tc.winExpected))
} else {
assert.Equal(t, resMap["cc"], filepath.Join(tmpKpmHome, tc.expected))
}
}

RunTestWithGlobalLockAndKpmCli(t, tc.name, 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
Loading