Skip to content

Commit

Permalink
Merge pull request #22 from Peefy/add-run-entry-for-test
Browse files Browse the repository at this point in the history
feat: add run flags for the test tool
  • Loading branch information
Peefy authored Dec 1, 2023
2 parents d390787 + 31c6158 commit 955f2e5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 21 deletions.
24 changes: 14 additions & 10 deletions cmd/kcl/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ import (
)

func appendLangFlags(o *options.RunOptions, flags *pflag.FlagSet) {
flags.StringSliceVarP(&o.Arguments, "argument", "D", []string{},
"Specify the top-level argument")
flags.StringSliceVarP(&o.Settings, "setting", "Y", []string{},
"Specify the command line setting files")
flags.StringSliceVarP(&o.Overrides, "overrides", "O", []string{},
"Specify the configuration override path and value")
flags.StringSliceVarP(&o.PathSelectors, "path_selector", "S", []string{},
"Specify the path selectors")
flags.StringSliceVarP(&o.ExternalPackages, "external", "E", []string{},
"Specify the mapping of package name and path where the package is located")
flags.StringVarP(&o.Output, "output", "o", "",
"Specify the YAML/JSON output file path")
flags.StringVarP(&o.Tag, "tag", "t", "",
Expand All @@ -24,16 +16,28 @@ func appendLangFlags(o *options.RunOptions, flags *pflag.FlagSet) {
"Specify the output format")
flags.BoolVarP(&o.DisableNone, "disable_none", "n", false,
"Disable dumping None values")
flags.BoolVarP(&o.StrictRangeCheck, "strict_range_check", "r", false,
"Do perform strict numeric range checks")
flags.BoolVarP(&o.Debug, "debug", "d", false,
"Run in debug mode")
flags.BoolVarP(&o.SortKeys, "sort_keys", "k", false,
"Sort output result keys")
appendRunnerFlags(o, flags)
}

func appendRunnerFlags(o *options.RunOptions, flags *pflag.FlagSet) {
flags.StringSliceVarP(&o.Arguments, "argument", "D", []string{},
"Specify the top-level argument")
flags.StringSliceVarP(&o.Settings, "setting", "Y", []string{},
"Specify the command line setting files")
flags.StringSliceVarP(&o.Overrides, "overrides", "O", []string{},
"Specify the configuration override path and value")
flags.StringSliceVarP(&o.ExternalPackages, "external", "E", []string{},
"Specify the mapping of package name and path where the package is located")
flags.BoolVarP(&o.Vendor, "vendor", "V", false,
"Run in vendor mode")
flags.BoolVar(&o.NoStyle, "no_style", false,
"Set to prohibit output of command line waiting styles, including colors, etc.")
flags.BoolVarP(&o.Quiet, "quiet", "q", false,
"Set the quiet mode (no output)")
flags.BoolVarP(&o.StrictRangeCheck, "strict_range_check", "r", false,
"Do perform strict numeric range checks")
}
46 changes: 35 additions & 11 deletions cmd/kcl/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package cmd

import (
"errors"
"fmt"
"os"

"github.com/acarl005/stripansi"
"github.com/spf13/cobra"
"kcl-lang.io/cli/pkg/options"
kcl "kcl-lang.io/kcl-go"
"kcl-lang.io/kcl-go/pkg/tools/testing"
)
Expand Down Expand Up @@ -34,6 +37,7 @@ that starts with "test_*".
// NewTestCmd returns the test command.
func NewTestCmd() *cobra.Command {
o := new(kcl.TestOptions)
runOpts := options.NewRunOptions()
cmd := &cobra.Command{
Use: "test",
Short: "KCL test tool",
Expand All @@ -44,17 +48,7 @@ func NewTestCmd() *cobra.Command {
args = append(args, ".")
}
o.PkgList = args
result, err := kcl.Test(o)
if err != nil {
return err
}
if len(result.Info) == 0 {
fmt.Println("no test files")
return nil
} else {
reporter := testing.DefaultReporter(os.Stdout)
return reporter.Report(&result)
}
return test(o, runOpts)
},
SilenceUsage: true,
Aliases: []string{"t"},
Expand All @@ -65,6 +59,36 @@ func NewTestCmd() *cobra.Command {
"Exist when meet the first fail test case in the test process.")
flags.StringVar(&o.RunRegRxp, "run", "",
"If specified, only run tests containing this string in their names.")
appendRunnerFlags(runOpts, flags)

return cmd
}

func test(o *kcl.TestOptions, runOpts *options.RunOptions) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
depsOpt, err := options.LoadDepsFrom(pwd, runOpts.Quiet)
if err != nil {
return err
}
result, err := kcl.Test(
o,
*options.CompileOptionFromCli(runOpts).Option,
*depsOpt,
)
if err != nil {
if runOpts.NoStyle {
err = errors.New(stripansi.Strip(err.Error()))
}
return err
}
if len(result.Info) == 0 {
fmt.Println("no test files")
return nil
} else {
reporter := testing.DefaultReporter(os.Stdout)
return reporter.Report(&result)
}
}
3 changes: 3 additions & 0 deletions examples/abstraction/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[package]
name = "abstraction"

[dependencies]
k8s = "1.28"
9 changes: 9 additions & 0 deletions examples/abstraction/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
sum = "aTxPUVZyr9MdiB3YdiY/8pCh9sC55yURnZdGlJsKG6Q="
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.28"
34 changes: 34 additions & 0 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"kcl-lang.io/kpm/pkg/api"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/runner"
)

Expand Down Expand Up @@ -266,3 +267,36 @@ func CompileOptionFromCli(o *RunOptions) *opt.CompileOptions {

return opts
}

// LoadDepsFrom parses the kcl external package option from a path.
// It will find `kcl.mod` recursively from the path, resolve deps
// in the `kcl.mod` and return the option. If not found, return the
// empty option.
func LoadDepsFrom(path string, quiet bool) (*kcl.Option, error) {
o := kcl.NewOption()
entry, errEvent := runner.FindRunEntryFrom([]string{path})
if errEvent != nil {
return o, errEvent
}
if entry.IsLocalFileWithKclMod() {
cli, err := client.NewKpmClient()
if err != nil {
return o, err
}
if quiet {
cli.SetLogWriter(nil)
}
pkg, err := pkg.LoadKclPkg(entry.PackageSource())
if err != nil {
return o, err
}
depsMap, err := cli.ResolveDepsIntoMap(pkg)
if err != nil {
return o, err
}
for depName, depPath := range depsMap {
o.Merge(kcl.WithExternalPkgs(fmt.Sprintf("%s=%s", depName, depPath)))
}
}
return o, nil
}

0 comments on commit 955f2e5

Please sign in to comment.