[#XX] Add describe flag to tests subcommand

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2025-04-15 15:21:11 +03:00
parent 9d720de804
commit 52d1cb2818
4 changed files with 84 additions and 32 deletions

View file

@ -1,27 +1,41 @@
package modules
import (
"cmp"
"fmt"
"os"
"slices"
"sort"
"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 to check",
Long: "Print all tests that will be checked by 'compatibility' command",
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 {
@ -30,34 +44,40 @@ func runTestsCmd(cmd *cobra.Command, _ []string) error {
return err
}
var tests []string
includeAll := viper.GetBool(allFlag)
groupTests := make(map[string][]string)
for _, group := range testStruct.Groups {
if group.Skip {
continue
}
groupTests[group.Name] = group.Tests
}
var tests []DescribedTest
for _, group := range testStruct.Groups {
if group.Skip {
if group.Skip && !includeAll {
continue
}
tests = append(tests, group.Tests...)
for _, include := range group.Include {
tests = append(tests, groupTests[include]...)
for _, test := range group.Tests {
tests = append(tests, DescribedTest{
Name: test,
Group: group.Name,
Skip: group.Skip,
Comment: group.Comment,
})
}
}
sort.Strings(tests)
tests = slices.Compact(tests)
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 []string) error {
func printTests(cmd *cobra.Command, res []DescribedTest) error {
w := cmd.OutOrStdout()
if outFile := viper.GetString(outputFlag); outFile != "" {
f, err := os.Create(outFile)
@ -69,10 +89,29 @@ func printTests(cmd *cobra.Command, res []string) error {
}
for i := range res {
if _, err := fmt.Fprintln(w, res[i]); err != nil {
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)
}