Skip to content

Commit 62458a3

Browse files
author
Steven Yuan
committed
Fix migrate command version matchers (smithy-lang#1769)
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.
1 parent f029cf2 commit 62458a3

File tree

10 files changed

+43
-2
lines changed

10 files changed

+43
-2
lines changed

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/MigrateCommand.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
final class MigrateCommand implements Command {
7171

7272
private static final Logger LOGGER = Logger.getLogger(MigrateCommand.class.getName());
73-
private static final Pattern VERSION_1 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"1\\.0\"\\s*$");
74-
private static final Pattern VERSION_2 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"2\\.0\"\\s*$");
73+
private static final Pattern VERSION_1 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"1(\\.0)?\"\\s*$");
74+
private static final Pattern VERSION_2 = Pattern.compile("(?m)^\\s*\\$\\s*version:\\s*\"2(\\.0)?\"\\s*$");
7575
private final String parentCommandName;
7676

7777
MigrateCommand(String parentCommandName) {

smithy-cli/src/test/java/software/amazon/smithy/cli/commands/MigrateCommandTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ public void testUpgradeDirectoryWithJar() throws Exception {
114114
assertDirEqual(baseDir.getParent().resolve("v2"), tempDir);
115115
}
116116

117+
@ParameterizedTest(name = "{1}")
118+
@MethodSource("noopSource")
119+
public void testUpgradeV2Noop(Path noopTestFilePath, String name) {
120+
Model model = Model.assembler().addImport(noopTestFilePath).assemble().unwrap();
121+
String actual = new MigrateCommand("smithy").upgradeFile(model, noopTestFilePath);
122+
String expected = IoUtils.readUtf8File(noopTestFilePath);
123+
124+
if (!actual.equals(expected)) {
125+
Assertions.fail("Expected models to be equal:\n\nActual:\n\n" + actual + "\n\nExpected:\n\n" + expected);
126+
}
127+
}
128+
129+
public static Stream<Arguments> noopSource() throws Exception {
130+
Path start = Paths.get(MigrateCommandTest.class.getResource("upgrade/no-op").toURI());
131+
return Files.walk(start)
132+
.filter(path -> Files.isRegularFile(path))
133+
.map(path -> Arguments.of(path, path.getFileName().toString().replace(".v2.smithy", "")));
134+
}
135+
117136
private void assertDirEqual(Path actualDir, Path expectedDir) throws Exception {
118137
Set<Path> files = Files.walk(actualDir)
119138
.filter(Files::isRegularFile)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$version: "1.0"
2+
3+
namespace com.example
4+
5+
string Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$version: "2.0"
2+
3+
namespace com.example
4+
5+
string Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$version: "1"
2+
3+
namespace com.example
4+
5+
string Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$version: "2.0"
2+
3+
namespace com.example
4+
5+
string Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$version: "2.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$version: "2"

0 commit comments

Comments
 (0)