/
tester_context.go
113 lines (88 loc) · 3.19 KB
/
tester_context.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
package tester_context
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
)
// TesterContextTestCase represents one element in the CODECRAFTERS_TEST_CASES environment variable
type TesterContextTestCase struct {
// Slug is the slug of the test case. Example: "bind-to-port"
Slug string `json:"slug"`
// TesterLogPrefix is the prefix that'll be used for all logs emitted by the tester. Example: "stage-1"
TesterLogPrefix string `json:"tester_log_prefix"`
// Title is the title of the test case. Example: "Stage #1: Bind to a port"
Title string `json:"title"`
}
// TesterContext holds all flags passed in via environment variables, or from the codecrafters.yml file
type TesterContext struct {
ExecutablePath string
IsDebug bool
TestCases []TesterContextTestCase
ShouldSkipAntiCheatTestCases bool
}
type yamlConfig struct {
Debug bool `yaml:"debug"`
}
func (c TesterContext) Print() {
fmt.Println("Debug =", c.IsDebug)
}
// GetContext parses flags and returns a Context object
func GetTesterContext(env map[string]string, executableFileName string) (TesterContext, error) {
submissionDir, ok := env["CODECRAFTERS_SUBMISSION_DIR"]
if !ok {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_SUBMISSION_DIR env var not found")
}
testCasesJson, ok := env["CODECRAFTERS_TEST_CASES_JSON"]
if !ok {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_TEST_CASES_JSON env var not found")
}
testCases := []TesterContextTestCase{}
if err := json.Unmarshal([]byte(testCasesJson), &testCases); err != nil {
return TesterContext{}, fmt.Errorf("failed to parse CODECRAFTERS_TEST_CASES_JSON: %s", err)
}
var shouldSkipAntiCheatTestCases = false
skipAntiCheatValue, ok := env["CODECRAFTERS_SKIP_ANTI_CHEAT"]
if ok && skipAntiCheatValue == "true" {
shouldSkipAntiCheatTestCases = true
}
for _, testCase := range testCases {
if testCase.Slug == "" {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_TEST_CASES_JSON contains a test case with an empty slug")
}
if testCase.TesterLogPrefix == "" {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_TEST_CASES_JSON contains a test case with an empty tester_log_prefix")
}
if testCase.Title == "" {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_TEST_CASES_JSON contains a test case with an empty title")
}
}
configPath := path.Join(submissionDir, "codecrafters.yml")
executablePath := path.Join(submissionDir, executableFileName)
yamlConfig, err := readFromYAML(configPath)
if err != nil {
return TesterContext{}, err
}
if len(testCases) == 0 {
return TesterContext{}, fmt.Errorf("CODECRAFTERS_TEST_CASES is empty")
}
// TODO: test if executable exists?
return TesterContext{
ExecutablePath: executablePath,
IsDebug: yamlConfig.Debug,
TestCases: testCases,
ShouldSkipAntiCheatTestCases: shouldSkipAntiCheatTestCases,
}, nil
}
func readFromYAML(configPath string) (yamlConfig, error) {
c := &yamlConfig{}
fileContents, err := ioutil.ReadFile(configPath)
if err != nil {
return yamlConfig{}, err
}
if err := yaml.Unmarshal(fileContents, c); err != nil {
return yamlConfig{}, err
}
return *c, nil
}