Skip to content

Commit

Permalink
fix: add a test cases for issue 266
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Nov 26, 2024
1 parent dc28291 commit f597530
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down
223 changes: 223 additions & 0 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/client/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "add_with_commit"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -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" }

Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -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" }

Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
2 changes: 1 addition & 1 deletion pkg/downloader/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f597530

Please sign in to comment.