Skip to content

Commit

Permalink
Loosen migrate command version 2 matcher
Browse files Browse the repository at this point in the history
Previously, Smithy 2.0 files with a version of `"2"` would be upgraded
incorrectly, ending up with duplicate and invalid version control statements,
e.g. upgrading the following:

```smithy
$version: "2"

// ...
```

would result in:

```smithy
$version: "2.0"

$version: "2"

// ...
```

This change loosens the version 2.0 matcher to match version control
statements without the decimal suffix `.0`.
  • Loading branch information
Steven Yuan committed May 11, 2023
1 parent c959438 commit 7b3e309
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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_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 @@
$version: "2.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$version: "2"

0 comments on commit 7b3e309

Please sign in to comment.