Skip to content

Commit d01d362

Browse files
committed
Implement print case command
1 parent bde0eb8 commit d01d362

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

commands/printcase.go

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package commands
2+
3+
import (
4+
"errors"
5+
"github.com/urfave/cli/v2"
6+
"github.com/chermehdi/egor/config"
7+
"github.com/fatih/color"
8+
"os"
9+
"path"
10+
"fmt"
11+
"bufio"
12+
"strconv"
13+
)
14+
15+
func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO {
16+
var testCase *TestCaseIO
17+
for _, input := range egorMeta.Inputs {
18+
if input.GetId() == id {
19+
testCase = &TestCaseIO {
20+
Id : input.GetId(),
21+
Name : input.Name,
22+
InputPath : input.Path,
23+
OutputPath: "",
24+
Custom: input.Custom,
25+
}
26+
27+
break
28+
}
29+
}
30+
31+
if testCase == nil {
32+
return nil
33+
}
34+
35+
for _, output := range egorMeta.Outputs {
36+
if output.Name == testCase.Name {
37+
testCase.OutputPath = output.Path
38+
}
39+
}
40+
41+
if testCase == nil {
42+
return nil
43+
}
44+
return testCase
45+
}
46+
47+
func PrintTestCaseInput(testCase *TestCaseIO) {
48+
color.Green("Input:")
49+
color.Green("========================================================")
50+
file, err := config.OpenFileFromPath(testCase.InputPath)
51+
if err != nil {
52+
color.Red("Failed to read test case input")
53+
} else {
54+
scanner := bufio.NewScanner(file)
55+
for scanner.Scan() {
56+
fmt.Println(scanner.Text())
57+
}
58+
}
59+
60+
}
61+
62+
func PrintTestCaseOutput(testCase *TestCaseIO) {
63+
color.Green("Output:")
64+
color.Green("========================================================")
65+
file, err := config.OpenFileFromPath(testCase.OutputPath)
66+
if err != nil {
67+
color.Red("Failed to read test case input")
68+
} else {
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("--input-only and --output-only cannot be exiting both.")
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("Cannot parse test id, a number required!")
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("Count not find test case with given 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+
var PrintCaseCommand = cli.Command{
131+
Name: "printcase",
132+
Aliases: []string{"pc"},
133+
Usage: "print input and/or output of a given test case",
134+
UsageText: "print input and/or output of a given test case",
135+
Action: PrintCaseAction,
136+
Flags: []cli.Flag{
137+
&cli.BoolFlag{
138+
Name: "output-only",
139+
Usage: "Print the output only of the test case",
140+
Value: false,
141+
},
142+
&cli.BoolFlag{
143+
Name: "input-only",
144+
Usage: "Print the input only of the test case",
145+
Value: false,
146+
},
147+
},
148+
}

commands/printcase_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package commands
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestGetTestCase(t *testing.T) {
9+
metaData := createSimpleDummyMetaData()
10+
testCase := GetTestCase(metaData, 0)
11+
12+
assert.Equal(t, testCase.Id, 0)
13+
assert.Equal(t, testCase.Id, metaData.Inputs[0].GetId())
14+
assert.Equal(t, testCase.InputPath, metaData.Inputs[0].Path)
15+
assert.Equal(t, testCase.OutputPath, metaData.Outputs[0].Path)
16+
assert.Equal(t, testCase.Custom, metaData.Inputs[0].Custom)
17+
}

commands/showcases_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func createSimpleDummyMetaData() config.EgorMeta{
3030
}
3131

3232
func TestGetTestCases(t *testing.T) {
33-
metaData := createDummyMetaData()
33+
metaData := createSimpleDummyMetaData()
3434
testCases := GetTestCases(metaData)
3535
inputs := metaData.Inputs
3636
outputs := metaData.Outputs

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func main() {
4545
&commands.ConfigCommand,
4646
&commands.CaseCommand,
4747
&commands.ShowCasesCommand,
48+
&commands.PrintCaseCommand,
4849
},
4950
}
5051

0 commit comments

Comments
 (0)