@@ -188,11 +188,7 @@ public void checkTreeFromFile(File testFile, boolean goldenFileNextToTestFile) t
188
188
File goldenFile = goldenFileNextToTestFile
189
189
? new File (testFile .getParentFile (), getFileName (testFile ) + GOLDEN_FILE_EXT )
190
190
: getGoldenFile (GOLDEN_FILE_EXT );
191
- if (REGENERATE_TREE || !goldenFile .exists ()) {
192
- try (FileWriter fw = new FileWriter (goldenFile )) {
193
- fw .write (tree );
194
- }
195
- }
191
+ writeGoldenFileIfMissing (goldenFile , tree );
196
192
assertDescriptionMatches (tree , goldenFile );
197
193
}
198
194
@@ -204,24 +200,15 @@ public void checkScopeFromFile(File testFile, boolean goldenFileNextToTestFile)
204
200
File goldenScopeFile = goldenFileNextToTestFile
205
201
? new File (testFile .getParentFile (), getFileName (testFile ) + SCOPE_FILE_EXT )
206
202
: getGoldenFile (SCOPE_FILE_EXT );
207
- if (REGENERATE_TREE || !goldenScopeFile .exists ()) {
208
- try (FileWriter fw = new FileWriter (goldenScopeFile )) {
209
- fw .write (env .toString ());
210
- }
211
- }
203
+ writeGoldenFileIfMissing (goldenScopeFile , env .toString ());
212
204
assertDescriptionMatches (env .toString (), goldenScopeFile );
213
205
}
214
206
215
207
public void checkTreeResult (String source , InputType inputType /* , Frame frame */ ) throws Exception {
216
208
SSTNode resultNew = parse (source , getFileName (), inputType /* , frame */ );
217
209
String tree = printTreeToString (resultNew );
218
210
File goldenFile = getGoldenFile (GOLDEN_FILE_EXT );
219
- if (REGENERATE_TREE || !goldenFile .exists ()) {
220
- try (FileWriter fw = new FileWriter (goldenFile )) {
221
- fw .write (tree );
222
- }
223
-
224
- }
211
+ writeGoldenFileIfMissing (goldenFile , tree );
225
212
assertDescriptionMatches (tree , goldenFile );
226
213
}
227
214
@@ -261,12 +248,20 @@ public void checkScopeResult(String source, InputType inputType) throws Exceptio
261
248
ModTy mod = parse (source , "<module>" , inputType );
262
249
File goldenScopeFile = getGoldenFile (SCOPE_FILE_EXT );
263
250
ScopeEnvironment env = ScopeEnvironment .analyze (mod , errorCallback , EMPTY_FUTURE );
264
- if (REGENERATE_TREE || !goldenScopeFile .exists ()) {
265
- try (FileWriter fw = new FileWriter (goldenScopeFile )) {
266
- fw .write (env .toString ());
251
+ writeGoldenFileIfMissing (goldenScopeFile , env .toString ());
252
+ assertDescriptionMatches (env .toString (), goldenScopeFile );
253
+ }
254
+
255
+ private static void writeGoldenFileIfMissing (File goldenFile , String contents ) throws IOException {
256
+ if (REGENERATE_TREE || !goldenFile .exists ()) {
257
+ if (System .getenv ("CI" ) != null ) {
258
+ fail ("Missing golden file " + goldenFile );
259
+ } else {
260
+ try (FileWriter fw = new FileWriter (goldenFile )) {
261
+ fw .write (contents );
262
+ }
267
263
}
268
264
}
269
- assertDescriptionMatches (env .toString (), goldenScopeFile );
270
265
}
271
266
272
267
// public void checkSSTNodeOffsets(SSTNode node) {
@@ -281,17 +276,7 @@ protected String printTreeToString(SSTNode node) {
281
276
}
282
277
283
278
protected void assertDescriptionMatches (String actual , File goldenFile ) throws Exception {
284
- if (!goldenFile .exists ()) {
285
- if (!goldenFile .createNewFile ()) {
286
- fail ("Cannot create file " + goldenFile .getAbsolutePath ());
287
- }
288
- try (FileWriter fw = new FileWriter (goldenFile )) {
289
- fw .write (actual );
290
- }
291
- fail ("Created generated golden file " + goldenFile .getAbsolutePath () + "\n Please re-run the test." );
292
- }
293
279
String expected = readFile (goldenFile );
294
-
295
280
assertDescriptionMatches (actual , expected , goldenFile .getName ());
296
281
}
297
282
0 commit comments