Skip to content

Commit

Permalink
fix: fix missing feature flag for mvs (#571)
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Dec 19, 2024
1 parent 54737ca commit 47dddb0
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 13 deletions.
13 changes: 10 additions & 3 deletions pkg/client/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
109 changes: 109 additions & 0 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package client

import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"

"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"
)
Expand Down Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions pkg/client/deperated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_mvs/h12/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "h12"
edition = "v0.11.0-alpha.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.2"
9 changes: 9 additions & 0 deletions pkg/client/test_data/add_with_mvs/h12/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mvs/h12/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_mvs/h14/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "h14"
edition = "v0.11.0-alpha.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.4"
9 changes: 9 additions & 0 deletions pkg/client/test_data/add_with_mvs/h14/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mvs/h14/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "pkg_with_mvs"
edition = "v0.11.0-alpha.1"
version = "0.0.1"
8 changes: 8 additions & 0 deletions pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.expect
Original file line number Diff line number Diff line change
@@ -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" }
Empty file.
17 changes: 17 additions & 0 deletions pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.expect
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mvs/pkg_with_mvs/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "pkg_without_mvs"
edition = "v0.11.0-alpha.1"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -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" }
Empty file.
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mvs/pkg_without_mvs/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
20 changes: 12 additions & 8 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 47dddb0

Please sign in to comment.