Skip to content

Commit 6110854

Browse files
authored
Merge pull request #123 from salesforcecli/wr/throwOnMismatch
fix: throw an error when versions don't match
2 parents ec8fdc2 + 76f3ba3 commit 6110854

File tree

2 files changed

+107
-7
lines changed

2 files changed

+107
-7
lines changed

src/commands/cli/versions/inspect.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as path from 'path';
1010
import * as fg from 'fast-glob';
1111
import { exec } from 'shelljs';
1212
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
13-
import { fs, Messages } from '@salesforce/core';
13+
import { fs, Messages, SfdxError } from '@salesforce/core';
1414
import { green, red, cyan, yellow, bold } from 'chalk';
1515
import { PackageJson } from '../../../package';
1616

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

26-
type Info = {
26+
export type Info = {
2727
origin: string;
2828
version: string;
2929
channel: Channel;
3030
location: Location;
3131
dependencies?: Dependency[];
3232
};
3333

34-
type Dependency = {
34+
export type Dependency = {
3535
name: string;
3636
version: string;
3737
};
3838

39-
enum Channel {
39+
export enum Channel {
4040
LEGACY = 'legacy',
4141
STABLE = 'stable',
4242
STABLE_RC = 'stable-rc',
4343
LATEST = 'latest',
4444
LATEST_RC = 'latest-rc',
4545
}
4646

47-
enum Location {
47+
export enum Location {
4848
ARCHIVE = 'archive',
4949
NPM = 'npm',
5050
}
@@ -267,6 +267,8 @@ export default class Inspect extends SfdxCommand {
267267
}
268268

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

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

299-
const npmAndArchivesMatch =
301+
npmAndArchivesMatch =
300302
new Set(
301303
results.filter((r) => r.channel === npmChannel || r.channel === archiveChannel).map((r) => r.version)
302304
).size === 1;
@@ -307,5 +309,9 @@ export default class Inspect extends SfdxCommand {
307309
);
308310
});
309311
}
312+
// npmAndArchivesMatch can be undefined
313+
if ((npmAndArchivesMatch !== undefined && !npmAndArchivesMatch) || !allMatch) {
314+
throw new SfdxError('Version Mismatch');
315+
}
310316
}
311317
}

test/commands/inspect.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2020, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
import { IConfig } from '@oclif/config';
8+
import { expect } from '@salesforce/command/lib/test';
9+
import Inspect from '../../lib/commands/cli/versions/inspect';
10+
import { Channel, Info, Location } from '../../src/commands/cli/versions/inspect';
11+
12+
describe('cli:versions:inspect', () => {
13+
let cmd;
14+
beforeEach(() => {
15+
cmd = new Inspect(['-c', 'stable', '-l', 'archive'], {} as IConfig);
16+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
17+
// @ts-ignore
18+
cmd.ux = {
19+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
20+
// @ts-ignore
21+
log: () => {},
22+
};
23+
});
24+
25+
it('should throw an error when versions are different', () => {
26+
const results: Info[] = [
27+
{
28+
location: Location.ARCHIVE,
29+
version: '1.0.0',
30+
origin: '',
31+
channel: Channel.STABLE,
32+
dependencies: [{ name: 'dep1', version: '1.0.0' }],
33+
},
34+
{
35+
location: Location.ARCHIVE,
36+
version: '1.1.1',
37+
origin: '',
38+
channel: Channel.STABLE,
39+
dependencies: [{ name: 'dep1', version: '1.1.1' }],
40+
},
41+
];
42+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
43+
// @ts-ignore private method
44+
expect(() => cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE])).to.throw('Version Mismatch');
45+
});
46+
47+
it('should throw an error when versions are different and locations are different', () => {
48+
const results: Info[] = [
49+
{
50+
location: Location.NPM,
51+
version: '1.0.0',
52+
origin: '',
53+
channel: Channel.STABLE,
54+
dependencies: [{ name: 'dep1', version: '1.0.0' }],
55+
},
56+
{
57+
location: Location.ARCHIVE,
58+
version: '1.1.1',
59+
origin: '',
60+
channel: Channel.STABLE,
61+
dependencies: [{ name: 'dep1', version: '1.1.1' }],
62+
},
63+
];
64+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
65+
// @ts-ignore private method
66+
expect(() => cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE])).to.throw('Version Mismatch');
67+
});
68+
69+
it('should not throw an error when versions match', () => {
70+
const results: Info[] = [
71+
{
72+
location: Location.ARCHIVE,
73+
version: '1.0.0',
74+
origin: '',
75+
channel: Channel.STABLE,
76+
dependencies: [{ name: 'dep1', version: '1.0.0' }],
77+
},
78+
{
79+
location: Location.ARCHIVE,
80+
version: '1.0.0',
81+
origin: '',
82+
channel: Channel.STABLE,
83+
dependencies: [{ name: 'dep1', version: '1.0.0' }],
84+
},
85+
];
86+
try {
87+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
88+
// @ts-ignore private method
89+
cmd.logResults(results, Location.ARCHIVE, [Channel.STABLE]);
90+
} catch (e) {
91+
expect(e).to.be.undefined;
92+
}
93+
});
94+
});

0 commit comments

Comments
 (0)