Skip to content

Commit 147ff1d

Browse files
mehdi.cheracher@gmail.comchermehdi
authored andcommitted
wip: working basic java judge
1 parent 951f73b commit 147ff1d

File tree

4 files changed

+94
-18
lines changed

4 files changed

+94
-18
lines changed

commands/run.go

+68-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package commands
22

33
import (
4-
"bufio"
54
"bytes"
65
"errors"
76
"fmt"
87
"github.com/chermehdi/egor/config"
8+
"github.com/fatih/color"
9+
"github.com/jedib0t/go-pretty/table"
910
"io/ioutil"
1011
"os"
1112
"os/exec"
@@ -23,6 +24,13 @@ var (
2324
WA int8 = 3
2425
)
2526

27+
var (
28+
green = color.New(color.FgGreen).SprintfFunc()
29+
red = color.New(color.FgRed).SprintfFunc()
30+
magenta = color.New(color.FgMagenta).SprintfFunc()
31+
yellow = color.New(color.FgYellow).SprintfFunc()
32+
)
33+
2634
// Checks the output of a given testcase against it's expected output
2735
type Checker interface {
2836
// Execute the check (got, expected) and returns
@@ -36,8 +44,8 @@ type DiffChecker struct {
3644

3745
func (c *DiffChecker) Check(got, expected string) error {
3846
// Compare the trimmed output from both input and output
39-
if strings.Trim(got, " \t\n\r") != strings.Trim(expected, " \t\n\r") {
40-
return errors.New(fmt.Sprintf("Checker failed, expected %s, found %s", got, expected))
47+
if strings.TrimRight(got, " \t\n\r") != strings.TrimRight(expected, " \t\n\r") {
48+
return errors.New(fmt.Sprintf("Checker failed, expected:\n%s\nfound:\n%s", expected, got))
4149
}
4250
return nil
4351
}
@@ -94,21 +102,56 @@ type Judge interface {
94102

95103
// This represents the result of running the testcases of a given task
96104
type JudgeReport interface {
97-
Add(status CaseStatus)
105+
// Add the pair To the list of executions processed
106+
Add(status CaseStatus, description CaseDescription)
98107

99-
Display() string
108+
// Display the current report to os.Stdout
109+
Display()
100110
}
101111

102112
type ConsoleJudgeReport struct {
103113
Stats []CaseStatus
114+
Descs []CaseDescription
104115
}
105116

106-
func (c *ConsoleJudgeReport) Add(status CaseStatus) {
117+
func (c *ConsoleJudgeReport) Add(status CaseStatus, description CaseDescription) {
107118
c.Stats = append(c.Stats, status)
119+
c.Descs = append(c.Descs, description)
108120
}
109121

110-
func (c *ConsoleJudgeReport) Display() string {
111-
panic("implement me")
122+
func getDisplayStatus(status int8) string {
123+
switch status {
124+
case AC:
125+
return green("AC")
126+
case RE:
127+
return magenta("RE")
128+
case SK:
129+
return yellow("SK")
130+
case WA:
131+
return red("WA")
132+
}
133+
return "Unknown"
134+
}
135+
136+
func (c *ConsoleJudgeReport) Display() {
137+
t := table.NewWriter()
138+
t.SetOutputMirror(os.Stdout)
139+
t.AppendHeader(table.Row{"#", "Test Name", "Status", "Custom", "Additional infos"})
140+
for i, stat := range c.Stats {
141+
output := "None"
142+
if stat.CheckerError != nil {
143+
output = fmt.Sprintf("FAILED, %s", stat.CheckerError.Error())
144+
}
145+
t.AppendRow([]interface{}{
146+
i,
147+
c.Descs[i].InputFile,
148+
getDisplayStatus(stat.Status),
149+
c.Descs[i].CustomCase,
150+
output,
151+
})
152+
}
153+
t.SetStyle(table.StyleLight)
154+
t.Render()
112155
}
113156

114157
func newJudgeReport() JudgeReport {
@@ -130,14 +173,18 @@ func (judge *JavaJudge) Setup() error {
130173
return err
131174
}
132175
workDirPath := path.Join(currentDir, "work")
133-
if err = os.Mkdir(workDirPath, 777); err != nil {
176+
if err = os.Mkdir(workDirPath, 0777); err != nil {
134177
return err
135178
}
136179
//TODO(chermehdi): make the executables path configurable #14
137-
138180
// Compilation for Java
139-
cmd := exec.Command("javac", judge.Meta.TaskFile, "-d", workDirPath)
181+
var stderrBuffer bytes.Buffer
182+
cmd := exec.Command("javac", judge.Meta.TaskFile, "-d", "work")
183+
cmd.Dir = currentDir
184+
fmt.Printf("javac %s -d \"%s\"\n", judge.Meta.TaskFile, workDirPath)
185+
cmd.Stderr = &stderrBuffer
140186
if err = cmd.Run(); err != nil {
187+
color.Red("Could not compile %s, Cause: \n", stderrBuffer.String())
141188
return err
142189
}
143190
judge.CurrentWorkDir = workDirPath
@@ -146,8 +193,7 @@ func (judge *JavaJudge) Setup() error {
146193

147194
func (judge *JavaJudge) RunTestCase(desc CaseDescription) CaseStatus {
148195
// We suppose that all java executables will be called Main
149-
execFilePath := path.Join(judge.CurrentWorkDir, "Main")
150-
cmd := exec.Command("java", execFilePath)
196+
cmd := exec.Command("java", "Main")
151197
cmd.Dir = judge.CurrentWorkDir
152198
inputFile, err := os.Open(desc.InputFile)
153199
if err != nil {
@@ -169,7 +215,7 @@ func (judge *JavaJudge) RunTestCase(desc CaseDescription) CaseStatus {
169215

170216
var stderrBuffer bytes.Buffer
171217
cmd.Stdin = inputFile
172-
cmd.Stdout = bufio.NewWriter(outputFile)
218+
cmd.Stdout = outputFile
173219
cmd.Stderr = &stderrBuffer
174220
if err = cmd.Run(); err != nil {
175221
return CaseStatus{
@@ -188,6 +234,7 @@ func (judge *JavaJudge) RunTestCase(desc CaseDescription) CaseStatus {
188234
}
189235
}
190236
output, err := ioutil.ReadFile(desc.WorkFile)
237+
191238
if err != nil {
192239
return CaseStatus{
193240
Status: RE,
@@ -204,7 +251,7 @@ func (judge *JavaJudge) RunTestCase(desc CaseDescription) CaseStatus {
204251
}
205252
return CaseStatus{
206253
Status: AC,
207-
CheckerError: err,
254+
CheckerError: nil,
208255
Stderr: stderrBuffer.String(),
209256
}
210257
}
@@ -254,7 +301,7 @@ func (judge *PythonJudge) Cleanup() error {
254301
func NewJudgeFor(meta config.EgorMeta) (Judge, error) {
255302
switch meta.TaskLang {
256303
case "java":
257-
return &JavaJudge{Meta: meta}, nil
304+
return &JavaJudge{Meta: meta, Checker: &DiffChecker{}}, nil
258305
case "cpp":
259306
case "c":
260307
return &CppJudge{Meta: meta}, nil
@@ -265,6 +312,7 @@ func NewJudgeFor(meta config.EgorMeta) (Judge, error) {
265312
}
266313

267314
func RunAction(_ *cli.Context) error {
315+
color.Green("in the run action")
268316
configuration, err := config.LoadDefaultConfiguration()
269317
if err != nil {
270318
return err
@@ -288,12 +336,15 @@ func RunAction(_ *cli.Context) error {
288336
}
289337
defer judge.Cleanup()
290338
report := newJudgeReport()
339+
291340
for i, input := range egorMeta.Inputs {
292341
output := egorMeta.Outputs[i]
293342
caseDescription := NewCaseDescription(input, output)
294343
status := judge.RunTestCase(*caseDescription)
295-
report.Add(status)
344+
report.Add(status, *caseDescription)
296345
}
346+
report.Display()
347+
return nil
297348
}
298349

299350
var TestCommand = cli.Command{

commands/showcases.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func GetTestCases(egorMeta config.EgorMeta) []*TestCaseIO {
4646
func PrintTestCasesTable(testCases []*TestCaseIO) {
4747
t := table.NewWriter()
4848
t.SetOutputMirror(os.Stdout)
49-
t.AppendHeader(table.Row{"#", "Test Name", "Input Path", "Output Path", "Custon"})
49+
t.AppendHeader(table.Row{"#", "Test Name", "Input Path", "Output Path", "Custom"})
5050
for _, testCase := range testCases {
5151
t.AppendRow([]interface{}{
5252
testCase.Id,

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ go 1.13
44

55
require (
66
github.com/fatih/color v1.9.0
7+
github.com/go-openapi/strfmt v0.19.4 // indirect
8+
github.com/jedib0t/go-pretty v4.3.0+incompatible
9+
github.com/mattn/go-runewidth v0.0.8 // indirect
710
github.com/stretchr/testify v1.4.0
811
github.com/urfave/cli/v2 v2.1.1
912
gopkg.in/yaml.v2 v2.2.2

go.sum

+22
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a h1:idn718Q4B6AGu/h5Sxe66HYVdqdGu2l9Iebqhi/AEoA=
3+
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
24
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
35
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
46
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
69
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
710
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
11+
github.com/go-openapi/errors v0.19.2 h1:a2kIyV3w+OS3S97zxUndRVD46+FhGOUBDFY7nmu4CsY=
12+
github.com/go-openapi/errors v0.19.2/go.mod h1:qX0BLWsyaKfvhluLejVpVNwNRdXZhEbTA4kxxpKBC94=
13+
github.com/go-openapi/strfmt v0.19.4 h1:eRvaqAhpL0IL6Trh5fDsGnGhiXndzHFuA05w6sXH6/g=
14+
github.com/go-openapi/strfmt v0.19.4/go.mod h1:eftuHTlB/dI8Uq8JJOyRlieZf+WkkxUuk0dgdHXr2Qk=
15+
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
16+
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
17+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
18+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
19+
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
20+
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
821
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
922
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
1023
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
1124
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
1225
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
26+
github.com/mattn/go-runewidth v0.0.8 h1:3tS41NlGYSmhhe/8fhGRzc+z3AYCw1Fe1WAyLuujKs0=
27+
github.com/mattn/go-runewidth v0.0.8/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
28+
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
29+
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
1330
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1431
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1532
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
1633
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
1734
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
1835
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
1936
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
37+
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
38+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2039
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
2140
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
41+
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
2242
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
2343
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
44+
go.mongodb.org/mongo-driver v1.0.3 h1:GKoji1ld3tw2aC+GX1wbr/J2fX13yNacEYoJ8Nhr0yU=
45+
go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
2446
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2547
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
2648
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

0 commit comments

Comments
 (0)