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

fix: 'kpm run' will trigger the automatic download of the dependencies #198

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
12 changes: 12 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
searchPath = c.homePath
}

// alian the dependencies between kcl.mod and kcl.mod.lock
for name, d := range kclPkg.ModFile.Dependencies.Deps {
if _, ok := kclPkg.Dependencies.Deps[name]; !ok {
kclPkg.Dependencies.Deps[name] = d
}
}

for name, d := range kclPkg.Dependencies.Deps {
searchFullPath := filepath.Join(searchPath, d.FullName)
if !update {
Expand Down Expand Up @@ -161,6 +168,11 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
}
}

// update the kcl.mod and kcl.mod.lock.
err := kclPkg.UpdateModAndLockFile()
if err != nil {
return err
}
return nil
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ func TestVendorDeps(t *testing.T) {
os.RemoveAll(filepath.Join(testDir, "my_kcl"))
}

func TestResolveDepsWithOnlyKclMod(t *testing.T) {
testDir := getTestDir("resolve_dep_with_kclmod")
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false)
kclPkg, err := pkg.LoadKclPkg(testDir)
assert.Equal(t, err, nil)
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
depsMap, err := kpmcli.ResolveDepsIntoMap(kclPkg)
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)
defer func() {
err := os.Remove(filepath.Join(testDir, "kcl.mod.lock"))
assert.Equal(t, err, nil)
}()
}

func TestResolveDepsVendorMode(t *testing.T) {
testDir := getTestDir("resolve_deps")
kpm_home := filepath.Join(testDir, "kpm_home")
Expand Down
7 changes: 7 additions & 0 deletions pkg/client/test_data/resolve_dep_with_kclmod/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "resolve_dep_with_kclmod"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.17"
10 changes: 10 additions & 0 deletions pkg/client/test_data/resolve_dep_with_kclmod/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import k8s.api.core.v1 as k8core

k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
name = "main-container"
image = "nginx"
ports = [{containerPort = 80}]
}]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kpm: downloading 'test/k8s:1.27' from 'localhost:5001/test/k8s:1.27'.
apiVersion: v1
kind: Pod
metadata:
name: web-app
spec:
containers:
- image: nginx
name: main-container
ports:
- containerPort: 80
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_only_kcl_mod"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.27"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import k8s.api.core.v1 as k8core

k8core.Pod {
metadata.name = "web-app"
spec.containers = [{
name = "main-container"
image = "nginx"
ports = [{containerPort = 80}]
}]
}
Loading