Skip to content

Commit

Permalink
fix: 'kpm run' will trigger the automatic download of the dependencies (
Browse files Browse the repository at this point in the history
#198)

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Oct 11, 2023
1 parent d861866 commit 01c6aa5
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 0 deletions.
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
Empty file.
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}]
}]
}

0 comments on commit 01c6aa5

Please sign in to comment.