Skip to content

Commit

Permalink
refactor: the same traversing tripartite dependency logics are moved … (
Browse files Browse the repository at this point in the history
#545)

* refactor: the same traversing tripartite dependency logics are moved into the resolver

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Nov 15, 2024
1 parent d591035 commit ad777e1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 184 deletions.
35 changes: 8 additions & 27 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,18 @@ import (
)

func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestUpdateWithKclMod", testUpdateWithKclMod)
test.RunTestWithGlobalLock(t, "TestUpdateWithKclModlock", testUpdateWithKclModlock)
test.RunTestWithGlobalLock(t, "TestUpdateWithNoSumCheck", testUpdateWithNoSumCheck)
test.RunTestWithGlobalLock(t, "TestAddWithDiffVersionNoSumCheck", testAddWithDiffVersionNoSumCheck)
test.RunTestWithGlobalLock(t, "TestAddWithDiffVersionWithSumCheck", testAddWithDiffVersionWithSumCheck)
test.RunTestWithGlobalLock(t, "TestDownloadOci", testDownloadOci)
test.RunTestWithGlobalLock(t, "TestAddWithOciDownloader", testAddWithOciDownloader)
test.RunTestWithGlobalLock(t, "TestAddDefaultRegistryDep", testAddDefaultRegistryDep)
test.RunTestWithGlobalLock(t, "TestUpdateDefaultRegistryDep", testUpdateDefaultRegistryDep)
test.RunTestWithGlobalLock(t, "TestAddWithNoSumCheck", testAddWithNoSumCheck)
test.RunTestWithGlobalLock(t, "TestAddWithGitCommit", testAddWithGitCommit)
test.RunTestWithGlobalLock(t, "TestDependenciesOrder", testDependenciesOrder)
test.RunTestWithGlobalLock(t, "TestPkgWithInVendorMode", testPkgWithInVendorMode)
test.RunTestWithGlobalLock(t, "TestResolveMetadataInJsonStrWithPackage", testResolveMetadataInJsonStrWithPackage)
test.RunTestWithGlobalLock(t, "TestResolveMetadataInJsonStr", testResolveMetadataInJsonStr)
test.RunTestWithGlobalLock(t, "testPackageCurrentPkgPath", testPackageCurrentPkgPath)
test.RunTestWithGlobalLock(t, "TestUpdateKclModAndLock", testUpdateKclModAndLock)
test.RunTestWithGlobalLock(t, "TestResolveDepsWithOnlyKclMod", testResolveDepsWithOnlyKclMod)
test.RunTestWithGlobalLock(t, "TestResolveDepsVendorMode", testResolveDepsVendorMode)
test.RunTestWithGlobalLock(t, "TestCompileWithEntryFile", testCompileWithEntryFile)
Expand Down Expand Up @@ -436,17 +431,14 @@ func TestInitEmptyPkg(t *testing.T) {
assert.Equal(t, testKclPkg.ModFile.Pkg.Edition, runner.GetKclVersion())
}

func testUpdateKclModAndLock(t *testing.T) {
func testUpdateKclModAndLock(t *testing.T, kpmcli *KpmClient) {
testDir := initTestDir("test_data_add_deps")
// Init an empty package
kclPkg := pkg.NewKclPkg(&opt.InitOptions{
Name: "test_add_deps",
InitPath: testDir,
})

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
err = kpmcli.InitEmptyPkg(&kclPkg)
err := kpmcli.InitEmptyPkg(&kclPkg)
assert.Equal(t, err, nil)

dep := pkg.Dependency{
Expand Down Expand Up @@ -1060,14 +1052,11 @@ func TestGetReleasesFromSource(t *testing.T) {
assert.Equal(t, releasesVersions[:5], []string{"1.14", "1.14.1", "1.15", "1.15.1", "1.16"})
}

func testUpdateWithKclMod(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

func testUpdateWithKclMod(t *testing.T, kpmcli *KpmClient) {
testDir := getTestDir("test_update")
src_testDir := filepath.Join(testDir, "test_update_kcl_mod")
dest_testDir := filepath.Join(testDir, "test_update_kcl_mod_tmp")
err = copy.Copy(src_testDir, dest_testDir)
err := copy.Copy(src_testDir, dest_testDir)
assert.Equal(t, err, nil)

kclPkg, err := kpmcli.LoadPkgFromPath(dest_testDir)
Expand All @@ -1092,14 +1081,11 @@ func testUpdateWithKclMod(t *testing.T) {
}()
}

func testUpdateWithKclModlock(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

func testUpdateWithKclModlock(t *testing.T, kpmcli *KpmClient) {
testDir := getTestDir("test_update")
src_testDir := filepath.Join(testDir, "test_update_kcl_mod_lock")
dest_testDir := filepath.Join(testDir, "test_update_kcl_mod_lock_tmp")
err = copy.Copy(src_testDir, dest_testDir)
err := copy.Copy(src_testDir, dest_testDir)
assert.Equal(t, err, nil)

kclPkg, err := pkg.LoadKclPkg(dest_testDir)
Expand Down Expand Up @@ -1197,10 +1183,8 @@ func testAddWithNoSumCheck(t *testing.T) {
}()
}

func testUpdateWithNoSumCheck(t *testing.T) {
func testUpdateWithNoSumCheck(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("test_update_no_sum_check")
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)
Expand Down Expand Up @@ -1695,7 +1679,7 @@ func verifyFileContent(t *testing.T, filePath, expectPath string) {
assert.Equal(t, contentStr, expectContentStr)
}

func testUpdateDefaultRegistryDep(t *testing.T) {
func testUpdateDefaultRegistryDep(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("update_with_default_dep")

pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
Expand All @@ -1711,9 +1695,6 @@ func testUpdateDefaultRegistryDep(t *testing.T) {
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)

Expand Down
38 changes: 6 additions & 32 deletions pkg/client/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package client

import (
"fmt"
"path/filepath"

"github.com/dominikbraun/graph"
"golang.org/x/mod/module"
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/resolver"
)
Expand Down Expand Up @@ -156,37 +154,13 @@ func (c *KpmClient) Graph(opts ...GraphOption) (*DepGraph, error) {
}
depResolver.ResolveFuncs = append(depResolver.ResolveFuncs, resolverFunc)

for _, depName := range modDeps.Keys() {
dep, ok := modDeps.Get(depName)
if !ok {
return nil, fmt.Errorf("failed to get dependency %s", depName)
}

// Check if the dependency is a local path and it is not an absolute path.
// If it is not an absolute path, transform the path to an absolute path.
var depSource *downloader.Source
if dep.Source.IsLocalPath() && !filepath.IsAbs(dep.Source.Local.Path) {
depSource = &downloader.Source{
Local: &downloader.Local{
Path: filepath.Join(kMod.HomePath, dep.Source.Local.Path),
},
}
} else {
depSource = &dep.Source
}
err := depResolver.Resolve(
resolver.WithEnableCache(true),
resolver.WithResolveKclMod(kMod),
)

err := resolverFunc(&dep, kMod)
if err != nil {
return nil, err
}

err = depResolver.Resolve(
resolver.WithEnableCache(true),
resolver.WithSource(depSource),
)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

return dGraph, nil
Expand Down
69 changes: 22 additions & 47 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package client

import (
"fmt"
"path/filepath"

"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/resolver"
)
Expand Down Expand Up @@ -34,16 +32,16 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
}
}

kpkg := opts.kpkg
if kpkg == nil {
kMod := opts.kpkg
if kMod == nil {
return nil, fmt.Errorf("kcl package is nil")
}

modDeps := kpkg.ModFile.Dependencies.Deps
modDeps := kMod.ModFile.Dependencies.Deps
if modDeps == nil {
return nil, fmt.Errorf("kcl.mod dependencies is nil")
}
lockDeps := kpkg.Dependencies.Deps
lockDeps := kMod.Dependencies.Deps
if lockDeps == nil {
return nil, fmt.Errorf("kcl.mod.lock dependencies is nil")
}
Expand All @@ -58,72 +56,49 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
}
// ResolveFunc is the function for resolving each dependency when traversing the dependency graph.
resolverFunc := func(dep *pkg.Dependency, parentPkg *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 := existDep.VersionLessThan(dep); less && err == nil {
kpkg.ModFile.Dependencies.Deps.Set(dep.Name, *dep)
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.
// it will be added to the kcl.mod.lock file not the kcl.mod file.
kMod.ModFile.Dependencies.Deps.Set(dep.Name, *selectedModDep)
}

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 := existDep.VersionLessThan(dep); less && err == nil {
kpkg.Dependencies.Deps.Set(dep.Name, *dep)
if less, err := dep.VersionLessThan(&existDep); less && err == nil {
selectedDep = &existDep
}
} else {
// if the dependency does not exist in the lock file,
// the dependency is a new dependency and will be added to the lock file.
kpkg.Dependencies.Deps.Set(dep.Name, *dep)
}
selectedDep.LocalFullPath = dep.LocalFullPath
kMod.Dependencies.Deps.Set(dep.Name, *selectedDep)

return nil
}
depResolver.ResolveFuncs = append(depResolver.ResolveFuncs, resolverFunc)

// Iterate all the dependencies of the package in kcl.mod and resolve each dependency.
for _, depName := range modDeps.Keys() {
dep, ok := modDeps.Get(depName)
if !ok {
return nil, fmt.Errorf("failed to get dependency %s", depName)
}

// Check if the dependency is a local path and it is not an absolute path.
// If it is not an absolute path, transform the path to an absolute path.
var depSource *downloader.Source
if dep.Source.IsLocalPath() && !filepath.IsAbs(dep.Source.Local.Path) {
depSource = &downloader.Source{
Local: &downloader.Local{
Path: filepath.Join(kpkg.HomePath, dep.Source.Local.Path),
},
}
} else {
depSource = &dep.Source
}

err := resolverFunc(&dep, kpkg)
if err != nil {
return nil, err
}
err := depResolver.Resolve(
resolver.WithResolveKclMod(kMod),
resolver.WithEnableCache(true),
)

err = depResolver.Resolve(
resolver.WithEnableCache(true),
resolver.WithSource(depSource),
)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}

err := kpkg.UpdateModAndLockFile()
err = kMod.UpdateModAndLockFile()
if err != nil {
return nil, err
}

return kpkg, nil
return kMod, nil
}
13 changes: 9 additions & 4 deletions pkg/client/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import (
)

func TestUpdate(t *testing.T) {
RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclMod", testUpdateWithKclMod)
RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclModlock", testUpdateWithKclModlock)
RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithNoSumCheck", testUpdateWithNoSumCheck)
RunTestWithGlobalLockAndKpmCli(t, "TestUpdateDefaultRegistryDep", testUpdateDefaultRegistryDep)
RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclModAndLock", testUpdateKclModAndLock)
RunTestWithGlobalLockAndKpmCli(t, "TestUpdate", testUpdate)
}

func testUpdate(t *testing.T, kpmcli *KpmClient) {
features.Enable(features.SupportMVS)
testDir := getTestDir("test_update_with_mvs")
kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}

updates := []struct {
name string
Expand Down
Loading

0 comments on commit ad777e1

Please sign in to comment.