From 88424d3432caff9a5f356138d528883f7f1c00c2 Mon Sep 17 00:00:00 2001 From: peefy Date: Sat, 11 May 2024 23:46:22 +0800 Subject: [PATCH] fix: fail unit test cases Signed-off-by: peefy --- pkg/client/client_test.go | 23 +++++++++---------- .../resolve_deps/my_kcl_compile/kcl.mod.lock | 9 ++++++++ .../test_data/test_data_add_deps/kcl.mod | 2 +- pkg/cmd/cmd_flags.go | 2 +- pkg/cmd/cmd_graph.go | 2 +- pkg/cmd/cmd_import.go | 2 +- pkg/cmd/cmd_init.go | 2 +- pkg/cmd/cmd_login.go | 2 +- pkg/cmd/cmd_logout.go | 2 +- pkg/cmd/cmd_pkg.go | 2 +- pkg/cmd/cmd_pull.go | 2 +- pkg/cmd/cmd_push_test.go | 2 +- pkg/cmd/cmd_update.go | 2 +- pkg/package/modfile.go | 18 ++++++++++----- pkg/package/toml.go | 2 +- pkg/package/toml_test.go | 2 +- pkg/runner/runner.go | 2 +- pkg/utils/utils.go | 6 ++--- pkg/version/version.go | 2 +- 19 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 pkg/client/test_data/resolve_deps/my_kcl_compile/kcl.mod.lock diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 0b80ea07..8e46c2f5 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -120,18 +120,18 @@ func TestDownloadLatestOci(t *testing.T) { dep, err := kpmcli.Download(&depFromOci, "", testPath) assert.Equal(t, err, nil) assert.Equal(t, dep.Name, "helloworld") - assert.Equal(t, dep.FullName, "helloworld_0.1.1") - assert.Equal(t, dep.Version, "0.1.1") - assert.Equal(t, dep.Sum, "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ=") + assert.Equal(t, dep.FullName, "helloworld_0.1.2") + assert.Equal(t, dep.Version, "0.1.2") +2 assert.Equal(t, dep.Sum, "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ=") assert.NotEqual(t, dep.Source.Oci, nil) assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io") assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/helloworld") - assert.Equal(t, dep.Source.Oci.Tag, "0.1.1") + assert.Equal(t, dep.Source.Oci.Tag, "0.1.2") assert.Equal(t, dep.LocalFullPath, testPath) assert.Equal(t, err, nil) // Check whether the tar downloaded by `kpm add` has been deleted. - assert.Equal(t, utils.DirExists(filepath.Join(testPath, "helloworld_0.1.1.tar")), false) + assert.Equal(t, utils.DirExists(filepath.Join(testPath, "helloworld_0.1.2.tar")), false) assert.Equal(t, utils.DirExists(filepath.Join(getTestDir("download"), "helloworld")), false) } @@ -216,13 +216,12 @@ func TestParseKclModFile(t *testing.T) { modFilePath := filepath.Join(testDir, "kcl.mod") // Write modFileContent to modFilePath - modFileContent := ` - [dependencies] - teleport = "0.1.0" - rabbitmq = "0.0.1" - gitdep = { git = "git://example.com/repo.git", tag = "v1.0.0" } - localdep = { path = "/path/to/local/dependency" } - ` + modFileContent := `[dependencies] +teleport = "0.1.0" +rabbitmq = "0.0.1" +gitdep = { git = "git://example.com/repo.git", tag = "v1.0.0" } +localdep = { path = "/path/to/local/dependency" } +` err = os.WriteFile(modFilePath, []byte(modFileContent), 0644) assert.Nil(t, err, "error writing mod file") diff --git a/pkg/client/test_data/resolve_deps/my_kcl_compile/kcl.mod.lock b/pkg/client/test_data/resolve_deps/my_kcl_compile/kcl.mod.lock new file mode 100644 index 00000000..25f3d72a --- /dev/null +++ b/pkg/client/test_data/resolve_deps/my_kcl_compile/kcl.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.kcl1] + name = "kcl1" + full_name = "kcl1" + sum = "c5bjxiHbwJqWCdBwXLOr9MydCTis3nJotrOzozkPsKo=" + [dependencies.kcl2] + name = "kcl2" + full_name = "kcl2" + sum = "OiA7IJfhi9bLp3d+Phc6ncgWE8XwpXqkGvhF5BOpf34=" diff --git a/pkg/client/test_data/test_data_add_deps/kcl.mod b/pkg/client/test_data/test_data_add_deps/kcl.mod index 3b53b8aa..2c66884a 100644 --- a/pkg/client/test_data/test_data_add_deps/kcl.mod +++ b/pkg/client/test_data/test_data_add_deps/kcl.mod @@ -4,5 +4,5 @@ edition = "v0.8.0" version = "0.0.1" [dependencies] -oci_name = "test_tag" +oci_name = { oci = "oci://test_reg/test_repo", tag = "test_tag" } name = { git = "test_url", tag = "test_tag" } diff --git a/pkg/cmd/cmd_flags.go b/pkg/cmd/cmd_flags.go index 616d9f0f..47ed65cb 100644 --- a/pkg/cmd/cmd_flags.go +++ b/pkg/cmd/cmd_flags.go @@ -1,7 +1,7 @@ // Copyright 2023 The KCL Authors. All rights reserved. // This file provides all the flags in the kpm cli. // -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_graph.go b/pkg/cmd/cmd_graph.go index 3d1ec0f1..7fbc0b5c 100644 --- a/pkg/cmd/cmd_graph.go +++ b/pkg/cmd/cmd_graph.go @@ -73,7 +73,7 @@ func KpmGraph(c *cli.Context, kpmcli *client.KpmClient) error { } // print the dependency graph to stdout. - root := fmt.Sprintf("%s@%s", kclPkg.GetPkgName(), kclPkg.GetPkgVersion()) + root := fmt.Sprintf("%s@%s", kclPkg.GetPkgName(), kclPkg.GetPkgVersion()) err = graph.BFS(depGraph, root, func(source string) bool { for target := range adjMap[source] { reporter.ReportMsgTo( diff --git a/pkg/cmd/cmd_import.go b/pkg/cmd/cmd_import.go index d8a14393..7c7d63cc 100644 --- a/pkg/cmd/cmd_import.go +++ b/pkg/cmd/cmd_import.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_init.go b/pkg/cmd/cmd_init.go index a0f38021..d9e51c3a 100644 --- a/pkg/cmd/cmd_init.go +++ b/pkg/cmd/cmd_init.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_login.go b/pkg/cmd/cmd_login.go index 1d5cbe67..6d477688 100644 --- a/pkg/cmd/cmd_login.go +++ b/pkg/cmd/cmd_login.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_logout.go b/pkg/cmd/cmd_logout.go index faa6d5f5..065bd878 100644 --- a/pkg/cmd/cmd_logout.go +++ b/pkg/cmd/cmd_logout.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_pkg.go b/pkg/cmd/cmd_pkg.go index 9d954e99..2a3dbe1a 100644 --- a/pkg/cmd/cmd_pkg.go +++ b/pkg/cmd/cmd_pkg.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_pull.go b/pkg/cmd/cmd_pull.go index 2e7086d5..33e30282 100644 --- a/pkg/cmd/cmd_pull.go +++ b/pkg/cmd/cmd_pull.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_push_test.go b/pkg/cmd/cmd_push_test.go index 11a59cfc..e7756546 100644 --- a/pkg/cmd/cmd_push_test.go +++ b/pkg/cmd/cmd_push_test.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/cmd/cmd_update.go b/pkg/cmd/cmd_update.go index dca866db..330d3678 100644 --- a/pkg/cmd/cmd_update.go +++ b/pkg/cmd/cmd_update.go @@ -1,5 +1,5 @@ // Copyright 2023 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package cmd diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 9c59f06f..1f906bac 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -28,12 +28,18 @@ const ( // 'Package' is the kcl package section of 'kcl.mod'. type Package struct { - Name string `toml:"name,omitempty"` // kcl package name - Edition string `toml:"edition,omitempty"` // kcl compiler version - Version string `toml:"version,omitempty"` // kcl package version - Description string `toml:"description,omitempty"` // kcl package description - Include []string `toml:"include,omitempty"` // kcl package include - Exclude []string `toml:"exclude,omitempty"` // kcl package exclude + // The name of the package. + Name string `toml:"name,omitempty"` + // The kcl compiler version + Edition string `toml:"edition,omitempty"` + // The version of the package. + Version string `toml:"version,omitempty"` + // Description denotes the description of the package. + Description string `toml:"description,omitempty"` // kcl package description + // Exclude denote the files to include when publishing. + Include []string `toml:"include,omitempty"` + // Exclude denote the files to exclude when publishing. + Exclude []string `toml:"exclude,omitempty"` } // 'ModFile' is kcl package file 'kcl.mod'. diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 904a3b22..02d582cc 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -271,7 +271,7 @@ func (pkg *Package) UnmarshalTOML(data interface{}) error { if v, ok := meta[EXCLUDE_FLAG].([]interface{}); ok { pkg.Exclude = convertToStringArray(v) } - + return nil } diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index 71f9eca5..003d4ff8 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -64,7 +64,7 @@ func TestMarshalTOML(t *testing.T) { fmt.Printf("expected_toml == got_data: '%t'\n", expected_toml == got_data) fmt.Printf("reversed_expected_toml == got_data: '%t'\n", reversed_expected_toml == got_data) assert.Equal(t, (utils.RmNewline(expected_toml) == utils.RmNewline(got_data)) || - (utils.RmNewline(reversed_expected_toml) == utils.RmNewline(got_data)), true) + (utils.RmNewline(reversed_expected_toml) == utils.RmNewline(got_data)), true, "got data is ", expected_toml, reversed_expected_toml, got_data) } func TestUnMarshalTOML(t *testing.T) { diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 07f31b70..6d0d42a4 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -55,4 +55,4 @@ func (compiler *Compiler) Run() (*kcl.KCLResultList, error) { // GetKclVersion fetches the kcl version func GetKclVersion() string { return string(scripts.KclvmVersionType_latest) -} \ No newline at end of file +} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 2c6e8151..77cd615a 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -149,12 +149,12 @@ func TarDir(srcDir string, tarPath string, include []string, exclude []string) e getNewPattern := func(ex string) string { newPath := ex - if !strings.HasPrefix(ex, srcDir + "/") { - newPath = srcDir + "/" + ex + if !strings.HasPrefix(ex, srcDir+string(filepath.Separator)) { + newPath = filepath.Join(srcDir, ex) } return newPath } - + for _, ex := range exclude { if matched, _ := filepath.Match(getNewPattern(ex), path); matched { return nil diff --git a/pkg/version/version.go b/pkg/version/version.go index 7668f5de..4e7b39a8 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,5 +1,5 @@ // Copyright 2022 The KCL Authors. All rights reserved. -// Deprecated: The entire contents of this file will be deprecated. +// Deprecated: The entire contents of this file will be deprecated. // Please use the kcl cli - https://github.com/kcl-lang/cli. package version