Skip to content

Commit

Permalink
Merge branch 'kcl-lang:main' into feature-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ravjot07 authored Nov 17, 2024
2 parents c33f9b1 + 840dc89 commit 1bafde3
Show file tree
Hide file tree
Showing 29 changed files with 275 additions and 274 deletions.
74 changes: 16 additions & 58 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,30 @@ 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, "TestRunWithOciDownloader", testRunWithOciDownloader)
test.RunTestWithGlobalLock(t, "TestAddWithOciDownloader", testAddWithOciDownloader)
test.RunTestWithGlobalLock(t, "TestAddDefaultRegistryDep", testAddDefaultRegistryDep)
test.RunTestWithGlobalLock(t, "TestUpdateDefaultRegistryDep", testUpdateDefaultRegistryDep)
test.RunTestWithGlobalLock(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep)
test.RunTestWithGlobalLock(t, "TestAddWithNoSumCheck", testAddWithNoSumCheck)
test.RunTestWithGlobalLock(t, "TestAddWithGitCommit", testAddWithGitCommit)
test.RunTestWithGlobalLock(t, "TestDependenciesOrder", testDependenciesOrder)
test.RunTestWithGlobalLock(t, "TestRunInVendor", testRunInVendor)
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)
test.RunTestWithGlobalLock(t, "TestDownloadLatestOci", testDownloadLatestOci)
test.RunTestWithGlobalLock(t, "TestDownloadGitWithPackage", testDownloadGitWithPackage)
test.RunTestWithGlobalLock(t, "TestModandLockFilesWithGitPackageDownload", testModandLockFilesWithGitPackageDownload)
test.RunTestWithGlobalLock(t, "TestDependencyGraph", testDependencyGraph)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
test.RunTestWithGlobalLock(t, "TestRunWithNoSumCheck", testRunWithGitPackage)
test.RunTestWithGlobalLock(t, "TestRunGit", testRunGit)
test.RunTestWithGlobalLock(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
test.RunTestWithGlobalLock(t, "TestVendorWithGlobalLock", testVendorWithGlobalLock)
test.RunTestWithGlobalLock(t, "TestPull", testPull)
test.RunTestWithGlobalLock(t, "TestPullWithInsecureSkipTLSverify", testPullWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
test.RunTestWithGlobalLock(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
test.RunTestWithGlobalLock(t, "TestGraph", testGraph)
}

Expand Down Expand Up @@ -445,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 @@ -1069,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 @@ -1101,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 @@ -1206,10 +1183,11 @@ 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)
defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()

var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)
Expand All @@ -1226,15 +1204,12 @@ func testUpdateWithNoSumCheck(t *testing.T) {
kpmcli.SetNoSumCheck(false)
kclPkg, err = kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)
kclPkg.NoSumCheck = false

err = kpmcli.UpdateDeps(kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
assert.Equal(t, buf.String(), "")

defer func() {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}

func testAddWithDiffVersionNoSumCheck(t *testing.T) {
Expand Down Expand Up @@ -1704,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 @@ -1720,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 Expand Up @@ -1757,7 +1729,7 @@ func testUpdateDefaultRegistryDep(t *testing.T) {
}()
}

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

pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak")
Expand All @@ -1773,9 +1745,6 @@ func testRunDefaultRegistryDep(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 Expand Up @@ -1996,10 +1965,7 @@ func TestRunLocalWithArgs(t *testing.T) {
}
}

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

func testRunRemoteWithArgsInvalid(t *testing.T, kpmcli *KpmClient) {
logbuf := new(bytes.Buffer)
kpmcli.SetLogWriter(logbuf)

Expand All @@ -2025,10 +1991,8 @@ func testRunRemoteWithArgsInvalid(t *testing.T) {
}
}

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

logbuf := new(bytes.Buffer)
kpmcli.SetLogWriter(logbuf)
Expand Down Expand Up @@ -2091,13 +2055,7 @@ func testRunRemoteWithArgs(t *testing.T) {
}
}

func testRunInVendor(t *testing.T) {
// Create a new kpm client.
kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}

func testRunInVendor(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("test_run_in_vendor")
workdir := filepath.Join(pkgPath, "pkg")

Expand Down
28 changes: 7 additions & 21 deletions pkg/client/deperated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ import (
"kcl-lang.io/kpm/pkg/utils"
)

func TestRunWithNoSumCheck(t *testing.T) {
func testRunWithNoSumCheck(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("test_run_no_sum_check")

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

opts := opt.DefaultCompileOptions()
opts.SetNoSumCheck(true)
opts.SetPkgPath(pkgPath)

_, err = kpmcli.CompileWithOpts(opts)
_, err := kpmcli.CompileWithOpts(opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)

Expand All @@ -42,12 +39,9 @@ func TestRunWithNoSumCheck(t *testing.T) {
}()
}

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

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

opts := opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)

Expand All @@ -63,11 +57,8 @@ func testRunWithGitPackage(t *testing.T) {
}()
}

func testRunWithOciDownloader(t *testing.T) {
kpmCli, err := NewKpmClient()
func testRunWithOciDownloader(t *testing.T, kpmCli *KpmClient) {
path := getTestDir("test_oci_downloader")
assert.Equal(t, err, nil)

kpmCli.DepDownloader = downloader.NewOciDownloader("linux/amd64")

var buf bytes.Buffer
Expand All @@ -84,10 +75,7 @@ func testRunWithOciDownloader(t *testing.T) {
assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!")
}

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

func testRunGit(t *testing.T, kpmcli *KpmClient) {
testPath := getTestDir("test_run_git")

opts := opt.DefaultCompileOptions()
Expand All @@ -107,15 +95,13 @@ func testRunGit(t *testing.T) {
assert.Equal(t, utils.RmNewline(string(bytes)), utils.RmNewline(string(resultStr)))
}

func testRunOciWithSettingsFile(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
func testRunOciWithSettingsFile(t *testing.T, kpmcli *KpmClient) {
kpmcli.SetLogWriter(nil)
opts := opt.DefaultCompileOptions()
opts.SetEntries([]string{})
opts.Merge(kcl.WithSettings(filepath.Join(".", "test_data", "test_run_oci_with_settings", "kcl.yaml")))
opts.SetHasSettingsYaml(true)
_, err = kpmcli.CompileOciPkg("oci://ghcr.io/kcl-lang/helloworld", "", opts)
_, err := kpmcli.CompileOciPkg("oci://ghcr.io/kcl-lang/helloworld", "", opts)
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
34 changes: 28 additions & 6 deletions pkg/client/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"github.com/otiai10/copy"
"gotest.tools/v3/assert"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/utils"
)

func testRunWithModSpecVersion(t *testing.T) {
func testRunWithModSpecVersion(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("test_run_with_modspec_version")
modbkPath := filepath.Join(pkgPath, "kcl.mod.bk")
modPath := filepath.Join(pkgPath, "kcl.mod")
Expand Down Expand Up @@ -41,11 +42,6 @@ func testRunWithModSpecVersion(t *testing.T) {
}
}()

kpmcli, err := NewKpmClient()
if err != nil {
t.Errorf("Failed to create kpm client: %v", err)
}

res, err := kpmcli.Run(
WithRunSource(
&downloader.Source{
Expand Down Expand Up @@ -83,3 +79,29 @@ func testRunWithModSpecVersion(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 TestRun(t *testing.T) {
features.Enable(features.SupportNewStorage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader)
RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep)
RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithGitPackage", testRunWithGitPackage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunGit", testRunGit)
RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)

features.Disable(features.SupportNewStorage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader)
RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep)
RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithGitPackage", testRunWithGitPackage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunGit", testRunGit)
RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
}
Loading

0 comments on commit 1bafde3

Please sign in to comment.