package modules import ( "cmp" "fmt" "os" "slices" "git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/s3" "git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/templates" "github.com/spf13/cobra" "github.com/spf13/viper" ) var testsCmd = &cobra.Command{ Use: "tests", Short: "Print tests info", Long: "Print all handled tests", Example: `s3-tests-parser tests s3-tests-parser tests --output tests.txt`, RunE: runTestsCmd, } const ( describeFlag = "describe" ) func initTestsCmd() { testsCmd.Flags().String(outputFlag, "", "file to write output, if missed the stdout is used") testsCmd.Flags().Bool(allFlag, false, "include all tests") testsCmd.Flags().Bool(describeFlag, false, "describe test (its group, skip status, reason etc.)") } type DescribedTest struct { Name string Group string Skip bool Comment string } func runTestsCmd(cmd *cobra.Command, _ []string) error { testStruct, err := s3.ParseTestsStruct() if err != nil { return err } includeAll := viper.GetBool(allFlag) var tests []DescribedTest for _, group := range testStruct.Groups { if group.Skip && !includeAll { continue } for _, test := range group.Tests { tests = append(tests, DescribedTest{ Name: test, Group: group.Name, Skip: group.Skip, Comment: group.Comment, }) } } slices.SortFunc(tests, func(a, b DescribedTest) int { return cmp.Compare(a.Name, b.Name) }) tests = slices.CompactFunc(tests, func(a DescribedTest, b DescribedTest) bool { return a.Name == b.Name }) if viper.GetBool(describeFlag) { return describeTests(cmd, tests) } return printTests(cmd, tests) } func printTests(cmd *cobra.Command, res []DescribedTest) error { w := cmd.OutOrStdout() if outFile := viper.GetString(outputFlag); outFile != "" { f, err := os.Create(outFile) if err != nil { return fmt.Errorf("create out file: %w", err) } w = f defer f.Close() } for i := range res { if _, err := fmt.Fprintln(w, res[i].Name); err != nil { return err } } return nil } func describeTests(cmd *cobra.Command, res []DescribedTest) error { w := cmd.OutOrStdout() if outFile := viper.GetString(outputFlag); outFile != "" { f, err := os.Create(outFile) if err != nil { return fmt.Errorf("create out file: %w", err) } w = f defer f.Close() } outTemplate, err := templates.GetDescribeTemplate() if err != nil { return fmt.Errorf("form describe template: %w", err) } return outTemplate.Execute(w, res) }