diff --git a/pkg/client/client.go b/pkg/client/client.go index 80ca68a6..870acdda 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -228,11 +228,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..07cd1389 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, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(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