From bcda7bc19caf13956956fff809ae3ef7de862f7e Mon Sep 17 00:00:00 2001 From: Lenin Mehedy Date: Tue, 13 Feb 2024 14:53:33 +1100 Subject: [PATCH] fix: fix version comparison Signed-off-by: Lenin Mehedy Signed-off-by: Jeromy Cannon --- solo/src/core/dependency_manager.mjs | 3 +-- solo/src/core/helpers.mjs | 12 +++++++++--- solo/test/unit/core/dependency_manager.test.mjs | 2 +- solo/test/unit/core/helpers.test.mjs | 4 ++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/solo/src/core/dependency_manager.mjs b/solo/src/core/dependency_manager.mjs index 4e387f367..325ac43e5 100644 --- a/solo/src/core/dependency_manager.mjs +++ b/solo/src/core/dependency_manager.mjs @@ -40,6 +40,7 @@ export class DependencyManager extends ShellRunner { try { const output = await this.run(`${core.constants.HELM} version --short`) const parts = output[0].split('+') + this.logger.debug(`Found dependency ${constants.HELM}:${parts[0]}`) return helpers.compareVersion(DependencyManager.depVersions.get(constants.HELM), parts[0]) >= 0 } catch (e) { this.logger.error(`failed to check helm dependency:${e.message}`, e) @@ -63,8 +64,6 @@ export class DependencyManager extends ShellRunner { } if (!status) { - - this.logger.warn(`Dependency ${dep}:${DependencyManager.depVersions.get(dep)} is not found`) throw new FullstackTestingError(`${dep}:^${DependencyManager.depVersions.get(dep)} is not found`) } diff --git a/solo/src/core/helpers.mjs b/solo/src/core/helpers.mjs index 497ec4af5..3921aaac3 100644 --- a/solo/src/core/helpers.mjs +++ b/solo/src/core/helpers.mjs @@ -102,11 +102,17 @@ export function parseSemver (semver) { * @return {number} */ export function compareVersion (target, candidate) { - if (target === candidate) return 0 - const v1 = parseSemver(target) const v2 = parseSemver(candidate) - if (v2.major >= v1.major && v2.minor >= v1.minor && v2.patch >= v1.patch) { + + if (v2.major === v1.major && v2.minor === v1.minor && v2.patch === v1.patch) { + return 0 + } + + if ((v2.major > v1.major) || + (v2.major >= v1.major && v2.minor > v1.minor) || + (v2.major >= v1.major && v2.minor >= v1.minor && v2.patch >= v1.patch) + ) { return 1 } diff --git a/solo/test/unit/core/dependency_manager.test.mjs b/solo/test/unit/core/dependency_manager.test.mjs index 62699d3d1..1c35f6a3c 100644 --- a/solo/test/unit/core/dependency_manager.test.mjs +++ b/solo/test/unit/core/dependency_manager.test.mjs @@ -31,7 +31,7 @@ describe('DependencyManager', () => { describe('checkDependency', () => { it('should fail during invalid dependency check', async () => { - await expect(depManager.checkDependency('INVALID_PROGRAM')).rejects.toThrowError(new FullstackTestingError('INVALID_PROGRAM is not found')) + await expect(depManager.checkDependency('INVALID_PROGRAM')).rejects.toThrowError(new FullstackTestingError('INVALID_PROGRAM:^undefined is not found')) }) it('should succeed during kubectl dependency check', async () => { await expect(depManager.checkDependency(constants.HELM)).resolves.toBe(true) diff --git a/solo/test/unit/core/helpers.test.mjs b/solo/test/unit/core/helpers.test.mjs index e6051585a..46c558a83 100644 --- a/solo/test/unit/core/helpers.test.mjs +++ b/solo/test/unit/core/helpers.test.mjs @@ -65,5 +65,9 @@ describe('Helpers', () => { it('should fail with minor version lower than target', () => { expect(helpers.compareVersion('v3.14.0', 'v3.11.0')).toBe(-1) }) + + it('should succeed with a later version', () => { + expect(helpers.compareVersion('v3.12.3', 'v3.14.0')).toBe(1) + }) }) })