Skip to content

Commit a0277df

Browse files
authored
Merge pull request #15 from Peefy/feat-test-tool-cli
feat: add test tool cli and add e2e test cases
2 parents 075ce50 + 26cfa58 commit a0277df

File tree

9 files changed

+113
-11
lines changed

9 files changed

+113
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ coverage.out
3131
build/
3232
.vscode/
3333
.kclvm/
34+
_kcl_test.k
3435

3536
.DS_store

cmd/kcl/commands/play.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func runPlayground(addr string) error {
5757
fmt.Printf("[Info] Playground listens at %s\n", addr)
5858
go func() {
5959
time.Sleep(time.Second * 2)
60-
openBrowser(addr)
60+
_ = openBrowser(addr)
6161
}()
6262
return play.Run(addr, &opts)
6363
}

cmd/kcl/commands/root.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// fmt format tool
1313
// lint lint tool
1414
// vet vet tool
15-
// test unit/integration/benchmark test tool (Not yet implemented)
15+
// test unit/integration/benchmark test tool
1616
// deps dependency analysis, providing dependency diagrams for KCL modules and packages (Not yet implemented)
1717
// server run a KCL server to provider REST APIs for other applications
1818
// clean remove object files and cached files
@@ -109,6 +109,7 @@ func New() *cobra.Command {
109109
cmd.AddCommand(NewLintCmd())
110110
cmd.AddCommand(NewDocCmd())
111111
cmd.AddCommand(NewFmtCmd())
112+
cmd.AddCommand(NewTestCmd())
112113
cmd.AddCommand(NewVetCmd())
113114
cmd.AddCommand(NewCleanCmd())
114115
cmd.AddCommand(NewImportCmd())

cmd/kcl/commands/test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright The KCL Authors. All rights reserved.
2+
3+
package cmd
4+
5+
import (
6+
"fmt"
7+
"os"
8+
9+
"github.com/spf13/cobra"
10+
kcl "kcl-lang.io/kcl-go"
11+
"kcl-lang.io/kcl-go/pkg/tools/testing"
12+
)
13+
14+
const (
15+
testDesc = `
16+
This command automates testing the packages named by the import paths.
17+
18+
'KCL test' re-compiles each package along with any files with names matching
19+
the file pattern "*_test.k". These additional files can contain test functions
20+
that starts with "test_*".
21+
`
22+
testExample = ` # Test whole current package recursively
23+
kcl test ./...
24+
25+
# Test package named 'pkg'
26+
kcl test pkg
27+
28+
# Test with the fail fast mode.
29+
kcl test ./... --fail-fast
30+
31+
# Test with the regex expression filter 'test_func'
32+
kcl test ./... --run test_func
33+
`
34+
)
35+
36+
// NewTestCmd returns the test command.
37+
func NewTestCmd() *cobra.Command {
38+
o := new(kcl.TestOptions)
39+
cmd := &cobra.Command{
40+
Use: "test",
41+
Short: "KCL test tool",
42+
Long: testDesc,
43+
Example: testExample,
44+
RunE: func(_ *cobra.Command, args []string) error {
45+
if len(args) == 0 {
46+
args = append(args, ".")
47+
}
48+
o.PkgList = args
49+
result, err := kcl.Test(o)
50+
if err != nil {
51+
return err
52+
}
53+
if len(result.Info) == 0 {
54+
fmt.Println("no test files")
55+
return nil
56+
} else {
57+
reporter := testing.DefaultReporter(os.Stdout)
58+
return reporter.Report(&result)
59+
}
60+
},
61+
SilenceUsage: true,
62+
Aliases: []string{"t"},
63+
}
64+
65+
flags := cmd.Flags()
66+
flags.BoolVar(&o.FailFast, "fail-fast", false,
67+
"Exist when meet the first fail test case in the test process.")
68+
flags.StringVar(&o.RunRegRxp, "run", "",
69+
"If specified, only run tests containing this string in their names.")
70+
71+
return cmd
72+
}

examples/abstraction/Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ docker:
44
k8s:
55
kcl main.k kubernetes_render.k
66

7+
kcl-test:
8+
kcl test
9+
710
test:
811
make docker
9-
make k8s
12+
make k8s
13+
make kcl-test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import manifests
2+
3+
import .app
4+
5+
# Convert the `App` model into Kubernetes Deployment and Service Manifests
6+
test_kubernetesRender = lambda {
7+
a = app.App {
8+
name = "app"
9+
containers.ngnix = {
10+
image = "ngnix"
11+
ports = [{containerPort = 80}]
12+
}
13+
service.ports = [{ port = 80 }]
14+
}
15+
deployment_got = kubernetesRender(a)
16+
assert deployment_got[0].kind == "Deployment"
17+
assert deployment_got[1].kind == "Service"
18+
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.19
55
require (
66
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
77
github.com/spf13/cobra v1.7.0
8-
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231114095059-90254869c6e3
8+
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231124040835-17b297a9a177
99
kcl-lang.io/kcl-openapi v0.5.2-0.20231116071001-d8316c05cd2d
1010
kcl-lang.io/kcl-playground v0.5.1-0.20230919072953-347ab8959295
1111
kcl-lang.io/kpm v0.4.1
@@ -81,7 +81,7 @@ require (
8181
k8s.io/klog/v2 v2.100.1 // indirect
8282
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
8383
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
84-
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2 // indirect
84+
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1 // indirect
8585
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
8686
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
8787
sigs.k8s.io/yaml v1.3.0 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -1284,10 +1284,10 @@ k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
12841284
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
12851285
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
12861286
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
1287-
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2 h1:ct6HPq+ssN4kNwMnyqyJAJ35YGiWPMgV4vWyyqzqpYo=
1288-
kcl-lang.io/kcl-artifact-go v0.7.0-alpha.2/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
1289-
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231114095059-90254869c6e3 h1:sgTak3oF6blGPTvooh0hvA3HFKwNdSC/Df5SVG2Gb+w=
1290-
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231114095059-90254869c6e3/go.mod h1:GnXGUfNp+72g4ichID2VkfI679SCLEnmSuTPF7+N1H0=
1287+
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1 h1:u1l/OXWblOiDHabN+6ApSHTZwNyOskuOCf90fXreGu4=
1288+
kcl-lang.io/kcl-artifact-go v0.7.0-beta.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
1289+
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231124040835-17b297a9a177 h1:sCggavAK0yBFLQE2D+wsYZFbJfLmFhz5PDrakcc3H2U=
1290+
kcl-lang.io/kcl-go v0.7.0-alpha.2.0.20231124040835-17b297a9a177/go.mod h1:yf8pMkic+1m4wedG9MLxrkeVoqFhIrMbY+x4nPPg2O8=
12911291
kcl-lang.io/kcl-openapi v0.5.2-0.20231116071001-d8316c05cd2d h1:wbaI/FjzeMbzFGEjzBBA4o5AotRAy376IoHXZh/oNJQ=
12921292
kcl-lang.io/kcl-openapi v0.5.2-0.20231116071001-d8316c05cd2d/go.mod h1:Ai9mFztCVKkRSFabczO/r5hCNdqaNtAc2ZIRxTeV0Mk=
12931293
kcl-lang.io/kcl-playground v0.5.1-0.20230919072953-347ab8959295 h1:RUY3w6jNAB6GoMo27CO1oS4C4gjyWdMXfOTNpTEnkjs=

pkg/options/run.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,20 @@ func (o *RunOptions) writeResult(result *kcl.KCLResultList) error {
183183
}
184184

185185
if o.Output == "" {
186-
o.Writer.Write(output)
186+
_, err := o.Writer.Write(output)
187+
if err != nil {
188+
return err
189+
}
187190
} else {
188191
file, err := os.OpenFile(o.Output, os.O_CREATE|os.O_RDWR, 0744)
189192
if err != nil {
190193
return err
191194
}
192195
defer file.Close()
193-
file.Write(output)
196+
_, err = file.Write(output)
197+
if err != nil {
198+
return err
199+
}
194200
}
195201
return nil
196202
}

0 commit comments

Comments
 (0)