Skip to content

Commit 49b9895

Browse files
committed
Correctly handle "-snapshot" and "+build" in semantic versioning
This fix a regression introduced in: 048a8a6 (VersionHelper now correctly strip snapshot info) actually neither 048a8a6 nor the version before are correct becuase: 048a8a6 - strips all the extra `-snapshot` and `+build` previous - doesn't handle the case `x.y-snapshot` Now both are handled correctly and a test has been added to verify this. To be completely semver compliant we should deny versions in the format `x.y`, but this will break all legacy version that have been published until now, so this changed should be postponed for the next major release of the IDE. Fix #5251
1 parent 0272252 commit 49b9895

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of Arduino.
3+
*
4+
* Copyright 2016 Arduino LLC (http://www.arduino.cc/)
5+
*
6+
* Arduino is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
* As a special exception, you may use this file as part of a free software
21+
* library without restriction. Specifically, if other files instantiate
22+
* templates or use macros or inline functions from this file, or you compile
23+
* this file and link it with other files to produce an executable, this
24+
* file does not by itself cause the resulting executable to be covered by
25+
* the GNU General Public License. This exception does not however
26+
* invalidate any other reasons why the executable file might be covered by
27+
* the GNU General Public License.
28+
*/
29+
30+
package cc.arduino.contributions;
31+
32+
import static org.junit.Assert.assertEquals;
33+
34+
import org.junit.Test;
35+
36+
public class VersionHelperTest {
37+
38+
@Test
39+
public void testVersions() throws Exception {
40+
assertEquals("1.0.0", VersionHelper.valueOf("1.0.0").toString());
41+
assertEquals("1.0.0", VersionHelper.valueOf("1.0").toString());
42+
assertEquals("1.0.0", VersionHelper.valueOf("1").toString());
43+
assertEquals("1.0.0-abc", VersionHelper.valueOf("1.0.0-abc").toString());
44+
assertEquals("1.0.0-abc", VersionHelper.valueOf("1.0-abc").toString());
45+
assertEquals("1.0.0-abc", VersionHelper.valueOf("1-abc").toString());
46+
assertEquals("1.0.0+abc", VersionHelper.valueOf("1.0.0+abc").toString());
47+
assertEquals("1.0.0+abc", VersionHelper.valueOf("1.0+abc").toString());
48+
assertEquals("1.0.0+abc", VersionHelper.valueOf("1+abc").toString());
49+
assertEquals("1.0.0-def+abc", VersionHelper.valueOf("1.0.0-def+abc").toString());
50+
assertEquals("1.0.0-def+abc", VersionHelper.valueOf("1.0-def+abc").toString());
51+
assertEquals("1.0.0-def+abc", VersionHelper.valueOf("1-def+abc").toString());
52+
assertEquals("1.0.0+def-abc", VersionHelper.valueOf("1.0.0+def-abc").toString());
53+
assertEquals("1.0.0+def-abc", VersionHelper.valueOf("1.0+def-abc").toString());
54+
assertEquals("1.0.0+def-abc", VersionHelper.valueOf("1+def-abc").toString());
55+
}
56+
57+
}

arduino-core/src/cc/arduino/contributions/VersionHelper.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,26 @@ public static Version valueOf(String ver) {
3838
return null;
3939
}
4040
try {
41-
if (ver.contains("-")) {
42-
// Strip snapshot info
43-
ver = ver.split("-")[0];
41+
// Allow x.y-something, assuming x.y.0-something
42+
// Allow x-something, assuming x.0.0-something
43+
String version = ver;
44+
String extra = "";
45+
String split[] = ver.split("[+-]", 2);
46+
if (split.length == 2) {
47+
version = split[0];
48+
extra = ver.substring(version.length()); // includes separator + or -
4449
}
45-
String[] verParts = ver.split("\\.");
46-
if (verParts.length < 3) {
47-
if (verParts.length == 2) {
48-
return Version.forIntegers(Integer.valueOf(verParts[0]), Integer.valueOf(verParts[1]));
49-
} else {
50-
return Version.forIntegers(Integer.valueOf(verParts[0]));
51-
}
52-
} else {
50+
String[] parts = version.split("\\.");
51+
if (parts.length >= 3) {
5352
return Version.valueOf(ver);
5453
}
54+
if (parts.length == 2) {
55+
version += ".0";
56+
}
57+
if (parts.length == 1) {
58+
version += ".0.0";
59+
}
60+
return Version.valueOf(version + extra);
5561
} catch (Exception e) {
5662
System.err.println("Invalid version found: " + ver);
5763
return null;

0 commit comments

Comments
 (0)