7
7
package main
8
8
9
9
import (
10
- "archive/zip"
11
10
"bytes"
12
11
"flag"
13
12
"fmt"
14
13
"go/format"
15
- "io"
16
- "io/ioutil"
17
14
"log"
18
- "net/http"
19
15
"os"
20
- "regexp "
16
+ "path/filepath "
21
17
"strconv"
22
18
"strings"
23
19
"text/template"
@@ -64,30 +60,6 @@ const srcTemplate = "// Generated by tomltestgen for toml-test ref {{.Ref}} on {
64
60
"}\n " +
65
61
"{{end}}\n "
66
62
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
-
91
63
func kebabToCamel (kebab string ) string {
92
64
camel := ""
93
65
nextUpper := true
@@ -107,19 +79,6 @@ func kebabToCamel(kebab string) string {
107
79
return camel
108
80
}
109
81
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
-
123
82
func templateGoStr (input string ) string {
124
83
return strconv .Quote (input )
125
84
}
@@ -138,61 +97,57 @@ func main() {
138
97
flag .Usage = usage
139
98
flag .Parse ()
140
99
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
-
152
100
collection := testsCollection {
153
101
Ref : * ref ,
154
102
Timestamp : time .Now ().Format (time .RFC3339 ),
155
103
}
156
104
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" ))
158
109
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 ++
161
123
}
162
124
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 )
195
143
}
144
+
145
+ collection .Valid = append (collection .Valid , valid {
146
+ Name : name ,
147
+ Input : string (tomlContent ),
148
+ JsonRef : string (jsonContent ),
149
+ })
150
+ collection .Count ++
196
151
}
197
152
198
153
log .Printf ("Collected %d tests from toml-test\n " , collection .Count )
@@ -202,7 +157,7 @@ func main() {
202
157
}
203
158
t := template .Must (template .New ("src" ).Funcs (funcMap ).Parse (srcTemplate ))
204
159
buf := new (bytes.Buffer )
205
- err = t .Execute (buf , collection )
160
+ err : = t .Execute (buf , collection )
206
161
if err != nil {
207
162
panic (err )
208
163
}
@@ -216,7 +171,7 @@ func main() {
216
171
return
217
172
}
218
173
219
- err = os .WriteFile (* out , outputBytes , 0644 )
174
+ err = os .WriteFile (* out , outputBytes , 0o644 )
220
175
if err != nil {
221
176
panic (err )
222
177
}
0 commit comments