|
| 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 | +} |
0 commit comments