Skip to content

Commit b6e01c7

Browse files
fix: allow version strings of the format m.n.o-dev.p
1 parent f76ac68 commit b6e01c7

3 files changed

Lines changed: 61 additions & 5 deletions

File tree

src/main/java/com/regnosys/rosetta/generator/python/PythonCodeGeneratorCLI.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@
6363
* to every generated namespace (e.g. {@code finos} turns {@code cdm.x.y} into
6464
* {@code finos.cdm.x.y})</li>
6565
* <li><b>-v, --version &lt;version&gt;</b>: Version for the generated Python
66-
* package, in <code>#.#.#</code> format (e.g. {@code 1.2.3}). Defaults to
66+
* package, in <code>#.#.#</code> (e.g. {@code 1.2.3}) or
67+
* <code>#.#.#-dev.#</code> (e.g. {@code 1.2.3-dev.4}) format. The dev form is
68+
* converted to PEP 440 format (e.g. {@code 1.2.3.dev4}). Defaults to
6769
* {@code 0.0.0} when omitted. An error is raised if the supplied value does not
68-
* match the required format.</li>
70+
* match either required format.</li>
6971
* <li><b>-e, --allow-errors</b>: Continue generation even if Rosetta
7072
* validation errors occur</li>
7173
* <li><b>-w, --fail-on-warnings</b>: Treat Rosetta validation warnings as
@@ -95,6 +97,7 @@
9597
public class PythonCodeGeneratorCLI {
9698
private static final Logger LOGGER = LoggerFactory.getLogger(PythonCodeGeneratorCLI.class);
9799
private static final Pattern VALID_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+\\.\\d+");
100+
private static final Pattern DEV_VERSION_PATTERN = Pattern.compile("(\\d+\\.\\d+\\.\\d+)-dev\\.(\\d+)");
98101
static final String DEFAULT_VERSION = "0.0.0";
99102

100103
public static void main(String[] args) {
@@ -149,9 +152,14 @@ public int execute(String[] args) {
149152
String namespacePrefix = cmd.getOptionValue("x");
150153
String versionInput = cmd.getOptionValue("v");
151154

152-
if (versionInput != null && !VALID_VERSION_PATTERN.matcher(versionInput).matches()) {
153-
System.err.println("Invalid version format '" + versionInput + "': must be #.#.# (e.g. 1.2.3)");
154-
return 1;
155+
if (versionInput != null) {
156+
java.util.regex.Matcher devMatcher = DEV_VERSION_PATTERN.matcher(versionInput);
157+
if (devMatcher.matches()) {
158+
versionInput = devMatcher.group(1) + ".dev" + devMatcher.group(2);
159+
} else if (!VALID_VERSION_PATTERN.matcher(versionInput).matches()) {
160+
System.err.println("Invalid version format '" + versionInput + "': must be #.#.# (e.g. 1.2.3) or #.#.#-dev.# (e.g. 1.2.3-dev.4)");
161+
return 1;
162+
}
155163
}
156164

157165
if (cmd.hasOption("s")) {

src/main/java/com/regnosys/rosetta/generator/python/util/PythonCodeGeneratorUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public static String cleanVersion(String version) {
119119
}
120120

121121
String[] versionParts = version.split("\\.");
122+
if (versionParts.length == 4 && versionParts[3].matches("dev\\d+")) {
123+
return version;
124+
}
122125
if (versionParts.length > 2) {
123126
String thirdPart = versionParts[2].replaceAll("[^\\d]", "");
124127
return versionParts[0] + "." + versionParts[1] + "." + thirdPart;

src/test/java/com/regnosys/rosetta/generator/python/PythonCodeGeneratorCLITest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,51 @@ void testInvalidVersionFormatWithSnapshotSuffixReturnsError() {
114114
assertEquals(1, exitCode, "Should return 1 when version has a non-numeric suffix");
115115
}
116116

117+
@Test
118+
void testDevVersionFormatIsAcceptedAndConverted() throws IOException {
119+
Path validFile = createValidRosettaFile(tempDir);
120+
Path outputDir = tempDir.resolve("python");
121+
TestCLI cli = new TestCLI();
122+
123+
int exitCode = cli.execute(new String[] {
124+
"-f", validFile.toString(),
125+
"-t", outputDir.toString(),
126+
"-v", "1.2.3-dev.4"
127+
});
128+
129+
assertEquals(0, exitCode, "Should return 0 when version is in m.n.o-dev.p format");
130+
Path toml = outputDir.resolve("pyproject.toml");
131+
assertTrue(Files.exists(toml), "pyproject.toml should be written to the output directory");
132+
String tomlContent = Files.readString(toml);
133+
assertTrue(tomlContent.contains("version = \"1.2.3.dev4\""),
134+
"pyproject.toml should contain the converted PEP 440 dev version");
135+
}
136+
137+
@Test
138+
void testDevVersionWithZeroPatchIsAccepted() throws IOException {
139+
Path validFile = createValidRosettaFile(tempDir);
140+
Path outputDir = tempDir.resolve("python");
141+
TestCLI cli = new TestCLI();
142+
143+
int exitCode = cli.execute(new String[] {
144+
"-f", validFile.toString(),
145+
"-t", outputDir.toString(),
146+
"-v", "1.2.3-dev.0"
147+
});
148+
149+
assertEquals(0, exitCode, "Should return 0 when dev counter is 0");
150+
String tomlContent = Files.readString(outputDir.resolve("pyproject.toml"));
151+
assertTrue(tomlContent.contains("version = \"1.2.3.dev0\""),
152+
"pyproject.toml should contain the converted version with dev0");
153+
}
154+
155+
@Test
156+
void testDevVersionWithNonNumericDevCounterReturnsError() {
157+
PythonCodeGeneratorCLI cli = new PythonCodeGeneratorCLI();
158+
int exitCode = cli.execute(new String[] { "-s", ".", "-v", "1.2.3-dev.abc" });
159+
assertEquals(1, exitCode, "Should return 1 when dev counter is not numeric");
160+
}
161+
117162
@Test
118163
void testValidVersionAppearsInToml() throws IOException {
119164
Path validFile = createValidRosettaFile(tempDir);

0 commit comments

Comments
 (0)