Skip to content

Commit

Permalink
Merge pull request #239 from zong-zhe/fix-update-npe
Browse files Browse the repository at this point in the history
fix: fix the npe when 'kpm add'
  • Loading branch information
Peefy authored Dec 25, 2023
2 parents 7aa8c2c + deb86e0 commit 9e4d623
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 1 deletion.
4 changes: 4 additions & 0 deletions aaa/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"
Empty file added aaa/kcl.mod.lock
Empty file.
1 change: 1 addition & 0 deletions aaa/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
5 changes: 4 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,10 @@ func (c *KpmClient) downloadDeps(deps pkg.Dependencies, lockDeps pkg.Dependencie
}

if !lockedDep.IsFromLocal() {
if !c.noSumCheck && expectedSum != "" && lockedDep.Sum != expectedSum && existDep.FullName == d.FullName {
if !c.noSumCheck && expectedSum != "" &&
lockedDep.Sum != expectedSum &&
existDep != nil &&
existDep.FullName == d.FullName {
return nil, reporter.NewErrorEvent(
reporter.CheckSumMismatch,
errors.CheckSumMismatchError,
Expand Down
124 changes: 124 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,3 +874,127 @@ func TestUpdateWithNoSumCheck(t *testing.T) {
_ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock"))
}()
}

func TestAddWithDiffVersionNoSumCheck(t *testing.T) {
pkgPath := getTestDir("test_add_diff_version")

pkgWithSumCheckPath := filepath.Join(pkgPath, "no_sum_check")
pkgWithSumCheckPathModBak := filepath.Join(pkgWithSumCheckPath, "kcl.mod.bak")
pkgWithSumCheckPathMod := filepath.Join(pkgWithSumCheckPath, "kcl.mod")
pkgWithSumCheckPathModExpect := filepath.Join(pkgWithSumCheckPath, "kcl.mod.expect")
pkgWithSumCheckPathModLock := filepath.Join(pkgWithSumCheckPath, "kcl.mod.lock")

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(pkgWithSumCheckPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang",
PkgName: "helloworld",
Tag: "0.1.1",
},
},
NoSumCheck: true,
}

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(pkgWithSumCheckPathModLock), false)

modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(string(modContentStr), "\n", "")
assert.Equal(t, err, nil)
modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")
assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

opts.NoSumCheck = false
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(pkgWithSumCheckPathModLock), true)
modContent, err = os.ReadFile(pkgWithSumCheckPathMod)
modContentStr = strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

defer func() {
_ = os.Remove(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}

func TestAddWithDiffVersionWithSumCheck(t *testing.T) {
pkgPath := getTestDir("test_add_diff_version")

pkgWithSumCheckPath := filepath.Join(pkgPath, "with_sum_check")
pkgWithSumCheckPathModBak := filepath.Join(pkgWithSumCheckPath, "kcl.mod.bak")
pkgWithSumCheckPathMod := filepath.Join(pkgWithSumCheckPath, "kcl.mod")
pkgWithSumCheckPathModExpect := filepath.Join(pkgWithSumCheckPath, "kcl.mod.expect")
pkgWithSumCheckPathModLock := filepath.Join(pkgWithSumCheckPath, "kcl.mod.lock")
pkgWithSumCheckPathModLockBak := filepath.Join(pkgWithSumCheckPath, "kcl.mod.lock.bak")
pkgWithSumCheckPathModLockExpect := filepath.Join(pkgWithSumCheckPath, "kcl.mod.lock.expect")

err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(pkgWithSumCheckPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: pkgPath,
RegistryOpts: opt.RegistryOptions{
Oci: &opt.OciOptions{
Reg: "ghcr.io",
Repo: "kcl-lang",
PkgName: "helloworld",
Tag: "0.1.1",
},
},
}

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)

modContent, err := os.ReadFile(pkgWithSumCheckPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect)
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(pkgWithSumCheckPathModLock)
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
assert.Equal(t, err, nil)
modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect)
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(pkgWithSumCheckPathMod)
_ = os.Remove(pkgWithSumCheckPathModLock)
}()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "no_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "no_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
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,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
sum = "aqrvSsd8zGHzRERbOzxYxARmK6QjvpQMYC1OqemdZvc="
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'

0 comments on commit 9e4d623

Please sign in to comment.