Skip to content

Commit

Permalink
Fixed escaping of path string literals in test-with-junit.
Browse files Browse the repository at this point in the history
Important especially on Windows, where backslashes in paths would
otherwise cause string syntax errors in the generated test scripts.
  • Loading branch information
jukka committed Oct 27, 2010
1 parent 6a86bc1 commit 84785b9
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public void execute() throws MojoExecutionException {
for (NamespaceInFile namespace : ns) {
if (xmlEscapeOutput) {
// Assumes with-junit-output uses with-test-out internally when necessary. xml escape anything sent to *out*.
runTestLine.append("(with-open [writer (clojure.java.io/writer \"" + testOutputDirectory + "/" + namespace.getName() + ".xml\") ");
runTestLine.append("(with-open [writer (clojure.java.io/writer \"" + escapeFilePath(testOutputDirectory, namespace.getName() + ".xml") + "\") ");
runTestLine.append(" escaped (xml-escaping-writer writer)] ");
runTestLine.append("(binding [*test-out* writer *out* escaped] (with-junit-output ");
runTestLine.append("(run-tests");
runTestLine.append(" '" + namespace.getName());
runTestLine.append("))))");
} else {
// Use with-test-out to fix with-junit-output until clojure #431 is fixed
runTestLine.append("(with-open [writer (clojure.java.io/writer \"" + testOutputDirectory + "/" + namespace.getName() + ".xml\")] ");
runTestLine.append("(with-open [writer (clojure.java.io/writer \"" + escapeFilePath(testOutputDirectory, namespace.getName() + ".xml") + "\")] ");
runTestLine.append("(binding [*test-out* writer] (with-test-out (with-junit-output ");
runTestLine.append("(run-tests");
runTestLine.append(" '" + namespace.getName());
Expand Down Expand Up @@ -127,4 +127,17 @@ public void execute() throws MojoExecutionException {
}
}

/**
* Escapes the given file path so that it's safe for inclusion in a
* Clojure string literal.
*
* @param directory directory path
* @param file file name
* @return escaped file path, ready for inclusion in a string literal
*/
private static String escapeFilePath(String directory, String file) {
// TODO: Should handle also possible newlines, etc.
return new File(directory, file).getPath().replace("\\", "\\\\");
}

}

0 comments on commit 84785b9

Please sign in to comment.