Skip to content

Commit

Permalink
fix: throw an error when versions don't match
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed May 6, 2021
1 parent ce17303 commit cca7498
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/commands/cli/versions/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as path from 'path';
import * as fg from 'fast-glob';
import { exec } from 'shelljs';
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { fs, Messages } from '@salesforce/core';
import { fs, Messages, SfdxError } from '@salesforce/core';
import { green, red, cyan, yellow, bold } from 'chalk';
import { PackageJson } from '../../../package';

Expand All @@ -23,28 +23,28 @@ const STABLE_PATH = 'https://developer.salesforce.com/media/salesforce-cli/sfdx/
const STABLE_RC_PATH = 'https://developer.salesforce.com/media/salesforce-cli/sfdx/channels/stable-rc';
const SALESFORCE_DEP_GLOBS = ['@salesforce/**/*', 'salesforce-alm', 'salesforcedx'];

type Info = {
export type Info = {
origin: string;
version: string;
channel: Channel;
location: Location;
dependencies?: Dependency[];
};

type Dependency = {
export type Dependency = {
name: string;
version: string;
};

enum Channel {
export enum Channel {
LEGACY = 'legacy',
STABLE = 'stable',
STABLE_RC = 'stable-rc',
LATEST = 'latest',
LATEST_RC = 'latest-rc',
}

enum Location {
export enum Location {
ARCHIVE = 'archive',
NPM = 'npm',
}
Expand Down Expand Up @@ -267,6 +267,8 @@ export default class Inspect extends SfdxCommand {
}

private logResults(results: Info[], locations: Location[], channels: Channel[]): void {
let allMatch: boolean;
let npmAndArchivesMatch: boolean;
this.ux.log();
results.forEach((result) => {
this.ux.log(bold(`${result.origin}: ${green(result.version)}`));
Expand All @@ -282,7 +284,7 @@ export default class Inspect extends SfdxCommand {
this.ux.log(`${'All archives match?'} ${archivesMatch ? green(archivesMatch) : yellow(archivesMatch)}`);

channels.forEach((channel) => {
const allMatch = new Set(results.filter((r) => r.channel === channel).map((r) => r.version)).size === 1;
allMatch = new Set(results.filter((r) => r.channel === channel).map((r) => r.version)).size === 1;
this.ux.log(
`${`All ${Location.ARCHIVE}@${channel} versions match?`} ${allMatch ? green(allMatch) : red(allMatch)}`
);
Expand All @@ -296,7 +298,7 @@ export default class Inspect extends SfdxCommand {
const npmChannel = CHANNEL_MAPPING[Location.NPM][channel];
const archiveChannel = CHANNEL_MAPPING[Location.ARCHIVE][channel];

const npmAndArchivesMatch =
npmAndArchivesMatch =
new Set(
results.filter((r) => r.channel === npmChannel || r.channel === archiveChannel).map((r) => r.version)
).size === 1;
Expand All @@ -307,5 +309,9 @@ export default class Inspect extends SfdxCommand {
);
});
}
// npmAndArchivesMatch can be undefined
if ((npmAndArchivesMatch !== undefined && !npmAndArchivesMatch) || !allMatch) {
throw new SfdxError('Version Mismatch');
}
}
}
94 changes: 94 additions & 0 deletions test/commands/inspect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { IConfig } from '@oclif/config';
import { expect } from '@salesforce/command/lib/test';
import Inspect from '../../lib/commands/cli/versions/inspect';
import { Channel, Info, Location } from '../../src/commands/cli/versions/inspect';

describe('cli:versions:inspect', () => {
let cmd;
beforeEach(() => {
cmd = new Inspect(['-c', 'stable', '-l', 'archive'], {} as IConfig);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cmd.ux = {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
log: () => {},
};
});

it('should throw an error when versions are different', () => {
const results: Info[] = [
{
location: Location.ARCHIVE,
version: '1.0.0',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.0.0' }],
},
{
location: Location.ARCHIVE,
version: '1.1.1',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.1.1' }],
},
];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore private method
expect(() => cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE])).to.throw('Version Mismatch');
});

it('should throw an error when versions are different and locations are different', () => {
const results: Info[] = [
{
location: Location.NPM,
version: '1.0.0',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.0.0' }],
},
{
location: Location.ARCHIVE,
version: '1.1.1',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.1.1' }],
},
];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore private method
expect(() => cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE])).to.throw('Version Mismatch');
});

it('should not throw an error when versions match', () => {
const results: Info[] = [
{
location: Location.ARCHIVE,
version: '1.0.0',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.0.0' }],
},
{
location: Location.ARCHIVE,
version: '1.0.0',
origin: '',
channel: Channel.STABLE,
dependencies: [{ name: 'dep1', version: '1.0.0' }],
},
];
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore private method
cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE]);
} catch (e) {
expect(e).to.be.undefined;
}
});
});

0 comments on commit cca7498

Please sign in to comment.