1616
1717package com .google .javascript .jscomp ;
1818
19+ import static com .google .common .collect .ImmutableList .toImmutableList ;
20+ import static com .google .common .truth .Truth .assertThat ;
1921import static com .google .javascript .jscomp .CompilerTestCase .srcs ;
22+ import static java .util .stream .Collectors .joining ;
2023
24+ import com .google .common .collect .ImmutableList ;
2125import com .google .javascript .rhino .Node ;
26+ import java .nio .file .Files ;
27+ import java .nio .file .Path ;
28+ import java .util .function .Consumer ;
29+ import java .util .stream .Stream ;
2230import org .junit .Before ;
2331import org .junit .Test ;
2432import org .junit .runner .RunWith ;
@@ -41,13 +49,22 @@ private void process(Node externs, Node root) {
4149
4250 private int inputCount ;
4351 private int outputCount ;
52+ private Consumer <CompilerOptions > extraOptions ;
4453
4554 @ Before
4655 public void setup () {
4756 disableValidateAstChangeMarking ();
4857 allowExternsChanges ();
4958 this .inputCount = 0 ;
5059 this .outputCount = 0 ;
60+ this .extraOptions = options -> {};
61+ }
62+
63+ @ Override
64+ public CompilerOptions getOptions () {
65+ CompilerOptions baseOptions = super .getOptions ();
66+ extraOptions .accept (baseOptions );
67+ return baseOptions ;
5168 }
5269
5370 String createPrettyPrinter (Node n ) {
@@ -109,6 +126,95 @@ public void testFoldConstants_multipleClosureUnawareBlocksInFile() {
109126 expectedClosureUnaware ("console.log(3);" , "console.log(33);" , "console.log(333);" )));
110127 }
111128
129+ private static String loadFile (Path path ) {
130+ try (Stream <String > lines = Files .lines (path )) {
131+ return lines .collect (joining ("\n " ));
132+ } catch (Exception e ) {
133+ throw new AssertionError ("Failed to load debug log at " + path , e );
134+ }
135+ }
136+
137+ private ImmutableList <Path > listFiles (Path dir ) {
138+ try (Stream <Path > files = Files .list (dir )) {
139+ return files .collect (toImmutableList ());
140+ } catch (Exception e ) {
141+ throw new AssertionError (e );
142+ }
143+ }
144+
145+ @ Test
146+ public void testSupportsDebugLogging () {
147+ enableDebugLogging (true );
148+ testNoWarning (closureUnaware ("console.log(0 + 1);" ));
149+
150+ Path dir =
151+ Path .of (
152+ this .getLastCompiler ().getOptions ().getDebugLogDirectory ().toString (),
153+ TranspileAndOptimizeClosureUnaware .class .getSimpleName ());
154+ assertThat (listFiles (dir )).isNotEmpty ();
155+ }
156+
157+ @ Test
158+ public void testSupportsPrintingSourceAfterEachPass () {
159+ enableDebugLogging (true );
160+ this .extraOptions =
161+ (CompilerOptions options ) -> {
162+ options .setPrintSourceAfterEachPass (true );
163+ options .setInputDelimiter ("~~test123~ : %name%" );
164+ options .setPrintInputDelimiter (true );
165+ };
166+
167+ testNoWarning (closureUnaware ("console.log(0 + 1);" ));
168+
169+ Path dir =
170+ Path .of (
171+ this .getLastCompiler ().getOptions ().getDebugLogDirectory ().toString (),
172+ TranspileAndOptimizeClosureUnaware .class .getSimpleName ());
173+ var firstSource = loadFile (dir .resolve ("Compiler/source_after_pass/000_parseInputs" ));
174+ assertThat (firstSource )
175+ .isEqualTo (
176+ """
177+ ~~test123~ : synthetic_base
178+ 'use strict';
179+ ~~test123~ : testcode.shadow0
180+ $jscomp_sink_closure_unaware_impl(function(){console.log(0+1)});\
181+ """ );
182+ }
183+
184+ @ Test
185+ public void testSupportsPrintingSourceAfterEachPass_withRegexpFilter () {
186+ enableDebugLogging (true );
187+ this .extraOptions =
188+ (CompilerOptions options ) -> {
189+ options .setPrintSourceAfterEachPass (true );
190+ options .setPrettyPrint (true );
191+ options .setFilesToPrintAfterEachPassRegexList (ImmutableList .of (".*shadow1" ));
192+ };
193+
194+ testNoWarning (
195+ srcs (
196+ closureUnaware ("log('0.0');" , "log('0.1');" , "log('0.2');" ),
197+ closureUnaware ("log('1.0');" , "log('1.1');" , "log('1.2');" )));
198+
199+ Path dir =
200+ Path .of (
201+ this .getLastCompiler ().getOptions ().getDebugLogDirectory ().toString (),
202+ TranspileAndOptimizeClosureUnaware .class .getSimpleName ());
203+ var firstSource = loadFile (dir .resolve ("Compiler/source_after_pass/000_parseInputs" ));
204+ assertThat (firstSource )
205+ .isEqualTo (
206+ """
207+ // testcode0.shadow1
208+ $jscomp_sink_closure_unaware_impl(function() {
209+ log("0.1");
210+ });
211+ // testcode1.shadow1
212+ $jscomp_sink_closure_unaware_impl(function() {
213+ log("1.1");
214+ });
215+ """ );
216+ }
217+
112218 private String closureUnaware (String ... closureUnaware ) {
113219 String prefix =
114220 String .format (
0 commit comments