diff --git a/pkg/client/client.go b/pkg/client/client.go index 25d2bc65..dda5f26a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -759,6 +759,8 @@ func (c *KpmClient) AddDepToPkg(kclPkg *pkg.KclPkg, d *pkg.Dependency) error { // If the dependency is from the local path, do nothing. if d.IsFromLocal() { + kclPkg.ModFile.Dependencies.Deps[d.Name] = *d + kclPkg.Dependencies.Deps[d.Name] = *d return nil } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 00b44831..d63e270e 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1500,3 +1500,43 @@ func testRunWithOciDownloader(t *testing.T) { strings.Contains(buf.String(), "downloading 'zong-zhe/helloworld:0.0.3' from 'ghcr.io/zong-zhe/helloworld:0.0.3'") assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") } + +func TestAddLocalPath(t *testing.T) { + + kpmCli, err := NewKpmClient() + assert.Equal(t, err, nil) + + path := getTestDir("test_add_local_path") + err = copy.Copy(filepath.Join(path, "kcl.mod.bak"), filepath.Join(path, "kcl.mod")) + assert.Equal(t, err, nil) + + kclPkg, err := kpmCli.LoadPkgFromPath(path) + assert.Equal(t, err, nil) + + opts := opt.AddOptions{ + LocalPath: path, + RegistryOpts: opt.RegistryOptions{ + Local: &opt.LocalOptions{ + Path: filepath.Join(path, "dep"), + }, + }, + } + _, err = kpmCli.AddDepWithOpts(kclPkg, &opts) + assert.Equal(t, err, nil) + + modFile, err := kpmCli.LoadModFile(path) + assert.Equal(t, err, nil) + assert.Equal(t, len(modFile.Deps), 1) + assert.Equal(t, modFile.Deps["dep"].Name, "dep") + assert.Equal(t, modFile.Deps["dep"].LocalFullPath, filepath.Join(path, "dep")) + + kclPkg1, err := kpmCli.LoadPkgFromPath(path) + assert.Equal(t, err, nil) + assert.Equal(t, kclPkg1.Dependencies.Deps["dep"].Name, "dep") + assert.Equal(t, kclPkg1.Dependencies.Deps["dep"].FullName, "dep_0.0.1") + assert.Equal(t, kclPkg1.Dependencies.Deps["dep"].Version, "0.0.1") + defer func() { + _ = os.Remove(filepath.Join(path, "kcl.mod.lock")) + _ = os.Remove(filepath.Join(path, "kcl.mod")) + }() +} diff --git a/pkg/client/test_data/test_add_local_path/dep/kcl.mod b/pkg/client/test_data/test_add_local_path/dep/kcl.mod new file mode 100644 index 00000000..d86c1727 --- /dev/null +++ b/pkg/client/test_data/test_add_local_path/dep/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "dep" +edition = "v0.9.0" +version = "0.0.1" + diff --git a/pkg/client/test_data/test_add_local_path/dep/kcl.mod.lock b/pkg/client/test_data/test_add_local_path/dep/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_add_local_path/dep/main.k b/pkg/client/test_data/test_add_local_path/dep/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_add_local_path/dep/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/test_add_local_path/kcl.mod.bak b/pkg/client/test_data/test_add_local_path/kcl.mod.bak new file mode 100644 index 00000000..665ee79e --- /dev/null +++ b/pkg/client/test_data/test_add_local_path/kcl.mod.bak @@ -0,0 +1,5 @@ +[package] +name = "test_add_local_path" +edition = "v0.9.0" +version = "0.0.1" + diff --git a/pkg/client/test_data/test_add_local_path/kcl.mod.lock.bak b/pkg/client/test_data/test_add_local_path/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_add_local_path/main.k b/pkg/client/test_data/test_add_local_path/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_add_local_path/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file