Skip to content

Commit

Permalink
fix: fix version comparison
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
Signed-off-by: Jeromy Cannon <[email protected]>
  • Loading branch information
leninmehedy authored and jeromy-cannon committed Feb 13, 2024
1 parent 8dee121 commit bcda7bc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions solo/src/core/dependency_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`)
}

Expand Down
12 changes: 9 additions & 3 deletions solo/src/core/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion solo/test/unit/core/dependency_manager.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions solo/test/unit/core/helpers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})

0 comments on commit bcda7bc

Please sign in to comment.