s3-tests-parser/cmd/parser/modules/tests.go
Denis Kirillov 8e325c79e5 [#6] Add new 'tests' command
Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
2024-11-22 12:01:01 +03:00

75 lines
1.5 KiB
Go

package modules
import (
"fmt"
"os"
"slices"
"sort"
"git.frostfs.info/TrueCloudLab/s3-tests-parser/internal/s3"
"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",
Example: `s3-tests-parser tests
s3-tests-parser tests --output tests.txt`,
RunE: runTestsCmd,
}
func initTestsCmd() {
testsCmd.Flags().String(outputFlag, "", "file to write output, if missed the stdout is used")
}
func runTestsCmd(cmd *cobra.Command, _ []string) error {
testStruct, err := s3.ParseTestsStruct()
if err != nil {
return err
}
var tests []string
groupTests := make(map[string][]string)
for _, group := range testStruct.Groups {
groupTests[group.Name] = group.Tests
}
for _, group := range testStruct.Groups {
if group.Skip {
continue
}
tests = append(tests, group.Tests...)
for _, include := range group.Include {
tests = append(tests, groupTests[include]...)
}
}
sort.Strings(tests)
tests = slices.Compact(tests)
return printTests(cmd, tests)
}
func printTests(cmd *cobra.Command, res []string) 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]); err != nil {
return err
}
}
return nil
}