-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
151 lines (122 loc) · 2.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bytes"
"context"
"encoding/csv"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path"
"time"
)
type Result struct {
DataName string
ProgramName string
Data string
ExitCode int
Timeout bool
Stdout string
Stderr string
}
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
const (
programDirName = "program"
testdataDirName = "testdata"
)
var results []Result
testdata, err := os.ReadDir(testdataDirName)
if err != nil {
return err
}
programs, err := os.ReadDir(programDirName)
if err != nil {
return err
}
for _, data := range testdata {
dataContent, err := os.ReadFile(path.Join(testdataDirName, data.Name()))
if err != nil {
return err
}
for _, program := range programs {
result := execute(string(dataContent), data.Name(), program.Name(), testdataDirName, programDirName)
log.Printf("data:%-40s program:%-20s", result.DataName, result.ProgramName)
results = append(results, result)
}
}
if err := exportAsTSV(results); err != nil {
return err
}
return nil
}
func execute(data string, dataName string, programName string, testdataDirName string, programDirName string) Result {
ctx, cancel := signal.NotifyContext(context.Background(), os.Kill)
defer cancel()
var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, "bash", "main.sh", path.Join("..", "..", testdataDirName, dataName))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = path.Join(programDirName, programName)
if err := cmd.Start(); err != nil {
panic(err)
}
done := make(chan error)
go func() { done <- cmd.Wait() }()
var timeout bool
select {
case <-done:
timeout = false
case <-time.After(3 * time.Second):
go cmd.Process.Kill()
timeout = true
}
return Result{
DataName: dataName,
ProgramName: programName,
Data: data,
ExitCode: cmd.ProcessState.ExitCode(),
Timeout: timeout,
Stdout: takeInitText(stdout.String()),
Stderr: stderr.String(),
}
}
func takeInitText(text string) string {
const maxLength = 512
if len(text) < maxLength {
return text
}
return text[:maxLength]
}
func exportAsTSV(results []Result) error {
file, err := os.Create("results.tsv")
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
writer.Comma = '\t'
if err := writer.Write([]string{"data name", "program name", "data", "stdout", "stderr", "exit code", "timeout"}); err != nil {
return err
}
for _, result := range results {
if err := writer.Write([]string{
result.DataName,
result.ProgramName,
result.Data,
result.Stdout,
result.Stderr,
fmt.Sprintf("%v", result.ExitCode),
fmt.Sprintf("%v", result.Timeout),
}); err != nil {
return err
}
}
return nil
}