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: remove the autoupdate of kcl.mod when use 'kpm metadata' #217

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
5 changes: 5 additions & 0 deletions pkg/client/test_data/test_metadata_offline/beautiful.kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "test_metadata_offline"
edition = "0.0.1"
version = "0.0.1"

Empty file.
1 change: 1 addition & 0 deletions pkg/client/test_data/test_metadata_offline/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
10 changes: 10 additions & 0 deletions pkg/client/test_data/test_metadata_offline/ugly.kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]





name = "test_metadata_offline"
edition = "0.0.1"
version = "0.0.1"

7 changes: 0 additions & 7 deletions pkg/cmd/cmd_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading