|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/chermehdi/egor/config" |
| 5 | + "github.com/fatih/color" |
| 6 | + "github.com/jedib0t/go-pretty/table" |
| 7 | + "github.com/urfave/cli/v2" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | +) |
| 11 | + |
| 12 | +// print test cases table |
| 13 | +func PrintTestCasesTable(inputFiles, outputFiles map[string]config.IoFile) { |
| 14 | + t := table.NewWriter() |
| 15 | + t.SetOutputMirror(os.Stdout) |
| 16 | + t.AppendHeader(table.Row{"#", "Test Name", "Input Path", "Output Path", "Custon"}) |
| 17 | + for key, inputFile := range inputFiles { |
| 18 | + id := inputFile.GetId() |
| 19 | + testName := inputFile.Name |
| 20 | + inputPath := inputFile.Path |
| 21 | + outputPath := "" |
| 22 | + custom := inputFile.Custom |
| 23 | + |
| 24 | + outputFile, ok := outputFiles[key] |
| 25 | + if ok { |
| 26 | + outputPath = outputFile.Path |
| 27 | + } |
| 28 | + |
| 29 | + t.AppendRow([]interface{}{id, testName, inputPath, outputPath, custom}) |
| 30 | + } |
| 31 | + t.SetStyle(table.StyleLight) |
| 32 | + t.Render() |
| 33 | +} |
| 34 | + |
| 35 | +// construct inputs and outputs maps from an egor meta data where keys are test names. |
| 36 | +func GetIoFilesMaps(egorMeta config.EgorMeta) (map[string]config.IoFile, map[string]config.IoFile) { |
| 37 | + inputFiles := make(map[string]config.IoFile) |
| 38 | + for _, inputFile := range egorMeta.Inputs { |
| 39 | + inputFiles[inputFile.Name] = inputFile |
| 40 | + } |
| 41 | + |
| 42 | + outputFiles := make(map[string]config.IoFile) |
| 43 | + for _, outputFile := range egorMeta.Outputs { |
| 44 | + outputFiles[outputFile.Name] = outputFile |
| 45 | + } |
| 46 | + |
| 47 | + return inputFiles, outputFiles |
| 48 | +} |
| 49 | + |
| 50 | +// print task test cases |
| 51 | +func PrintTestCases(egorMeta config.EgorMeta) error { |
| 52 | + inputFiles, outputFiles := GetIoFilesMaps(egorMeta) |
| 53 | + PrintTestCasesTable(inputFiles, outputFiles) |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +// list test cases information command action |
| 58 | +// TODO(Eroui): [Refactoring] Duplicate code while loading meta data, consider refactoring... |
| 59 | +func ShowCasesAction(context *cli.Context) error { |
| 60 | + cwd, err := os.Getwd() |
| 61 | + if err != nil { |
| 62 | + color.Red("Failed to list test cases!") |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + configuration, err := config.LoadDefaultConfiguration() |
| 67 | + if err != nil { |
| 68 | + color.Red("Failed to load egor configuration") |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + configFileName := configuration.ConfigFileName |
| 73 | + metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName)) |
| 74 | + if err != nil { |
| 75 | + color.Red("Failed to load egor MetaData ") |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + color.Green("Listing %d testcase(s)...", metaData.CountTestCases()) |
| 80 | + color.Green("") |
| 81 | + |
| 82 | + err = PrintTestCases(metaData) |
| 83 | + if err != nil { |
| 84 | + color.Red("Error while printing test cases") |
| 85 | + return err |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// Command to print the list of test cases input and outputs into the consol. |
| 91 | +// Running this command will fetch egor meta data and load all inputs and outputs meta data |
| 92 | +// and prints it as an array into the consol. |
| 93 | +var ShowCasesCommand = cli.Command{ |
| 94 | + Name: "showcases", |
| 95 | + Aliases: []string{"listcases"}, |
| 96 | + Usage: "list information about test cases", |
| 97 | + UsageText: "list meta data about of the tests cases in the current task", |
| 98 | + Action: ShowCasesAction, |
| 99 | +} |
0 commit comments