From f4c29012a7fa88981437c6a5a9305aa622c42d46 Mon Sep 17 00:00:00 2001 From: peefy Date: Thu, 30 Nov 2023 20:54:37 +0800 Subject: [PATCH] feat: add run flags for the test tool Signed-off-by: peefy --- cmd/kcl/commands/flags.go | 24 ++++++++++-------- cmd/kcl/commands/test.go | 41 ++++++++++++++++++++++--------- examples/abstraction/kcl.mod | 3 +++ examples/abstraction/kcl.mod.lock | 9 +++++++ pkg/options/run.go | 34 +++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 21 deletions(-) diff --git a/cmd/kcl/commands/flags.go b/cmd/kcl/commands/flags.go index 6e65492..c046f98 100644 --- a/cmd/kcl/commands/flags.go +++ b/cmd/kcl/commands/flags.go @@ -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", "", @@ -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") } diff --git a/cmd/kcl/commands/test.go b/cmd/kcl/commands/test.go index e97cc0e..078b69f 100644 --- a/cmd/kcl/commands/test.go +++ b/cmd/kcl/commands/test.go @@ -7,6 +7,7 @@ import ( "os" "github.com/spf13/cobra" + "kcl-lang.io/cli/pkg/options" kcl "kcl-lang.io/kcl-go" "kcl-lang.io/kcl-go/pkg/tools/testing" ) @@ -36,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", @@ -46,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"}, @@ -67,6 +59,33 @@ 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 { + return err + } + if len(result.Info) == 0 { + fmt.Println("no test files") + return nil + } else { + reporter := testing.DefaultReporter(os.Stdout) + return reporter.Report(&result) + } +} diff --git a/examples/abstraction/kcl.mod b/examples/abstraction/kcl.mod index c51d42c..e1fff80 100644 --- a/examples/abstraction/kcl.mod +++ b/examples/abstraction/kcl.mod @@ -1,2 +1,5 @@ [package] +name = "abstraction" +[dependencies] +k8s = "1.28" diff --git a/examples/abstraction/kcl.mod.lock b/examples/abstraction/kcl.mod.lock index e69de29..8c61a23 100644 --- a/examples/abstraction/kcl.mod.lock +++ b/examples/abstraction/kcl.mod.lock @@ -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" diff --git a/pkg/options/run.go b/pkg/options/run.go index b08a232..03e54a1 100644 --- a/pkg/options/run.go +++ b/pkg/options/run.go @@ -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" ) @@ -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 +}