From 5e626d48388f3ca2f241f67cb3dc9ddd846fe776 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 21 Nov 2023 17:32:44 +0800 Subject: [PATCH 1/2] fix: remove the autoupdate of kcl.mod when use 'kpm metadata' Signed-off-by: zongz --- pkg/client/client.go | 11 +++--- pkg/client/client_test.go | 38 +++++++++++++++++++ .../test_metadata_offline/beautiful.kcl.mod | 5 +++ .../test_metadata_offline/kcl.mod.lock | 0 .../test_data/test_metadata_offline/main.k | 1 + .../test_metadata_offline/ugly.kcl.mod | 10 +++++ pkg/cmd/cmd_metadata.go | 7 ---- 7 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 pkg/client/test_data/test_metadata_offline/beautiful.kcl.mod create mode 100644 pkg/client/test_data/test_metadata_offline/kcl.mod.lock create mode 100644 pkg/client/test_data/test_metadata_offline/main.k create mode 100644 pkg/client/test_data/test_metadata_offline/ugly.kcl.mod diff --git a/pkg/client/client.go b/pkg/client/client.go index 4de108cd..66d6e8c2 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -234,11 +234,12 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro } } } - - // update the kcl.mod and kcl.mod.lock. - err := kclPkg.UpdateModAndLockFile() - if err != nil { - return err + if update { + // update the kcl.mod and kcl.mod.lock. + err := kclPkg.UpdateModAndLockFile() + if err != nil { + return err + } } return nil } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index cf045a15..009f625a 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -740,3 +740,41 @@ func TestUpdateWithKclModlock(t *testing.T) { assert.Equal(t, err, nil) }() } + +func TestMetadataOffline(t *testing.T) { + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + testDir := getTestDir("test_metadata_offline") + kclMod := filepath.Join(testDir, "kcl.mod") + uglyKclMod := filepath.Join(testDir, "ugly.kcl.mod") + BeautifulKclMod := filepath.Join(testDir, "beautiful.kcl.mod") + + uglyContent, err := os.ReadFile(uglyKclMod) + assert.Equal(t, err, nil) + err = copy.Copy(uglyKclMod, kclMod) + assert.Equal(t, err, nil) + defer func() { + err := os.Remove(kclMod) + assert.Equal(t, err, nil) + }() + + beautifulContent, err := os.ReadFile(BeautifulKclMod) + assert.Equal(t, err, nil) + kclPkg, err := pkg.LoadKclPkg(testDir) + assert.Equal(t, err, nil) + + res, err := kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, false) + assert.Equal(t, err, nil) + assert.Equal(t, res, "{\"packages\":{}}") + content_after_metadata, err := os.ReadFile(kclMod) + assert.Equal(t, err, nil) + assert.Equal(t, string(content_after_metadata), string(uglyContent)) + + res, err = kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, true) + assert.Equal(t, err, nil) + assert.Equal(t, res, "{\"packages\":{}}") + content_after_metadata, err = os.ReadFile(kclMod) + assert.Equal(t, err, nil) + assert.Equal(t, string(content_after_metadata), string(beautifulContent)) +} diff --git a/pkg/client/test_data/test_metadata_offline/beautiful.kcl.mod b/pkg/client/test_data/test_metadata_offline/beautiful.kcl.mod new file mode 100644 index 00000000..6e9ba5af --- /dev/null +++ b/pkg/client/test_data/test_metadata_offline/beautiful.kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "test_metadata_offline" +edition = "0.0.1" +version = "0.0.1" + diff --git a/pkg/client/test_data/test_metadata_offline/kcl.mod.lock b/pkg/client/test_data/test_metadata_offline/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_metadata_offline/main.k b/pkg/client/test_data/test_metadata_offline/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_metadata_offline/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod b/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod new file mode 100644 index 00000000..ea2fabea --- /dev/null +++ b/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod @@ -0,0 +1,10 @@ +[package] + + + + + +name = "test_metadata_offline" +edition = "0.0.1" +version = "0.0.1" + diff --git a/pkg/cmd/cmd_metadata.go b/pkg/cmd/cmd_metadata.go index 2f36c1b4..db54801e 100644 --- a/pkg/cmd/cmd_metadata.go +++ b/pkg/cmd/cmd_metadata.go @@ -79,13 +79,6 @@ func NewMetadataCmd(kpmcli *client.KpmClient) *cli.Command { return err } - if autoUpdate { - err = kclPkg.UpdateModAndLockFile() - if err != nil { - return err - } - } - fmt.Println(jsonStr) return nil From 03f7991d4adaef270d1e7f929e526e06b7ae4914 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 21 Nov 2023 17:44:31 +0800 Subject: [PATCH 2/2] fix: fix windows test case Signed-off-by: zongz --- pkg/client/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 009f625a..07cd1389 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -776,5 +776,5 @@ func TestMetadataOffline(t *testing.T) { assert.Equal(t, res, "{\"packages\":{}}") content_after_metadata, err = os.ReadFile(kclMod) assert.Equal(t, err, nil) - assert.Equal(t, string(content_after_metadata), string(beautifulContent)) + assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(beautifulContent))) }