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 config file as URL on Windows #254

Merged
merged 1 commit into from
Jun 1, 2018
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
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### 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/))

* Fixed a bug in configuration file resolution on Windows when file is denoted by a URL. ([#254](https://github.com/diffplug/spotless/pull/254))

### 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))

* Fixed a bug in `LicenseHeaderStep` which caused an exception with some malformed date-aware licenses. ([#222](https://github.com/diffplug/spotless/pull/222))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.diffplug.spotless.maven;

import static com.diffplug.common.base.Strings.*;
import static com.diffplug.common.base.Strings.isNullOrEmpty;

import java.io.File;
import java.util.UUID;

import org.codehaus.plexus.resource.ResourceManager;
import org.codehaus.plexus.resource.loader.FileResourceCreationException;
Expand All @@ -26,6 +27,8 @@

public class FileLocator {

static final String TMP_RESOURCE_FILE_PREFIX = "spotless-resource-";

private final ResourceManager resourceManager;

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

private static String tmpOutputFileName(String path) {
String nameWithExtension = FileUtils.filename(path);
String extension = FileUtils.extension(path);
String name = nameWithExtension.replace('.' + extension, "");
return name + '-' + System.currentTimeMillis() + '.' + extension;
return TMP_RESOURCE_FILE_PREFIX + UUID.randomUUID() + '.' + extension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package com.diffplug.spotless.maven;

import static com.diffplug.spotless.maven.FileLocator.TMP_RESOURCE_FILE_PREFIX;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

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

@Test
public void locateValidFile() throws Exception {
String path = Paths.get("tmp", "configs", "my-config.xml").toString();
File tmpOutputFile = new File("tmp-my-config.xml");
public void locateXmlFile() throws Exception {
testFileLocator(Paths.get("tmp", "configs", "my-config.xml").toString(), "xml");
}

@Test
public void locatePropertiesFile() throws Exception {
testFileLocator(Paths.get("home", "ubuntu", "my-other-config.properties").toString(), "properties");
}

@Test
public void locateConfFileWithIncorrectSeparators() throws Exception {
String oppositeSeparator = "/".equals(File.separator) ? "\\" : "/";
String path = "tmp" + oppositeSeparator + "configs" + oppositeSeparator + "hello.conf";

testFileLocator(path, "conf");
}

private void testFileLocator(String path, String extension) throws Exception {
File tmpOutputFile = new File("tmp-file");
when(resourceManager.getResourceAsFile(any(), any())).thenReturn(tmpOutputFile);

File locatedFile = fileLocator.locateFile(path);

assertEquals(tmpOutputFile, locatedFile);

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