diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index b6fd6e36..1de90bea 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -2000,7 +2000,7 @@ func testRunRemoteWithArgsInvalid(t *testing.T, kpmcli *KpmClient) { { sourceURL: "git://github.com/kcl-lang/flask-demo-kcl-manifests?commit=8308200&mod=cc:0.0.2", expectedLog: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests' with commit '8308200'\n", - expectedErrMsg: "kcl.mod with package 'cc:0.0.2' not found", + expectedErrMsg: "package 'cc:0.0.2' not found", }, } diff --git a/pkg/client/issues_test.go b/pkg/client/issues_test.go index e2d68ad3..1eaa0115 100644 --- a/pkg/client/issues_test.go +++ b/pkg/client/issues_test.go @@ -9,6 +9,7 @@ import ( "strings" "testing" + "github.com/otiai10/copy" "github.com/stretchr/testify/assert" "kcl-lang.io/kpm/pkg/downloader" "kcl-lang.io/kpm/pkg/features" @@ -207,3 +208,225 @@ func TestKpmIssue550(t *testing.T) { RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc) } } + +func TestKpmIssue226(t *testing.T) { + testPath := "github.com/kcl-lang/kpm/issues/226" + test_add_dep_with_git_commit := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + modPath := filepath.Join(rootPath, testPath, "add_with_commit") + modFileBk := filepath.Join(modPath, "kcl.mod.bk") + LockFileBk := filepath.Join(modPath, "kcl.mod.lock.bk") + modFile := filepath.Join(modPath, "kcl.mod") + LockFile := filepath.Join(modPath, "kcl.mod.lock") + modFileExpect := filepath.Join(modPath, "kcl.mod.expect") + LockFileExpect := filepath.Join(modPath, "kcl.mod.lock.expect") + + defer func() { + _ = os.RemoveAll(modFile) + _ = os.RemoveAll(LockFile) + }() + + err := copy.Copy(modFileBk, modFile) + if err != nil { + t.Fatal(err) + } + err = copy.Copy(LockFileBk, LockFile) + if err != nil { + t.Fatal(err) + } + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + kpkg, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + if err != nil { + t.Fatal(err) + } + + err = kpmcli.Add( + WithAddKclPkg(kpkg), + WithAddSource( + &downloader.Source{ + Git: &downloader.Git{ + Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", + Commit: "ade147b", + }, + }, + ), + ) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(buf.String()), + "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit 'ade147b'"+ + "adding dependency 'flask_manifests'"+ + "add dependency 'flask_manifests:0.0.1' successfully") + + modFileContent, err := os.ReadFile(modFile) + if err != nil { + t.Fatal(err) + } + lockFileContent, err := os.ReadFile(LockFile) + if err != nil { + t.Fatal(err) + } + + modFileExpectContent, err := os.ReadFile(modFileExpect) + if err != nil { + t.Fatal(err) + } + + lockFileExpectContent, err := os.ReadFile(LockFileExpect) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(modFileContent)), utils.RmNewline(string(modFileExpectContent))) + assert.Equal(t, utils.RmNewline(string(lockFileContent)), utils.RmNewline(string(lockFileExpectContent))) + } + + test_update_with_git_commit := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + modPath := filepath.Join(rootPath, testPath, "update_check_version") + modFileBk := filepath.Join(modPath, "kcl.mod.bk") + LockFileBk := filepath.Join(modPath, "kcl.mod.lock.bk") + modFile := filepath.Join(modPath, "kcl.mod") + LockFile := filepath.Join(modPath, "kcl.mod.lock") + modFileExpect := filepath.Join(modPath, "kcl.mod.expect") + LockFileExpect := filepath.Join(modPath, "kcl.mod.lock.expect") + + defer func() { + _ = os.RemoveAll(modFile) + _ = os.RemoveAll(LockFile) + }() + + err := copy.Copy(modFileBk, modFile) + if err != nil { + t.Fatal(err) + } + err = copy.Copy(LockFileBk, LockFile) + if err != nil { + t.Fatal(err) + } + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + kpkg, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + if err != nil { + t.Fatal(err) + } + + _, err = kpmcli.Update( + WithUpdatedKclPkg(kpkg), + ) + + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(buf.String()), + "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit 'ade147b'") + + modFileContent, err := os.ReadFile(modFile) + if err != nil { + t.Fatal(err) + } + lockFileContent, err := os.ReadFile(LockFile) + if err != nil { + t.Fatal(err) + } + + modFileExpectContent, err := os.ReadFile(modFileExpect) + if err != nil { + t.Fatal(err) + } + + lockFileExpectContent, err := os.ReadFile(LockFileExpect) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(modFileContent)), utils.RmNewline(string(modFileExpectContent))) + assert.Equal(t, utils.RmNewline(string(lockFileContent)), utils.RmNewline(string(lockFileExpectContent))) + } + + test_update_with_git_commit_invalid := func(t *testing.T, kpmcli *KpmClient) { + rootPath := getTestDir("issues") + modPath := filepath.Join(rootPath, testPath, "update_check_version_invalid") + modFileBk := filepath.Join(modPath, "kcl.mod.bk") + LockFileBk := filepath.Join(modPath, "kcl.mod.lock.bk") + modFile := filepath.Join(modPath, "kcl.mod") + LockFile := filepath.Join(modPath, "kcl.mod.lock") + modFileExpect := filepath.Join(modPath, "kcl.mod.expect") + LockFileExpect := filepath.Join(modPath, "kcl.mod.lock.expect") + + defer func() { + _ = os.RemoveAll(modFile) + _ = os.RemoveAll(LockFile) + }() + + err := copy.Copy(modFileBk, modFile) + if err != nil { + t.Fatal(err) + } + err = copy.Copy(LockFileBk, LockFile) + if err != nil { + t.Fatal(err) + } + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + kpkg, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + if err != nil { + t.Fatal(err) + } + + _, err = kpmcli.Update( + WithUpdatedKclPkg(kpkg), + ) + + assert.Equal(t, err.Error(), "package 'flask_manifests:0.100.0' not found") + + assert.Equal(t, utils.RmNewline(buf.String()), + "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit 'ade147b'") + + modFileContent, err := os.ReadFile(modFile) + if err != nil { + t.Fatal(err) + } + lockFileContent, err := os.ReadFile(LockFile) + if err != nil { + t.Fatal(err) + } + + modFileExpectContent, err := os.ReadFile(modFileExpect) + if err != nil { + t.Fatal(err) + } + + lockFileExpectContent, err := os.ReadFile(LockFileExpect) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(modFileContent)), utils.RmNewline(string(modFileExpectContent))) + assert.Equal(t, utils.RmNewline(string(lockFileContent)), utils.RmNewline(string(lockFileExpectContent))) + } + + RunTestWithGlobalLockAndKpmCli(t, "add_dep_with_git_commit", test_add_dep_with_git_commit) + RunTestWithGlobalLockAndKpmCli(t, "update_with_git_commit", test_update_with_git_commit) + RunTestWithGlobalLockAndKpmCli(t, "update_with_git_commit_invalid", test_update_with_git_commit_invalid) +} diff --git a/pkg/client/pull_test.go b/pkg/client/pull_test.go index a24546ca..9c6d75b3 100644 --- a/pkg/client/pull_test.go +++ b/pkg/client/pull_test.go @@ -180,7 +180,7 @@ func testPullWithModSpec(t *testing.T) { WithLocalPath(pulledPath), WithPullSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.2"), ) - assert.Equal(t, err.Error(), "kcl.mod with package 'subhelloworld:0.0.2' not found") + assert.Equal(t, err.Error(), "package 'subhelloworld:0.0.2' not found") } func testPullWithOnlySpec(t *testing.T) { diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.bk new file mode 100644 index 00000000..e9ea48aa --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.bk @@ -0,0 +1,4 @@ +[package] +name = "add_with_commit" +edition = "v0.10.0" +version = "0.0.1" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.expect new file mode 100644 index 00000000..b1e1b186 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "add_with_commit" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +flask_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/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.lock.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.lock.bk new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.lock.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.lock.expect new file mode 100644 index 00000000..4dc84770 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask_manifests] + name = "flask_manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + commit = "ade147b" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/add_with_commit/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.bk new file mode 100644 index 00000000..c28e12d4 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.bk @@ -0,0 +1,8 @@ +[package] +name = "update_check_version" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +flask_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/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.expect new file mode 100644 index 00000000..7f514166 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "update_check_version" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +flask_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/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.bk new file mode 100644 index 00000000..4dc84770 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.bk @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask_manifests] + name = "flask_manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + commit = "ade147b" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.expect new file mode 100644 index 00000000..4dc84770 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask_manifests] + name = "flask_manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + commit = "ade147b" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.bk new file mode 100644 index 00000000..c980c956 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.bk @@ -0,0 +1,8 @@ +[package] +name = "update_check_version_invalid" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +flask_manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.100.0" } + diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.expect new file mode 100644 index 00000000..35e246ad --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "update_check_version_invalid" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +flask_manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.100.0" } diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.bk b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.bk new file mode 100644 index 00000000..4dc84770 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.bk @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask_manifests] + name = "flask_manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + commit = "ade147b" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.expect b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.expect new file mode 100644 index 00000000..4dc84770 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/kcl.mod.lock.expect @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.flask_manifests] + name = "flask_manifests" + full_name = "flask_manifests_0.0.1" + version = "0.0.1" + url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" + commit = "ade147b" diff --git a/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/main.k b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/issues/github.com/kcl-lang/kpm/issues/226/update_check_version_invalid/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/downloader/utils.go b/pkg/downloader/utils.go index 41594140..041ff07a 100644 --- a/pkg/downloader/utils.go +++ b/pkg/downloader/utils.go @@ -104,7 +104,7 @@ func FindPackageByModSpec(root string, modSpec *ModSpec) (string, error) { return "", err } if result == "" { - return "", fmt.Errorf("kcl.mod with package '%s:%s' not found", modSpec.Name, modSpec.Version) + return "", fmt.Errorf("package '%s:%s' not found", modSpec.Name, modSpec.Version) } return result, nil } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 07b81be8..61508ba9 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -642,7 +642,7 @@ func FindPackage(root, targetPackage string) (string, error) { return "", err } if result == "" { - return "", fmt.Errorf("kcl.mod with package '%s' not found", targetPackage) + return "", fmt.Errorf("package '%s' not found", targetPackage) } return result, nil }