From e399ee7a9adf2c17a06f103f4f50e7d3823cc87c Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Thu, 11 May 2023 16:47:17 -0700 Subject: [PATCH] Fix migrate command version matchers Previously, Smithy models with versions without a decimal suffix (`.0`) would not be matched properly in the `smithy migrate` command. For Smithy models with a version of `"1"`, the upgraded files would result in conflicting version control statements, e.g. ```smithy $version: "2.0" $version: "1" // ... ``` Similarly for Smithy models with a version of `"2"`, the files would incorrectly attempt to be upgraded, e.g. ```smithy $version: "2.0" $version: "2" // ... ``` This change loosens the version matchers to match the [`Version::fromString`](https://github.com/awslabs/smithy/blob/c95943844a65a6623f79ac647e54326d87a68c24/smithy-model/src/main/java/software/amazon/smithy/model/loader/Version.java#L205-L216) implementation. --- .../smithy/cli/commands/MigrateCommand.java | 4 ++-- .../cli/commands/MigrateCommandTest.java | 19 +++++++++++++++++++ ...ion.v1.smithy => version-absent.v1.smithy} | 0 ...ion.v2.smithy => version-absent.v2.smithy} | 0 .../cases/version-decimal-suffix.v1.smithy | 5 +++++ .../cases/version-decimal-suffix.v2.smithy | 5 +++++ .../cases/version-no-decimal-suffix.v1.smithy | 5 +++++ .../cases/version-no-decimal-suffix.v2.smithy | 5 +++++ .../upgrade/no-op/decimal-suffix.v2.smithy | 1 + .../upgrade/no-op/no-decimal-suffix.v2.smithy | 1 + 10 files changed, 43 insertions(+), 2 deletions(-) rename smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/{version.v1.smithy => version-absent.v1.smithy} (100%) rename smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/{version.v2.smithy => version-absent.v2.smithy} (100%) create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v1.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v2.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v1.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v2.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/decimal-suffix.v2.smithy create mode 100644 smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/no-decimal-suffix.v2.smithy diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/MigrateCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/MigrateCommand.java index a922398d227..e081e6c0d26 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/MigrateCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/MigrateCommand.java @@ -70,8 +70,8 @@ final class MigrateCommand implements Command { private static final Logger LOGGER = Logger.getLogger(MigrateCommand.class.getName()); - private static final Pattern VERSION_1 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"1\\.0\"\\s*$"); - private static final Pattern VERSION_2 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"2\\.0\"\\s*$"); + private static final Pattern VERSION_1 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"1(\\.0)?\"\\s*$"); + private static final Pattern VERSION_2 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"2(\\.0)?\"\\s*$"); private final String parentCommandName; MigrateCommand(String parentCommandName) { diff --git a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/MigrateCommandTest.java b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/MigrateCommandTest.java index 9d2c42aa14a..7c20b8cf330 100644 --- a/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/MigrateCommandTest.java +++ b/smithy-cli/src/test/java/software/amazon/smithy/cli/commands/MigrateCommandTest.java @@ -114,6 +114,25 @@ public void testUpgradeDirectoryWithJar() throws Exception { assertDirEqual(baseDir.getParent().resolve("v2"), tempDir); } + @ParameterizedTest(name = "{1}") + @MethodSource("noopSource") + public void testUpgradeV2Noop(Path noopTestFilePath, String name) { + Model model = Model.assembler().addImport(noopTestFilePath).assemble().unwrap(); + String actual = new MigrateCommand("smithy").upgradeFile(model, noopTestFilePath); + String expected = IoUtils.readUtf8File(noopTestFilePath); + + if (!actual.equals(expected)) { + Assertions.fail("Expected models to be equal:\n\nActual:\n\n" + actual + "\n\nExpected:\n\n" + expected); + } + } + + public static Stream noopSource() throws Exception { + Path start = Paths.get(MigrateCommandTest.class.getResource("upgrade/no-op").toURI()); + return Files.walk(start) + .filter(path -> Files.isRegularFile(path)) + .map(path -> Arguments.of(path, path.getFileName().toString().replace(".v2.smithy", ""))); + } + private void assertDirEqual(Path actualDir, Path expectedDir) throws Exception { Set files = Files.walk(actualDir) .filter(Files::isRegularFile) diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version.v1.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-absent.v1.smithy similarity index 100% rename from smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version.v1.smithy rename to smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-absent.v1.smithy diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-absent.v2.smithy similarity index 100% rename from smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version.v2.smithy rename to smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-absent.v2.smithy diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v1.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v1.smithy new file mode 100644 index 00000000000..75513885011 --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v1.smithy @@ -0,0 +1,5 @@ +$version: "1.0" + +namespace com.example + +string Foo diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v2.smithy new file mode 100644 index 00000000000..1c4e1bafe8a --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-decimal-suffix.v2.smithy @@ -0,0 +1,5 @@ +$version: "2.0" + +namespace com.example + +string Foo diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v1.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v1.smithy new file mode 100644 index 00000000000..15fa4f3d80f --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v1.smithy @@ -0,0 +1,5 @@ +$version: "1" + +namespace com.example + +string Foo diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v2.smithy new file mode 100644 index 00000000000..1c4e1bafe8a --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/cases/version-no-decimal-suffix.v2.smithy @@ -0,0 +1,5 @@ +$version: "2.0" + +namespace com.example + +string Foo diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/decimal-suffix.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/decimal-suffix.v2.smithy new file mode 100644 index 00000000000..7343178e2b7 --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/decimal-suffix.v2.smithy @@ -0,0 +1 @@ +$version: "2.0" diff --git a/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/no-decimal-suffix.v2.smithy b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/no-decimal-suffix.v2.smithy new file mode 100644 index 00000000000..ec40852bad1 --- /dev/null +++ b/smithy-cli/src/test/resources/software/amazon/smithy/cli/commands/upgrade/no-op/no-decimal-suffix.v2.smithy @@ -0,0 +1 @@ +$version: "2"