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 the missing update when dependency in kcl.mod and kcl.mod.lo… #225

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 6 additions & 4 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro

// alian the dependencies between kcl.mod and kcl.mod.lock
// clean the dependencies in kcl.mod.lock which not in kcl.mod
for name := range kclPkg.Dependencies.Deps {
if _, ok := kclPkg.ModFile.Dependencies.Deps[name]; !ok {
// clean the dependencies in kcl.mod.lock and kcl.mod which have different version
for name, dep := range kclPkg.Dependencies.Deps {
modDep, ok := kclPkg.ModFile.Dependencies.Deps[name]
if !ok || !dep.WithTheSameVersion(modDep) {
reporter.ReportMsgTo(
fmt.Sprintf("removing '%s'", name),
fmt.Sprintf("removing '%s' with version '%s'", name, dep.Version),
c.logWriter,
)
delete(kclPkg.Dependencies.Deps, name)
Expand All @@ -175,7 +177,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
for name, d := range kclPkg.ModFile.Dependencies.Deps {
if _, ok := kclPkg.Dependencies.Deps[name]; !ok {
reporter.ReportMsgTo(
fmt.Sprintf("adding '%s'", name),
fmt.Sprintf("adding '%s' with version '%s'", name, d.Version),
c.logWriter,
)
kclPkg.Dependencies.Deps[name] = d
Expand Down
6 changes: 6 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ type Dependency struct {
Source `json:"-"`
}

// WithTheSameVersion will check whether two dependencies have the same version.
func (d Dependency) WithTheSameVersion(other Dependency) bool {
return d.Name == other.Name &&
d.Version == other.Version
}

// GetLocalFullPath will get the local path of a dependency.
func (dep *Dependency) GetLocalFullPath(rootpath string) string {
if dep.IsFromLocal() {
Expand Down
20 changes: 20 additions & 0 deletions pkg/package/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ func TestModFileWithDesc(t *testing.T) {
assert.Equal(t, err, nil)
}

func TestWithTheSameVersion(t *testing.T) {
d := Dependency{
Name: "test",
Version: "0.0.1",
}

d2 := Dependency{
Name: "test",
Version: "0.0.2",
}

assert.Equal(t, d.WithTheSameVersion(d2), false)

d2.Version = "0.0.1"
assert.Equal(t, d.WithTheSameVersion(d2), true)

d2.Name = "test2"
assert.Equal(t, d.WithTheSameVersion(d2), false)
}

func TestModFileExists(t *testing.T) {
testDir := initTestDir("test_data_modfile")
// there is no 'kcl.mod' and 'kcl.mod.lock'.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
adding 'k8s'
adding 'k8s' with version '1.27'
downloading 'test/k8s:1.27' from 'localhost:5001/test/k8s:1.27'
apiVersion: v1
kind: Pod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm update
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
removing 'k8s' with version '1.27'
adding 'k8s' with version '1.14'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_update"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.14"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.27"
version = "1.27"
sum = "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM="
reg = "localhost:5001"
repo = "test/k8s"
oci_tag = "1.27"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Loading