Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more test cases for api 'Init' #560

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/client/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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}})
}
91 changes: 91 additions & 0 deletions pkg/client/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading
Loading