Skip to content

Commit bc85dc8

Browse files
authored
semver: fix Maven difference (google#191)
There is a bug when computing Maven difference: `1.0.0` is considered to have a minor diff with `1.0.1` - this is because `1.0.0` is canonicalized to `1` and returns `-1` for minor and patch numbers. This PR fixes this issue by returning `0` as the number of an empty element.
1 parent 884efa3 commit bc85dc8

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

util/semver/maven.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,10 @@ func compareMavenQualifier(a, b string) int {
384384
}
385385

386386
// num returns the number of the i'th element. If the element
387-
// is not a number, it returns -1.
387+
// is empty or not a number, it returns 0.
388388
func (m *mavenExtension) num(i int) int64 {
389389
if i >= len(m.elems) {
390-
return -1
390+
return 0
391391
}
392392
str := m.elems[i].str
393393
// By the rules of building elements, we know that if the

util/semver/maven_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,13 @@ func TestMavenDifference(t *testing.T) {
232232
diff Diff
233233
}{
234234
{"1", "1", 0, Same},
235+
{"1", "1.0.0", 0, Same},
235236
{"1.2", "1.2", 0, Same},
236237
{"1.2.3", "1.2.3", 0, Same},
237238
{"2", "1", 1, DiffMajor},
238239
{"1.3", "1.2", 1, DiffMinor},
239240
{"1.2.4", "1.2.3", 1, DiffPatch},
241+
{"1.0.1", "1.0.0", 1, DiffPatch},
240242
{"1.a", "1.b", -1, DiffOther},
241243
}
242244
for _, test := range tests {

0 commit comments

Comments
 (0)