forked from codecrafters-io/tester-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester_context.go
69 lines (54 loc) · 1.55 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
package tester_utils
import (
"fmt"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
)
// testerContext holds all flags that a user has passed in
type testerContext struct {
executablePath string
isDebug bool
currentStageSlug string
}
type yamlConfig struct {
Debug bool `yaml:"debug"`
}
func (c testerContext) print() {
fmt.Println("Debug =", c.isDebug)
fmt.Println("Stage =", c.currentStageSlug)
}
// 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")
}
currentStageSlug, ok := env["CODECRAFTERS_CURRENT_STAGE_SLUG"]
if !ok {
return testerContext{}, fmt.Errorf("CODECRAFTERS_CURRENT_STAGE_SLUG env var not found")
}
configPath := path.Join(submissionDir, "codecrafters.yml")
executablePath := path.Join(submissionDir, executableFileName)
yamlConfig, err := readFromYAML(configPath)
if err != nil {
return testerContext{}, err
}
// TODO: test if executable exists?
return testerContext{
executablePath: executablePath,
isDebug: yamlConfig.Debug,
currentStageSlug: currentStageSlug,
}, 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
}