Skip to content

Commit

Permalink
Fix various Windows issues with tests
Browse files Browse the repository at this point in the history
* Fix tests with resources to work on Windows.
* Fix line-ending sensitive tests.
  • Loading branch information
mtdowling committed Sep 23, 2020
1 parent 51a52d7 commit 717858a
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void performsSubstitutionsByDefault() {
.unwrap();

ObjectNode expected = Node.parse(
IoUtils.readUtf8File(getClass().getResource("substitution-performed.json").getPath()))
IoUtils.readUtf8Resource(getClass(), "substitution-performed.json"))
.expectObjectNode();
OpenApiConfig config = new OpenApiConfig();
config.setService(ShapeId.from("example.smithy#MyService"));
Expand All @@ -55,7 +55,7 @@ public void pluginCanBeDisabled() {
.unwrap();

ObjectNode expected = Node.parse(
IoUtils.readUtf8File(getClass().getResource("substitution-not-performed.json").getPath()))
IoUtils.readUtf8Resource(getClass(), "substitution-not-performed.json"))
.expectObjectNode();

OpenApiConfig config = new OpenApiConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
Expand Down Expand Up @@ -149,6 +150,10 @@ public void rewritesArgsArrayToUnderscoreArgs() {
}

private String getResourcePath(String name) {
return SmithyBuildTest.class.getResource(name).getPath();
try {
return Paths.get(SmithyBuildTest.class.getResource(name).toURI()).toString();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ConfigurableSmithyBuildPluginTest {
@Test
public void loadsConfigurationClass() {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/a.smithy").getPath())
.addImport(getClass().getResource("sources/a.smithy"))
.assemble()
.unwrap();
MockManifest manifest = new MockManifest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -18,10 +19,10 @@

public class SourcesPluginTest {
@Test
public void copiesFilesForSourceProjection() {
public void copiesFilesForSourceProjection() throws URISyntaxException {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/a.smithy").getPath())
.addImport(getClass().getResource("sources/b.smithy").getPath())
.addImport(getClass().getResource("sources/a.smithy"))
.addImport(getClass().getResource("sources/b.smithy"))
.addImport(getClass().getResource("sources/c/c.json"))
.addImport(getClass().getResource("notsources/d.smithy"))
.assemble()
Expand All @@ -31,10 +32,12 @@ public void copiesFilesForSourceProjection() {
.fileManifest(manifest)
.model(model)
.originalModel(model)
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent()))
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent()))
.build();
new SourcesPlugin().execute(context);
String manifestString = manifest.getFileString("manifest").get();
// Normalize for Windows.
manifestString = manifestString.replace("\\", "/");

assertThat(manifestString, containsString("a.smithy\n"));
assertThat(manifestString, containsString("b.smithy\n"));
Expand All @@ -46,9 +49,9 @@ public void copiesFilesForSourceProjection() {
}

@Test
public void copiesModelFromJarWithSourceProjection() {
public void copiesModelFromJarWithSourceProjection() throws URISyntaxException {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/jar-import.jar").getPath())
.addImport(getClass().getResource("sources/jar-import.jar"))
.addImport(getClass().getResource("notsources/d.smithy"))
.assemble()
.unwrap();
Expand All @@ -57,10 +60,12 @@ public void copiesModelFromJarWithSourceProjection() {
.fileManifest(manifest)
.model(model)
.originalModel(model)
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").getPath())))
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI())))
.build();
new SourcesPlugin().execute(context);
String manifestString = manifest.getFileString("manifest").get();
// Normalize for Windows.
manifestString = manifestString.replace("\\", "/");

assertThat(manifestString, containsString("jar-import/a.smithy\n"));
assertThat(manifestString, containsString("jar-import/b/b.smithy\n"));
Expand All @@ -72,9 +77,9 @@ public void copiesModelFromJarWithSourceProjection() {
}

@Test
public void copiesModelFromJarWithNonSourceProjection() {
public void copiesModelFromJarWithNonSourceProjection() throws URISyntaxException {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/jar-import.jar").getPath())
.addImport(getClass().getResource("sources/jar-import.jar"))
.addImport(getClass().getResource("notsources/d.smithy"))
.assemble()
.unwrap();
Expand All @@ -85,10 +90,12 @@ public void copiesModelFromJarWithNonSourceProjection() {
.projection("foo", projection)
.model(model)
.originalModel(model)
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").getPath())))
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/jar-import.jar").toURI())))
.build();
new SourcesPlugin().execute(context);
String manifestString = manifest.getFileString("manifest").get();
// Normalize for Windows.
manifestString = manifestString.replace("\\", "/");

assertThat(manifestString, containsString("model.json"));
assertThat(manifestString, not(containsString("jar-import")));
Expand All @@ -98,11 +105,11 @@ public void copiesModelFromJarWithNonSourceProjection() {
}

@Test
public void copiesOnlyFilesFromSourcesForProjection() {
public void copiesOnlyFilesFromSourcesForProjection() throws URISyntaxException {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/a.smithy").getPath())
.addImport(getClass().getResource("sources/b.smithy").getPath())
.addImport(getClass().getResource("sources/c/c.json").getPath())
.addImport(getClass().getResource("sources/a.smithy"))
.addImport(getClass().getResource("sources/b.smithy"))
.addImport(getClass().getResource("sources/c/c.json"))
.addImport(getClass().getResource("notsources/d.smithy"))
.assemble()
.unwrap();
Expand All @@ -113,7 +120,7 @@ public void copiesOnlyFilesFromSourcesForProjection() {
.fileManifest(manifest)
.model(model)
.originalModel(model)
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent()))
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent()))
.build();
new SourcesPlugin().execute(context);
String manifestString = manifest.getFileString("manifest").get();
Expand All @@ -135,7 +142,7 @@ public void copiesOnlyFilesFromSourcesForProjection() {
}

@Test
public void treatsNewlyAddedShapesAsNewSources() {
public void treatsNewlyAddedShapesAsNewSources() throws URISyntaxException {
Model originalModel = Model.assembler().assemble().unwrap();
Model newModel = Model.assembler()
.addShape(StringShape.builder().id("a.b#MyString").build())
Expand All @@ -148,7 +155,7 @@ public void treatsNewlyAddedShapesAsNewSources() {
.fileManifest(manifest)
.originalModel(originalModel)
.model(newModel)
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").getPath()).getParent()))
.sources(ListUtils.of(Paths.get(getClass().getResource("sources/a.smithy").toURI()).getParent()))
.build();
new SourcesPlugin().execute(context);
String modelString = manifest.getFileString("model.json").get();
Expand All @@ -157,10 +164,10 @@ public void treatsNewlyAddedShapesAsNewSources() {
}

@Test
public void doesNotAllowConflicts() {
public void doesNotAllowConflicts() throws URISyntaxException {
Model model = Model.assembler()
.addImport(getClass().getResource("sources/a.smithy").getPath())
.addImport(getClass().getResource("conflicting/a.smithy").getPath())
.addImport(getClass().getResource("sources/a.smithy"))
.addImport(getClass().getResource("conflicting/a.smithy"))
.assemble()
.unwrap();
MockManifest manifest = new MockManifest();
Expand All @@ -169,8 +176,8 @@ public void doesNotAllowConflicts() {
.model(model)
.originalModel(model)
.sources(ListUtils.of(
Paths.get(getClass().getResource("sources/a.smithy").getPath()),
Paths.get(getClass().getResource("conflicting/a.smithy").getPath())))
Paths.get(getClass().getResource("sources/a.smithy").toURI()),
Paths.get(getClass().getResource("conflicting/a.smithy").toURI())))
.build();

Assertions.assertThrows(SourcesConflictException.class, () -> new SourcesPlugin().execute(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -38,8 +40,8 @@ public void skipsMissingConfigFiles() {
}

@Test
public void addsConfigFilesWhenFound() {
String configFile = getClass().getResource("smithy-build-a.json").getPath();
public void addsConfigFilesWhenFound() throws URISyntaxException {
String configFile = Paths.get(getClass().getResource("smithy-build-a.json").toURI()).toString();
BuildParameterBuilder.Result result = new BuildParameterBuilder()
.addConfigIfExists(configFile)
.build();
Expand Down Expand Up @@ -213,10 +215,10 @@ public void projectionBuildTaggedSourcesRemovedFromModelDiscovery() {
}

@Test
public void findsProjectionJarsWithSourceTags() {
String a = getClass().getResource("jars/a/a.jar").getPath();
String b = getClass().getResource("jars/b/b.jar").getPath();
String c = getClass().getResource("jars/c/c.jar").getPath();
public void findsProjectionJarsWithSourceTags() throws URISyntaxException {
String a = Paths.get(getClass().getResource("jars/a/a.jar").toURI()).toString();
String b = Paths.get(getClass().getResource("jars/b/b.jar").toURI()).toString();
String c = Paths.get(getClass().getResource("jars/c/c.jar").toURI()).toString();
String separator = System.getProperty("path.separator");
String buildCp = a + separator + b + separator + c;

Expand All @@ -237,14 +239,14 @@ public void findsProjectionJarsWithSourceTags() {
}

@Test
public void usesCustomSeparator() {
public void usesCustomSeparator() throws URISyntaxException {
String currentSeparator = System.getProperty("path.separator");

try {
System.setProperty("path.separator", "|");
String a = getClass().getResource("jars/a/a.jar").getPath();
String b = getClass().getResource("jars/b/b.jar").getPath();
String c = getClass().getResource("jars/c/c.jar").getPath();
String a = Paths.get(getClass().getResource("jars/a/a.jar").toURI()).toString();
String b = Paths.get(getClass().getResource("jars/b/b.jar").toURI()).toString();
String c = Paths.get(getClass().getResource("jars/c/c.jar").toURI()).toString();
String buildCp = a + "|" + b + "|" + c;

BuildParameterBuilder.Result result = new BuildParameterBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.cli.CliError;
Expand All @@ -31,7 +33,7 @@ public void usesModelDiscoveryWithCustomValidClasspath() throws Exception {
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);

String dir = getClass().getResource("valid.jar").getPath();
String dir = Paths.get(getClass().getResource("valid.jar").toURI()).toString();
SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir);
System.setOut(out);

Expand All @@ -42,7 +44,7 @@ public void usesModelDiscoveryWithCustomValidClasspath() throws Exception {
@Test
public void usesModelDiscoveryWithCustomInvalidClasspath() {
CliError e = Assertions.assertThrows(CliError.class, () -> {
String dir = getClass().getResource("invalid.jar").getPath();
String dir = Paths.get(getClass().getResource("invalid.jar").toURI()).toString();
SmithyCli.create().run("ast", "--debug", "--discover-classpath", dir);
});

Expand All @@ -60,8 +62,8 @@ public void failsOnUnknownTrait() {
}

@Test
public void allowsUnknownTrait() {
String model = getClass().getResource("unknown-trait.smithy").getPath();
public void allowsUnknownTrait() throws URISyntaxException {
String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString();
SmithyCli.create().run("ast", "--allow-unknown-traits", model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.cli.CliError;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception {
System.setOut(printStream);

CliError e = Assertions.assertThrows(CliError.class, () -> {
String model = getClass().getResource("unknown-trait.smithy").getPath();
String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString();
SmithyCli.create().run("build", model);
});

Expand All @@ -61,7 +62,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception {

@Test
public void printsSuccessfulProjections() throws Exception {
String model = getClass().getResource("valid-model.smithy").getPath();
String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString();

PrintStream out = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand All @@ -83,8 +84,8 @@ public void validationFailuresCausedByProjectionsAreDetected() throws Exception
System.setOut(printStream);

CliError e = Assertions.assertThrows(CliError.class, () -> {
String model = getClass().getResource("valid-model.smithy").getPath();
String config = getClass().getResource("projection-build-failure.json").getPath();
String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString();
String config = Paths.get(getClass().getResource("projection-build-failure.json").toURI()).toString();
SmithyCli.create().run("build", "--debug", "--config", config, model);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.cli.CliError;
Expand Down Expand Up @@ -44,7 +45,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception {
System.setOut(outPrintStream);

CliError e = Assertions.assertThrows(CliError.class, () -> {
String model = getClass().getResource("unknown-trait.smithy").getPath();
String model = Paths.get(getClass().getResource("unknown-trait.smithy").toURI()).toString();
SmithyCli.create().run("select", "--selector", "string", model);
});

Expand All @@ -64,7 +65,7 @@ public void dumpsOutValidationErrorsAndFails() throws Exception {

@Test
public void printsSuccessfulMatchesToStdout() throws Exception {
String model = getClass().getResource("valid-model.smithy").getPath();
String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString();

PrintStream out = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand All @@ -82,7 +83,7 @@ public void printsSuccessfulMatchesToStdout() throws Exception {

@Test
public void printsJsonVarsToStdout() throws Exception {
String model = getClass().getResource("valid-model.smithy").getPath();
String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString();

// Take over stdout.
PrintStream out = System.out;
Expand All @@ -101,7 +102,7 @@ public void printsJsonVarsToStdout() throws Exception {

@Test
public void readsSelectorFromStdinToo() throws Exception {
String model = getClass().getResource("valid-model.smithy").getPath();
String model = Paths.get(getClass().getResource("valid-model.smithy").toURI()).toString();

// Send the selector through input stream.
InputStream in = System.in;
Expand Down
Loading

0 comments on commit 717858a

Please sign in to comment.