Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] feat: make 'kpm run' supports mvs #478

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func TestPackageApi(t *testing.T) {
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27")
assert.Equal(t, dep.Version, "1.27")
assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Oci.Tag, "1.27")
assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27")

assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27"))

Expand Down
1 change: 1 addition & 0 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList
}

kclPkg.SetVendorMode(opts.IsVendor())
kclPkg.NoSumCheck = opts.NoSumCheck()

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/kcl.mod.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
name = "k8s"
full_name = "k8s_1.27"
version = "1.27"
sum = "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM="
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.27"
27 changes: 10 additions & 17 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,19 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen
// Since redownloads are not triggered if local dependencies exists,
// indirect dependencies are also synchronized to the lock file by `lockDeps`.
func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) error {
if kclPkg.IsVendorMode() {
// In the vendor mode, the search path is the vendor subdirectory of the current package.
err := c.VendorDeps(kclPkg)
if err != nil {
return err
}
} else {
// In the non-vendor mode, the search path is the KCL_PKG_PATH.
err := c.resolvePkgDeps(kclPkg, &kclPkg.Dependencies, update)
if err != nil {
return err
}

_, err := c.Update(
WithUpdatedKclPkg(kclPkg),
WithUpdateEnableVendor(kclPkg.IsVendorMode()),
WithUpdateVendorPath(kclPkg.LocalVendorPath()),
WithUpdateOffline(!update),
)
if err != nil {
return err
}
return nil
}

// Deprecated: Use `KpmClient.Update()` instead.
func (c *KpmClient) resolvePkgDeps(kclPkg *pkg.KclPkg, lockDeps *pkg.Dependencies, update bool) error {
var searchPath string
kclPkg.NoSumCheck = c.noSumCheck
Expand Down Expand Up @@ -473,10 +469,6 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error {
return err
}

_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
)

return err
}

Expand Down Expand Up @@ -538,6 +530,7 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
}

kclPkg.SetVendorMode(opts.IsVendor())
kclPkg.NoSumCheck = opts.NoSumCheck()

globalPkgPath, err := env.GetAbsPkgPath()
if err != nil {
Expand Down
97 changes: 78 additions & 19 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,27 @@ func TestVendorDeps(t *testing.T) {
FullName: "kcl1",
Version: "0.0.1",
Sum: kcl1Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl1",
Tag: "0.0.1",
},
},
}

depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2",
Version: "0.0.1",
Sum: kcl2Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl2",
Tag: "0.0.1",
},
},
}

mppTest := orderedmap.NewOrderedMap[string, pkg.Dependency]()
Expand Down Expand Up @@ -553,6 +567,7 @@ func TestVendorDeps(t *testing.T) {
assert.Equal(t, utils.DirExists(filepath.Join(mykclVendorPath, "kcl1_0.0.1")), true)
assert.Equal(t, utils.DirExists(filepath.Join(mykclVendorPath, "kcl2_0.0.1")), true)

kclPkg.SetVendorMode(true)
maps, err := kpmcli.ResolveDepsIntoMap(&kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, len(maps), 2)
Expand All @@ -571,8 +586,8 @@ func TestResolveDepsWithOnlyKclMod(t *testing.T) {
assert.Equal(t, err, nil)
assert.Equal(t, len(depsMap), 1)
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), true)
assert.Equal(t, depsMap["k8s"], filepath.Join(kpmcli.homePath, "k8s_1.17"))
assert.Equal(t, utils.DirExists(filepath.Join(kpmcli.homePath, "k8s_1.17")), true)
assert.Equal(t, depsMap["helloworld"], filepath.Join(kpmcli.homePath, "helloworld_0.1.2"))
assert.Equal(t, utils.DirExists(filepath.Join(kpmcli.homePath, "helloworld_0.1.2")), true)
defer func() {
err := os.Remove(filepath.Join(testDir, "kcl.mod.lock"))
assert.Equal(t, err, nil)
Expand All @@ -592,13 +607,27 @@ func TestResolveDepsVendorMode(t *testing.T) {
FullName: "kcl1_0.0.1",
Version: "0.0.1",
Sum: kcl1Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl1",
Tag: "0.0.1",
},
},
}

depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2_0.0.1",
Version: "0.0.1",
Sum: kcl2Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl2",
Tag: "0.0.1",
},
},
}

mppTest := orderedmap.NewOrderedMap[string, pkg.Dependency]()
Expand Down Expand Up @@ -657,13 +686,27 @@ func TestCompileWithEntryFile(t *testing.T) {
FullName: "kcl1_0.0.1",
Version: "0.0.1",
Sum: kcl1Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl1",
Tag: "0.0.1",
},
},
}
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))
depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2_0.0.1",
Version: "0.0.1",
Sum: kcl2Sum,
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl2",
Tag: "0.0.1",
},
},
}

mppTest := orderedmap.NewOrderedMap[string, pkg.Dependency]()
Expand Down Expand Up @@ -810,7 +853,17 @@ func TestResolveMetadataInJsonStr(t *testing.T) {
assert.Equal(t, utils.DirExists(vendorDir), false)
assert.Equal(t, utils.DirExists(filepath.Join(vendorDir, "flask-demo-kcl-manifests_ade147b")), false)
assert.Equal(t, err, nil)
expectedStr := "{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"\"}}}"

expectedPath := filepath.Join("not_exist", "flask-demo-kcl-manifests_ade147b")
if runtime.GOOS == "windows" {
expectedPath = strings.ReplaceAll(expectedPath, "\\", "\\\\")
}

expectedStr := fmt.Sprintf(
"{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"%s\"}}}",
expectedPath,
)

assert.Equal(t, res, expectedStr)
defer func() {
if r := os.RemoveAll(filepath.Join("not_exist", "flask-demo-kcl-manifests_ade147b")); r != nil {
Expand Down Expand Up @@ -839,13 +892,13 @@ func TestTestResolveMetadataInJsonStrWithPackage(t *testing.T) {
Deps: make(map[string]pkg.Dependency),
}

localFullPath, err := utils.FindPackage(filepath.Join(globalPkgPath, "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8"), "helloworld")
localFullPath, err := utils.FindPackage(filepath.Join(globalPkgPath, "flask-demo-kcl-manifests_8308200"), "cc")
assert.Equal(t, err, nil)

expectedDep.Deps["helloworld"] = pkg.Dependency{
Name: "helloworld",
FullName: "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8",
Version: "ee03122b5f45b09eb48694422fc99a0772f6bba8",
expectedDep.Deps["cc"] = pkg.Dependency{
Name: "cc",
FullName: "flask-demo-kcl-manifests_8308200",
Version: "8308200",
LocalFullPath: localFullPath,
}

Expand Down Expand Up @@ -873,19 +926,19 @@ func TestTestResolveMetadataInJsonStrWithPackage(t *testing.T) {
assert.Equal(t, err, nil)

assert.Equal(t, utils.DirExists(vendorDir), true)
assert.Equal(t, utils.DirExists(filepath.Join(vendorDir, "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8")), true)
assert.Equal(t, utils.DirExists(filepath.Join(vendorDir, "flask-demo-kcl-manifests_8308200")), true)

localFullPath, err = utils.FindPackage(filepath.Join(vendorDir, "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8"), "helloworld")
localFullPath, err = utils.FindPackage(filepath.Join(vendorDir, "flask-demo-kcl-manifests_8308200"), "cc")
assert.Equal(t, err, nil)

expectedDep = pkg.DependenciesUI{
Deps: make(map[string]pkg.Dependency),
}

expectedDep.Deps["helloworld"] = pkg.Dependency{
Name: "helloworld",
FullName: "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8",
Version: "ee03122b5f45b09eb48694422fc99a0772f6bba8",
expectedDep.Deps["cc"] = pkg.Dependency{
Name: "cc",
FullName: "flask-demo-kcl-manifests_8308200",
Version: "8308200",
LocalFullPath: localFullPath,
}

Expand Down Expand Up @@ -1139,15 +1192,15 @@ func TestMetadataOffline(t *testing.T) {

beautifulContent, err := os.ReadFile(BeautifulKclMod)
assert.Equal(t, err, nil)
kclPkg, err := pkg.LoadKclPkg(testDir)
kclPkg, err := kpmcli.LoadPkgFromPath(testDir)
assert.Equal(t, err, nil)

res, err := kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, false)
assert.Equal(t, err, nil)
assert.Equal(t, res, "{\"packages\":{}}")
content_after_metadata, err := os.ReadFile(kclMod)
assert.Equal(t, err, nil)
assert.Equal(t, string(content_after_metadata), string(uglyContent))
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(uglyContent)))

res, err = kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, true)
assert.Equal(t, err, nil)
Expand Down Expand Up @@ -1204,8 +1257,9 @@ func TestRunWithGitPackage(t *testing.T) {
opts.SetPkgPath(pkgPath)

compileResult, err := kpmcli.CompileWithOpts(opts)
fmt.Printf("err: %v\n", err)
assert.Equal(t, err, nil)
expectedCompileResult := `{"apiVersion": "v1", "kind": "Pod", "metadata": {"name": "web-app"}, "spec": {"containers": [{"image": "nginx", "name": "main-container", "ports": [{"containerPort": 80}]}]}}`
expectedCompileResult := `{"b": "Hello World!"}`
assert.Equal(t, expectedCompileResult, compileResult.GetRawJsonResult())

assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true)
Expand Down Expand Up @@ -1294,6 +1348,7 @@ func TestUpdateWithNoSumCheck(t *testing.T) {
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
assert.Equal(t, err, nil)

kclPkg.NoSumCheck = true
err = kpmcli.UpdateDeps(kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false)
Expand Down Expand Up @@ -2260,14 +2315,18 @@ func TestVirtualPackageVisiter(t *testing.T) {
assert.Equal(t, err, nil)

v := NewVisitor(*pkgSource, kpmcli)
err = v.Visit(pkgSource, func(p *pkg.KclPkg) error {
visitFunc := func(p *pkg.KclPkg) error {
assert.Contains(t, p.GetPkgName(), "vPkg_")
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod"))
assert.Equal(t, os.IsNotExist(err), true)
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod.lock"))
assert.Equal(t, os.IsNotExist(err), true)
return nil
})
}
err = v.Visit(
WithVisitSource(pkgSource),
WithVisitFunc(visitFunc),
)
assert.Equal(t, err, nil)
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod"))
assert.Equal(t, os.IsNotExist(err), true)
Expand Down
Loading