Skip to content

Commit

Permalink
Merge pull request #539 from kcl-lang/add-test-case
Browse files Browse the repository at this point in the history
feat: add a test case for 'k8s = {version = "1.27"}'
  • Loading branch information
Peefy authored Nov 12, 2024
2 parents e41835e + b25662a commit 29013ca
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
test.RunTestWithGlobalLock(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)

features.Enable(features.SupportNewStorage)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", testAddWithModSpec)
Expand Down
85 changes: 85 additions & 0 deletions pkg/client/run_test.go
Original file line number Diff line number Diff line change
@@ -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)))
}
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_run_with_modspec_version/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions pkg/client/test_data/test_run_with_modspec_version/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import helloworld

res = helloworld.The_first_kcl_program

0 comments on commit 29013ca

Please sign in to comment.