From 47dddb02f7a6c9f650e971c7cae6fa367e8baa69 Mon Sep 17 00:00:00 2001 From: zongz <68977949+zong-zhe@users.noreply.github.com> Date: Thu, 19 Dec 2024 20:20:34 +0800 Subject: [PATCH] fix: fix missing feature flag for mvs (#571) Signed-off-by: zongz --- pkg/client/add.go | 13 ++- pkg/client/add_test.go | 109 ++++++++++++++++++ pkg/client/deperated.go | 4 +- pkg/client/test_data/add_with_mvs/h12/kcl.mod | 7 ++ .../test_data/add_with_mvs/h12/kcl.mod.lock | 9 ++ pkg/client/test_data/add_with_mvs/h12/main.k | 1 + pkg/client/test_data/add_with_mvs/h14/kcl.mod | 7 ++ .../test_data/add_with_mvs/h14/kcl.mod.lock | 9 ++ pkg/client/test_data/add_with_mvs/h14/main.k | 1 + .../add_with_mvs/pkg_with_mvs/kcl.mod.bk | 4 + .../add_with_mvs/pkg_with_mvs/kcl.mod.expect | 8 ++ .../add_with_mvs/pkg_with_mvs/kcl.mod.lock.bk | 0 .../pkg_with_mvs/kcl.mod.lock.expect | 17 +++ .../add_with_mvs/pkg_with_mvs/main.k | 1 + .../add_with_mvs/pkg_without_mvs/kcl.mod.bk | 4 + .../pkg_without_mvs/kcl.mod.expect | 8 ++ .../pkg_without_mvs/kcl.mod.lock.bk | 0 .../pkg_without_mvs/kcl.mod.lock.expect | 17 +++ .../add_with_mvs/pkg_without_mvs/main.k | 1 + pkg/client/update.go | 20 ++-- 20 files changed, 227 insertions(+), 13 deletions(-) create mode 100644 pkg/client/test_data/add_with_mvs/h12/kcl.mod create mode 100644 pkg/client/test_data/add_with_mvs/h12/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_mvs/h12/main.k create mode 100644 pkg/client/test_data/add_with_mvs/h14/kcl.mod create mode 100644 pkg/client/test_data/add_with_mvs/h14/kcl.mod.lock create mode 100644 pkg/client/test_data/add_with_mvs/h14/main.k create mode 100644 pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.bk create mode 100644 pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.expect create mode 100644 pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.bk create mode 100644 pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/add_with_mvs/pkg_with_mvs/main.k create mode 100644 pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.bk create mode 100644 pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.expect create mode 100644 pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.bk create mode 100644 pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/add_with_mvs/pkg_without_mvs/main.k diff --git a/pkg/client/add.go b/pkg/client/add.go index 11cb0b34..791dbc30 100644 --- a/pkg/client/add.go +++ b/pkg/client/add.go @@ -5,6 +5,7 @@ import ( "path/filepath" "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/features" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" "kcl-lang.io/kpm/pkg/utils" @@ -206,12 +207,18 @@ func (c *KpmClient) Add(options ...AddOption) error { }) } - // Add the dependency to the kcl.mod file. - if modExistDep, ok := addedPkg.ModFile.Dependencies.Deps.Get(dep.Name); ok { - if less, err := modExistDep.VersionLessThan(&dep); less && err == nil { + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { + // Add the dependency to the kcl.mod file. + // and select the greater version of the dependency in dependencies graph. + if modExistDep, ok := addedPkg.ModFile.Dependencies.Deps.Get(dep.Name); ok { + if less, err := modExistDep.VersionLessThan(&dep); less && err == nil { + addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep) + } + } else { addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep) } } else { + // Add the dependency to the kcl.mod file directly. addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep) } succeedMsgInfo = fmt.Sprintf("add dependency '%s:%s' successfully", depPkg.ModFile.Pkg.Name, depPkg.ModFile.Pkg.Version) diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index b089ea9d..2f66c9f1 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -2,6 +2,7 @@ package client import ( "bytes" + "fmt" "os" "path/filepath" "testing" @@ -9,6 +10,7 @@ import ( "github.com/otiai10/copy" "github.com/stretchr/testify/assert" "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/features" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/utils" ) @@ -497,3 +499,110 @@ func TestAddRenameWithNoSpec(t *testing.T) { assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod))) assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock))) } + +func TestAddWithMvs(t *testing.T) { + defer features.Disable(features.SupportMVS) + testDir := getTestDir("add_with_mvs") + pkgWithMvsPath := filepath.Join(testDir, "pkg_with_mvs") + pkgWithoutMvsPath := filepath.Join(testDir, "pkg_without_mvs") + + testFunc := func(t *testing.T, kpmcli *KpmClient) { + var modbkPath, modPath, modExpect, lockbkPath, lockPath, lockExpect, pkgPath string + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { + pkgPath = pkgWithMvsPath + modbkPath = filepath.Join(pkgWithMvsPath, "kcl.mod.bk") + modPath = filepath.Join(pkgWithMvsPath, "kcl.mod") + modExpect = filepath.Join(pkgWithMvsPath, "kcl.mod.expect") + lockbkPath = filepath.Join(pkgWithMvsPath, "kcl.mod.lock.bk") + lockPath = filepath.Join(pkgWithMvsPath, "kcl.mod.lock") + lockExpect = filepath.Join(pkgWithMvsPath, "kcl.mod.lock.expect") + } else { + pkgPath = pkgWithoutMvsPath + modbkPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.bk") + modPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod") + modExpect = filepath.Join(pkgWithoutMvsPath, "kcl.mod.expect") + lockbkPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock.bk") + lockPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock") + lockExpect = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock.expect") + } + + err := copy.Copy(modbkPath, modPath) + if err != nil { + t.Fatal(err) + } + + err = copy.Copy(lockbkPath, lockPath) + if err != nil { + t.Fatal(err) + } + + defer func() { + // remove the copied files + err := os.RemoveAll(modPath) + if err != nil { + t.Fatal(err) + } + err = os.RemoveAll(lockPath) + if err != nil { + t.Fatal(err) + } + }() + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(pkgPath), + pkg.WithSettings(kpmcli.GetSettings()), + ) + + if err != nil { + t.Fatal(err) + } + + err = kpmcli.Add( + WithAddKclPkg(kmod), + WithAddSourceUrl("../h14"), + ) + + if err != nil { + t.Fatal(err) + } + + err = kpmcli.Add( + WithAddKclPkg(kmod), + WithAddSourceUrl("../h12"), + ) + + if err != nil { + t.Fatal(err) + } + + expectedMod, err := os.ReadFile(modExpect) + if err != nil { + t.Fatal(err) + } + gotMod, err := os.ReadFile(modPath) + if err != nil { + t.Fatal(err) + } + + expectedLock, err := os.ReadFile(lockExpect) + if err != nil { + t.Fatal(err) + } + + gotLock, err := os.ReadFile(lockPath) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod))) + assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock))) + + } + + features.Enable(features.SupportMVS) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddWithMvs", TestFunc: testFunc}}) + fmt.Print("TestAddWithMvs done\n") + features.Disable(features.SupportMVS) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddWithoutMvs", TestFunc: testFunc}}) + fmt.Print("TestAddWithoutMvs done\n") +} diff --git a/pkg/client/deperated.go b/pkg/client/deperated.go index ae4a7467..cceb3f2f 100644 --- a/pkg/client/deperated.go +++ b/pkg/client/deperated.go @@ -432,7 +432,7 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error { return err } - if ok, err := features.Enabled(features.SupportMVS); err != nil && ok { + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { _, err = c.Update( WithUpdatedKclPkg(kclPkg), WithOffline(false), @@ -531,7 +531,7 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk kclPkg.Dependencies.Deps.Delete(d.Name) } - if ok, err := features.Enabled(features.SupportMVS); err != nil && ok { + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { // After adding the new dependency, // Iterate through all the dependencies and select the version by mvs _, err = c.Update( diff --git a/pkg/client/test_data/add_with_mvs/h12/kcl.mod b/pkg/client/test_data/add_with_mvs/h12/kcl.mod new file mode 100644 index 00000000..79817718 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h12/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "h12" +edition = "v0.11.0-alpha.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/add_with_mvs/h12/kcl.mod.lock b/pkg/client/test_data/add_with_mvs/h12/kcl.mod.lock new file mode 100644 index 00000000..7eaf9cba --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h12/kcl.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" + sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ=" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.2" diff --git a/pkg/client/test_data/add_with_mvs/h12/main.k b/pkg/client/test_data/add_with_mvs/h12/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h12/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_mvs/h14/kcl.mod b/pkg/client/test_data/add_with_mvs/h14/kcl.mod new file mode 100644 index 00000000..8533c72c --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h14/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "h14" +edition = "v0.11.0-alpha.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.4" diff --git a/pkg/client/test_data/add_with_mvs/h14/kcl.mod.lock b/pkg/client/test_data/add_with_mvs/h14/kcl.mod.lock new file mode 100644 index 00000000..52056478 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h14/kcl.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.4" + version = "0.1.4" + sum = "9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U=" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.4" diff --git a/pkg/client/test_data/add_with_mvs/h14/main.k b/pkg/client/test_data/add_with_mvs/h14/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/h14/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.bk b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.bk new file mode 100644 index 00000000..5674b432 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.bk @@ -0,0 +1,4 @@ +[package] +name = "pkg_with_mvs" +edition = "v0.11.0-alpha.1" +version = "0.0.1" diff --git a/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.expect b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.expect new file mode 100644 index 00000000..44b573dd --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.expect @@ -0,0 +1,8 @@ +[package] +name = "pkg_with_mvs" +edition = "v0.11.0-alpha.1" +version = "0.0.1" + +[dependencies] +h14 = { path = "../h14", version = "0.0.1" } +h12 = { path = "../h12", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.bk b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.bk new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.expect b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.expect new file mode 100644 index 00000000..7001b2ac --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.expect @@ -0,0 +1,17 @@ +[dependencies] + [dependencies.h12] + name = "h12" + full_name = "h12_0.0.1" + version = "0.0.1" + [dependencies.h14] + name = "h14" + full_name = "h14_0.0.1" + version = "0.0.1" + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.4" + version = "0.1.4" + sum = "9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U=" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.4" diff --git a/pkg/client/test_data/add_with_mvs/pkg_with_mvs/main.k b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_with_mvs/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.bk b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.bk new file mode 100644 index 00000000..034ba84a --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.bk @@ -0,0 +1,4 @@ +[package] +name = "pkg_without_mvs" +edition = "v0.11.0-alpha.1" +version = "0.0.1" diff --git a/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.expect b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.expect new file mode 100644 index 00000000..6a9b06e3 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.expect @@ -0,0 +1,8 @@ +[package] +name = "pkg_without_mvs" +edition = "v0.11.0-alpha.1" +version = "0.0.1" + +[dependencies] +h14 = { path = "../h14", version = "0.0.1" } +h12 = { path = "../h12", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.bk b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.bk new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.expect b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.expect new file mode 100644 index 00000000..35429433 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.expect @@ -0,0 +1,17 @@ +[dependencies] + [dependencies.h12] + name = "h12" + full_name = "h12_0.0.1" + version = "0.0.1" + [dependencies.h14] + name = "h14" + full_name = "h14_0.0.1" + version = "0.0.1" + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" + sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ=" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.2" diff --git a/pkg/client/test_data/add_with_mvs/pkg_without_mvs/main.k b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_mvs/pkg_without_mvs/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/update.go b/pkg/client/update.go index 1bc12d69..1b1cb52b 100644 --- a/pkg/client/update.go +++ b/pkg/client/update.go @@ -106,10 +106,12 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { selectedModDep := dep // Check if the dependency exists in the mod file. if existDep, exist := modDeps.Get(dep.Name); exist { - // if the dependency exists in the mod file, - // check the version and select the greater one. - if less, err := dep.VersionLessThan(&existDep); less && err == nil { - selectedModDep = &existDep + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { + // if the dependency exists in the mod file, + // check the version and select the greater one. + if less, err := dep.VersionLessThan(&existDep); less && err == nil { + selectedModDep = &existDep + } } // if the dependency does not exist in the mod file, // the dependency is a indirect dependency. @@ -120,10 +122,12 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { selectedDep := dep // Check if the dependency exists in the lock file. if existDep, exist := lockDeps.Get(dep.Name); exist { - // If the dependency exists in the lock file, - // check the version and select the greater one. - if less, err := dep.VersionLessThan(&existDep); less && err == nil { - selectedDep = &existDep + if ok, err := features.Enabled(features.SupportMVS); err == nil && ok { + // If the dependency exists in the lock file, + // check the version and select the greater one. + if less, err := dep.VersionLessThan(&existDep); less && err == nil { + selectedDep = &existDep + } } } selectedDep.LocalFullPath = dep.LocalFullPath