From 161f02d15352eea6ce925d7457896b096d204127 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 3 Dec 2024 17:44:35 +0800 Subject: [PATCH] feat: add more test cases for api 'Init' Signed-off-by: zongz --- pkg/client/add_test.go | 8 +- pkg/client/check_test.go | 10 +- pkg/client/init.go | 91 ++++++ pkg/client/init_test.go | 295 ++++++++++++++++++ pkg/client/issues_test.go | 10 +- pkg/client/run_test.go | 42 +-- pkg/client/test.go | 57 ++-- .../test_data/test_init/init_0/.gitkeep | 0 .../test_data/test_init/init_1/.gitkeep | 0 .../test_data/test_init/init_2/.gitkeep | 0 .../test_data/test_init/init_3/.gitkeep | 0 .../test_data/test_init/init_4/.gitkeep | 0 pkg/client/test_data/test_init/init_5/kcl.mod | 7 + .../test_data/test_init/init_5/kcl.mod.lock | 9 + pkg/client/test_data/test_init/init_5/main.k | 4 + pkg/client/update_test.go | 12 +- pkg/client/vendor_test.go | 4 +- 17 files changed, 480 insertions(+), 69 deletions(-) create mode 100644 pkg/client/init_test.go create mode 100644 pkg/client/test_data/test_init/init_0/.gitkeep create mode 100644 pkg/client/test_data/test_init/init_1/.gitkeep create mode 100644 pkg/client/test_data/test_init/init_2/.gitkeep create mode 100644 pkg/client/test_data/test_init/init_3/.gitkeep create mode 100644 pkg/client/test_data/test_init/init_4/.gitkeep create mode 100644 pkg/client/test_data/test_init/init_5/kcl.mod create mode 100644 pkg/client/test_data/test_init/init_5/kcl.mod.lock create mode 100644 pkg/client/test_data/test_init/init_5/main.k diff --git a/pkg/client/add_test.go b/pkg/client/add_test.go index fa6c5ff2..b089ea9d 100644 --- a/pkg/client/add_test.go +++ b/pkg/client/add_test.go @@ -167,7 +167,7 @@ func TestAddWithModSpec(t *testing.T) { assert.Equal(t, utils.RmNewline(tt.msg), utils.RmNewline(buf.String())) } - RunTestWithGlobalLockAndKpmCli(t, tt.name, testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tt.name, TestFunc: testFunc}}) expectedMod, err := os.ReadFile(modExpect) if err != nil { @@ -257,7 +257,7 @@ func TestAddRenameWithModSpec(t *testing.T) { ), utils.RmNewline(buf.String())) } - RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithModSpec", testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddRenameWithModSpec", TestFunc: testFunc}}) expectedMod, err := os.ReadFile(modExpect) if err != nil { @@ -374,7 +374,7 @@ func TestAddWithOnlyModSpec(t *testing.T) { assert.Equal(t, utils.RmNewline(tc.msg), utils.RmNewline(buf.String())) } - RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tc.name, TestFunc: testFunc}}) expectedMod, err := os.ReadFile(modExpect) if err != nil { @@ -473,7 +473,7 @@ func TestAddRenameWithNoSpec(t *testing.T) { } } - RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithNoSpec", testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddRenameWithNoSpec", TestFunc: testFunc}}) expectedMod, err := os.ReadFile(modExpect) if err != nil { diff --git a/pkg/client/check_test.go b/pkg/client/check_test.go index b65a4c0d..14ad9c9f 100644 --- a/pkg/client/check_test.go +++ b/pkg/client/check_test.go @@ -27,7 +27,7 @@ func TestModCheckPass(t *testing.T) { t.Fatalf("failed to check kcl package: %v", err) } } - RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_pass", testModCheckPass) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_pass", TestFunc: testModCheckPass}}) } func TestModCheckNameFailed(t *testing.T) { @@ -47,7 +47,7 @@ func TestModCheckNameFailed(t *testing.T) { assert.Equal(t, err.Error(), "invalid name: invalid/mod/name") } - RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_name_failed", testModCheckNameFailed) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_name_failed", TestFunc: testModCheckNameFailed}}) } func TestModCheckVersionFailed(t *testing.T) { @@ -66,7 +66,7 @@ func TestModCheckVersionFailed(t *testing.T) { assert.Equal(t, err.Error(), "invalid version: invalid_version for version_failed") } - RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_version_failed", testModCheckVersionFailed) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_version_failed", TestFunc: testModCheckVersionFailed}}) } func TestCheckDepSumPass(t *testing.T) { @@ -89,7 +89,7 @@ func TestCheckDepSumPass(t *testing.T) { } } - RunTestWithGlobalLockAndKpmCli(t, "TestCheckDepSumPass", testDepSumFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestCheckDepSumPass", TestFunc: testDepSumFunc}}) } func TestCheckDepSumFailed(t *testing.T) { @@ -117,5 +117,5 @@ func TestCheckDepSumFailed(t *testing.T) { "expected '9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U=', got 'invalid_sum'") } - RunTestWithGlobalLockAndKpmCli(t, "TestCheckDepSumFailed", testDepSumFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestCheckDepSumFailed", TestFunc: testDepSumFunc}}) } diff --git a/pkg/client/init.go b/pkg/client/init.go index d6d3ff56..07d685b6 100644 --- a/pkg/client/init.go +++ b/pkg/client/init.go @@ -2,14 +2,105 @@ package client import ( "fmt" + "os" "path/filepath" + "github.com/hashicorp/go-version" "kcl-lang.io/kpm/pkg/constants" + "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" "kcl-lang.io/kpm/pkg/utils" ) +// InitOptions contains the options for initializing a kcl package. +type InitOptions struct { + ModPath string + ModName string + ModVersion string + WorkDir string +} + +type InitOption func(*InitOptions) error + +func WithInitWorkDir(workDir string) InitOption { + return func(opts *InitOptions) error { + opts.WorkDir = workDir + return nil + } +} + +func WithInitModVersion(modVersion string) InitOption { + return func(opts *InitOptions) error { + opts.ModVersion = modVersion + return nil + } +} + +func WithInitModPath(modPath string) InitOption { + return func(opts *InitOptions) error { + opts.ModPath = modPath + return nil + } +} + +func WithInitModName(modName string) InitOption { + return func(opts *InitOptions) error { + opts.ModName = modName + return nil + } +} + +func (c *KpmClient) Init(options ...InitOption) error { + opts := &InitOptions{} + for _, option := range options { + if err := option(opts); err != nil { + return err + } + } + + modPath := opts.ModPath + modName := opts.ModName + modVer := opts.ModVersion + + if modVer != "" { + _, err := version.NewVersion(modVer) + if err != nil { + return err + } + } + + workDir, err := filepath.Abs(opts.WorkDir) + if err != nil { + return err + } + + if !filepath.IsAbs(modPath) { + modPath = filepath.Join(workDir, modPath) + } + + if len(modName) == 0 { + modName = filepath.Base(modPath) + } else { + modPath = filepath.Join(modPath, modName) + } + + if !utils.DirExists(modPath) { + err := os.MkdirAll(modPath, os.ModePerm) + if err != nil { + return err + } + } + + kclPkg := pkg.NewKclPkg(&opt.InitOptions{ + InitPath: modPath, + Name: modName, + Version: modVer, + }) + + return c.InitEmptyPkg(&kclPkg) +} + // createIfNotExist will create a file if it does not exist. func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) error { reporter.ReportMsgTo(fmt.Sprintf("creating new :%s", filepath), c.GetLogWriter()) diff --git a/pkg/client/init_test.go b/pkg/client/init_test.go new file mode 100644 index 00000000..433d782d --- /dev/null +++ b/pkg/client/init_test.go @@ -0,0 +1,295 @@ +package client + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "kcl-lang.io/kpm/pkg/downloader" + pkg "kcl-lang.io/kpm/pkg/package" + "kcl-lang.io/kpm/pkg/runner" + "kcl-lang.io/kpm/pkg/utils" +) + +// test 'kcl mod init ' +func TestModInitPath(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init") + defer os.RemoveAll(workDir) + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitModPath(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "main.k"))) + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "init") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.0.1") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitPath", TestFunc: testFunc}}) +} + +// test 'kcl mod init ' +func TestModInitName(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_0") + defer os.RemoveAll(filepath.Join(workDir, "InitModName")) + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitWorkDir(workDir), + WithInitModName("InitModName"), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "InitModName", "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "InitModName", "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "InitModName", "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(workDir, "InitModName", "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(workDir, "InitModName", "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(filepath.Join(workDir, "InitModName")), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "InitModName") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.0.1") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitName", TestFunc: testFunc}}) +} + +// test 'kcl mod init --version ' +func TestModInitOnlyVersion(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_1") + defer func() { + _ = os.Remove(filepath.Join(workDir, "kcl.mod")) + _ = os.Remove(filepath.Join(workDir, "kcl.mod.lock")) + _ = os.Remove(filepath.Join(workDir, "main.k")) + }() + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitWorkDir(workDir), + WithInitModVersion("0.1.100"), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "init_1") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.1.100") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitOnlyVersion", TestFunc: testFunc}}) +} + +// test 'kcl mod init --version ' +func TestModInitNameVersion(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_1") + modPath := filepath.Join(workDir, "InitModName") + defer func() { + os.RemoveAll(modPath) + }() + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitWorkDir(workDir), + WithInitModName("InitModName"), + WithInitModVersion("0.1.100"), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(modPath, "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(modPath, "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "InitModName") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.1.100") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitNameVersion", TestFunc: testFunc}}) +} + +// test 'kcl mod init --version --path ' +func TestModInitNameVersionPath(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_2") + modPath := filepath.Join(workDir, "InitModName") + defer func() { + _ = os.RemoveAll(modPath) + }() + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitModPath(workDir), + WithInitModName("InitModName"), + WithInitModVersion("0.1.100"), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(modPath, "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(modPath, "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(modPath, "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(modPath), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "InitModName") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.1.100") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitNameVersionPath", TestFunc: testFunc}}) +} + +// test 'kcl mod init' +func TestModInitPwd(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_4") + defer func() { + _ = os.Remove(filepath.Join(workDir, "kcl.mod")) + _ = os.Remove(filepath.Join(workDir, "kcl.mod.lock")) + _ = os.Remove(filepath.Join(workDir, "main.k")) + }() + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitWorkDir(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "init_4") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.0.1") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + } + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitPwd", TestFunc: testFunc}}) +} + +// test 'kcl mod init' with existing mod file +func TestModInitWithExistFile(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testPath := getTestDir("test_init") + workDir := filepath.Join(testPath, "init_5") + + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + + err := kpmcli.Init( + WithInitWorkDir(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(buf.String()), + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod"))+ + fmt.Sprintf("'%s' already exists", filepath.Join(workDir, "kcl.mod"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "kcl.mod.lock"))+ + fmt.Sprintf("'%s' already exists", filepath.Join(workDir, "kcl.mod.lock"))+ + fmt.Sprintf("creating new :%s", filepath.Join(workDir, "main.k"))+ + fmt.Sprintf("'%s' already exists", filepath.Join(workDir, "main.k"))) + + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod"))) + assert.True(t, utils.DirExists(filepath.Join(workDir, "kcl.mod.lock"))) + + kmod, err := pkg.LoadKclPkgWithOpts( + pkg.WithPath(workDir), + ) + + assert.Nil(t, err) + assert.Equal(t, kmod.ModFile.Pkg.Name, "init_5_exist") + assert.Equal(t, kmod.ModFile.Pkg.Version, "0.1.1") + assert.Equal(t, kmod.ModFile.Pkg.Edition, runner.GetKclVersion()) + assert.Equal(t, kmod.ModFile.Dependencies.Deps.Len(), 1) + assert.Equal(t, kmod.Dependencies.Deps.Len(), 1) + + res, err := kpmcli.Run( + WithRunSource(&downloader.Source{ + Local: &downloader.Local{ + Path: workDir, + }, + }), + ) + + assert.Nil(t, err) + assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), + "The_first_kcl_program: Hello World!"+"The_second_kcl_program: test") + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestModInitWithExitModFile", TestFunc: testFunc}}) +} diff --git a/pkg/client/issues_test.go b/pkg/client/issues_test.go index 1eaa0115..247bdef5 100644 --- a/pkg/client/issues_test.go +++ b/pkg/client/issues_test.go @@ -93,7 +93,7 @@ func TestKclIssue1760(t *testing.T) { assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!") } - RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tc.name, TestFunc: testFunc}}) } } @@ -205,7 +205,7 @@ func TestKpmIssue550(t *testing.T) { } } - RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tc.name, TestFunc: testFunc}}) } } @@ -426,7 +426,7 @@ func TestKpmIssue226(t *testing.T) { assert.Equal(t, utils.RmNewline(string(lockFileContent)), utils.RmNewline(string(lockFileExpectContent))) } - RunTestWithGlobalLockAndKpmCli(t, "add_dep_with_git_commit", test_add_dep_with_git_commit) - RunTestWithGlobalLockAndKpmCli(t, "update_with_git_commit", test_update_with_git_commit) - RunTestWithGlobalLockAndKpmCli(t, "update_with_git_commit_invalid", test_update_with_git_commit_invalid) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "add_dep_with_git_commit", TestFunc: test_add_dep_with_git_commit}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "update_with_git_commit", TestFunc: test_update_with_git_commit}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "update_with_git_commit_invalid", TestFunc: test_update_with_git_commit_invalid}}) } diff --git a/pkg/client/run_test.go b/pkg/client/run_test.go index 890eb9a3..42d2ead7 100644 --- a/pkg/client/run_test.go +++ b/pkg/client/run_test.go @@ -82,28 +82,28 @@ func testRunWithModSpecVersion(t *testing.T, kpmcli *KpmClient) { func TestRun(t *testing.T) { features.Enable(features.SupportNewStorage) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader) - RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep) - RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor) - RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid) - RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithGitPackage", testRunWithGitPackage) - RunTestWithGlobalLockAndKpmCli(t, "TestRunGit", testRunGit) - RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithOciDownloader", TestFunc: testRunWithOciDownloader}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunDefaultRegistryDep", TestFunc: testRunDefaultRegistryDep}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunInVendor", TestFunc: testRunInVendor}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunRemoteWithArgsInvalid", TestFunc: testRunRemoteWithArgsInvalid}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunRemoteWithArgs", TestFunc: testRunRemoteWithArgs}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithNoSumCheck", TestFunc: testRunWithNoSumCheck}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithGitPackage", TestFunc: testRunWithGitPackage}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunGit", TestFunc: testRunGit}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunOciWithSettingsFile", TestFunc: testRunOciWithSettingsFile}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithModSpecVersion", TestFunc: testRunWithModSpecVersion}}) features.Disable(features.SupportNewStorage) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader) - RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep) - RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor) - RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid) - RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithGitPackage", testRunWithGitPackage) - RunTestWithGlobalLockAndKpmCli(t, "TestRunGit", testRunGit) - RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile) - RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithOciDownloader", TestFunc: testRunWithOciDownloader}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunDefaultRegistryDep", TestFunc: testRunDefaultRegistryDep}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunInVendor", TestFunc: testRunInVendor}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunRemoteWithArgsInvalid", TestFunc: testRunRemoteWithArgsInvalid}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunRemoteWithArgs", TestFunc: testRunRemoteWithArgs}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithNoSumCheck", TestFunc: testRunWithNoSumCheck}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithGitPackage", TestFunc: testRunWithGitPackage}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunGit", TestFunc: testRunGit}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunOciWithSettingsFile", TestFunc: testRunOciWithSettingsFile}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestRunWithModSpecVersion", TestFunc: testRunWithModSpecVersion}}) } func TestRunWithHyphenEntries(t *testing.T) { testFunc := func(t *testing.T, kpmcli *KpmClient) { @@ -131,5 +131,5 @@ func TestRunWithHyphenEntries(t *testing.T) { assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expect))) } - RunTestWithGlobalLockAndKpmCli(t, "testRunWithHyphenEntries", testFunc) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "testRunWithHyphenEntries", TestFunc: testFunc}}) } diff --git a/pkg/client/test.go b/pkg/client/test.go index 712c0eef..b6c5897a 100644 --- a/pkg/client/test.go +++ b/pkg/client/test.go @@ -1,7 +1,6 @@ package client import ( - "fmt" "os" "path/filepath" "testing" @@ -26,35 +25,41 @@ func initTestDir(subDir string) string { return testDir } -// Use a global variable to store the kpmcli instance. -func RunTestWithGlobalLockAndKpmCli(t *testing.T, name string, testFunc func(t *testing.T, kpmcli *KpmClient)) { - t.Run(name, func(t *testing.T) { - kpmcli, err := NewKpmClient() - if err != nil { - t.Errorf("Error acquiring lock: %v", err) - } - err = kpmcli.AcquirePackageCacheLock() - if err != nil { - t.Errorf("Error acquiring lock: %v", err) - } +type TestSuite struct { + Name string + TestFunc func(t *testing.T, kpmcli *KpmClient) +} - defer func() { - err = kpmcli.ReleasePackageCacheLock() - if err != nil { - t.Errorf("Error acquiring lock: %v", err) - } - }() +// Use a global variable to store the kpmcli instance. +func RunTestWithGlobalLockAndKpmCli(t *testing.T, testSuites []TestSuite) { + kpmcli, err := NewKpmClient() + if err != nil { + t.Errorf("Error acquiring lock: %v", err) + } + err = kpmcli.AcquirePackageCacheLock() + if err != nil { + t.Errorf("Error acquiring lock: %v", err) + } - // create a tmp dir as kpm home for test - tmpDir, err := os.MkdirTemp("", "") + defer func() { + err = kpmcli.ReleasePackageCacheLock() if err != nil { t.Errorf("Error acquiring lock: %v", err) } - // clean the temp dir. - defer os.RemoveAll(tmpDir) - kpmcli.SetHomePath(tmpDir) + }() + + // create a tmp dir as kpm home for test + tmpDir, err := os.MkdirTemp("", "") + if err != nil { + t.Errorf("Error acquiring lock: %v", err) + } + // clean the temp dir. + defer os.RemoveAll(tmpDir) + kpmcli.SetHomePath(tmpDir) - testFunc(t, kpmcli) - fmt.Printf("%s completed\n", name) - }) + for _, testSuite := range testSuites { + t.Run(testSuite.Name, func(t *testing.T) { + testSuite.TestFunc(t, kpmcli) + }) + } } diff --git a/pkg/client/test_data/test_init/init_0/.gitkeep b/pkg/client/test_data/test_init/init_0/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_init/init_1/.gitkeep b/pkg/client/test_data/test_init/init_1/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_init/init_2/.gitkeep b/pkg/client/test_data/test_init/init_2/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_init/init_3/.gitkeep b/pkg/client/test_data/test_init/init_3/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_init/init_4/.gitkeep b/pkg/client/test_data/test_init/init_4/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_init/init_5/kcl.mod b/pkg/client/test_data/test_init/init_5/kcl.mod new file mode 100644 index 00000000..dc1470b5 --- /dev/null +++ b/pkg/client/test_data/test_init/init_5/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "init_5_exist" +edition = "v0.10.0" +version = "0.1.1" + +[dependencies] +helloworld = "0.1.4" diff --git a/pkg/client/test_data/test_init/init_5/kcl.mod.lock b/pkg/client/test_data/test_init/init_5/kcl.mod.lock new file mode 100644 index 00000000..52056478 --- /dev/null +++ b/pkg/client/test_data/test_init/init_5/kcl.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.4" + version = "0.1.4" + sum = "9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U=" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.4" diff --git a/pkg/client/test_data/test_init/init_5/main.k b/pkg/client/test_data/test_init/init_5/main.k new file mode 100644 index 00000000..c862d51c --- /dev/null +++ b/pkg/client/test_data/test_init/init_5/main.k @@ -0,0 +1,4 @@ +import helloworld as hw + +The_first_kcl_program = hw.The_first_kcl_program +The_second_kcl_program = "test" \ No newline at end of file diff --git a/pkg/client/update_test.go b/pkg/client/update_test.go index 701a115a..dba38d1e 100644 --- a/pkg/client/update_test.go +++ b/pkg/client/update_test.go @@ -12,12 +12,12 @@ import ( ) func TestUpdate(t *testing.T) { - RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclMod", testUpdateWithKclMod) - RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclModlock", testUpdateWithKclModlock) - RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithNoSumCheck", testUpdateWithNoSumCheck) - RunTestWithGlobalLockAndKpmCli(t, "TestUpdateDefaultRegistryDep", testUpdateDefaultRegistryDep) - RunTestWithGlobalLockAndKpmCli(t, "TestUpdateWithKclModAndLock", testUpdateKclModAndLock) - RunTestWithGlobalLockAndKpmCli(t, "TestUpdate", testUpdate) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdateWithKclMod", TestFunc: testUpdateWithKclMod}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdateWithKclModlock", TestFunc: testUpdateWithKclModlock}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdateWithNoSumCheck", TestFunc: testUpdateWithNoSumCheck}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdateDefaultRegistryDep", TestFunc: testUpdateDefaultRegistryDep}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdateWithKclModAndLock", TestFunc: testUpdateKclModAndLock}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestUpdate", TestFunc: testUpdate}}) } func testUpdate(t *testing.T, kpmcli *KpmClient) { diff --git a/pkg/client/vendor_test.go b/pkg/client/vendor_test.go index b9117eee..ab6754cd 100644 --- a/pkg/client/vendor_test.go +++ b/pkg/client/vendor_test.go @@ -107,6 +107,6 @@ func testVendorWithMVS(t *testing.T, kpmcli *KpmClient) { } func TestVendorWithGlobalLock(t *testing.T) { - RunTestWithGlobalLockAndKpmCli(t, "TestVendorDeps", testVendorDeps) - RunTestWithGlobalLockAndKpmCli(t, "TestVendorWithMVS", testVendorWithMVS) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestVendorDeps", TestFunc: testVendorDeps}}) + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestVendorWithMVS", TestFunc: testVendorWithMVS}}) }