From e932351ab7099f2ea0f18ebb491dcce89a33f970 Mon Sep 17 00:00:00 2001 From: Michal Gorecki Date: Thu, 20 Feb 2025 15:57:43 +0100 Subject: [PATCH] newt: Add --list flag to test command Now it's possible to show all available unit tests and testable packages --- newt/cli/build_cmds.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/newt/cli/build_cmds.go b/newt/cli/build_cmds.go index ce16115368..ebb0c2f735 100644 --- a/newt/cli/build_cmds.go +++ b/newt/cli/build_cmds.go @@ -23,6 +23,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strings" "github.com/spf13/cobra" @@ -235,8 +236,8 @@ func pkgnames(pkgs []*pkg.LocalPackage) string { return s } -func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool) { - if len(args) < 1 { +func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell bool, list bool) { + if ((len(args) < 1) && !list) || ((len(args) > 0) && list) { NewtUsage(cmd, nil) } @@ -247,6 +248,30 @@ func testRunCmd(cmd *cobra.Command, args []string, exclude string, executeShell // Verify and resolve each specified package. testAll := false packs := []*pkg.LocalPackage{} + + if list { + packItfs := proj.PackagesOfType(pkg.PACKAGE_TYPE_UNITTEST) + testableNames := testablePkgList() + packs = make([]*pkg.LocalPackage, len(packItfs)) + for i, p := range packItfs { + packs[i] = p.(*pkg.LocalPackage) + } + + names := testableNames + for _, p := range packs { + names = append(names, p.FullName()) + } + + sort.Strings(names) + + for _, name := range names { + util.StatusMessage(util.VERBOSITY_DEFAULT, "%s\n", name) + + } + + return + } + for _, pkgName := range args { if pkgName == "all" { testAll = true @@ -470,16 +495,21 @@ func AddBuildCommands(cmd *cobra.Command) { }) var exclude string + var list bool testCmd := &cobra.Command{ Use: "test [package-names...] | all", Short: "Executes unit tests for one or more packages", Run: func(cmd *cobra.Command, args []string) { - testRunCmd(cmd, args, exclude, executeShell) + testRunCmd(cmd, args, exclude, executeShell, list) }, } testCmd.Flags().StringVarP(&exclude, "exclude", "e", "", "Comma separated list of packages to exclude") testCmd.Flags().BoolVar(&executeShell, "executeShell", false, "Execute build command using /bin/sh (Linux and MacOS only)") + testCmd.Flags().BoolVar(&list, + "list", false, + "Show all available unit tests") + cmd.AddCommand(testCmd) AddTabCompleteFn(testCmd, func() []string { return append(testablePkgList(), "all", "allexcept")