Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move from sfdx examples/commands/defaults to sf #462

Merged
merged 9 commits into from
May 5, 2023
41 changes: 41 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,44 @@ jobs:
windows-unit-tests:
needs: yarn-lockfile-check
uses: salesforcecli/github-workflows/.github/workflows/unitTestsWindows.yml@main
xNuts-pa:
needs: linux-unit-tests
name: xNuts-pa
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
command:
- 'yarn test:nuts'
uses: salesforcecli/github-workflows/.github/workflows/externalNut.yml@main
with:
packageName: '@salesforce/cli-plugins-testkit'
externalProjectGitUrl: 'https://github.com/salesforcecli/plugin-auth'
preBuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
postbuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
preExternalBuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
command: ${{matrix.command}}
os: ${{matrix.os}}
useCache: false
secrets: inherit

xNuts-pu:
needs: linux-unit-tests
name: xNuts-pu
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
command:
- 'yarn test:nuts'
uses: salesforcecli/github-workflows/.github/workflows/externalNut.yml@main
with:
packageName: '@salesforce/cli-plugins-testkit'
externalProjectGitUrl: 'https://github.com/salesforcecli/plugin-user'
preBuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
postbuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
preExternalBuildCommands: 'shx rm -rf node_modules/@salesforce/cli-plugins-testkit/samples/**/*.ts'
command: ${{matrix.command}}
os: ${{matrix.os}}
useCache: false
secrets: inherit
41 changes: 21 additions & 20 deletions SAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ import { execCmd } from '@salesforce/cli-plugins-testkit';

describe('execCmd', () => {
// This would actually be set in the shell or CI environment.
process.env.TESTKIT_EXECUTABLE_PATH = 'sfdx';
process.env.TESTKIT_EXECUTABLE_PATH = 'sf';

it('should use the specified Salesforce CLI executable', () => {
execCmd('config:list');
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('TestSession', () => {
});

it('should run a command from within a generated project', () => {
execCmd('force:source:convert', { ensureExitCode: 0 });
execCmd('project:convert:source', { ensureExitCode: 0 });
});

after(async () => {
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('TestSession', () => {
});

it('should allow access to anything on TestSession without a project', () => {
execCmd(`config:set instanceUrl=${testSession.id}`, { ensureExitCode: 0 });
execCmd(`config:set org-instance-url=${testSession.id}`, { ensureExitCode: 0 });
});

after(async () => {
Expand All @@ -278,6 +278,7 @@ describe('TestSession', () => {
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';
import { tmpdir } from 'os';
import { expect } from 'chai';

describe('TestSession', () => {
let testSession: TestSession;
Expand All @@ -291,7 +292,7 @@ describe('TestSession', () => {
});

it('should use overridden session directory', () => {
execCmd(`config:set instanceUrl=${testSession.id}`);
execCmd(`config:set org-instance-url=${testSession.id}`);
expect(testSession.dir).to.equal(tmpdir());
});

Expand Down Expand Up @@ -366,7 +367,7 @@ describe('TestSession', () => {
});

it('should archive the TestSession contents in process.cwd() when a test fails', () => {
execCmd(`config:set instanceUrl=${testSession.id}`, { ensureExitCode: 0 });
execCmd(`config:set org-instance-url=${testSession.id}`, { ensureExitCode: 0 });
});

afterEach(async function () {
Expand Down Expand Up @@ -396,10 +397,10 @@ describe('TestSession', () => {
project: {
name: 'MyTestProject',
},
scratchOrgs: [{ executable: 'sfdx', edition: 'developer' }],
scratchOrgs: [{ executable: 'sf', edition: 'developer' }],
});

execCmd('force:source:push', { cli: 'sfdx' });
execCmd('project:deploy:start', { cli: 'sf' });
});

it('using testkit to run commands with an org', () => {
Expand Down Expand Up @@ -434,7 +435,7 @@ describe('TestSession', () => {

it('using testkit to run commands with an org', () => {
const username = [...testSession.orgs.keys()][0];
execCmd(`force:source:deploy -x package.xml -u ${username}`, { ensureExitCode: 0 });
execCmd(`project:deploy:start -x package.xml -o ${username}`, { ensureExitCode: 0 });
});

after(async () => {
Expand All @@ -459,7 +460,7 @@ describe('TestSession', () => {
project: {
sourceDir: path.join(process.cwd(), 'localTestProj'),
},
scratchOrgs: [{ executable: 'sfdx', config: 'config/project-scratch-def.json' }],
scratchOrgs: [{ executable: 'sf', config: 'config/project-scratch-def.json' }],
});
});

Expand All @@ -474,7 +475,7 @@ describe('TestSession', () => {
});
testSession.stubCwd(project2.dir);
const username = [...testSession.orgs.keys()][0];
execCmd(`force:source:pull -u ${username}`);
execCmd(`project:retrieve:start -o ${username}`);
});

after(async () => {
Expand Down Expand Up @@ -509,26 +510,26 @@ describe('TestSession', () => {
},
scratchOrgs: [
// rely on defaultusername
{ executable: 'sfdx', config: 'config/project-scratch-def.json', setDefault: true },
{ executable: 'sf', config: 'config/project-scratch-def.json', setDefault: true },
// explicitly set a username
{ executable: 'sfdx', config: 'config/project-scratch-def.json', username },
{ executable: 'sf', config: 'config/project-scratch-def.json', username },
],
});
});

it('should use both orgs created as part of setupCommands', () => {
const firstOrg = testSession.orgs.get('default');
execCmd(`force:source:retrieve -m ApexClass -u ${firstOrg}`, { ensureExitCode: 0 });
execCmd(`force:source:retrieve -p force-app -u ${username}`, { ensureExitCode: 0 });
execCmd(`project:retrieve:start -m ApexClass -o ${firstOrg}`, { ensureExitCode: 0 });
execCmd(`project:retrieve:start -p force-app -o ${username}`, { ensureExitCode: 0 });
});

it('should create a 3rd org and get the username from the json output', () => {
// Note that this org will not be deleted for you by TestSession.
const jsonOutput = execCmd<{ username: string }>('force:org:create -f config/project-scratch-def.json --json', {
cli: 'sfdx',
const jsonOutput = execCmd<{ username: string }>('env:create:scratch -f config/project-scratch-def.json --json', {
cli: 'sf',
}).jsonOutput;
const thirdOrg = jsonOutput.result.username;
execCmd(`force:source:pull -u ${thirdOrg}`);
execCmd(`project:retrieve:start -o ${thirdOrg}`);
});

after(async () => {
Expand All @@ -541,8 +542,8 @@ describe('Sample NUT 2', () => {
before(async () => {
// NOTE: this is for demonstration purposes and doesn't work as is
// since shelljs does not return promises, but conveys the point.
const org1 = shelljs.exec('sfdx force:org:create edition=Developer', { async: true });
const org2 = shelljs.exec('sfdx force:org:create edition=Developer', { async: true });
const org1 = shelljs.exec('sf env:create:scratch edition=Developer', { async: true });
const org2 = shelljs.exec('sf env:create:scratch edition=Developer', { async: true });
await Promise.all([org1, org2]);
});
});
Expand Down Expand Up @@ -675,6 +676,6 @@ export TESTKIT_EXEC_SHELL=powershell.exe
1. Clean the TestSession in a code block that always runs (e.g., mocha’s after() ) to keep your plugin clean. You can always choose to zip a project or test session after tests run or on each test failure.
1. Point your CI jobs to different CLI executables using the TESTKIT_EXECUTABLE_PATH env var to ensure your plugin works with the various ways the CLI can be installed. By default it will use your plugin’s `./bin/run` but you can define a local or global npm install path or install from public archives.
1. Use a naming pattern for test files that use the testkit. These are not unit tests so we like to refer to them as “NUTs” and have a convention of `*.nut.ts` so we can run them separately from unit tests.
1. Use `SFDX_USE_GENERIC_UNIX_KEYCHAIN=true` to prevent authentication keychain issues.
1. Use `SF_USE_GENERIC_UNIX_KEYCHAIN=true` to prevent authentication keychain issues.
1. When writing TypeScript NUTs remember to pass the expected type to execCmd so JSON results are typed for you.
1. Take advantage of TestSession's automatic authentication using the appropriate environment variables.
8 changes: 7 additions & 1 deletion samples/TestSession.sample1.nut.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (c) 2023, 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 { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';

Expand All @@ -13,7 +19,7 @@ describe('TestSession', () => {
});

it('should run a command from within a generated project', () => {
execCmd('force:source:convert', { ensureExitCode: 0 });
execCmd('project:convert:source', { ensureExitCode: 0 });
});

after(async () => {
Expand Down
8 changes: 7 additions & 1 deletion samples/TestSession.sample10.nut.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (c) 2023, 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 { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';

Expand All @@ -15,7 +21,7 @@ describe('TestSession', () => {

it('using testkit to run commands with an org', () => {
const username = [...testSession.orgs.keys()][0];
execCmd(`force:source:deploy -x package.xml -u ${username}`, { ensureExitCode: 0 });
execCmd(`project:deploy:start -x package.xml -o ${username}`, { ensureExitCode: 0 });
});

after(async () => {
Expand Down
12 changes: 9 additions & 3 deletions samples/TestSession.sample11.nut.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/*
* Copyright (c) 2023, 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 } from '../src/execCmd';
import { TestSession } from '../src/testSession';
import { TestProject } from '../src/testProject';
import * as path from 'path';

describe('TestSession', () => {
let testSession: TestSession;
Expand All @@ -11,7 +17,7 @@ describe('TestSession', () => {
project: {
sourceDir: path.join(process.cwd(), 'localTestProj'),
},
scratchOrgs: [{ executable: 'sfdx', config: 'config/project-scratch-def.json' }],
scratchOrgs: [{ executable: 'sf', config: 'config/project-scratch-def.json' }],
});
});

Expand All @@ -26,7 +32,7 @@ describe('TestSession', () => {
});
testSession.stubCwd(project2.dir);
const username = [...testSession.orgs.keys()][0];
execCmd(`force:source:pull -u ${username}`);
execCmd(`project:retrieve:start -o ${username}`);
});

after(async () => {
Expand Down
26 changes: 16 additions & 10 deletions samples/TestSession.sample12.nut.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/*
* Copyright (c) 2023, 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 shelljs from 'shelljs';
import { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';
import * as shelljs from 'shelljs';

/*
NOTE: Scratch orgs can take a while to create so you may want to create them in parallel
Expand All @@ -20,26 +26,26 @@ describe('TestSession', () => {
},
scratchOrgs: [
// rely on defaultusername
{ executable: 'sfdx', config: 'config/project-scratch-def.json', setDefault: true },
{ executable: 'sf', config: 'config/project-scratch-def.json', setDefault: true },
// explicitly set a username
{ executable: 'sfdx', config: 'config/project-scratch-def.json', username },
{ executable: 'sf', config: 'config/project-scratch-def.json', username },
],
});
});

it('should use both orgs created as part of setupCommands', () => {
const firstOrg = testSession.orgs.get('default');
execCmd(`force:source:retrieve -m ApexClass -u ${firstOrg}`, { ensureExitCode: 0 });
execCmd(`force:source:retrieve -p force-app -u ${username}`, { ensureExitCode: 0 });
execCmd(`project:retrieve:start -m ApexClass -o ${firstOrg}`, { ensureExitCode: 0 });
execCmd(`project:retrieve:start -p force-app -o ${username}`, { ensureExitCode: 0 });
});

it('should create a 3rd org and get the username from the json output', () => {
// Note that this org will not be deleted for you by TestSession.
const jsonOutput = execCmd<{ username: string }>('force:org:create -f config/project-scratch-def.json --json', {
cli: 'sfdx',
const jsonOutput = execCmd<{ username: string }>('env:create:scratch -f config/project-scratch-def.json --json', {
cli: 'sf',
}).jsonOutput;
const thirdOrg = jsonOutput.result.username;
execCmd(`force:source:pull -u ${thirdOrg}`);
execCmd(`project:retrieve:start -o ${thirdOrg}`);
});

after(async () => {
Expand All @@ -52,8 +58,8 @@ describe('Sample NUT 2', () => {
before(async () => {
// NOTE: this is for demonstration purposes and doesn't work as is
// since shelljs does not return promises, but conveys the point.
const org1 = shelljs.exec('sfdx force:org:create edition=Developer', { async: true });
const org2 = shelljs.exec('sfdx force:org:create edition=Developer', { async: true });
const org1 = shelljs.exec('sf env:create:scratch edition=Developer', { async: true });
const org2 = shelljs.exec('sf env:create:scratch edition=Developer', { async: true });
await Promise.all([org1, org2]);
});
});
8 changes: 7 additions & 1 deletion samples/TestSession.sample4.nut.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (c) 2023, 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 { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';

Expand All @@ -9,7 +15,7 @@ describe('TestSession', () => {
});

it('should allow access to anything on TestSession without a project', () => {
execCmd(`config:set instanceUrl=${testSession.id}`, { ensureExitCode: 0 });
execCmd(`config:set org-instance-url=${testSession.id}`, { ensureExitCode: 0 });
});

after(async () => {
Expand Down
12 changes: 9 additions & 3 deletions samples/TestSession.sample5.nut.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/*
* Copyright (c) 2023, 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 { tmpdir } from 'os';
import { expect } from 'chai';
import { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';
import { expect } from 'chai';
import { tmpdir } from 'os';

describe('TestSession', () => {
let testSession: TestSession;
Expand All @@ -15,7 +21,7 @@ describe('TestSession', () => {
});

it('should use overridden session directory', () => {
execCmd(`config:set instanceUrl=${testSession.id}`);
execCmd(`config:set org-instance-url=${testSession.id}`);
expect(testSession.dir).to.equal(tmpdir());
});

Expand Down
8 changes: 7 additions & 1 deletion samples/TestSession.sample8.nut.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright (c) 2023, 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 { execCmd } from '../src/execCmd';
import { TestSession } from '../src/testSession';

Expand All @@ -9,7 +15,7 @@ describe('TestSession', () => {
});

it('should archive the TestSession contents in process.cwd() when a test fails', () => {
execCmd(`config:set instanceUrl=${testSession.id}`, { ensureExitCode: 0 });
execCmd(`config:set org-instance-url=${testSession.id}`, { ensureExitCode: 0 });
});

afterEach(async function () {
Expand Down
Loading