@salesforce/command
Version:
Salesforce CLI base command class
110 lines • 5.22 kB
JavaScript
/*
* 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
*/
/* eslint-disable @typescript-eslint/no-unsafe-argument */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TestContext = exports.$$ = exports.test = exports.OclifConfig = exports.loadConfig = exports.command = exports.Config = exports.FancyTypes = exports.expect = void 0;
const oclifTest = require("@oclif/test");
const test_1 = require("@oclif/test");
Object.defineProperty(exports, "command", { enumerable: true, get: function () { return test_1.command; } });
Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return test_1.Config; } });
Object.defineProperty(exports, "expect", { enumerable: true, get: function () { return test_1.expect; } });
Object.defineProperty(exports, "FancyTypes", { enumerable: true, get: function () { return test_1.FancyTypes; } });
const core_1 = require("@oclif/core");
Object.defineProperty(exports, "OclifConfig", { enumerable: true, get: function () { return core_1.Config; } });
const core_2 = require("@salesforce/core");
const testSetup_1 = require("@salesforce/core/lib/testSetup");
Object.defineProperty(exports, "TestContext", { enumerable: true, get: function () { return testSetup_1.TestContext; } });
const ts_types_1 = require("@salesforce/ts-types");
const load_config_1 = require("@oclif/test/lib/load-config");
Object.defineProperty(exports, "loadConfig", { enumerable: true, get: function () { return load_config_1.loadConfig; } });
load_config_1.loadConfig.root = (0, ts_types_1.ensure)(module.parent).filename;
const $$ = (0, testSetup_1.testSetup)();
exports.$$ = $$;
function find(orgs, predicate) {
return (0, ts_types_1.definiteValuesOf)(orgs).filter(predicate)[0];
}
const withOrg = (org = {}, setAsDefault = true) => ({
// TODO: properly type the dictionary
// eslint-disable-next-line @typescript-eslint/no-explicit-any
run(ctx) {
if (!ctx.orgs) {
ctx.orgs = {};
}
if (!org.username) {
org.username = 'test@org.com';
}
ctx.orgs[org.username] = {};
// Override org if it exists on context
ctx.orgs[org.username].orgs = {
[org.username]: Object.assign({
orgId: '0x012123',
instanceUrl: 'http://na30.salesforce.com',
loginUrl: 'https://login.salesforce.com',
created: '1519163543003',
isDevHub: false,
default: setAsDefault,
}, org),
};
// eslint-disable-next-line @typescript-eslint/require-await
const readOrg = async function () {
return (0, ts_types_1.asJsonMap)(ctx.orgs[org.username], {});
};
// eslint-disable-next-line @typescript-eslint/require-await
const writeOrg = async function () {
const foundOrg = (0, ts_types_1.asJsonMap)(ctx.orgs[org.username], {});
return ((0, ts_types_1.ensure)($$.configStubs.AuthInfoConfig).contents = foundOrg);
};
$$.configStubs.AuthInfoConfig = {
retrieveContents: readOrg,
updateContents: writeOrg,
};
const defaultOrg = find(ctx.orgs, (o) => !!o.default && !o.isDevHub);
const defaultDevHubOrg = find(ctx.orgs, (o) => !!o.default && !!o.isDevHub);
$$.configStubs.Config = {
contents: {
defaultusername: defaultOrg?.username,
defaultdevhubusername: defaultDevHubOrg?.username,
},
};
},
});
const withConnectionRequest = (fakeFunction) => ({
run() {
$$.fakeConnectionRequest = fakeFunction;
},
});
const withProject = (SfProjectJson) => ({
run() {
// Restore first if already stubbed by $$.inProject()
/* eslint-disable-next-line @typescript-eslint/unbound-method */
const projPathStub = core_2.SfProject.resolveProjectPath;
if (projPathStub.restore) {
projPathStub.restore();
}
$$.SANDBOX.stub(core_2.SfProject, 'resolveProjectPath').callsFake((path) => $$.localPathRetriever(path ?? $$.id));
const DEFAULT_PROJECT_JSON = {
sfdcLoginUrl: 'https://login.salesforce.com',
};
$$.configStubs.SfProjectJson = {
contents: Object.assign({}, DEFAULT_PROJECT_JSON, SfProjectJson),
};
},
});
/**
* @deprecated We recommend unit tests test smaller units of code, not entire commands.
* For command-level tests, we recommend using NUTs (real-world tests, see https://github.com/salesforcecli/cli-plugins-testkit)
* If you have existing tests you want to maintain, import oclif/test directly and make use of TextContext from sfdx-core: https://github.com/forcedotcom/sfdx-core/tree/v3#using-testsetup
*
*/
const test = oclifTest.test
.register('withOrg', withOrg)
.register('withConnectionRequest', withConnectionRequest)
.register('withProject', withProject);
exports.test = test;
exports.default = test;
//# sourceMappingURL=index.js.map
;