|
| 1 | +package smarttest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "strconv" |
| 10 | + |
| 11 | + "github.com/signadot/cli/internal/config" |
| 12 | + "github.com/signadot/cli/internal/print" |
| 13 | + "github.com/signadot/cli/internal/repoconfig" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +func newList(tConfig *config.SmartTest) *cobra.Command { |
| 18 | + cfg := &config.SmartTestList{ |
| 19 | + SmartTest: tConfig, |
| 20 | + } |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "list", |
| 23 | + Short: "List local tests", |
| 24 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 25 | + return list(cmd.Context(), cfg, cmd.OutOrStdout(), cmd.ErrOrStderr(), args) |
| 26 | + }, |
| 27 | + } |
| 28 | + cfg.AddFlags(cmd) |
| 29 | + return cmd |
| 30 | +} |
| 31 | + |
| 32 | +func list(ctx context.Context, cfg *config.SmartTestList, wOut, wErr io.Writer, |
| 33 | + args []string) error { |
| 34 | + if err := cfg.InitAPIConfig(); err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + if err := validateList(cfg); err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + testFiles, _, err := testFilesAndRepo(cfg) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + // render the structured output |
| 45 | + return listOutput(cfg, wOut, testFiles) |
| 46 | +} |
| 47 | + |
| 48 | +func testFilesAndRepo(cfg *config.SmartTestList) ([]repoconfig.TestFile, *repoconfig.GitRepo, error) { |
| 49 | + if cfg.File == "-" { |
| 50 | + host, err := os.Hostname() |
| 51 | + if err != nil { |
| 52 | + host = "unknown" |
| 53 | + } |
| 54 | + pid := strconv.Itoa(os.Getpid()) |
| 55 | + return []repoconfig.TestFile{ |
| 56 | + { |
| 57 | + Name: host + "-" + "stdin-" + pid, |
| 58 | + Reader: os.Stdin, |
| 59 | + }, |
| 60 | + }, nil, nil |
| 61 | + } |
| 62 | + // create a test finder |
| 63 | + // NOTE: at most one of cfg.{Dir,File} is non-empty |
| 64 | + tf, err := repoconfig.NewTestFinder(cfg.Directory+cfg.File, cfg.FilterLabels, cfg.WithoutLabels) |
| 65 | + if err != nil { |
| 66 | + return nil, nil, err |
| 67 | + } |
| 68 | + |
| 69 | + // find tests |
| 70 | + testFiles, err := tf.FindTestFiles() |
| 71 | + if err != nil { |
| 72 | + return nil, nil, fmt.Errorf("error finding test files: %w", err) |
| 73 | + } |
| 74 | + if len(testFiles) == 0 { |
| 75 | + return nil, nil, errors.New("could not find any test") |
| 76 | + } |
| 77 | + return testFiles, tf.GetGitRepo(), nil |
| 78 | +} |
| 79 | + |
| 80 | +func validateList(cfg *config.SmartTestList) error { |
| 81 | + if cfg.Directory != "" && cfg.File != "" { |
| 82 | + return fmt.Errorf("cannot specify both directory and file") |
| 83 | + } |
| 84 | + if cfg.Directory != "" { |
| 85 | + st, err := os.Stat(cfg.Directory) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("unable to stat input directory: %w", err) |
| 88 | + } |
| 89 | + if !st.IsDir() { |
| 90 | + return fmt.Errorf("%q is not a directory", cfg.Directory) |
| 91 | + } |
| 92 | + } |
| 93 | + if cfg.File != "" && cfg.File != "-" { |
| 94 | + st, err := os.Stat(cfg.File) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("unable to stat input file: %w", err) |
| 97 | + } |
| 98 | + if st.IsDir() { |
| 99 | + return fmt.Errorf("%q is not a file", cfg.File) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +func listOutput(cfg *config.SmartTestList, w io.Writer, tfs []repoconfig.TestFile) error { |
| 107 | + switch cfg.OutputFormat { |
| 108 | + case config.OutputFormatDefault: |
| 109 | + for i := range tfs { |
| 110 | + tf := &tfs[i] |
| 111 | + if _, err := fmt.Fprintf(w, tf.Name+"\n"); err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + return nil |
| 116 | + case config.OutputFormatYAML: |
| 117 | + return print.RawYAML(w, tfs) |
| 118 | + case config.OutputFormatJSON: |
| 119 | + return print.RawJSON(w, tfs) |
| 120 | + default: |
| 121 | + return fmt.Errorf("unsupported output format: %q", cfg.OutputFormat) |
| 122 | + } |
| 123 | +} |
0 commit comments