Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test failures when build under Windows #834

Merged
merged 4 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ private File findDockerFileFile(Logger log) {
if (dFile.isAbsolute()) {
throw new IllegalArgumentException("<dockerFile> can not be absolute path if <dockerFileDir> also set.");
}
if (EnvUtil.isWindows() && !EnvUtil.isValidWindowsFileName(dockerFile)) {
throw new IllegalArgumentException(String.format("Invalid Windows file name %s for <dockerFile>", dockerFile));
}
return new File(dockerFileDir, dockerFile);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static String interpolate(File dockerFile, Properties properties, String
try (BufferedReader reader = new BufferedReader(new FileReader(dockerFile))) {
String line;
while ((line = reader.readLine()) != null) {
ret.append(interpolateLine(line, properties, delimiters)).append("\n");
ret.append(interpolateLine(line, properties, delimiters)).append(System.lineSeparator());
}
}
return ret.toString();
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/fabric8/maven/docker/util/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,35 @@ public static Date loadTimestamp(File tsFile) throws MojoExecutionException {
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}

/**
* Validate that the provided filename is a valid Windows filename.
*
* The validation of the Windows filename is copied from stackoverflow: https://stackoverflow.com/a/6804755
*
* @param filename the filename
* @return filename is a valid Windows filename
*/
public static boolean isValidWindowsFileName(String filename) {

Pattern pattern = Pattern.compile(
"# Match a valid Windows filename (unspecified file system). \n" +
"^ # Anchor to start of string. \n" +
"(?! # Assert filename is not: CON, PRN, \n" +
" (?: # AUX, NUL, COM1, COM2, COM3, COM4, \n" +
" CON|PRN|AUX|NUL| # COM5, COM6, COM7, COM8, COM9, \n" +
" COM[1-9]|LPT[1-9] # LPT1, LPT2, LPT3, LPT4, LPT5, \n" +
" ) # LPT6, LPT7, LPT8, and LPT9... \n" +
" (?:\\.[^.]*)? # followed by optional extension \n" +
" $ # and end of string \n" +
") # End negative lookahead assertion. \n" +
"[^<>:\"/\\\\|?*\\x00-\\x1F]* # Zero or more valid filename chars.\n" +
"[^<>:\"/\\\\|?*\\x00-\\x1F\\ .] # Last char is not a space or dot. \n" +
"$ # Anchor to end of string. ",
Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.COMMENTS);
Matcher matcher = pattern.matcher(filename);
boolean isMatch = matcher.matches();
return isMatch;
}

}
8 changes: 8 additions & 0 deletions src/test/java/io/fabric8/maven/docker/util/EnvUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ public void fixupPath() throws Exception {


}

@Test
public void isValidWindowsFileName() {

assertFalse(EnvUtil.isValidWindowsFileName("/Dockerfile"));
assertTrue(EnvUtil.isValidWindowsFileName("Dockerfile"));
assertFalse(EnvUtil.isValidWindowsFileName("Dockerfile/"));
}

private Properties getTestProperties(String ... vals) {
Properties ret = new Properties();
Expand Down