Skip to content

Commit

Permalink
Fix migrate command version matchers (#1769)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Steven Yuan authored May 12, 2023
1 parent c959438 commit 3675c72
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arguments> 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<Path> files = Files.walk(actualDir)
.filter(Files::isRegularFile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$version: "1.0"

namespace com.example

string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$version: "2.0"

namespace com.example

string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$version: "1"

namespace com.example

string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$version: "2.0"

namespace com.example

string Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$version: "2.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$version: "2"

0 comments on commit 3675c72

Please sign in to comment.