22// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33package org .aya .test ;
44
5- import com .intellij .openapi .util .text .Strings ;
65import kala .collection .Seq ;
76import kala .collection .SeqView ;
87import org .aya .cli .single .CompilerFlags ;
2524import java .nio .file .Paths ;
2625import java .util .Locale ;
2726
28- import static org .junit .jupiter .api .Assertions .assertEquals ;
27+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2928import static org .junit .jupiter .api .Assertions .fail ;
3029
3130public class TestRunner {
32- public static final @ NotNull Path DEFAULT_TEST_DIR = Paths .get ("src" , "test" , "resources" ).toAbsolutePath ();
33- public static final @ NotNull Path TMP_FILE = DEFAULT_TEST_DIR .resolve ("tmp.aya" );
31+ public static final @ NotNull Path TEST_DIR = Paths .get ("src" , "test" , "resources" ).toAbsolutePath ();
32+ private static final @ NotNull Path FIXTURE_DIR = TEST_DIR .resolve ("negative" );
33+ private static final @ NotNull Path TMP_FILE = TEST_DIR .resolve ("tmp.aya" );
3434 public static final @ NotNull SourceFileLocator LOCATOR = new SourceFileLocator () { };
3535 @ BeforeAll public static void startDash () { Global .NO_RANDOM_NAME = true ; }
3636
3737 @ Test public void negative () throws Exception {
38- Seq .of (
38+ var toCheck = Seq .of (
3939 ParseError .class ,
4040 ExprTyckError .class ,
4141 GoalAndMeta .class ,
@@ -45,7 +45,19 @@ public class TestRunner {
4545 TerckError .class ,
4646 PatCohError .class ,
4747 ClassError .class
48- ).forEachChecked (TestRunner ::expectFixture );
48+ ).mapNotNullChecked (TestRunner ::expectFixture );
49+ if (toCheck .isNotEmpty ()) {
50+ new ProcessBuilder ("git" , "add" , FIXTURE_DIR .toString ())
51+ .inheritIO ()
52+ .start ()
53+ .waitFor ();
54+ }
55+ for (var file : toCheck ) {
56+ var name = file .toString ();
57+ var proc = new ProcessBuilder ("git" , "diff" , "--cached" , name ).start ();
58+ var output = new String (proc .getInputStream ().readAllBytes ());
59+ assertTrue (output .isBlank (), output );
60+ }
4961 Files .deleteIfExists (TMP_FILE );
5062 }
5163
@@ -60,55 +72,40 @@ public static void main(String... args) throws Exception {
6072 new TestRunner ().negative ();
6173 }
6274
63- private static String instantiateVars (String template ) {
64- return template .replace ("$FILE" , TMP_FILE .toString ());
65- }
66-
67- private static String instantiateHoles (String template ) {
75+ private static String replaceFileName (String template ) {
6876 return template .replace (TMP_FILE .toString (), "$FILE" );
6977 }
7078
71- private static void checkOutput (Path expectedOutFile , String hookOut ) {
72- try {
73- var output = Strings .convertLineSeparators (hookOut );
74- var expected = instantiateVars (Strings .convertLineSeparators (
75- Files .readString (expectedOutFile , StandardCharsets .UTF_8 )));
76- assertEquals (expected , output , expectedOutFile .getFileName ().toString ());
77- } catch (IOException e ) {
78- fail ("error reading file " + expectedOutFile .toAbsolutePath ());
79- }
80- }
81-
82- private static void expectFixture (Class <?> fixturesClass ) throws IllegalAccessException , IOException {
83- var result = runFixtureClass (fixturesClass );
84- var expectedOutFile = DEFAULT_TEST_DIR
85- .resolve ("negative" )
86- .resolve (fixturesClass .getSimpleName () + ".txt" );
79+ /// @return not null for a file to check, null if we're good to go
80+ private static Path expectFixture (Class <?> fixturesClass ) throws IllegalAccessException , IOException , InterruptedException {
81+ var result = replaceFileName (runFixtureClass (fixturesClass ));
82+ var expectedOutFile = FIXTURE_DIR .resolve (fixturesClass .getSimpleName () + ".txt" );
8783 if (Files .exists (expectedOutFile )) {
88- checkOutput (expectedOutFile , result );
84+ writeWorkflow (expectedOutFile , result );
85+ return expectedOutFile ;
8986 } else {
90- System .out .println (); // add line break after `--->`
91- generateWorkflow (expectedOutFile , result );
87+ System .out .println (); // add line break before `NOTE`
88+ writeWorkflow (expectedOutFile , result );
89+ System .out .printf (Locale .getDefault (),
90+ """
91+ NOTE: write the following output to `%s`.
92+ ----------------------------------------
93+ %s
94+ ----------------------------------------
95+ """ ,
96+ expectedOutFile .getFileName (),
97+ result
98+ );
9299 }
100+ return null ;
93101 }
94102
95- private static void generateWorkflow (Path expectedOutFile , String hookOut ) {
96- hookOut = instantiateHoles (hookOut );
103+ private static void writeWorkflow (Path expectedOutFile , String hookOut ) {
97104 try {
98105 FileUtil .writeString (expectedOutFile , hookOut );
99106 } catch (IOException e ) {
100107 fail ("error generating todo file " + expectedOutFile .toAbsolutePath ());
101108 }
102- System .out .printf (Locale .getDefault (),
103- """
104- NOTE: write the following output to `%s`.
105- ----------------------------------------
106- %s
107- ----------------------------------------
108- """ ,
109- expectedOutFile .getFileName (),
110- hookOut
111- );
112109 }
113110
114111 private static String runFixtureClass (Class <?> fixturesClass )
@@ -141,7 +138,7 @@ private static void runSingleCase(String code, SingleFileCompiler compiler) thro
141138 }
142139
143140 public static @ NotNull CompilerFlags flags () {
144- var modulePaths = SeqView .of (DEFAULT_TEST_DIR .resolve ("shared/src" ));
141+ var modulePaths = SeqView .of (TEST_DIR .resolve ("shared/src" ));
145142 return new CompilerFlags (CompilerFlags .Message .ASCII ,
146143 false , false , null , modulePaths , null );
147144 }
0 commit comments