|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/urfave/cli/v2" |
| 6 | + "github.com/chermehdi/egor/config" |
| 7 | + "github.com/chermehdi/egor/utils" |
| 8 | + "github.com/fatih/color" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "fmt" |
| 12 | + "bufio" |
| 13 | + "strconv" |
| 14 | +) |
| 15 | + |
| 16 | +func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO { |
| 17 | + var testCase *TestCaseIO |
| 18 | + for _, input := range egorMeta.Inputs { |
| 19 | + if input.GetId() == id { |
| 20 | + testCase = &TestCaseIO { |
| 21 | + Id : input.GetId(), |
| 22 | + Name : input.Name, |
| 23 | + InputPath : input.Path, |
| 24 | + OutputPath: "", |
| 25 | + Custom: input.Custom, |
| 26 | + } |
| 27 | + |
| 28 | + break |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if testCase == nil { |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + for _, output := range egorMeta.Outputs { |
| 37 | + if output.Name == testCase.Name { |
| 38 | + testCase.OutputPath = output.Path |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if testCase == nil { |
| 43 | + return nil |
| 44 | + } |
| 45 | + return testCase |
| 46 | +} |
| 47 | + |
| 48 | +func PrintTestCaseInput(testCase *TestCaseIO) { |
| 49 | + |
| 50 | + file, err := config.OpenFileFromPath(testCase.InputPath) |
| 51 | + if err != nil { |
| 52 | + color.Red("Failed to read test case input") |
| 53 | + } else { |
| 54 | + color.Green(utils.GetHeader("Input", "========", 10)) |
| 55 | + scanner := bufio.NewScanner(file) |
| 56 | + for scanner.Scan() { |
| 57 | + fmt.Println(scanner.Text()) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +func PrintTestCaseOutput(testCase *TestCaseIO) { |
| 64 | + file, err := config.OpenFileFromPath(testCase.OutputPath) |
| 65 | + if err != nil { |
| 66 | + color.Red("Failed to read test case input") |
| 67 | + } else { |
| 68 | + color.Green(utils.GetHeader("Output", "========", 10)) |
| 69 | + scanner := bufio.NewScanner(file) |
| 70 | + for scanner.Scan() { |
| 71 | + fmt.Println(scanner.Text()) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func PrintCaseAction(context *cli.Context) error { |
| 77 | + if context.Bool("input-only") && context.Bool("output-only") { |
| 78 | + color.Red("only --input-only or --output-only should be set.") |
| 79 | + return errors.New("Invalid commands arguments") |
| 80 | + } |
| 81 | + |
| 82 | + if context.NArg() == 0 { |
| 83 | + color.Red("Test id required argument missing!") |
| 84 | + return errors.New("Missing required argument 'test_id'") |
| 85 | + } |
| 86 | + |
| 87 | + id, err := strconv.Atoi(context.Args().Get(0)) |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + color.Red(fmt.Sprintf("Cannot parse test id = '%s', a number required!", context.Args().Get(0))) |
| 91 | + return errors.New(fmt.Sprintf("Failed to parse test id = %s", context.Args().Get(0))) |
| 92 | + } |
| 93 | + |
| 94 | + cwd, err := os.Getwd() |
| 95 | + if err != nil { |
| 96 | + color.Red("Failed to list test cases!") |
| 97 | + return err |
| 98 | + } |
| 99 | + |
| 100 | + configuration, err := config.LoadDefaultConfiguration() |
| 101 | + if err != nil { |
| 102 | + color.Red("Failed to load egor configuration") |
| 103 | + return err |
| 104 | + } |
| 105 | + |
| 106 | + configFileName := configuration.ConfigFileName |
| 107 | + metaData, err := config.LoadMetaFromPath(path.Join(cwd, configFileName)) |
| 108 | + if err != nil { |
| 109 | + color.Red("Failed to load egor MetaData ") |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + testCase := GetTestCase(metaData, id) |
| 114 | + if testCase == nil { |
| 115 | + color.Red(fmt.Sprintf("Could not find test case with id = %d", id)) |
| 116 | + return errors.New(fmt.Sprintf("Unknown test case with id %d", id)) |
| 117 | + } |
| 118 | + |
| 119 | + if !context.Bool("output-only") { |
| 120 | + PrintTestCaseInput(testCase) |
| 121 | + } |
| 122 | + |
| 123 | + if !context.Bool("input-only") { |
| 124 | + PrintTestCaseOutput(testCase) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// Command to print a test case. this command can be used to print inputs and/or outputs |
| 131 | +// to the consol. The user can choose to print the input only or the output only. The |
| 132 | +// user should provide a valid test id. |
| 133 | +// Running this command will fetch egor meta data, get the test case with the given id, |
| 134 | +// and then print the content of the input and/or of the output files. |
| 135 | +var PrintCaseCommand = cli.Command{ |
| 136 | + Name: "printcase", |
| 137 | + Aliases: []string{"pc"}, |
| 138 | + Usage: "print input and/or output of a given test case", |
| 139 | + UsageText: "print input and/or output of a given test case", |
| 140 | + Action: PrintCaseAction, |
| 141 | + Flags: []cli.Flag{ |
| 142 | + &cli.BoolFlag{ |
| 143 | + Name: "output-only", |
| 144 | + Usage: "Print the output only of the test case", |
| 145 | + Value: false, |
| 146 | + }, |
| 147 | + &cli.BoolFlag{ |
| 148 | + Name: "input-only", |
| 149 | + Usage: "Print the input only of the test case", |
| 150 | + Value: false, |
| 151 | + }, |
| 152 | + }, |
| 153 | +} |
0 commit comments