-
-
Notifications
You must be signed in to change notification settings - Fork 658
/
Copy pathgen.go
204 lines (176 loc) · 7.18 KB
/
gen.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gen
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"go/format"
"net/http"
"os"
"path/filepath"
"runtime"
"text/template"
"time"
)
const (
// canonicalDataURL is the URL for the raw canonical-data.json data and requires the exercise name.
canonicalDataURL = "https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises/%s/canonical-data.json"
// commitsURL is the GitHub api endpoint for the commit history of canonical-data.json and requires the exercise name.
commitsURL = "https://api.github.com/repos/exercism/problem-specifications/commits?path=exercises/%s/canonical-data.json"
// defaultOrigin is the origin used in the header of cases_test.go
defaultOrigin = "exercism/problem-specifications"
)
// problemSpecificationsDir is the location of the problem-specifications repository on the filesystem.
// We're making the assumption that the problem-specifications repository has been cloned to
// the same parent directory as the Exercism Go track repository.
// E.g.
//
// $ tree -L 1 .
// .
// ├── problem-specifications
// └── go
var problemSpecificationsDir string
// exerciseDir is the location of the exercise and also the cases_test.go file.
// This assumes that the generator script lives in the .meta directory within
// the exercise directory. Falls back to the present working directory.
var exerciseDir string
// httpClient creates a http client with a timeout of 20 seconds in order not to get stuck waiting for a response.
var httpClient = &http.Client{Timeout: 20 * time.Second}
// Header tells how the test data was generated, for display in the header of cases_test.go
type Header struct {
Commit string
Origin string
}
// String generates the header for cases_test.go file.
func (h Header) String() string {
return fmt.Sprintf(`// This is an auto-generated file. Do not change it manually. Run the generator to update the file.
// See https://github.com/exercism/go#synchronizing-tests-and-instructions
// Source: %s
// Commit: %s
`, h.Origin, h.Commit)
}
type testCase struct {
UUID string `json:"uuid"`
Description string `json:"description"`
Comments []string `json:"comments"`
Property string `json:"property"`
Scenario string `json:"scenario"`
Input interface{} `json:"input"`
Expected interface{} `json:"expected"`
}
// Gen generates the exercise cases_test.go file from the relevant canonical-data.json
func Gen(exercise string, tests map[string]interface{}, t *template.Template) error {
var githubToken = ""
flag.StringVar(&githubToken, "github_token", "", "Token used in Authorization header for Github")
flag.Parse()
// Determine the exercise directory.
// Use runtime.Caller to determine path location of the generator main package.
// Call frames: 0 is this frame, Gen().
// 1 should be a func in <exercise-dir>/.meta/gen.go (generator main package)
// which is calling Gen().
// |
// V
if _, path, _, ok := runtime.Caller(1); ok {
// Construct a path 2 directories higher than the path for .meta/gen.go (should be exercise directory).
exerciseDir = filepath.Join(path, "..", "..")
} else {
return fmt.Errorf("unable to determine directory of exercise %s", exercise)
}
if exerciseDir == "" {
exerciseDir = "."
}
problemSpecificationsDir = filepath.Join(exerciseDir, "..", "..", "..", "..", "problem-specifications")
jFile := filepath.Join("exercises", exercise, "canonical-data.json")
fmt.Printf("[LOCAL] fetching %s test data from canonical-data.json\n", exercise)
var header Header
jTestData, err := getLocalTestData(jFile)
if err == nil {
header, err = getLatestLocalCommitMessage(jFile)
}
if err != nil {
// fetch json data remotely if there's no local file
fmt.Printf("[LOCAL] No test data found: %v\n", err)
fmt.Printf("[REMOTE] fetching %s test data\n", exercise)
jTestData, err = getRemoteTestData(exercise)
if err != nil {
return err
}
header, err = getRemoteCommit(exercise, githubToken)
if err != nil {
return err
}
}
if !json.Valid(jTestData) {
return fmt.Errorf("[ERROR] canonical-data.json seems not to be valid json")
}
// read tests.toml file to find which test cases should be excluded
tomlFile := filepath.Join(exerciseDir, ".meta", "tests.toml")
fmt.Printf("[LOCAL] reading tests.toml file from exercise directory %s\n", tomlFile)
excludedTests, err := getExcludedTestCases(tomlFile)
if err != nil {
return fmt.Errorf("[LOCAL] unable to read tests.toml file (%v)", err)
}
fmt.Println("collecting and filtering all test cases from the fetched test data")
allTestCases, err := getAllTestCasesFiltered(jTestData, excludedTests)
if err != nil {
return fmt.Errorf("failed to get filtered test-cases: %w", err)
}
var casesPerProperty = map[string][]testCase{}
for _, testCase := range *allTestCases {
casesPerProperty[testCase.Property] = append(casesPerProperty[testCase.Property], testCase)
}
for property, testCases := range casesPerProperty {
fmt.Printf(" > parsing cases for property %s\n", property)
marshal, err := json.Marshal(testCases)
if err != nil {
return fmt.Errorf("[ERROR] failed to marshal test cases with property %s: %w", property, err)
}
// valueForProperty is an instance of the struct (defined in gen.go) for the current property
// and is used to unmarshal the test cases
valueForProperty, ok := tests[property]
if !ok {
return fmt.Errorf("[ERROR] failed to get struct for tests with property %s", property)
}
err = json.Unmarshal(marshal, valueForProperty)
if err != nil {
return err
}
}
var testData = testCase{}
err = json.Unmarshal(jTestData, &testData)
if err != nil {
return fmt.Errorf("failed to unmarshal root test data: %v", err)
}
// package up a little meta data
d := struct {
Header
Comments []string
J map[string]interface{}
}{Header: header, Comments: testData.Comments, J: tests}
casesFile := filepath.Join(exerciseDir, "cases_test.go")
// render the Go test cases
var out bytes.Buffer
if err := t.Execute(&out, &d); err != nil {
return fmt.Errorf("[ERROR] template.Execute failed. The template has a semantic error: %w", err)
}
casesFileContent := out.Bytes()
formattedFileContent, err := format.Source(casesFileContent)
if err != nil {
fmt.Print("[ERROR] failed to format the output with gofmt (the generated source has a syntax error)")
casesFileContent = append(casesFileContent, []byte(fmt.Sprintf("// !NOTE: Error during source formatting: Line:Column %v\n", err))...)
// Save the raw unformatted, error-containing source for purposes of debugging the generator.
_ = outputSource("ERROR", casesFile, casesFileContent)
return err
}
// write output file for the Go test cases.
return outputSource("SUCCESS", casesFile, formattedFileContent)
}
// outputSource writes the src text to the given fileName and outputs a log message with given [status].
func outputSource(status, fileName string, src []byte) error {
err := os.WriteFile(fileName, src, 0o644) //nolint:gosec // 644 are the default permissions for a new file on *nix systems
if err != nil {
return fmt.Errorf("[FAILED] %q\n", err)
}
fmt.Printf("[%s] output: %s\n", status, fileName)
return nil
}