Fix compatiblity with Microsoft Windows

Closes gh-879
This commit is contained in:
Stephane Nicoll
2019-05-28 14:30:51 +02:00
parent 62d0a94364
commit e6f287b298
6 changed files with 29 additions and 12 deletions

View File

@@ -60,13 +60,17 @@ class GradleWrapperContributorTests {
private Consumer<Path> isNotExecutable() { private Consumer<Path> isNotExecutable() {
return (path) -> { return (path) -> {
if (Files.isExecutable(path)) { if (supportsExecutableFlag() && Files.isExecutable(path)) {
throw Failures.instance().failure(String throw Failures.instance().failure(String
.format("%nExpecting:%n <%s>%nto not be executable.", path)); .format("%nExpecting:%n <%s>%nto not be executable.", path));
} }
}; };
} }
private static boolean supportsExecutableFlag() {
return !System.getProperty("os.name").startsWith("Windows");
}
Path contribute(String gradleVersion) throws IOException { Path contribute(String gradleVersion) throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-"); Path projectDir = Files.createTempDirectory(this.directory, "project-");
new GradleWrapperContributor(gradleVersion).contribute(projectDir); new GradleWrapperContributor(gradleVersion).contribute(projectDir);

View File

@@ -52,13 +52,17 @@ class MavenWrapperContributorTests {
private Consumer<Path> isNotExecutable() { private Consumer<Path> isNotExecutable() {
return (path) -> { return (path) -> {
if (Files.isExecutable(path)) { if (supportsExecutableFlag() && Files.isExecutable(path)) {
throw Failures.instance().failure(String throw Failures.instance().failure(String
.format("%nExpecting:%n <%s>%nto not be executable.", path)); .format("%nExpecting:%n <%s>%nto not be executable.", path));
} }
}; };
} }
private static boolean supportsExecutableFlag() {
return !System.getProperty("os.name").startsWith("Windows");
}
Path contribute() throws IOException { Path contribute() throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-"); Path projectDir = Files.createTempDirectory(this.directory, "project-");
new MavenWrapperContributor().contribute(projectDir); new MavenWrapperContributor().contribute(projectDir);

View File

@@ -42,8 +42,8 @@ public class SourceCodeAssert {
} }
public SourceCodeAssert equalsTo(Resource expected) { public SourceCodeAssert equalsTo(Resource expected) {
try (InputStream stream = expected.getInputStream()) { try (InputStream in = expected.getInputStream()) {
String expectedContent = StreamUtils.copyToString(stream, String expectedContent = StreamUtils.copyToString(in,
Charset.forName("UTF-8")); Charset.forName("UTF-8"));
assertThat(this.content).describedAs("Content for %s", this.name) assertThat(this.content).describedAs("Content for %s", this.name)
.isEqualTo(expectedContent.replaceAll("\r\n", "\n")); .isEqualTo(expectedContent.replaceAll("\r\n", "\n"));

View File

@@ -17,6 +17,7 @@
package io.spring.initializr.generator.test.project; package io.spring.initializr.generator.test.project;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult; import java.nio.file.FileVisitResult;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@@ -75,7 +76,8 @@ public class ProjectStructure {
} }
/** /**
* Return the relative paths of all files. * Return the relative paths of all files. For consistency, always use {@code /} as
* path separator.
* @return the relative path of all files * @return the relative path of all files
*/ */
public List<String> getRelativePathsOfProjectFiles() { public List<String> getRelativePathsOfProjectFiles() {
@@ -84,8 +86,7 @@ public class ProjectStructure {
Files.walkFileTree(this.projectDirectory, new SimpleFileVisitor<Path>() { Files.walkFileTree(this.projectDirectory, new SimpleFileVisitor<Path>() {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
relativePaths.add(ProjectStructure.this.projectDirectory relativePaths.add(createRelativePath(file));
.relativize(file).toString());
return FileVisitResult.CONTINUE; return FileVisitResult.CONTINUE;
} }
}); });
@@ -96,4 +97,12 @@ public class ProjectStructure {
return relativePaths; return relativePaths;
} }
private String createRelativePath(Path file) {
String relativePath = this.projectDirectory.relativize(file).toString();
if (FileSystems.getDefault().getSeparator().equals("\\")) {
return relativePath.replace('\\', '/');
}
return relativePath;
}
} }

View File

@@ -16,6 +16,7 @@
package io.spring.initializr.metadata; package io.spring.initializr.metadata;
import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -202,9 +203,8 @@ public final class InitializrMetadataBuilder {
@Override @Override
public void customize(InitializrMetadata metadata) { public void customize(InitializrMetadata metadata) {
logger.info("Loading initializr metadata from " + this.resource); logger.info("Loading initializr metadata from " + this.resource);
try { try (InputStream in = this.resource.getInputStream()) {
String content = StreamUtils.copyToString(this.resource.getInputStream(), String content = StreamUtils.copyToString(in, UTF_8);
UTF_8);
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
InitializrMetadata anotherMetadata = objectMapper.readValue(content, InitializrMetadata anotherMetadata = objectMapper.readValue(content,
InitializrMetadata.class); InitializrMetadata.class);

View File

@@ -243,8 +243,8 @@ public abstract class AbstractInitializrIntegrationTests {
protected JSONObject readJsonFrom(String path) { protected JSONObject readJsonFrom(String path) {
try { try {
ClassPathResource resource = new ClassPathResource(path); ClassPathResource resource = new ClassPathResource(path);
try (InputStream stream = resource.getInputStream()) { try (InputStream in = resource.getInputStream()) {
String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); String json = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
String placeholder = ""; String placeholder = "";
if (this instanceof AbstractInitializrControllerIntegrationTests) { if (this instanceof AbstractInitializrControllerIntegrationTests) {
placeholder = ((AbstractInitializrControllerIntegrationTests) this).host; placeholder = ((AbstractInitializrControllerIntegrationTests) this).host;