Skip to content

Commit 0d1f78f

Browse files
committed
improving malformed koan output on startup
1 parent 0877a6a commit 0d1f78f

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

‎koans-lib/src/com/sandwich/koan/runner/AppLauncher.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public class AppLauncher {
1616

1717
public static void main(final String... args) throws Throwable {
1818
Map<ArgumentType, CommandLineArgument> argsMap = new CommandLineArgumentBuilder(args);
19+
if(argsMap.containsKey(ArgumentType.RUN_KOANS)){
20+
FileMonitorFactory.getInstance(DirectoryManager.getProdMainDir())
21+
.addFileSavedListener(new KoanFileCompileAndRunListener(argsMap));
22+
}
1923
new CommandLineArgumentRunner(argsMap).run();
2024
if(ApplicationSettings.isDebug()){
2125
StringBuilder argsBuilder = new StringBuilder();
@@ -25,9 +29,5 @@ public static void main(final String... args) throws Throwable {
2529
}
2630
ApplicationUtils.getPresenter().displayMessage(argsBuilder.toString());
2731
}
28-
if(argsMap.containsKey(ArgumentType.RUN_KOANS)){
29-
FileMonitorFactory.getInstance(DirectoryManager.getProdMainDir())
30-
.addFileSavedListener(new KoanFileCompileAndRunListener(argsMap));
31-
}
3232
}
3333
}

‎koans-lib/src/com/sandwich/koan/runner/RunKoans.java‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
import com.sandwich.koan.Koan;
1212
import com.sandwich.koan.KoanMethod;
1313
import com.sandwich.koan.cmdline.behavior.AbstractArgumentBehavior;
14+
import com.sandwich.koan.constant.KoanConstants;
1415
import com.sandwich.koan.path.PathToEnlightenment;
1516
import com.sandwich.koan.path.PathToEnlightenment.Path;
1617
import com.sandwich.koan.path.xmltransformation.KoanElementAttributes;
1718
import com.sandwich.koan.result.KoanMethodResult;
1819
import com.sandwich.koan.result.KoanSuiteResult;
1920
import com.sandwich.koan.result.KoanSuiteResult.KoanResultBuilder;
2021
import com.sandwich.koan.util.ApplicationUtils;
22+
import com.sandwich.util.ExceptionUtils;
2123
import com.sandwich.util.KoanComparator;
2224
import com.sandwich.util.io.CompilationListener;
2325
import com.sandwich.util.io.DynamicClassLoader;
26+
import com.sandwich.util.io.FileUtils;
2427
import com.sandwich.util.io.KoanSuiteCompilationListener;
2528

2629
public class RunKoans extends AbstractArgumentBehavior {
@@ -53,13 +56,7 @@ KoanSuiteResult runKoans() {
5356
for (Entry<String, Map<String, KoanElementAttributes>> e : packages.getValue().entrySet()) {
5457
String name = e.getKey().substring(e.getKey().lastIndexOf('.')+1);
5558
if(failure == null){
56-
Object suite = constructSuite(loader, e.getKey(), compilationListener);
57-
while(compilationListener.isLastCompilationAttemptFailure()) {
58-
try {
59-
Thread.sleep(1000);
60-
} catch (InterruptedException e1) {}
61-
suite = constructSuite(loader, e.getKey(), compilationListener);
62-
}
59+
Object suite = safelyConstructSuite(loader, compilationListener, e);
6360
final List<KoanElementAttributes> attributes = new ArrayList<KoanElementAttributes>(e.getValue().values());
6461
final List<KoanMethod> methods = mergeJavaFilesMethodsAndThoseInXml(suite, attributes, pathToEnlightement.getOnlyMethodNameToRun());
6562
Collections.sort(methods, new KoanComparator());
@@ -90,6 +87,24 @@ KoanSuiteResult runKoans() {
9087
.passingCases(passingSuites).remainingCases(failingSuites).build();
9188
}
9289

90+
private Object safelyConstructSuite(DynamicClassLoader loader,
91+
KoanSuiteCompilationListener compilationListener,
92+
Entry<String, Map<String, KoanElementAttributes>> e) {
93+
// this is written strangely so stack trace will always be the same when constructSuite fails
94+
// this permits the app to only show the compilation failure once, despite the fact this
95+
// is getting hit every second.
96+
Object suite = null;
97+
while(suite == null || compilationListener.isLastCompilationAttemptFailure()) {
98+
suite = constructSuite(loader, e.getKey(), compilationListener);
99+
if(compilationListener.isLastCompilationAttemptFailure()){
100+
try {
101+
Thread.sleep(1000);
102+
} catch (InterruptedException e1) {}
103+
}
104+
}
105+
return suite;
106+
}
107+
93108
private List<KoanMethod> mergeJavaFilesMethodsAndThoseInXml(Object suite,
94109
final List<KoanElementAttributes> attributes, String onlyMethodNameToRun) {
95110
final List<KoanMethod> methods = new ArrayList<KoanMethod>();
@@ -137,6 +152,16 @@ private Object constructSuite(DynamicClassLoader loader, String className, Compi
137152
}
138153
} catch (Exception e1) {
139154
throw new RuntimeException(e1);
155+
} catch (Error e1) {
156+
if(e1.getClass() == Error.class && e1.getMessage().contains("Unresolved compilation problem")) {
157+
listener.compilationFailed(FileUtils.getSourceFileFromClass(className),
158+
new String[]{"(compilation yielded a class, but it cannot be cereated)"},
159+
-1, "Unable to load class \""+className+"\"." + KoanConstants.EOL +
160+
ExceptionUtils.convertToPopulatedStackTraceString(e1), e1);
161+
return null; // just consume this exception, this will have been logged and is handled
162+
}else{
163+
throw new RuntimeException(e1);
164+
}
140165
}
141166
return suite;
142167
}

‎koans-lib/src/com/sandwich/util/io/KoanSuiteCompilationListener.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class KoanSuiteCompilationListener implements CompilationListener {
88
private String lastMessageShown = null;
99

1010
public void compilationFailed(File src, String[] command, int exitCode, String errorMessage, Throwable x) {
11-
if(!errorMessage.equals(lastMessageShown)){
11+
if(lastMessageShown == null || !errorMessage.trim().equals(lastMessageShown.trim())){
1212
FileCompilerAction.LOGGING_HANDLER.compilationFailed(src, command, exitCode, errorMessage, x);
1313
}
1414
lastMessageShown = errorMessage;

‎koans/lib/koans.jar‎

632 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)