Skip to content

Commit

Permalink
fix: add package1:version:list command, UTs, NUTs
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Jul 26, 2022
1 parent dfe9826 commit ecfbcd4
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 20 deletions.
4 changes: 4 additions & 0 deletions messages/package1_version_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ Successfully created the package version list.
# action

Verify that you entered a valid package ID and that you are authorized in the org. Then try again.

# packageIdInvalid

Verify that you entered a valid package version ID (starts with 033) and try again.
25 changes: 11 additions & 14 deletions src/commands/force/package1/beta/version/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages, SfError } from '@salesforce/core';
import { Package1Display, package1Display } from '@salesforce/packaging';
import { CliUx } from '@oclif/core';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package1_version_display');
Expand Down Expand Up @@ -37,19 +36,17 @@ export class Package1VersionDisplayCommand extends SfdxCommand {
const conn = this.org.getConnection();
const results = await package1Display(conn, this.flags.packageversionid);

if (!this.flags.json) {
if (results.length === 0) {
CliUx.ux.log('No results found');
} else {
CliUx.ux.table(results, {
MetadataPackageVersionId: { header: 'MetadataPackageVersionId' },
MetadataPackageId: { header: 'MetadataPackageId' },
Name: { header: 'Name' },
Version: { header: 'Version' },
ReleaseState: { header: 'ReleaseState' },
BuildNumber: { header: 'BuildNumber' },
});
}
if (results.length === 0) {
this.ux.log('No results found');
} else {
this.ux.table(results, {
MetadataPackageVersionId: { header: 'MetadataPackageVersionId' },
MetadataPackageId: { header: 'MetadataPackageId' },
Name: { header: 'Name' },
Version: { header: 'Version' },
ReleaseState: { header: 'ReleaseState' },
BuildNumber: { header: 'BuildNumber' },
});
}

return results;
Expand Down
36 changes: 30 additions & 6 deletions src/commands/force/package1/beta/version/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import { flags, FlagsConfig, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import { Messages, SfError } from '@salesforce/core';
import { package1VersionList, Package1Display } from '@salesforce/packaging';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-packaging', 'package1_version_list');
Expand All @@ -15,18 +16,41 @@ export class Package1VersionListCommand extends SfdxCommand {
public static readonly description = messages.getMessage('cliDescription');
public static readonly longDescription = messages.getMessage('longDescription');
public static readonly help = messages.getMessage('cliHelp');
public static readonly supportsUsername = true;
public static readonly requiresProject = true;
public static readonly requiresUsername = true;
public static readonly flagsConfig: FlagsConfig = {
packageid: flags.id({
char: 'i',
description: messages.getMessage('packageId'),
longDescription: messages.getMessage('packageIdLong'),
validate: (id) => {
if (id.startsWith('033') && [18, 15].includes(id.length)) {
return true;
} else {
throw new SfError(messages.getMessage('packageIdInvalid'));
}
},
}),
};

public async run(): Promise<unknown> {
process.exitCode = 1;
return Promise.resolve('Not yet implemented');
public async run(): Promise<Package1Display[]> {
// TODO: remove eslint-disable lines once the `packaging` PR is merged
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-assignment
const result = await package1VersionList(this.org.getConnection(), this.flags.packageid);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,no-unused-expressions
if (result.length) {
this.ux.table(result, {
MetadataPackageVersionId: { header: 'MetadataPackageVersionId' },
MetadataPackageId: { header: 'MetadataPackageId' },
Name: { header: 'Name' },
Version: { header: 'Version' },
ReleaseState: { header: 'ReleaseState' },
BuildNumber: { header: 'BuildNumber' },
});
} else {
this.ux.log('No Results Found');
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return result;
}
}
121 changes: 121 additions & 0 deletions test/commands/force/package1/versionList.nut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2022, 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 * as path from 'path';
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { OrgConfigProperties } from '@salesforce/core';
import { expect } from 'chai';
import { Package1Display } from '@salesforce/packaging';

let session: TestSession;
let usernameOrAlias: string;
let packageId: string;

// TODO: na40 required as DevHub
before(async () => {
const executablePath = path.join(process.cwd(), 'bin', 'dev');
session = await TestSession.create({
setupCommands: [`${executablePath} config:get ${OrgConfigProperties.TARGET_DEV_HUB} --json`],
project: { name: 'package1VersionList' },
});
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
usernameOrAlias = (session.setup[0] as { result: [{ value: string }] }).result[0].value;

if (!usernameOrAlias) throw Error('no default username set');
});

after(async () => {
await session?.clean();
});

describe('package1:version:list', () => {
it('should list all 1gp packages in dev hub - human readable results', function () {
const command = `force:package1:beta:version:list -u ${usernameOrAlias}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout as string;
expect(output).to.match(
/MetadataPackageVersionId\s+?MetadataPackageId\s+?Name\s+?Version\s+?ReleaseState\s+?BuildNumber/
);
});

it('should list 1gp packages in dev hub - json', function () {
const command = `force:package1:beta:version:list -u ${usernameOrAlias} --json`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd<Package1Display[]>(command, { ensureExitCode: 0 }).jsonOutput.result[0];
expect(output).to.have.keys(
'MetadataPackageVersionId',
'MetadataPackageId',
'Name',
'Version',
'ReleaseState',
'BuildNumber'
);
expect(output.BuildNumber).to.be.a('number');
expect(output.ReleaseState).to.be.a('string');
expect(output.MetadataPackageVersionId).to.be.a('string');
expect(output.MetadataPackageId).to.be.a('string');
expect(output.Version).to.be.a('string');
});

before(() => {
const command = `force:package1:beta:version:list -u ${usernameOrAlias} --json`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
packageId = execCmd<Package1Display[]>(command, { ensureExitCode: 0 }).jsonOutput.result[0].MetadataPackageId;
});

it('should list all 1gp related to the package id - human readable results', function () {
const command = `force:package1:beta:version:list -i ${packageId} -u ${usernameOrAlias}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout as string;
expect(output).to.match(
/MetadataPackageVersionId\s+?MetadataPackageId\s+?Name\s+?Version\s+?ReleaseState\s+?BuildNumber/
);
});

it('should list 1gp packages in dev hub related to the package id - human readable results - no results', function () {
// fake package ID
const command = `force:package1:beta:version:list -i 03346000000MrC0AXX -u ${usernameOrAlias}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout as string;
expect(output.trim()).to.contain('No Results Found');
});

it("should validate packageversionid flag (doesn't start with 033)", function () {
// fake package ID - not an 033 package
const command = `force:package1:beta:version:list -i 03446000001ZfaAAAS -u ${usernameOrAlias}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd(command, { ensureExitCode: 1 }).shellOutput.stderr as string;
expect(output).to.contain('Verify that you entered a valid package version ID (starts with 033) and try again.');
});

it('should validate packageversionid flag (too short)', function () {
// fake package ID - not an 033 package
const command = `force:package1:beta:version:list -i 03346000001Zfa -u ${usernameOrAlias}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd(command, { ensureExitCode: 1 }).shellOutput.stderr as string;
expect(output).to.contain('Verify that you entered a valid package version ID (starts with 033) and try again.');
});

it('should list 1gp packages in dev hub related to the package id - json', function () {
const command = `force:package1:beta:version:list -i ${packageId} -u ${usernameOrAlias} --json`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const output = execCmd<Package1Display[]>(command, { ensureExitCode: 0 }).jsonOutput.result[0];
expect(output).to.have.keys(
'MetadataPackageVersionId',
'MetadataPackageId',
'Name',
'Version',
'ReleaseState',
'BuildNumber'
);
expect(output.BuildNumber).to.be.a('number');
expect(output.ReleaseState).to.be.a('string');
expect(output.MetadataPackageVersionId).to.be.a('string');
expect(output.MetadataPackageId).to.be.a('string');
expect(output.Version).to.be.a('string');
});
});

0 comments on commit ecfbcd4

Please sign in to comment.