Skip to content

Commit 47dddb0

Browse files
authored
fix: fix missing feature flag for mvs (#571)
Signed-off-by: zongz <zongzhe1024@163.com>
1 parent 54737ca commit 47dddb0

File tree

20 files changed

+227
-13
lines changed

20 files changed

+227
-13
lines changed

pkg/client/add.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66

77
"kcl-lang.io/kpm/pkg/downloader"
8+
"kcl-lang.io/kpm/pkg/features"
89
pkg "kcl-lang.io/kpm/pkg/package"
910
"kcl-lang.io/kpm/pkg/reporter"
1011
"kcl-lang.io/kpm/pkg/utils"
@@ -206,12 +207,18 @@ func (c *KpmClient) Add(options ...AddOption) error {
206207
})
207208
}
208209

209-
// Add the dependency to the kcl.mod file.
210-
if modExistDep, ok := addedPkg.ModFile.Dependencies.Deps.Get(dep.Name); ok {
211-
if less, err := modExistDep.VersionLessThan(&dep); less && err == nil {
210+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
211+
// Add the dependency to the kcl.mod file.
212+
// and select the greater version of the dependency in dependencies graph.
213+
if modExistDep, ok := addedPkg.ModFile.Dependencies.Deps.Get(dep.Name); ok {
214+
if less, err := modExistDep.VersionLessThan(&dep); less && err == nil {
215+
addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep)
216+
}
217+
} else {
212218
addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep)
213219
}
214220
} else {
221+
// Add the dependency to the kcl.mod file directly.
215222
addedPkg.ModFile.Dependencies.Deps.Set(dep.Name, dep)
216223
}
217224
succeedMsgInfo = fmt.Sprintf("add dependency '%s:%s' successfully", depPkg.ModFile.Pkg.Name, depPkg.ModFile.Pkg.Version)

pkg/client/add_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package client
22

33
import (
44
"bytes"
5+
"fmt"
56
"os"
67
"path/filepath"
78
"testing"
89

910
"github.com/otiai10/copy"
1011
"github.com/stretchr/testify/assert"
1112
"kcl-lang.io/kpm/pkg/downloader"
13+
"kcl-lang.io/kpm/pkg/features"
1214
pkg "kcl-lang.io/kpm/pkg/package"
1315
"kcl-lang.io/kpm/pkg/utils"
1416
)
@@ -497,3 +499,110 @@ func TestAddRenameWithNoSpec(t *testing.T) {
497499
assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod)))
498500
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
499501
}
502+
503+
func TestAddWithMvs(t *testing.T) {
504+
defer features.Disable(features.SupportMVS)
505+
testDir := getTestDir("add_with_mvs")
506+
pkgWithMvsPath := filepath.Join(testDir, "pkg_with_mvs")
507+
pkgWithoutMvsPath := filepath.Join(testDir, "pkg_without_mvs")
508+
509+
testFunc := func(t *testing.T, kpmcli *KpmClient) {
510+
var modbkPath, modPath, modExpect, lockbkPath, lockPath, lockExpect, pkgPath string
511+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
512+
pkgPath = pkgWithMvsPath
513+
modbkPath = filepath.Join(pkgWithMvsPath, "kcl.mod.bk")
514+
modPath = filepath.Join(pkgWithMvsPath, "kcl.mod")
515+
modExpect = filepath.Join(pkgWithMvsPath, "kcl.mod.expect")
516+
lockbkPath = filepath.Join(pkgWithMvsPath, "kcl.mod.lock.bk")
517+
lockPath = filepath.Join(pkgWithMvsPath, "kcl.mod.lock")
518+
lockExpect = filepath.Join(pkgWithMvsPath, "kcl.mod.lock.expect")
519+
} else {
520+
pkgPath = pkgWithoutMvsPath
521+
modbkPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.bk")
522+
modPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod")
523+
modExpect = filepath.Join(pkgWithoutMvsPath, "kcl.mod.expect")
524+
lockbkPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock.bk")
525+
lockPath = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock")
526+
lockExpect = filepath.Join(pkgWithoutMvsPath, "kcl.mod.lock.expect")
527+
}
528+
529+
err := copy.Copy(modbkPath, modPath)
530+
if err != nil {
531+
t.Fatal(err)
532+
}
533+
534+
err = copy.Copy(lockbkPath, lockPath)
535+
if err != nil {
536+
t.Fatal(err)
537+
}
538+
539+
defer func() {
540+
// remove the copied files
541+
err := os.RemoveAll(modPath)
542+
if err != nil {
543+
t.Fatal(err)
544+
}
545+
err = os.RemoveAll(lockPath)
546+
if err != nil {
547+
t.Fatal(err)
548+
}
549+
}()
550+
551+
kmod, err := pkg.LoadKclPkgWithOpts(
552+
pkg.WithPath(pkgPath),
553+
pkg.WithSettings(kpmcli.GetSettings()),
554+
)
555+
556+
if err != nil {
557+
t.Fatal(err)
558+
}
559+
560+
err = kpmcli.Add(
561+
WithAddKclPkg(kmod),
562+
WithAddSourceUrl("../h14"),
563+
)
564+
565+
if err != nil {
566+
t.Fatal(err)
567+
}
568+
569+
err = kpmcli.Add(
570+
WithAddKclPkg(kmod),
571+
WithAddSourceUrl("../h12"),
572+
)
573+
574+
if err != nil {
575+
t.Fatal(err)
576+
}
577+
578+
expectedMod, err := os.ReadFile(modExpect)
579+
if err != nil {
580+
t.Fatal(err)
581+
}
582+
gotMod, err := os.ReadFile(modPath)
583+
if err != nil {
584+
t.Fatal(err)
585+
}
586+
587+
expectedLock, err := os.ReadFile(lockExpect)
588+
if err != nil {
589+
t.Fatal(err)
590+
}
591+
592+
gotLock, err := os.ReadFile(lockPath)
593+
if err != nil {
594+
t.Fatal(err)
595+
}
596+
597+
assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod)))
598+
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
599+
600+
}
601+
602+
features.Enable(features.SupportMVS)
603+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddWithMvs", TestFunc: testFunc}})
604+
fmt.Print("TestAddWithMvs done\n")
605+
features.Disable(features.SupportMVS)
606+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddWithoutMvs", TestFunc: testFunc}})
607+
fmt.Print("TestAddWithoutMvs done\n")
608+
}

pkg/client/deperated.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error {
432432
return err
433433
}
434434

435-
if ok, err := features.Enabled(features.SupportMVS); err != nil && ok {
435+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
436436
_, err = c.Update(
437437
WithUpdatedKclPkg(kclPkg),
438438
WithOffline(false),
@@ -531,7 +531,7 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
531531
kclPkg.Dependencies.Deps.Delete(d.Name)
532532
}
533533

534-
if ok, err := features.Enabled(features.SupportMVS); err != nil && ok {
534+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
535535
// After adding the new dependency,
536536
// Iterate through all the dependencies and select the version by mvs
537537
_, err = c.Update(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "h12"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.2"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[dependencies]
2+
[dependencies.helloworld]
3+
name = "helloworld"
4+
full_name = "helloworld_0.1.2"
5+
version = "0.1.2"
6+
sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ="
7+
reg = "ghcr.io"
8+
repo = "kcl-lang/helloworld"
9+
oci_tag = "0.1.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "h14"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = "0.1.4"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[dependencies]
2+
[dependencies.helloworld]
3+
name = "helloworld"
4+
full_name = "helloworld_0.1.4"
5+
version = "0.1.4"
6+
sum = "9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U="
7+
reg = "ghcr.io"
8+
repo = "kcl-lang/helloworld"
9+
oci_tag = "0.1.4"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "pkg_with_mvs"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "pkg_with_mvs"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
h14 = { path = "../h14", version = "0.0.1" }
8+
h12 = { path = "../h12", version = "0.0.1" }

pkg/client/test_data/add_with_mvs/pkg_with_mvs/kcl.mod.lock.bk

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[dependencies]
2+
[dependencies.h12]
3+
name = "h12"
4+
full_name = "h12_0.0.1"
5+
version = "0.0.1"
6+
[dependencies.h14]
7+
name = "h14"
8+
full_name = "h14_0.0.1"
9+
version = "0.0.1"
10+
[dependencies.helloworld]
11+
name = "helloworld"
12+
full_name = "helloworld_0.1.4"
13+
version = "0.1.4"
14+
sum = "9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U="
15+
reg = "ghcr.io"
16+
repo = "kcl-lang/helloworld"
17+
oci_tag = "0.1.4"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "pkg_without_mvs"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "pkg_without_mvs"
3+
edition = "v0.11.0-alpha.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
h14 = { path = "../h14", version = "0.0.1" }
8+
h12 = { path = "../h12", version = "0.0.1" }

pkg/client/test_data/add_with_mvs/pkg_without_mvs/kcl.mod.lock.bk

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[dependencies]
2+
[dependencies.h12]
3+
name = "h12"
4+
full_name = "h12_0.0.1"
5+
version = "0.0.1"
6+
[dependencies.h14]
7+
name = "h14"
8+
full_name = "h14_0.0.1"
9+
version = "0.0.1"
10+
[dependencies.helloworld]
11+
name = "helloworld"
12+
full_name = "helloworld_0.1.2"
13+
version = "0.1.2"
14+
sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ="
15+
reg = "ghcr.io"
16+
repo = "kcl-lang/helloworld"
17+
oci_tag = "0.1.2"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = 'Hello World!'

pkg/client/update.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
106106
selectedModDep := dep
107107
// Check if the dependency exists in the mod file.
108108
if existDep, exist := modDeps.Get(dep.Name); exist {
109-
// if the dependency exists in the mod file,
110-
// check the version and select the greater one.
111-
if less, err := dep.VersionLessThan(&existDep); less && err == nil {
112-
selectedModDep = &existDep
109+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
110+
// if the dependency exists in the mod file,
111+
// check the version and select the greater one.
112+
if less, err := dep.VersionLessThan(&existDep); less && err == nil {
113+
selectedModDep = &existDep
114+
}
113115
}
114116
// if the dependency does not exist in the mod file,
115117
// the dependency is a indirect dependency.
@@ -120,10 +122,12 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
120122
selectedDep := dep
121123
// Check if the dependency exists in the lock file.
122124
if existDep, exist := lockDeps.Get(dep.Name); exist {
123-
// If the dependency exists in the lock file,
124-
// check the version and select the greater one.
125-
if less, err := dep.VersionLessThan(&existDep); less && err == nil {
126-
selectedDep = &existDep
125+
if ok, err := features.Enabled(features.SupportMVS); err == nil && ok {
126+
// If the dependency exists in the lock file,
127+
// check the version and select the greater one.
128+
if less, err := dep.VersionLessThan(&existDep); less && err == nil {
129+
selectedDep = &existDep
130+
}
127131
}
128132
}
129133
selectedDep.LocalFullPath = dep.LocalFullPath

0 commit comments

Comments
 (0)