UNPKG

ci-sf-plugin

Version:

Set of commands making CI and dev's life easier.

65 lines 3.09 kB
/* * 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 * as os from 'os'; import * as util from 'util'; import * as childProcess from 'child_process'; import { SfCommand, Flags, requiredOrgFlagWithDeprecations, } from '@salesforce/sf-plugins-core'; import { Messages, SfError } from '@salesforce/core'; import { ux } from '@oclif/core'; // promisify child process const execSync = util.promisify(childProcess.exec); Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); // Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, // or any library that is using the messages framework can also be loaded this way. const messages = Messages.loadMessages('ci-sf-plugin', 'apex.test.run'); export default class Run extends SfCommand { static description = messages.getMessage('commandDescription'); static examples = messages.getMessage('examples').split(os.EOL); static flags = { 'target-org': requiredOrgFlagWithDeprecations, // flag with a value (-n, --name=VALUE) 'test-level': Flags.string({ char: 'l', options: ['RunLocalTests', 'RunAllTestsInOrg'], default: 'RunLocalTests', description: messages.getMessage('testlevelFlagDescription') }) }; // Set this to true if your command requires a project workspace; 'requiresProject' is false by default static requiresProject = false; async run() { try { const { flags } = await this.parse(Run); // fetch needed flags const targetOrgUsername = flags['target-org'].getUsername(); const testLevelFlag = flags['test-level']; ux.action.start(messages.getMessage('infoRunningTests', [targetOrgUsername]), 'in progress', { stdout: true }); // build the command, without --json to see details const commandStr = `sf apex run test --test-level ${testLevelFlag} --wait 120 --synchronous --target-org ${targetOrgUsername}`; // execute and print output const commandPromise = execSync(commandStr, { encoding: 'utf8', maxBuffer: 1024 * 1024 }); commandPromise.child.stdout.on('data', (data) => { if (data?.trim()) { process.stdout.write(data); } }); commandPromise.child.stderr.on('data', (data) => { if (data?.trim()) { process.stderr.write(data.trim()); } }); const { stdout, stderr } = await commandPromise; ux.action.stop('done'); // Return an object return { stdout, stderr }; } catch (error) { ux.action.stop('failed'); throw new SfError(messages.getMessage('errorRunningTestsFailed', [JSON.stringify(error, null, 2)])); } } } //# sourceMappingURL=run.js.map