From c61ffdb8aceebb0f85c6441b93b1ae0f254e3511 Mon Sep 17 00:00:00 2001 From: zongz <68977949+zong-zhe@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:09:41 +0800 Subject: [PATCH] fix: fix the missing update when dependency in kcl.mod and kcl.mod.lock has different version (#225) Signed-off-by: zongz --- pkg/client/client.go | 10 ++++++---- pkg/package/modfile.go | 6 ++++++ pkg/package/modfile_test.go | 20 +++++++++++++++++++ .../test_suite.stdout | 2 +- .../test_suite.env | 2 ++ .../test_suite.input | 1 + .../test_suite.stderr | 0 .../test_suite.stdout | 2 ++ .../test_update/kcl.mod | 7 +++++++ .../test_update/kcl.mod.lock | 9 +++++++++ .../test_update/main.k | 1 + 11 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.env create mode 100644 test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.input create mode 100644 test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stderr create mode 100644 test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout create mode 100644 test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod create mode 100644 test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod.lock create mode 100644 test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/main.k diff --git a/pkg/client/client.go b/pkg/client/client.go index a6247a73..c910fade 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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) @@ -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 diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 6fa39847..09cd5e64 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -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() { diff --git a/pkg/package/modfile_test.go b/pkg/package/modfile_test.go index 909d7b32..97e7cb4b 100644 --- a/pkg/package/modfile_test.go +++ b/pkg/package/modfile_test.go @@ -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'. diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout index fd85fdf6..69214529 100644 --- a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stdout @@ -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 diff --git a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.env b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.env new file mode 100644 index 00000000..4c789529 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.env @@ -0,0 +1,2 @@ +KPM_HOME="" +KCLVM_VENDOR_HOME="" \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.input b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.input new file mode 100644 index 00000000..f17b39a2 --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.input @@ -0,0 +1 @@ +kpm update \ No newline at end of file diff --git a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stderr b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stderr new file mode 100644 index 00000000..e69de29b diff --git a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout new file mode 100644 index 00000000..101b394a --- /dev/null +++ b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout @@ -0,0 +1,2 @@ +removing 'k8s' with version '1.27' +adding 'k8s' with version '1.14' \ No newline at end of file diff --git a/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod new file mode 100644 index 00000000..873275be --- /dev/null +++ b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +k8s = "1.14" diff --git a/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod.lock b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod.lock new file mode 100644 index 00000000..7ca1df46 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/kcl.mod.lock @@ -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" diff --git a/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/main.k b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/test/e2e/test_suites/test_data/test_update_with_diff_version/test_update/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file