Skip to content

Commit 01c6aa5

Browse files
authored
fix: 'kpm run' will trigger the automatic download of the dependencies (#198)
Signed-off-by: zongz <zongzhe1024@163.com>
1 parent d861866 commit 01c6aa5

File tree

10 files changed

+79
-0
lines changed

10 files changed

+79
-0
lines changed

pkg/client/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
114114
searchPath = c.homePath
115115
}
116116

117+
// alian the dependencies between kcl.mod and kcl.mod.lock
118+
for name, d := range kclPkg.ModFile.Dependencies.Deps {
119+
if _, ok := kclPkg.Dependencies.Deps[name]; !ok {
120+
kclPkg.Dependencies.Deps[name] = d
121+
}
122+
}
123+
117124
for name, d := range kclPkg.Dependencies.Deps {
118125
searchFullPath := filepath.Join(searchPath, d.FullName)
119126
if !update {
@@ -161,6 +168,11 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
161168
}
162169
}
163170

171+
// update the kcl.mod and kcl.mod.lock.
172+
err := kclPkg.UpdateModAndLockFile()
173+
if err != nil {
174+
return err
175+
}
164176
return nil
165177
}
166178

pkg/client/client_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,25 @@ func TestVendorDeps(t *testing.T) {
296296
os.RemoveAll(filepath.Join(testDir, "my_kcl"))
297297
}
298298

299+
func TestResolveDepsWithOnlyKclMod(t *testing.T) {
300+
testDir := getTestDir("resolve_dep_with_kclmod")
301+
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false)
302+
kclPkg, err := pkg.LoadKclPkg(testDir)
303+
assert.Equal(t, err, nil)
304+
kpmcli, err := NewKpmClient()
305+
assert.Equal(t, err, nil)
306+
depsMap, err := kpmcli.ResolveDepsIntoMap(kclPkg)
307+
assert.Equal(t, err, nil)
308+
assert.Equal(t, len(depsMap), 1)
309+
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), true)
310+
assert.Equal(t, depsMap["k8s"], filepath.Join(kpmcli.homePath, "k8s_1.17"))
311+
assert.Equal(t, utils.DirExists(filepath.Join(kpmcli.homePath, "k8s_1.17")), true)
312+
defer func() {
313+
err := os.Remove(filepath.Join(testDir, "kcl.mod.lock"))
314+
assert.Equal(t, err, nil)
315+
}()
316+
}
317+
299318
func TestResolveDepsVendorMode(t *testing.T) {
300319
testDir := getTestDir("resolve_deps")
301320
kpm_home := filepath.Join(testDir, "kpm_home")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "resolve_dep_with_kclmod"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
k8s = "1.17"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import k8s.api.core.v1 as k8core
2+
3+
k8core.Pod {
4+
metadata.name = "web-app"
5+
spec.containers = [{
6+
name = "main-container"
7+
image = "nginx"
8+
ports = [{containerPort = 80}]
9+
}]
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_only_kcl_mod/test_suite.stderr

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
kpm: downloading 'test/k8s:1.27' from 'localhost:5001/test/k8s:1.27'.
2+
apiVersion: v1
3+
kind: Pod
4+
metadata:
5+
name: web-app
6+
spec:
7+
containers:
8+
- image: nginx
9+
name: main-container
10+
ports:
11+
- containerPort: 80
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_kpm_run_with_only_kcl_mod"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
k8s = "1.27"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import k8s.api.core.v1 as k8core
2+
3+
k8core.Pod {
4+
metadata.name = "web-app"
5+
spec.containers = [{
6+
name = "main-container"
7+
image = "nginx"
8+
ports = [{containerPort = 80}]
9+
}]
10+
}

0 commit comments

Comments
 (0)