@@ -188,11 +188,7 @@ public void checkTreeFromFile(File testFile, boolean goldenFileNextToTestFile) t
188188 File goldenFile = goldenFileNextToTestFile
189189 ? new File (testFile .getParentFile (), getFileName (testFile ) + GOLDEN_FILE_EXT )
190190 : 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 );
196192 assertDescriptionMatches (tree , goldenFile );
197193 }
198194
@@ -204,24 +200,15 @@ public void checkScopeFromFile(File testFile, boolean goldenFileNextToTestFile)
204200 File goldenScopeFile = goldenFileNextToTestFile
205201 ? new File (testFile .getParentFile (), getFileName (testFile ) + SCOPE_FILE_EXT )
206202 : 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 ());
212204 assertDescriptionMatches (env .toString (), goldenScopeFile );
213205 }
214206
215207 public void checkTreeResult (String source , InputType inputType /* , Frame frame */ ) throws Exception {
216208 SSTNode resultNew = parse (source , getFileName (), inputType /* , frame */ );
217209 String tree = printTreeToString (resultNew );
218210 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 );
225212 assertDescriptionMatches (tree , goldenFile );
226213 }
227214
@@ -261,12 +248,20 @@ public void checkScopeResult(String source, InputType inputType) throws Exceptio
261248 ModTy mod = parse (source , "<module>" , inputType );
262249 File goldenScopeFile = getGoldenFile (SCOPE_FILE_EXT );
263250 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+ }
267263 }
268264 }
269- assertDescriptionMatches (env .toString (), goldenScopeFile );
270265 }
271266
272267// public void checkSSTNodeOffsets(SSTNode node) {
@@ -281,17 +276,7 @@ protected String printTreeToString(SSTNode node) {
281276 }
282277
283278 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- }
293279 String expected = readFile (goldenFile );
294-
295280 assertDescriptionMatches (actual , expected , goldenFile .getName ());
296281 }
297282
0 commit comments