Skip to content

Commit 5f0a751

Browse files
authored
feat: add more test cases for api 'Init' (#560)
Signed-off-by: zongz <zongzhe1024@163.com>
1 parent cb9aa1e commit 5f0a751

File tree

17 files changed

+480
-69
lines changed

17 files changed

+480
-69
lines changed

pkg/client/add_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestAddWithModSpec(t *testing.T) {
167167
assert.Equal(t, utils.RmNewline(tt.msg), utils.RmNewline(buf.String()))
168168
}
169169

170-
RunTestWithGlobalLockAndKpmCli(t, tt.name, testFunc)
170+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tt.name, TestFunc: testFunc}})
171171

172172
expectedMod, err := os.ReadFile(modExpect)
173173
if err != nil {
@@ -257,7 +257,7 @@ func TestAddRenameWithModSpec(t *testing.T) {
257257
), utils.RmNewline(buf.String()))
258258
}
259259

260-
RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithModSpec", testFunc)
260+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddRenameWithModSpec", TestFunc: testFunc}})
261261

262262
expectedMod, err := os.ReadFile(modExpect)
263263
if err != nil {
@@ -374,7 +374,7 @@ func TestAddWithOnlyModSpec(t *testing.T) {
374374
assert.Equal(t, utils.RmNewline(tc.msg), utils.RmNewline(buf.String()))
375375
}
376376

377-
RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc)
377+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: tc.name, TestFunc: testFunc}})
378378

379379
expectedMod, err := os.ReadFile(modExpect)
380380
if err != nil {
@@ -473,7 +473,7 @@ func TestAddRenameWithNoSpec(t *testing.T) {
473473
}
474474
}
475475

476-
RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithNoSpec", testFunc)
476+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestAddRenameWithNoSpec", TestFunc: testFunc}})
477477

478478
expectedMod, err := os.ReadFile(modExpect)
479479
if err != nil {

pkg/client/check_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestModCheckPass(t *testing.T) {
2727
t.Fatalf("failed to check kcl package: %v", err)
2828
}
2929
}
30-
RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_pass", testModCheckPass)
30+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_pass", TestFunc: testModCheckPass}})
3131
}
3232

3333
func TestModCheckNameFailed(t *testing.T) {
@@ -47,7 +47,7 @@ func TestModCheckNameFailed(t *testing.T) {
4747

4848
assert.Equal(t, err.Error(), "invalid name: invalid/mod/name")
4949
}
50-
RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_name_failed", testModCheckNameFailed)
50+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_name_failed", TestFunc: testModCheckNameFailed}})
5151
}
5252

5353
func TestModCheckVersionFailed(t *testing.T) {
@@ -66,7 +66,7 @@ func TestModCheckVersionFailed(t *testing.T) {
6666

6767
assert.Equal(t, err.Error(), "invalid version: invalid_version for version_failed")
6868
}
69-
RunTestWithGlobalLockAndKpmCli(t, "test_mod_check_version_failed", testModCheckVersionFailed)
69+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_mod_check_version_failed", TestFunc: testModCheckVersionFailed}})
7070
}
7171

7272
func TestCheckDepSumPass(t *testing.T) {
@@ -89,7 +89,7 @@ func TestCheckDepSumPass(t *testing.T) {
8989
}
9090
}
9191

92-
RunTestWithGlobalLockAndKpmCli(t, "TestCheckDepSumPass", testDepSumFunc)
92+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestCheckDepSumPass", TestFunc: testDepSumFunc}})
9393
}
9494

9595
func TestCheckDepSumFailed(t *testing.T) {
@@ -117,5 +117,5 @@ func TestCheckDepSumFailed(t *testing.T) {
117117
"expected '9J9HOMhdypaDYf0J7PqtpGTdlkbxkN0HFEYhosHhf4U=', got 'invalid_sum'")
118118
}
119119

120-
RunTestWithGlobalLockAndKpmCli(t, "TestCheckDepSumFailed", testDepSumFunc)
120+
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "TestCheckDepSumFailed", TestFunc: testDepSumFunc}})
121121
}

pkg/client/init.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,105 @@ package client
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67

8+
"github.com/hashicorp/go-version"
79
"kcl-lang.io/kpm/pkg/constants"
10+
"kcl-lang.io/kpm/pkg/opt"
811
pkg "kcl-lang.io/kpm/pkg/package"
912
"kcl-lang.io/kpm/pkg/reporter"
1013
"kcl-lang.io/kpm/pkg/utils"
1114
)
1215

16+
// InitOptions contains the options for initializing a kcl package.
17+
type InitOptions struct {
18+
ModPath string
19+
ModName string
20+
ModVersion string
21+
WorkDir string
22+
}
23+
24+
type InitOption func(*InitOptions) error
25+
26+
func WithInitWorkDir(workDir string) InitOption {
27+
return func(opts *InitOptions) error {
28+
opts.WorkDir = workDir
29+
return nil
30+
}
31+
}
32+
33+
func WithInitModVersion(modVersion string) InitOption {
34+
return func(opts *InitOptions) error {
35+
opts.ModVersion = modVersion
36+
return nil
37+
}
38+
}
39+
40+
func WithInitModPath(modPath string) InitOption {
41+
return func(opts *InitOptions) error {
42+
opts.ModPath = modPath
43+
return nil
44+
}
45+
}
46+
47+
func WithInitModName(modName string) InitOption {
48+
return func(opts *InitOptions) error {
49+
opts.ModName = modName
50+
return nil
51+
}
52+
}
53+
54+
func (c *KpmClient) Init(options ...InitOption) error {
55+
opts := &InitOptions{}
56+
for _, option := range options {
57+
if err := option(opts); err != nil {
58+
return err
59+
}
60+
}
61+
62+
modPath := opts.ModPath
63+
modName := opts.ModName
64+
modVer := opts.ModVersion
65+
66+
if modVer != "" {
67+
_, err := version.NewVersion(modVer)
68+
if err != nil {
69+
return err
70+
}
71+
}
72+
73+
workDir, err := filepath.Abs(opts.WorkDir)
74+
if err != nil {
75+
return err
76+
}
77+
78+
if !filepath.IsAbs(modPath) {
79+
modPath = filepath.Join(workDir, modPath)
80+
}
81+
82+
if len(modName) == 0 {
83+
modName = filepath.Base(modPath)
84+
} else {
85+
modPath = filepath.Join(modPath, modName)
86+
}
87+
88+
if !utils.DirExists(modPath) {
89+
err := os.MkdirAll(modPath, os.ModePerm)
90+
if err != nil {
91+
return err
92+
}
93+
}
94+
95+
kclPkg := pkg.NewKclPkg(&opt.InitOptions{
96+
InitPath: modPath,
97+
Name: modName,
98+
Version: modVer,
99+
})
100+
101+
return c.InitEmptyPkg(&kclPkg)
102+
}
103+
13104
// createIfNotExist will create a file if it does not exist.
14105
func (c *KpmClient) createIfNotExist(filepath string, storeFunc func() error) error {
15106
reporter.ReportMsgTo(fmt.Sprintf("creating new :%s", filepath), c.GetLogWriter())

0 commit comments

Comments
 (0)