Skip to content

Commit 76f83fa

Browse files
authored
Merge pull request #17 from chermehdi/feature/print_case
Implement print case command
2 parents f36a8ed + 8b13732 commit 76f83fa

File tree

6 files changed

+197
-1
lines changed

6 files changed

+197
-1
lines changed

commands/printcase.go

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

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

utils/formatter.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func GetHeader(headerText, headerBorder string, headerRepeat int) string {
9+
return fmt.Sprintf("%s\n%s", headerText, strings.Repeat(headerBorder, headerRepeat))
10+
}

utils/formatter_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
"github.com/stretchr/testify/assert"
6+
"fmt"
7+
)
8+
9+
func TestGetHeader(t *testing.T) {
10+
header := GetHeader("my header", "==", 10);
11+
expected := fmt.Sprintf("my header\n====================");
12+
13+
assert.Equal(t, header, expected)
14+
}
15+

0 commit comments

Comments
 (0)