Skip to content

Commit 67ec076

Browse files
committed
Implement showcases/listcases command
1 parent 11a17bc commit 67ec076

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

commands/showcases.go

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

commands/showcases_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 TestGetIoFilesMaps(t *testing.T) {
9+
metaData := createDummyMetaData()
10+
inputs, outputs := GetIoFilesMaps(metaData)
11+
12+
assert.Equal(t, len(inputs), len(metaData.Inputs))
13+
assert.Equal(t, len(outputs), len(metaData.Outputs))
14+
assert.Equal(t, inputs["test-0"], metaData.Inputs[0])
15+
assert.Equal(t, inputs["test-1"], metaData.Inputs[1])
16+
assert.Equal(t, outputs["test-0"], metaData.Outputs[0])
17+
}

config/meta.go

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"io"
99
"os"
1010
"path"
11+
"strings"
12+
"strconv"
1113
)
1214

1315
type IoFile struct {
@@ -24,6 +26,15 @@ func NewIoFile(fileName, filePath string, customCase bool) IoFile {
2426
}
2527
}
2628

29+
func (ioFile *IoFile) GetId() int {
30+
tokens := strings.Split(ioFile.Name, "-")
31+
id, err := strconv.Atoi(tokens[1])
32+
if err != nil {
33+
return 0
34+
}
35+
return id
36+
}
37+
2738
// Type mapping to the `egor-meta.json` file.
2839
// The egor meta configuration is the source of truth for the task runner
2940
// so an update to it (either from the outside, or by invoking egor commands) can change
@@ -71,6 +82,13 @@ func NewEgorMeta(task Task, config Config) EgorMeta {
7182
TaskFile: taskFile,
7283
}
7384
}
85+
86+
// count the number of tests cases in the metadata. The number of tests is
87+
// the number of inputs
88+
func (egor *EgorMeta) CountTestCases() int {
89+
return len(egor.Inputs)
90+
}
91+
7492
func (egor *EgorMeta) toJson() (string, error) {
7593
var buffer bytes.Buffer
7694
encoder := json2.NewEncoder(&buffer)

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
&commands.ParseCommand,
4545
&commands.ConfigCommand,
4646
&commands.CaseCommand,
47+
&commands.ShowCasesCommand,
4748
},
4849
}
4950

0 commit comments

Comments
 (0)