Skip to content

Commit 9924e95

Browse files
committed
#599 mlUnitTest throws better error when it cannot write results
1 parent ccd4f96 commit 9924e95

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/main/groovy/com/marklogic/gradle/task/test/UnitTestTask.groovy

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,29 @@ class UnitTestTask extends MarkLogicTask {
7070
def report = new DefaultJUnitTestReporter().reportOnJUnitTestSuites(suites)
7171
println report
7272

73-
String resultsPath = "build/test-results/marklogic-unit-test"
73+
File resultsDir = new File(getProject().getProjectDir(), "build/test-results/marklogic-unit-test")
7474
String resultProperty = "unitTestResultsPath"
7575
if (project.hasProperty(resultProperty)) {
76-
resultsPath = project.property(resultProperty)
76+
resultsDir = new File(project.property(resultProperty))
7777
}
7878

79-
File resultsDir = new File(resultsPath)
8079
if (resultsDir.exists()) {
8180
try {
82-
FileUtils.cleanDirectory(resultsDir)
81+
FileUtils.deleteDirectory(resultsDir)
8382
println "Deleted existing results directory: " + resultsDir
8483
} catch (Exception e) {
8584
println "Unable to delete test results directory: " + resultsDir
8685
}
8786
}
88-
resultsDir.mkdirs()
87+
88+
// The resultsDir may exist in case the call to delete it failed, in which case the exception is logged
89+
// but not rethrown. In that case, we don't need to try to make the directory.
90+
if (!resultsDir.exists()) {
91+
if (!resultsDir.mkdirs()) {
92+
throw new GradleException("Unable to run tests; unable to create results directory at: " + resultsDir +
93+
"; please ensure you have write permission to this directory")
94+
}
95+
}
8996

9097
int fileCount = 0;
9198
boolean testsFailed = false
@@ -99,12 +106,11 @@ class UnitTestTask extends MarkLogicTask {
99106
fileCount++;
100107
}
101108

102-
println "\n" + fileCount + " test result files were written to: " + resultsPath
109+
println "\n" + fileCount + " test result files were written to: " + resultsDir
103110

104111
if (testsFailed) {
105-
throw new GradleException("There were failing tests. See the test results at: " + resultsPath)
112+
throw new GradleException("There were failing tests. See the test results at: " + resultsDir)
106113
}
107-
108114
} finally {
109115
client.release()
110116
}

0 commit comments

Comments
 (0)