Skip to content

Commit 358c8d2

Browse files
authored
Use toml-test to generate tests (#911)
Fixes: #909
1 parent fd8d0bf commit 358c8d2

File tree

4 files changed

+1467
-497
lines changed

4 files changed

+1467
-497
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ fuzz/
33
cmd/tomll/tomll
44
cmd/tomljson/tomljson
55
cmd/tomltestgen/tomltestgen
6-
dist
6+
dist
7+
tests/

Diff for: cmd/tomltestgen/main.go

+45-90
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
package main
88

99
import (
10-
"archive/zip"
1110
"bytes"
1211
"flag"
1312
"fmt"
1413
"go/format"
15-
"io"
16-
"io/ioutil"
1714
"log"
18-
"net/http"
1915
"os"
20-
"regexp"
16+
"path/filepath"
2117
"strconv"
2218
"strings"
2319
"text/template"
@@ -64,30 +60,6 @@ const srcTemplate = "// Generated by tomltestgen for toml-test ref {{.Ref}} on {
6460
"}\n" +
6561
"{{end}}\n"
6662

67-
func downloadTmpFile(url string) string {
68-
log.Println("starting to download file from", url)
69-
resp, err := http.Get(url)
70-
if err != nil {
71-
panic(err)
72-
}
73-
defer resp.Body.Close()
74-
75-
tmpfile, err := ioutil.TempFile("", "toml-test-*.zip")
76-
if err != nil {
77-
panic(err)
78-
}
79-
defer tmpfile.Close()
80-
81-
copiedLen, err := io.Copy(tmpfile, resp.Body)
82-
if err != nil {
83-
panic(err)
84-
}
85-
if resp.ContentLength > 0 && copiedLen != resp.ContentLength {
86-
panic(fmt.Errorf("copied %d bytes, request body had %d", copiedLen, resp.ContentLength))
87-
}
88-
return tmpfile.Name()
89-
}
90-
9163
func kebabToCamel(kebab string) string {
9264
camel := ""
9365
nextUpper := true
@@ -107,19 +79,6 @@ func kebabToCamel(kebab string) string {
10779
return camel
10880
}
10981

110-
func readFileFromZip(f *zip.File) string {
111-
reader, err := f.Open()
112-
if err != nil {
113-
panic(err)
114-
}
115-
defer reader.Close()
116-
bytes, err := ioutil.ReadAll(reader)
117-
if err != nil {
118-
panic(err)
119-
}
120-
return string(bytes)
121-
}
122-
12382
func templateGoStr(input string) string {
12483
return strconv.Quote(input)
12584
}
@@ -138,61 +97,57 @@ func main() {
13897
flag.Usage = usage
13998
flag.Parse()
14099

141-
url := "https://codeload.github.com/BurntSushi/toml-test/zip/" + *ref
142-
resultFile := downloadTmpFile(url)
143-
defer os.Remove(resultFile)
144-
log.Println("file written to", resultFile)
145-
146-
zipReader, err := zip.OpenReader(resultFile)
147-
if err != nil {
148-
panic(err)
149-
}
150-
defer zipReader.Close()
151-
152100
collection := testsCollection{
153101
Ref: *ref,
154102
Timestamp: time.Now().Format(time.RFC3339),
155103
}
156104

157-
zipFilesMap := map[string]*zip.File{}
105+
dirContent, _ := filepath.Glob("tests/invalid/**/*.toml")
106+
for _, f := range dirContent {
107+
filename := strings.TrimPrefix(f, "tests/valid/")
108+
name := kebabToCamel(strings.TrimSuffix(filename, ".toml"))
158109

159-
for _, f := range zipReader.File {
160-
zipFilesMap[f.Name] = f
110+
log.Printf("> [%s] %s\n", "invalid", name)
111+
112+
tomlContent, err := os.ReadFile(f)
113+
if err != nil {
114+
fmt.Printf("failed to read test file: %s\n", err)
115+
os.Exit(1)
116+
}
117+
118+
collection.Invalid = append(collection.Invalid, invalid{
119+
Name: name,
120+
Input: string(tomlContent),
121+
})
122+
collection.Count++
161123
}
162124

163-
testFileRegexp := regexp.MustCompile(`([^/]+/tests/(valid|invalid)/(.+))\.(toml)`)
164-
for _, f := range zipReader.File {
165-
groups := testFileRegexp.FindStringSubmatch(f.Name)
166-
if len(groups) > 0 {
167-
name := kebabToCamel(groups[3])
168-
testType := groups[2]
169-
170-
log.Printf("> [%s] %s\n", testType, name)
171-
172-
tomlContent := readFileFromZip(f)
173-
174-
switch testType {
175-
case "invalid":
176-
collection.Invalid = append(collection.Invalid, invalid{
177-
Name: name,
178-
Input: tomlContent,
179-
})
180-
collection.Count++
181-
case "valid":
182-
baseFilePath := groups[1]
183-
jsonFilePath := baseFilePath + ".json"
184-
jsonContent := readFileFromZip(zipFilesMap[jsonFilePath])
185-
186-
collection.Valid = append(collection.Valid, valid{
187-
Name: name,
188-
Input: tomlContent,
189-
JsonRef: jsonContent,
190-
})
191-
collection.Count++
192-
default:
193-
panic(fmt.Sprintf("unknown test type: %s", testType))
194-
}
125+
dirContent, _ = filepath.Glob("tests/valid/**/*.toml")
126+
for _, f := range dirContent {
127+
filename := strings.TrimPrefix(f, "tests/valid/")
128+
name := kebabToCamel(strings.TrimSuffix(filename, ".toml"))
129+
130+
log.Printf("> [%s] %s\n", "valid", name)
131+
132+
tomlContent, err := os.ReadFile(f)
133+
if err != nil {
134+
fmt.Printf("failed reading test file: %s\n", err)
135+
os.Exit(1)
136+
}
137+
138+
filename = strings.TrimSuffix(f, ".toml")
139+
jsonContent, err := os.ReadFile(filename + ".json")
140+
if err != nil {
141+
fmt.Printf("failed reading validation json: %s\n", err)
142+
os.Exit(1)
195143
}
144+
145+
collection.Valid = append(collection.Valid, valid{
146+
Name: name,
147+
Input: string(tomlContent),
148+
JsonRef: string(jsonContent),
149+
})
150+
collection.Count++
196151
}
197152

198153
log.Printf("Collected %d tests from toml-test\n", collection.Count)
@@ -202,7 +157,7 @@ func main() {
202157
}
203158
t := template.Must(template.New("src").Funcs(funcMap).Parse(srcTemplate))
204159
buf := new(bytes.Buffer)
205-
err = t.Execute(buf, collection)
160+
err := t.Execute(buf, collection)
206161
if err != nil {
207162
panic(err)
208163
}
@@ -216,7 +171,7 @@ func main() {
216171
return
217172
}
218173

219-
err = os.WriteFile(*out, outputBytes, 0644)
174+
err = os.WriteFile(*out, outputBytes, 0o644)
220175
if err != nil {
221176
panic(err)
222177
}

Diff for: toml_testgen_support_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate go run github.com/toml-lang/toml-test/cmd/toml-test@master -copy ./tests
12
//go:generate go run ./cmd/tomltestgen/main.go -o toml_testgen_test.go
23

34
// This is a support file for toml_testgen_test.go

0 commit comments

Comments
 (0)