Skip to content

Commit acbb7d3

Browse files
Akos Kittakittaakos
Akos Kitta
authored andcommitted
ATL-428: Fixed the semver ordering for installable
Signed-off-by: Akos Kitta <[email protected]>
1 parent 781747f commit acbb7d3

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

Diff for: arduino-ide-extension/src/common/protocol/installable.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as semver from 'semver';
12
import { naturalCompare } from './../utils';
23
import { ArduinoComponent } from './arduino-component';
34

@@ -18,6 +19,11 @@ export namespace Installable {
1819
/**
1920
* Most recent version comes first, then the previous versions. (`1.8.1`, `1.6.3`, `1.6.2`, `1.6.1` and so on.)
2021
*/
21-
export const COMPARATOR = (left: Version, right: Version) => naturalCompare(right, left);
22+
export const COMPARATOR = (left: Version, right: Version) => {
23+
if (semver.valid(left) && semver.valid(right)) {
24+
return semver.compare(left, right);
25+
}
26+
return naturalCompare(left, right);
27+
};
2228
}
2329
}

Diff for: arduino-ide-extension/src/node/boards-service-impl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export class BoardsServiceImpl implements BoardsService, Disposable {
375375
const pkg = packages.get(id);
376376
if (pkg) {
377377
pkg.availableVersions.push(platform.getLatest());
378-
pkg.availableVersions.sort(Installable.Version.COMPARATOR);
378+
pkg.availableVersions.sort(Installable.Version.COMPARATOR).reverse();
379379
} else {
380380
packages.set(id, toPackage(platform));
381381
}

Diff for: arduino-ide-extension/src/node/library-service-server-impl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export class LibraryServiceImpl implements LibraryService {
7979
.slice(0, 50)
8080
.map(item => {
8181
// TODO: This seems to contain only the latest item instead of all of the items.
82-
const availableVersions = item.getReleasesMap().getEntryList().map(([key, _]) => key).sort(Installable.Version.COMPARATOR);
82+
const availableVersions = item.getReleasesMap().getEntryList().map(([key, _]) => key).sort(Installable.Version.COMPARATOR).reverse();
8383
let installedVersion: string | undefined;
8484
const installed = installedLibsIdx.get(item.getName());
8585
if (installed) {
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { expect } from 'chai';
2+
import { Installable } from '../../common/protocol/installable';
3+
4+
describe('installable', () => {
5+
6+
describe('compare', () => {
7+
8+
const testMe = Installable.Version.COMPARATOR;
9+
10+
([
11+
['1.8.1', '1.8.1', 0],
12+
['1.8.1', '1.6.1', 1],
13+
['1.6.1', '1.8.1', -1],
14+
['1.6.1', '1.6.3', -1],
15+
['5.1.1', '5.1.0', 1],
16+
['5.1.0', '5.1.0-beta.1', 1],
17+
['5.1.0-beta.1', '5.1.0', -1],
18+
['5.1.0-beta.2', '5.1.0-beta.1', 1],
19+
['5.1.0-beta.1', '5.1.0-beta.2', -1],
20+
['5.1.0-beta.1', '5.1.1', -1],
21+
['1.1.0', '1.1.0-a', 1],
22+
['1.1.0-a', '1.1.0', -1],
23+
['COM1', 'COM2', -1],
24+
['COM1', 'COM10', -1],
25+
['COM10', 'COM1', 1],
26+
['COM10', 'COM2', 1],
27+
['COM2', 'COM10', -1],
28+
['COM10', 'COM10', 0],
29+
] as Array<[string, string, number]>).forEach(([left, right, expectation]) => {
30+
it(`'${left}' should be ${expectation === 0 ? 'equal to' : expectation < 0 ? 'less than' : 'greater than'} '${right}'`, () => {
31+
const actual = testMe(left, right);
32+
expect(actual).to.be.equal(expectation);
33+
});
34+
});
35+
36+
});
37+
38+
});

0 commit comments

Comments
 (0)