Skip to content

Commit

Permalink
Merge pull request #217 from zong-zhe/fix-metadata-autoupdate
Browse files Browse the repository at this point in the history
fix: remove the autoupdate of kcl.mod when use 'kpm metadata'
  • Loading branch information
Peefy authored Nov 21, 2023
2 parents 49c5829 + 03f7991 commit 7868e7e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 12 deletions.
11 changes: 6 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(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

0 comments on commit 7868e7e

Please sign in to comment.