Skip to content

Commit 2be2c45

Browse files
committed
Fix config file as URL on Windows
Configuration for various formatter steps can be located in separate files. Such files might live in the local filesystem and be identified as simple paths. They can also be remote files identified by a URL. Both types of files are copied to the output directory using Maven's `ResourceManager` which can handle both local and remote files. Previously, plugin tried to deduce the output file name based on the configured string. This was done using `FileUtils#filename()` method that expects to only work with system file separators. Issue occurred on Windows when URL was specified. Path separator on Windows is `\` and in URL it is `/`. Code wasn't able to extract the last path of the URL that denotes the file name. Instead, it treated the whole URL as a file name. URLs contain chars invalid for Windows paths and an exception was thrown on attempt to create the file. This commit fixes the problem by making code use predefined name pattern for all resolved resources. Name of the output file will not be guessed based on the configured path or URL. It will just be unique and have 'spotless-resource-' prefix.
1 parent c346971 commit 2be2c45

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Version 1.10.0-SNAPSHOT - TBD ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/snapshot/), [snapshot](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/spotless-maven-plugin/))
44

5+
* Fixed a bug in configuration file resolution on Windows when file is denoted by a URL. ([#254](https://github.com/diffplug/spotless/pull/254))
6+
57
### Version 1.0.0.BETA5 - May 14th 2018 ([javadoc](https://diffplug.github.io/spotless/javadoc/spotless-maven-plugin/1.0.0.BETA5/), [jcenter](https://bintray.com/diffplug/opensource/spotless-maven-plugin/1.0.0.BETA5))
68

79
* Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222))

plugin-maven/src/main/java/com/diffplug/spotless/maven/FileLocator.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18-
import static com.diffplug.common.base.Strings.*;
18+
import static com.diffplug.common.base.Strings.isNullOrEmpty;
1919

2020
import java.io.File;
21+
import java.util.UUID;
2122

2223
import org.codehaus.plexus.resource.ResourceManager;
2324
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
@@ -26,6 +27,8 @@
2627

2728
public class FileLocator {
2829

30+
static final String TMP_RESOURCE_FILE_PREFIX = "spotless-resource-";
31+
2932
private final ResourceManager resourceManager;
3033

3134
public FileLocator(ResourceManager resourceManager) {
@@ -48,9 +51,7 @@ public File locateFile(String path) {
4851
}
4952

5053
private static String tmpOutputFileName(String path) {
51-
String nameWithExtension = FileUtils.filename(path);
5254
String extension = FileUtils.extension(path);
53-
String name = nameWithExtension.replace('.' + extension, "");
54-
return name + '-' + System.currentTimeMillis() + '.' + extension;
55+
return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension;
5556
}
5657
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/FileLocatorTest.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
*/
1616
package com.diffplug.spotless.maven;
1717

18+
import static com.diffplug.spotless.maven.FileLocator.TMP_RESOURCE_FILE_PREFIX;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertNull;
2122
import static org.mockito.ArgumentMatchers.any;
2223
import static org.mockito.ArgumentMatchers.eq;
23-
import static org.mockito.Mockito.mock;
24-
import static org.mockito.Mockito.verify;
25-
import static org.mockito.Mockito.when;
24+
import static org.mockito.Mockito.*;
2625

2726
import java.io.File;
2827
import java.nio.file.Paths;
@@ -47,17 +46,32 @@ public void locateNull() {
4746
}
4847

4948
@Test
50-
public void locateValidFile() throws Exception {
51-
String path = Paths.get("tmp", "configs", "my-config.xml").toString();
52-
File tmpOutputFile = new File("tmp-my-config.xml");
49+
public void locateXmlFile() throws Exception {
50+
testFileLocator(Paths.get("tmp", "configs", "my-config.xml").toString(), "xml");
51+
}
52+
53+
@Test
54+
public void locatePropertiesFile() throws Exception {
55+
testFileLocator(Paths.get("home", "ubuntu", "my-other-config.properties").toString(), "properties");
56+
}
57+
58+
@Test
59+
public void locateConfFileWithIncorrectSeparators() throws Exception {
60+
String oppositeSeparator = "/".equals(File.separator) ? "\\" : "/";
61+
String path = "tmp" + oppositeSeparator + "configs" + oppositeSeparator + "hello.conf";
62+
63+
testFileLocator(path, "conf");
64+
}
65+
66+
private void testFileLocator(String path, String extension) throws Exception {
67+
File tmpOutputFile = new File("tmp-file");
5368
when(resourceManager.getResourceAsFile(any(), any())).thenReturn(tmpOutputFile);
5469

5570
File locatedFile = fileLocator.locateFile(path);
56-
5771
assertEquals(tmpOutputFile, locatedFile);
5872

5973
ArgumentCaptor<String> argCaptor = ArgumentCaptor.forClass(String.class);
6074
verify(resourceManager).getResourceAsFile(eq(path), argCaptor.capture());
61-
assertThat(argCaptor.getValue()).startsWith("my-config").endsWith(".xml");
75+
assertThat(argCaptor.getValue()).startsWith(TMP_RESOURCE_FILE_PREFIX).endsWith('.' + extension);
6276
}
6377
}

0 commit comments

Comments
 (0)