Skip to content

Commit 6db6ce0

Browse files
committed
Fix qualifier comparison
The old code doesn't take milestone and release candidate versions into account: M1 and M2 would be equal. The new code deals with that. Closes gh-1899
1 parent 9ca35c7 commit 6db6ce0

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

start-client/src/components/utils/Version.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
const strictRange = /\[(.*),(.*)\]/
22
const halfopenRightRange = /\[(.*),(.*)\)/
33
const halfopenLeftRange = /\((.*),(.*)\]/
4-
const qualifiers = ['M', 'RC', 'BUILD-SNAPSHOT', 'RELEASE']
54

65
export const parseQualifier = version => {
7-
const qual = (version || '')
8-
.replace(/\d+/g, '')
9-
.replace(/\./g, ' ')
10-
.replace(/\s/g, '')
11-
return qualifiers.indexOf(qual) > -1 ? qual : 'RELEASE'
6+
const splitted = (version || '').split('.')
7+
if (splitted.length < 3) {
8+
return 'RELEASE'
9+
}
10+
if (splitted.length === 4) {
11+
return splitted[3]
12+
}
13+
const last = splitted[2]
14+
const dash = last.indexOf('-')
15+
if (dash !== -1) {
16+
return last.substring(dash + 1)
17+
}
18+
return 'RELEASE'
19+
}
20+
21+
export const RELEASE_ORDER = 300000
22+
const SNAPSHOT_ORDER = 200000
23+
const RELEASE_CANDIDATE_ORDER = 100000
24+
const MILESTONE_ORDER = 0
25+
26+
const getQualifierOrder = qualifier => {
27+
if (qualifier === 'RELEASE') {
28+
return RELEASE_ORDER
29+
}
30+
if (qualifier === 'SNAPSHOT' || qualifier === 'BUILD-SNAPSHOT') {
31+
return SNAPSHOT_ORDER
32+
}
33+
if (qualifier.startsWith('RC')) {
34+
return RELEASE_CANDIDATE_ORDER + parseInt(qualifier.substring(2), 10)
35+
}
36+
if (qualifier.startsWith('M')) {
37+
return MILESTONE_ORDER + parseInt(qualifier.substring(1), 10)
38+
}
39+
return RELEASE_ORDER
1240
}
1341

1442
export const parseVersion = version => {
@@ -22,7 +50,7 @@ export const parseVersion = version => {
2250
version,
2351
short: `${r[0]}.${r[1]}.${r[2]}`,
2452
major: `${r[0]}.${r[1]}.x`,
25-
qualify: qualifiers.indexOf(parseQualifier(version)),
53+
qualify: getQualifierOrder(parseQualifier(version)),
2654
minor: +r[2],
2755
}
2856
}
@@ -43,7 +71,7 @@ export const compare = (a, b) => {
4371
return result
4472
}
4573
}
46-
const qualify = version => qualifiers.indexOf(parseQualifier(version))
74+
const qualify = version => getQualifierOrder(parseQualifier(version))
4775
result = qualify(a) - qualify(b)
4876
if (result !== 0) {
4977
return result
@@ -53,20 +81,19 @@ export const compare = (a, b) => {
5381

5482
export const parseReleases = releases => {
5583
return releases.map(release => {
56-
const version = parseVersion(release.key)
57-
return version
84+
return parseVersion(release.key)
5885
})
5986
}
6087

6188
export const isInRange = (version, range) => {
6289
if (!range) {
6390
return true
6491
}
65-
const strickMatch = range.match(strictRange)
66-
if (strickMatch) {
92+
const strictMatch = range.match(strictRange)
93+
if (strictMatch) {
6794
return (
68-
compare(strickMatch[1], version) <= 0 &&
69-
compare(strickMatch[2], version) >= 0
95+
compare(strictMatch[1], version) <= 0 &&
96+
compare(strictMatch[2], version) >= 0
7097
)
7198
}
7299
const horMatch = range.match(halfopenRightRange)

start-client/src/components/utils/__tests__/Version.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
parseReleases,
99
parseVersion,
1010
rangeToText,
11+
RELEASE_ORDER,
1112
} from '../Version'
1213

1314
/**
@@ -24,9 +25,9 @@ describe('parseQualifier', () => {
2425
})
2526
it('should return a specific qualifier', () => {
2627
let release = parseQualifier('1.0.0.M2')
27-
expect(release).toBe('M')
28+
expect(release).toBe('M2')
2829
release = parseQualifier('1.0.0.RC1')
29-
expect(release).toBe('RC')
30+
expect(release).toBe('RC1')
3031
release = parseQualifier('1.0.0.BUILD-SNAPSHOT')
3132
expect(release).toBe('BUILD-SNAPSHOT')
3233
})
@@ -41,7 +42,7 @@ describe('parseVersion', () => {
4142
expect(get(version, 'version')).toBe('1.0.1.RELEASE')
4243
expect(get(version, 'short')).toBe('1.0.1')
4344
expect(get(version, 'major')).toBe('1.0.x')
44-
expect(get(version, 'qualify')).toBe(3)
45+
expect(get(version, 'qualify')).toBe(RELEASE_ORDER)
4546
expect(get(version, 'minor')).toBe(1)
4647
})
4748

@@ -89,7 +90,7 @@ describe('parseReleases', () => {
8990
expect(get(result[0], 'version')).toBe('1.0.1.RELEASE')
9091
expect(get(result[0], 'short')).toBe('1.0.1')
9192
expect(get(result[0], 'major')).toBe('1.0.x')
92-
expect(get(result[0], 'qualify')).toBe(3)
93+
expect(get(result[0], 'qualify')).toBe(RELEASE_ORDER)
9394
expect(get(result[0], 'minor')).toBe(1)
9495
})
9596
})
@@ -112,6 +113,26 @@ describe('isInRange', () => {
112113
result = isInRange('2.0.0.M1', '[2.0.0.RELEASE,2.2.0.M1)')
113114
expect(result).toBe(false)
114115
})
116+
it('should match backend logic', () => {
117+
const rangeInclusive = '[3.4.0,4.0.0-M2]'
118+
const rangeExclusive = '[3.4.0,4.0.0-M3)'
119+
expect(isInRange('3.4.9', rangeInclusive)).toBe(true)
120+
expect(isInRange('3.4.9', rangeExclusive)).toBe(true)
121+
expect(isInRange('3.4.10-SNAPSHOT', rangeInclusive)).toBe(true)
122+
expect(isInRange('3.4.10-SNAPSHOT', rangeExclusive)).toBe(true)
123+
expect(isInRange('3.5.5', rangeInclusive)).toBe(true)
124+
expect(isInRange('3.5.5', rangeExclusive)).toBe(true)
125+
expect(isInRange('3.5.6-SNAPSHOT', rangeInclusive)).toBe(true)
126+
expect(isInRange('3.5.6-SNAPSHOT', rangeExclusive)).toBe(true)
127+
expect(isInRange('4.0.0-M1', rangeInclusive)).toBe(true)
128+
expect(isInRange('4.0.0-M1', rangeExclusive)).toBe(true)
129+
expect(isInRange('4.0.0-M2', rangeInclusive)).toBe(true)
130+
expect(isInRange('4.0.0-M2', rangeExclusive)).toBe(true)
131+
expect(isInRange('4.0.0-M3', rangeInclusive)).toBe(false)
132+
expect(isInRange('4.0.0-M3', rangeExclusive)).toBe(false)
133+
expect(isInRange('4.0.0-SNAPSHOT', rangeInclusive)).toBe(false)
134+
expect(isInRange('4.0.0-SNAPSHOT', rangeExclusive)).toBe(false)
135+
})
115136
})
116137

117138
/**

0 commit comments

Comments
 (0)