diff --git a/pkg/client/client.go b/pkg/client/client.go index 631ffb2d..12600348 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -829,6 +829,14 @@ func (c *KpmClient) AddDepToPkg(kclPkg *pkg.KclPkg, d *pkg.Dependency) error { return err } + if dep, ok := kclPkg.Dependencies.Deps.Get(d.Name); ok { + if dep1, ok := kclPkg.ModFile.Dependencies.Deps.Get(d.Name); ok { + if dep1.Git != nil { + dep1.Git.Version = dep.Version + } + } + } + return err } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 6b3a48bc..d8cb2a4a 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1270,6 +1270,144 @@ func TestAddWithGitCommit(t *testing.T) { }() } +func TestAddWithGitTag(t *testing.T) { + pkgPath := getTestDir("add_with_git_tag") + + testPkgPath := "" + if runtime.GOOS == "windows" { + testPkgPath = filepath.Join(pkgPath, "test_pkg_win") + } else { + testPkgPath = filepath.Join(pkgPath, "test_pkg") + } + + testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak") + testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod") + testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect") + testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock") + testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak") + testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(testPkgPathModBak, testPkgPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath) + assert.Equal(t, err, nil) + + opts := opt.AddOptions{ + LocalPath: testPkgPath, + RegistryOpts: opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", + Tag: "v0.1.0", + }, + }, + } + kpmcli.SetLogWriter(nil) + _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) + + assert.Equal(t, err, nil) + + modContent, err := os.ReadFile(testPkgPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(testPkgPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(testPkgPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(testPkgPathMod) + _ = os.Remove(testPkgPathModLock) + }() +} + +func TestAddWithGitBranch(t *testing.T) { + pkgPath := getTestDir("add_with_git_branch") + + testPkgPath := "" + if runtime.GOOS == "windows" { + testPkgPath = filepath.Join(pkgPath, "test_pkg_win") + } else { + testPkgPath = filepath.Join(pkgPath, "test_pkg") + } + + testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak") + testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod") + testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect") + testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock") + testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak") + testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(testPkgPathModBak, testPkgPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath) + assert.Equal(t, err, nil) + + opts := opt.AddOptions{ + LocalPath: testPkgPath, + RegistryOpts: opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", + Branch: "main", + }, + }, + } + kpmcli.SetLogWriter(nil) + _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) + + assert.Equal(t, err, nil) + + modContent, err := os.ReadFile(testPkgPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(testPkgPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(testPkgPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(testPkgPathMod) + _ = os.Remove(testPkgPathModLock) + }() +} + func TestLoadPkgFormOci(t *testing.T) { type testCase struct { Reg string diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.bak b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.bak new file mode 100644 index 00000000..446bc2a2 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.bak @@ -0,0 +1,6 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.expect b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.expect new file mode 100644 index 00000000..b4b66f19 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "main", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.lock.bak b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.lock.expect new file mode 100644 index 00000000..4219f34d --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask-demo-kcl-manifests] + name = "flask-demo-kcl-manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + branch = "main" diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg/main.k b/pkg/client/test_data/add_with_git_branch/test_pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.bak b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.bak new file mode 100644 index 00000000..446bc2a2 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.bak @@ -0,0 +1,6 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.expect b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.expect new file mode 100644 index 00000000..b4b66f19 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "main", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.lock.bak b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.lock.expect new file mode 100644 index 00000000..4219f34d --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg_win/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask-demo-kcl-manifests] + name = "flask-demo-kcl-manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + branch = "main" diff --git a/pkg/client/test_data/add_with_git_branch/test_pkg_win/main.k b/pkg/client/test_data/add_with_git_branch/test_pkg_win/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_git_branch/test_pkg_win/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect index 2950f92d..054ec3fc 100644 --- a/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect +++ b/pkg/client/test_data/add_with_git_commit/test_pkg/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b" } +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_commit/test_pkg_win/kcl.mod.expect b/pkg/client/test_data/add_with_git_commit/test_pkg_win/kcl.mod.expect index 2950f92d..054ec3fc 100644 --- a/pkg/client/test_data/add_with_git_commit/test_pkg_win/kcl.mod.expect +++ b/pkg/client/test_data/add_with_git_commit/test_pkg_win/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b" } +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.bak b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.bak new file mode 100644 index 00000000..446bc2a2 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.bak @@ -0,0 +1,6 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.expect b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.expect new file mode 100644 index 00000000..f98fc689 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tag = "v0.1.0", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.lock.bak b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.lock.expect new file mode 100644 index 00000000..e2d57ccb --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask-demo-kcl-manifests] + name = "flask-demo-kcl-manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + git_tag = "v0.1.0" diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg/main.k b/pkg/client/test_data/add_with_git_tag/test_pkg/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.bak b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.bak new file mode 100644 index 00000000..446bc2a2 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.bak @@ -0,0 +1,6 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.expect b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.expect new file mode 100644 index 00000000..f98fc689 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tag = "v0.1.0", version = "0.0.1" } diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.lock.bak b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.lock.expect b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.lock.expect new file mode 100644 index 00000000..e2d57ccb --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg_win/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask-demo-kcl-manifests] + name = "flask-demo-kcl-manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + git_tag = "v0.1.0" diff --git a/pkg/client/test_data/add_with_git_tag/test_pkg_win/main.k b/pkg/client/test_data/add_with_git_tag/test_pkg_win/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_git_tag/test_pkg_win/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/package/toml.go b/pkg/package/toml.go index f0c7eb6c..9d8706ce 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -266,6 +266,9 @@ func (dep *Dependencies) MarshalLockTOML() (string, error) { if !ok { break } + if dep.Source.Git != nil { + dep.Source.Git.Version = "" + } marshaledDeps[depKey] = dep }