From f8a53c05eee86eb0c3c6675e05598b0915bd69ec Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Mon, 14 Dec 2020 15:04:54 +0100 Subject: [PATCH] Fix building and testing on Windows --- build.gradle | 2 +- .../schema/fromsmithy/TestRunnerTest.java | 5 +++-- smithy-aws-protocol-tests/build.gradle | 2 +- .../amazon/smithy/build/SmithyBuildTest.java | 2 +- .../core/writer/CodegenWriterDelegatorTest.java | 11 +++++++---- .../validation/testrunner/SmithyTestCase.java | 2 +- .../InvalidSmithyModelLoaderRunnerTest.java | 16 ++++++++++++++-- .../smithy/model/loader/ModelAssemblerTest.java | 11 ++++++++++- .../shapes/SmithyIdlModelSerializerTest.java | 5 +++-- .../amazon/smithy/utils/IoUtilsTest.java | 10 ++++++---- 10 files changed, 47 insertions(+), 19 deletions(-) diff --git a/build.gradle b/build.gradle index 4aac721ce9f..ef00fa9dacb 100644 --- a/build.gradle +++ b/build.gradle @@ -28,7 +28,7 @@ plugins { ext { // Load the Smithy version from VERSION. - libraryVersion = project.file("VERSION").getText('UTF-8').replace("\n", "") + libraryVersion = project.file("VERSION").getText('UTF-8').replace(System.lineSeparator(), "") } println "Smithy version: '${libraryVersion}'" diff --git a/smithy-aws-cloudformation/src/test/java/software/amazon/smithy/aws/cloudformation/schema/fromsmithy/TestRunnerTest.java b/smithy-aws-cloudformation/src/test/java/software/amazon/smithy/aws/cloudformation/schema/fromsmithy/TestRunnerTest.java index 052ea391246..bbf57f5bd95 100644 --- a/smithy-aws-cloudformation/src/test/java/software/amazon/smithy/aws/cloudformation/schema/fromsmithy/TestRunnerTest.java +++ b/smithy-aws-cloudformation/src/test/java/software/amazon/smithy/aws/cloudformation/schema/fromsmithy/TestRunnerTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; @@ -62,12 +63,12 @@ public void generatesResources(String modelFile) { public static List integFiles() { try { - return Files.walk(Paths.get(TestRunnerTest.class.getResource("integ").getPath())) + return Files.walk(Paths.get(TestRunnerTest.class.getResource("integ").toURI())) .filter(Files::isRegularFile) .filter(file -> file.toString().endsWith(".smithy")) .map(Object::toString) .collect(Collectors.toList()); - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { throw new RuntimeException(e); } } diff --git a/smithy-aws-protocol-tests/build.gradle b/smithy-aws-protocol-tests/build.gradle index da15337e8d8..cc7649bb732 100644 --- a/smithy-aws-protocol-tests/build.gradle +++ b/smithy-aws-protocol-tests/build.gradle @@ -14,7 +14,7 @@ */ plugins { - id "software.amazon.smithy" version "0.5.1" + id "software.amazon.smithy" version "0.5.2" } description = "Defines protocol tests for AWS HTTP protocols." diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/SmithyBuildTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/SmithyBuildTest.java index d1ab154e407..83df27f87b6 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/SmithyBuildTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/SmithyBuildTest.java @@ -149,7 +149,7 @@ public void createsEmptyManifest() throws Exception { assertThat(files, hasItem(outputDirectory.resolve("source/sources/manifest"))); assertThat("\n", equalTo(IoUtils.readUtf8File(results.allArtifacts() - .filter(path -> path.toString().endsWith("/manifest")) + .filter(path -> path.toString().endsWith(System.getProperty("file.separator") + "manifest")) .findFirst() .get()))); } diff --git a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/writer/CodegenWriterDelegatorTest.java b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/writer/CodegenWriterDelegatorTest.java index fb66d693d14..fd35454f3ef 100644 --- a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/writer/CodegenWriterDelegatorTest.java +++ b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/writer/CodegenWriterDelegatorTest.java @@ -21,6 +21,7 @@ import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.is; +import java.nio.file.Paths; import java.util.Optional; import org.junit.jupiter.api.Test; import software.amazon.smithy.build.MockManifest; @@ -57,7 +58,7 @@ public void createsSymbolsAndFilesForShapeWriters() { delegator.useShapeWriter(shape, writer -> { }); assertThat(observer.onShapeWriterUseCalled, is(true)); - assertThat(delegator.getWriters(), hasKey("com/foo/Baz.bam")); + assertThat(delegator.getWriters(), hasKey(Paths.get("com/foo/Baz.bam").toString())); } @Test @@ -77,7 +78,7 @@ public void canObserveAndWriteBeforeEachFile() { writer.write("Hello"); }); - assertThat(delegator.getWriters().get("com/foo/Baz.bam").toString(), + assertThat(delegator.getWriters().get(Paths.get("com/foo/Baz.bam").toString()).toString(), equalTo("/// Writing com.foo#Baz\nHello\n")); } @@ -114,7 +115,8 @@ public void writesNewlineBetweenFiles() { writer.write("."); }); - assertThat(delegator.getWriters().get("foo/baz").toString(), equalTo(".\n\n.\n")); + assertThat(delegator.getWriters().get(Paths.get("foo/baz").toString()).toString(), + equalTo(".\n\n.\n")); } @Test @@ -133,7 +135,8 @@ public void canDisableNewlineBetweenFiles() { writer.writeInline("."); }); - assertThat(delegator.getWriters().get("foo/baz").toString(), equalTo("..\n")); + assertThat(delegator.getWriters().get(Paths.get("foo/baz").toString()).toString(), + equalTo("..\n")); } @Test diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java index fce49355a83..d2c1dd52ef4 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/validation/testrunner/SmithyTestCase.java @@ -144,7 +144,7 @@ private static String inferErrorFileLocation(String modelLocation) { private static List loadExpectedEvents(String errorsFileLocation) { String contents = IoUtils.readUtf8File(errorsFileLocation); - return Arrays.stream(contents.split("\n")) + return Arrays.stream(contents.split(System.lineSeparator())) .filter(line -> !line.trim().isEmpty()) .map(SmithyTestCase::parseValidationEvent) .collect(Collectors.toList()); diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/loader/InvalidSmithyModelLoaderRunnerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/loader/InvalidSmithyModelLoaderRunnerTest.java index e0731d65b38..91a4e9bcdc5 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/loader/InvalidSmithyModelLoaderRunnerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/loader/InvalidSmithyModelLoaderRunnerTest.java @@ -49,8 +49,8 @@ public void testParserRunner(String file) { .unwrap(); throw new IllegalStateException("Expected a parse error for " + file); } catch (RuntimeException e) { - String actualMessage = e.getMessage().replace("\n", "\\n"); - String expectedMessage = expectedError.replace("\n", "\\n"); + String actualMessage = cleanErrorMessage(e.getMessage()); + String expectedMessage = cleanErrorMessage(expectedError); if (!actualMessage.contains(expectedMessage)) { throw new IllegalStateException( String.format("Expected a different parse error for %s.\nExpected (%s)\nFound (%s)", @@ -60,6 +60,18 @@ public void testParserRunner(String file) { } } + private String cleanErrorMessage(String errorMessage) { + return errorMessage + // We'll never see EOF on Windows since we only get 2 context characters and those + // will be taken up by the line separator characters. + .replace("[EOF]", "") + // Make sure the line separators and representations of them are consistent across + // operating systems. + .replace("\r\n", "\\n") + .replace("\r", "\\n") + .replace("\n", "\\n"); + } + public static Collection data() throws Exception { try { Stream paths = Files.walk(Paths.get( diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java index 128b63a9d29..375cb1b71f8 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/loader/ModelAssemblerTest.java @@ -33,6 +33,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.FileSystemException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -44,6 +45,7 @@ import java.util.Optional; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import software.amazon.smithy.model.Model; @@ -268,7 +270,14 @@ public Path createSymbolicLink(Path target, String linkName) throws IOException if (Files.exists(link)) { Files.delete(link); } - return Files.createSymbolicLink(link, target); + try { + return Files.createSymbolicLink(link, target); + } catch (FileSystemException e) { + // Skip tests if symlinks are unable to be created. This can happen on Windows, for instance, where the + // permissions to create them are not enabled by default. + Assumptions.assumeFalse(System.getProperty("os.name").toLowerCase().contains("windows"), e.getMessage()); + throw e; + } } @Test diff --git a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java index 55928d35106..49e0fb28685 100644 --- a/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java +++ b/smithy-model/src/test/java/software/amazon/smithy/model/shapes/SmithyIdlModelSerializerTest.java @@ -43,7 +43,7 @@ public void testConversion(Path path) { } String serializedString = serialized.entrySet().iterator().next().getValue(); - Assertions.assertEquals(serializedString, IoUtils.readUtf8File(path)); + Assertions.assertEquals(serializedString, IoUtils.readUtf8File(path).replaceAll("\\R", "\n")); } @Test @@ -57,7 +57,8 @@ public void multipleNamespacesGenerateMultipleFiles() throws Exception { .basePath(outputDir) .build(); Map serialized = serializer.serialize(model); - serialized.forEach((path, generated) -> assertThat(generated, equalTo(IoUtils.readUtf8File(path)))); + serialized.forEach((path, generated) -> assertThat( + generated, equalTo(IoUtils.readUtf8File(path).replaceAll("\\R", "\n")))); } @Test diff --git a/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java b/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java index 8b1b8c7a2d3..c2679926605 100644 --- a/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java +++ b/smithy-utils/src/test/java/software/amazon/smithy/utils/IoUtilsTest.java @@ -70,23 +70,25 @@ public void readsFromStringPath() throws Exception { // Windows doesn't like the result of URL#getPath, so to test this // we create a Path from the URI, convert that to a string, then pass // it to the helper method which uses Paths.get again. - assertEquals("This is a test.\n", + assertEquals("This is a test." + System.lineSeparator(), IoUtils.readUtf8File(Paths.get(getClass().getResource("test.txt").toURI()).toString())); } @Test public void readsFromPath() throws URISyntaxException { - assertEquals("This is a test.\n", IoUtils.readUtf8File(Paths.get(getClass().getResource("test.txt").toURI()))); + assertEquals("This is a test." + System.lineSeparator(), + IoUtils.readUtf8File(Paths.get(getClass().getResource("test.txt").toURI()))); } @Test public void readsFromClass() { - assertEquals("This is a test.\n", IoUtils.readUtf8Resource(getClass(), "test.txt")); + assertEquals("This is a test." + System.lineSeparator(), + IoUtils.readUtf8Resource(getClass(), "test.txt")); } @Test public void readsFromClassLoader() { - assertEquals("This is a test.\n", IoUtils.readUtf8Resource( + assertEquals("This is a test." + System.lineSeparator(), IoUtils.readUtf8Resource( getClass().getClassLoader(), "software/amazon/smithy/utils/test.txt")); }