@@ -26,9 +26,11 @@ subprojects {
2626
2727 Boolean hasMainMethod = lines. join(' ' ). contains(' main(String[] args)' )
2828 Boolean hasMainParam = lines. find { it. startsWith(' // {main: ' )}
29+ Boolean willNotCompile = lines. find { it. startsWith(' // {CompileTimeError' ) }
30+ Boolean validateByHand = lines. find { it. startsWith(' // {ValidateByHand}' ) }
2931
3032 // add tasks for java sources with main methods
31- if (hasMainMethod || hasMainParam) {
33+ if (( hasMainMethod || hasMainParam) && ( ! willNotCompile) ) {
3234
3335 String maybeArgsLine = lines. find { it. startsWith(' // {Args: ' )}
3436
@@ -62,51 +64,76 @@ subprojects {
6264 maybeRunFirst = maybeRunFirstLine. trim(). replaceAll(' \\ /\\ / \\ {RunFirst: ' , ' ' ). replaceAll(' }' , ' ' )
6365 }
6466
65- task " $taskName " (type : JavaExec , dependsOn : maybeRunFirst) {
66- main = mainClass
67- classpath = sourceSets. main. runtimeClasspath
68- args = maybeArgs
69- jvmArgs = maybeJvmArgs
67+ // some java apps intentionally throw an exception
68+ Boolean maybeIgnoreExitValue = false
69+ if (lines. find { it. startsWith(' // {ThrowsException}' ) } != null ) {
70+ maybeIgnoreExitValue = true
7071 }
7172
72- Integer expectedOutputStartLine = lines. findIndexOf { it. startsWith(' /* Output:' ) }
73- Integer expectedOutputEndLine = -1
74- List expectedOutputLines = []
73+ // some java apps need a timeout
74+ Boolean timeOutDuringTesting = false
75+ if (lines. find { it. startsWith(' // {TimeOutDuringTesting}' ) } != null ) {
76+ timeOutDuringTesting = true
77+ }
78+
79+ if (timeOutDuringTesting) {
80+ // todo: apps with timeOutDuringTesting need special handling
81+ task " $taskName " << {
82+ println " not implemented"
83+ }
7584
76- if (expectedOutputStartLine != -1 ) {
77- expectedOutputStartLine + = 1
78- expectedOutputEndLine = lines. findIndexOf(expectedOutputStartLine, { it. startsWith(' */' ) })
79- if (expectedOutputEndLine == -1 ) {
80- expectedOutputEndLine = expectedOutputStartLine
85+ task " test$taskName " << {
86+ println " not implemented"
8187 }
82- expectedOutputLines = lines. subList(expectedOutputStartLine, expectedOutputEndLine)
8388 }
89+ else {
90+ task " $taskName " (type : JavaExec , dependsOn : maybeRunFirst) {
91+ main = mainClass
92+ classpath = sourceSets. main. runtimeClasspath
93+ args = maybeArgs
94+ jvmArgs = maybeJvmArgs
95+ ignoreExitValue = maybeIgnoreExitValue
96+ }
97+
98+ Integer expectedOutputStartLine = lines. findIndexOf { it. startsWith(' /* Output:' ) }
99+ Integer expectedOutputEndLine = -1
100+ List expectedOutputLines = []
101+
102+ if (expectedOutputStartLine != -1 ) {
103+ expectedOutputStartLine + = 1
104+ expectedOutputEndLine = lines. findIndexOf(expectedOutputStartLine, { it. startsWith(' */' ) })
105+ if (expectedOutputEndLine == -1 ) {
106+ expectedOutputEndLine = expectedOutputStartLine
107+ }
108+ expectedOutputLines = lines. subList(expectedOutputStartLine, expectedOutputEndLine)
109+ }
84110
85- OutputStream testStandardOutput = new ByteArrayOutputStream ()
86- OutputStream testErrorOutput = new ByteArrayOutputStream ()
87-
88- task " test$taskName " (type : JavaExec , dependsOn : maybeRunFirst) {
89- main = mainClass
90- classpath = sourceSets. main. runtimeClasspath
91- args = maybeArgs
92- jvmArgs = maybeJvmArgs
93- standardOutput = testStandardOutput
94- errorOutput = testErrorOutput
95- doLast {
96- // test the output here
97- println (" Expected" )
98- println (expectedOutputLines)
99- println (" STDOUT" )
100- println (testStandardOutput. toString(). readLines())
101- println (" STDERR" )
102- println (testErrorOutput. toString(). readLines())
103- throw new Exception (" Expected output did not match the actual output" )
111+ OutputStream testStandardOutput = new ByteArrayOutputStream ()
112+ OutputStream testErrorOutput = new ByteArrayOutputStream ()
113+
114+ task " test$taskName " (type : JavaExec , dependsOn : maybeRunFirst) {
115+ main = mainClass
116+ classpath = sourceSets. main. runtimeClasspath
117+ args = maybeArgs
118+ jvmArgs = maybeJvmArgs
119+ ignoreExitValue = maybeIgnoreExitValue
120+ standardOutput = testStandardOutput
121+ errorOutput = testErrorOutput
122+ doLast {
123+ // test the output here
124+ def rstrip = { it. replaceAll(' \\ s$' , ' ' ) }
125+ List testStandardOutputLines = testStandardOutput. toString(). readLines(). collect(rstrip)
126+
127+ assert expectedOutputLines. size() == testStandardOutputLines. size()
128+
129+ expectedOutputLines. eachWithIndex { expectedLine , i -> assert expectedLine == testStandardOutputLines. get(i) }
130+ }
104131 }
105132 }
106133 }
107134
108135 // exclude java sources that will not compile
109- if (lines . find { it . startsWith( ' // {CompileTimeError ' ) } != null ) {
136+ if (willNotCompile ) {
110137 sourceSets. main. java. excludes. add(it. name)
111138 }
112139 }
0 commit comments