diff --git a/aaa/kcl.mod b/aaa/kcl.mod new file mode 100644 index 00000000..60f561a7 --- /dev/null +++ b/aaa/kcl.mod @@ -0,0 +1,4 @@ +[package] +name = "aaa" +edition = "0.0.1" +version = "0.0.1" diff --git a/aaa/kcl.mod.lock b/aaa/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/aaa/main.k b/aaa/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/aaa/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/client.go b/pkg/client/client.go index fdff57f8..5fb8f2f5 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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, diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index dbf95168..61062d11 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -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) + }() +} diff --git a/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.bak b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.bak new file mode 100644 index 00000000..126d4126 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.bak @@ -0,0 +1,7 @@ +[package] +name = "no_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.0" diff --git a/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect new file mode 100644 index 00000000..f77d425c --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "no_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.1" diff --git a/pkg/client/test_data/test_add_diff_version/no_sum_check/main.k b/pkg/client/test_data/test_add_diff_version/no_sum_check/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/no_sum_check/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_diff_version/with_sum_check/kcl.mod.bak b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.bak new file mode 100644 index 00000000..a29e213d --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.bak @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.0" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect new file mode 100644 index 00000000..36ec88e0 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "with_sum_check" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.1" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak new file mode 100644 index 00000000..b57cac07 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak @@ -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" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect new file mode 100644 index 00000000..6270d4f4 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect @@ -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" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/main.k b/pkg/client/test_data/test_add_diff_version/with_sum_check/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file