ATL-428: Fixed the semver ordering for installable

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta 2020-11-04 09:59:21 +01:00 committed by Akos Kitta
parent 781747fe80
commit acbb7d32b2
4 changed files with 47 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import * as semver from 'semver';
import { naturalCompare } from './../utils';
import { ArduinoComponent } from './arduino-component';
@ -18,6 +19,11 @@ export namespace Installable {
/**
* Most recent version comes first, then the previous versions. (`1.8.1`, `1.6.3`, `1.6.2`, `1.6.1` and so on.)
*/
export const COMPARATOR = (left: Version, right: Version) => naturalCompare(right, left);
export const COMPARATOR = (left: Version, right: Version) => {
if (semver.valid(left) && semver.valid(right)) {
return semver.compare(left, right);
}
return naturalCompare(left, right);
};
}
}

View File

@ -375,7 +375,7 @@ export class BoardsServiceImpl implements BoardsService, Disposable {
const pkg = packages.get(id);
if (pkg) {
pkg.availableVersions.push(platform.getLatest());
pkg.availableVersions.sort(Installable.Version.COMPARATOR);
pkg.availableVersions.sort(Installable.Version.COMPARATOR).reverse();
} else {
packages.set(id, toPackage(platform));
}

View File

@ -79,7 +79,7 @@ export class LibraryServiceImpl implements LibraryService {
.slice(0, 50)
.map(item => {
// TODO: This seems to contain only the latest item instead of all of the items.
const availableVersions = item.getReleasesMap().getEntryList().map(([key, _]) => key).sort(Installable.Version.COMPARATOR);
const availableVersions = item.getReleasesMap().getEntryList().map(([key, _]) => key).sort(Installable.Version.COMPARATOR).reverse();
let installedVersion: string | undefined;
const installed = installedLibsIdx.get(item.getName());
if (installed) {

View File

@ -0,0 +1,38 @@
import { expect } from 'chai';
import { Installable } from '../../common/protocol/installable';
describe('installable', () => {
describe('compare', () => {
const testMe = Installable.Version.COMPARATOR;
([
['1.8.1', '1.8.1', 0],
['1.8.1', '1.6.1', 1],
['1.6.1', '1.8.1', -1],
['1.6.1', '1.6.3', -1],
['5.1.1', '5.1.0', 1],
['5.1.0', '5.1.0-beta.1', 1],
['5.1.0-beta.1', '5.1.0', -1],
['5.1.0-beta.2', '5.1.0-beta.1', 1],
['5.1.0-beta.1', '5.1.0-beta.2', -1],
['5.1.0-beta.1', '5.1.1', -1],
['1.1.0', '1.1.0-a', 1],
['1.1.0-a', '1.1.0', -1],
['COM1', 'COM2', -1],
['COM1', 'COM10', -1],
['COM10', 'COM1', 1],
['COM10', 'COM2', 1],
['COM2', 'COM10', -1],
['COM10', 'COM10', 0],
] as Array<[string, string, number]>).forEach(([left, right, expectation]) => {
it(`'${left}' should be ${expectation === 0 ? 'equal to' : expectation < 0 ? 'less than' : 'greater than'} '${right}'`, () => {
const actual = testMe(left, right);
expect(actual).to.be.equal(expectation);
});
});
});
});