From 3353eff13ac6a3a9b282860f3d5fc7d4328b3016 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 12 Nov 2024 16:12:50 +0800 Subject: [PATCH] feat: add a test case for 'k8s = {version = "1.27"}' Signed-off-by: zongz --- pkg/client/run_test.go | 85 +++++++++++++++++++ .../test_run_with_modspec_version/kcl.mod.bk | 7 ++ .../kcl.mod.expect | 7 ++ .../kcl.mod.lock.bk | 8 ++ .../kcl.mod.lock.expect | 8 ++ .../test_run_with_modspec_version/main.k | 3 + 6 files changed, 118 insertions(+) create mode 100644 pkg/client/run_test.go create mode 100644 pkg/client/test_data/test_run_with_modspec_version/kcl.mod.bk create mode 100644 pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect create mode 100644 pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.bk create mode 100644 pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/test_run_with_modspec_version/main.k diff --git a/pkg/client/run_test.go b/pkg/client/run_test.go new file mode 100644 index 00000000..369be39b --- /dev/null +++ b/pkg/client/run_test.go @@ -0,0 +1,85 @@ +package client + +import ( + "os" + "path/filepath" + "testing" + + "github.com/otiai10/copy" + "gotest.tools/v3/assert" + "kcl-lang.io/kpm/pkg/downloader" + "kcl-lang.io/kpm/pkg/utils" +) + +func TestRunWithModSpecVersion(t *testing.T) { + pkgPath := getTestDir("test_run_with_modspec_version") + modbkPath := filepath.Join(pkgPath, "kcl.mod.bk") + modPath := filepath.Join(pkgPath, "kcl.mod") + modExpect := filepath.Join(pkgPath, "kcl.mod.expect") + lockbkPath := filepath.Join(pkgPath, "kcl.mod.lock.bk") + lockPath := filepath.Join(pkgPath, "kcl.mod.lock") + lockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect") + err := copy.Copy(modbkPath, modPath) + if err != nil { + t.Fatal(err) + } + + err = copy.Copy(lockbkPath, lockPath) + if err != nil { + t.Fatal(err) + } + + defer func() { + // remove the copied files + err := os.RemoveAll(modPath) + if err != nil { + t.Fatal(err) + } + err = os.RemoveAll(lockPath) + if err != nil { + t.Fatal(err) + } + }() + + kpmcli, err := NewKpmClient() + if err != nil { + t.Errorf("Failed to create kpm client: %v", err) + } + + res, err := kpmcli.Run( + WithRunSource( + &downloader.Source{ + Local: &downloader.Local{ + Path: pkgPath, + }, + }, + ), + ) + + if err != nil { + t.Errorf("Failed to run package: %v", err) + } + + assert.Equal(t, res.GetRawYamlResult(), "res: Hello World!") + expectedMod, err := os.ReadFile(modExpect) + if err != nil { + t.Fatal(err) + } + gotMod, err := os.ReadFile(modPath) + if err != nil { + t.Fatal(err) + } + + expectedLock, err := os.ReadFile(lockExpect) + if err != nil { + t.Fatal(err) + } + + gotLock, err := os.ReadFile(lockPath) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod))) + assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock))) +} diff --git a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.bk b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.bk new file mode 100644 index 00000000..46d3ecdf --- /dev/null +++ b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.bk @@ -0,0 +1,7 @@ +[package] +name = "test_run_with_modspec_version" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +helloworld = { version = "0.1.2" } \ No newline at end of file diff --git a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect new file mode 100644 index 00000000..46d3ecdf --- /dev/null +++ b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "test_run_with_modspec_version" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +helloworld = { version = "0.1.2" } \ No newline at end of file diff --git a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.bk b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.bk new file mode 100644 index 00000000..c1bebf8c --- /dev/null +++ b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.bk @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.2" diff --git a/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.expect b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.expect new file mode 100644 index 00000000..c1bebf8c --- /dev/null +++ b/pkg/client/test_data/test_run_with_modspec_version/kcl.mod.lock.expect @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.2" diff --git a/pkg/client/test_data/test_run_with_modspec_version/main.k b/pkg/client/test_data/test_run_with_modspec_version/main.k new file mode 100644 index 00000000..db2619db --- /dev/null +++ b/pkg/client/test_data/test_run_with_modspec_version/main.k @@ -0,0 +1,3 @@ +import helloworld + +res = helloworld.The_first_kcl_program \ No newline at end of file