Skip to content

Commit

Permalink
feat: add --createissue flag, UT, bin/dev, fixed linked plugin check
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Oct 4, 2022
1 parent dc4e410 commit d554c2b
Show file tree
Hide file tree
Showing 8 changed files with 412 additions and 322 deletions.
11 changes: 6 additions & 5 deletions command-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[
{
"command": "info:releasenotes:display",
"command": "doctor",
"plugin": "@salesforce/plugin-info",
"flags": ["hook", "json", "loglevel", "version"],
"alias": ["whatsnew"]
"flags": ["command", "createissue", "json", "loglevel", "outputdir", "plugin"],
"alias": []
},
{
"command": "doctor",
"command": "info:releasenotes:display",
"plugin": "@salesforce/plugin-info",
"flags": ["command", "plugin", "outputdir", "json", "loglevel"]
"flags": ["hook", "json", "loglevel", "version"],
"alias": ["whatsnew"]
}
]
3 changes: 2 additions & 1 deletion messages/doctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Plugin providers can also implement their own doctor diagnostic tests by listeni
newissue: 'create a new GitHub issue and attach all diagnostic results',
plugin: 'specific plugin on which to run diagnostics ',
outputdir: 'directory to save all created files rather than the current working directory',
createissue: 'create a new issue on our GitHub repo and attach all diagnostic results',
},
examples: [
`Run CLI doctor diagnostics:
Expand All @@ -28,5 +29,5 @@ Run CLI doctor diagnostics for a specific plugin:
doctorNotInitializedError: 'Must first initialize a new SfDoctor.',
doctorAlreadyInitializedError: 'SfDoctor has already been initialized.',
pluginNotInstalledError:
'Specified plugin [%s] isn\'t installed. Install it, correct the name, or choose another plugin.',
"Specified plugin [%s] isn't installed. Install it, correct the name, or choose another plugin.",
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"got": "^11.8.2",
"marked": "^4.1.1",
"marked-terminal": "^4.2.0",
"open": "^8.4.0",
"proxy-agent": "^5.0.0",
"proxy-from-env": "^1.1.0",
"semver": "^7.3.5",
Expand Down Expand Up @@ -120,4 +121,4 @@
"publishConfig": {
"access": "public"
}
}
}
45 changes: 45 additions & 0 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path';
import { exec } from 'child_process';
import { flags, SfdxCommand } from '@salesforce/command';
import { Messages, Lifecycle, SfError } from '@salesforce/core';
import * as open from 'open';
import { Doctor as SFDoctor, SfDoctor, SfDoctorDiagnosis } from '../doctor';
import { DiagnosticStatus } from '../diagnostics';

Expand All @@ -36,6 +37,11 @@ export default class Doctor extends SfdxCommand {
char: 'o',
description: messages.getMessage('flags.outputdir'),
}),
createissue: flags.boolean({
char: 'i',
description: messages.getMessage('flags.createissue'),
default: false,
}),
};

// Array of promises that are various doctor tasks to perform
Expand All @@ -45,15 +51,29 @@ export default class Doctor extends SfdxCommand {
private outputDir: string;
private filesWrittenMsgs: string[] = [];

/**
* Only made into its own method for unit testing purposes
*
* @param url: url string to open
*/
public static async openUrl(url: string): Promise<void> {
await open(url);
}

public async run(): Promise<SfDoctorDiagnosis> {
this.doctor = SFDoctor.getInstance();
const lifecycle = Lifecycle.getInstance();

const pluginFlag = this.flags.plugin as string;
const commandFlag = this.flags.command as string;
const outputdirFlag = this.flags.outputdir as string;
const createissueFlag = this.flags.createissue as boolean;
this.outputDir = path.resolve(outputdirFlag ?? process.cwd());

if (createissueFlag) {
this.ux.log('===== TO HERE =====');
}

// eslint-disable-next-line @typescript-eslint/require-await
lifecycle.on<DiagnosticStatus>('Doctor:diagnostic', async (data) => {
this.ux.log(`${data.status} - ${data.testName}`);
Expand Down Expand Up @@ -105,6 +125,31 @@ export default class Doctor extends SfdxCommand {
this.ux.styledHeader('Suggestions');
diagnosis.suggestions.forEach((s) => this.ux.log(` * ${s}`));

if (createissueFlag) {
this.ux.log();
this.ux.log(diagnosis.cliConfig.userAgent);
this.ux.log(diagnosis.versionDetail.pluginVersions.join(os.EOL));
this.ux.log('SFDX ENV. VARS.');
this.ux.log(diagnosis.sfdxEnvVars.join(os.EOL));
this.ux.log('SF ENV. VARS.');
this.ux.log(diagnosis.sfEnvVars.join(os.EOL));
this.ux.log(`Windows: ${diagnosis.cliConfig.windows}`);
this.ux.log(`Shell: ${diagnosis.cliConfig.shell}`);
this.ux.log(`Channel: ${diagnosis.cliConfig.channel}`);
this.ux.log(diagnosis.cliConfig.userAgent);
this.ux.log('===== COPY FROM HERE ABOVE =====');
this.ux.log();
this.ux.log("Hi, let's create a new github issue for you!");

const title = await this.ux.prompt('Please enter a title for the issue');
const url = encodeURI(
`https://github.com/forcedotcom/cli/issues/new?title=${title}&template=bug_report.md&labels=doctor,investigating,${this.config.bin}`
);
this.ux.log("now please copy the above terminal output and place in the 'System Information' section in github");
await this.ux.prompt("press 'Enter' to continue", { required: false });
await Doctor.openUrl(url);
}

return diagnosis;
}

Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class Diagnostics {
let status: DiagnosticStatus['status'] = 'pass';

const plugins = this.config.plugins;
const linkedPlugins = plugins.filter((p) => p.name.includes('(link)'));
const linkedPlugins = plugins.filter((p) => p.type === 'link');
linkedPlugins.forEach((lp) => {
status = 'fail';
this.doctor.addSuggestion(messages.getMessage('linkedPluginWarning', [lp.name]));
Expand Down
30 changes: 28 additions & 2 deletions test/commands/doctor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { Lifecycle, Messages } from '@salesforce/core';
import { Config } from '@oclif/core';
import { VersionDetail } from '@oclif/plugin-version';
import DoctorCmd from '../../src/commands/doctor';
import { Doctor, SfDoctor, SfDoctorDiagnosis } from '../../src/doctor';
import { Diagnostics, DiagnosticStatus } from '../../src/diagnostics';
import { Doctor, SfDoctor, SfDoctorDiagnosis, Diagnostics, DiagnosticStatus } from '../../src';

Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/plugin-info', 'doctor');
Expand Down Expand Up @@ -59,6 +58,7 @@ describe('Doctor Command', () => {
let fsWriteFileSyncStub: sinon.SinonStub;
let diagnosticsRunStub: sinon.SinonStub;
let childProcessExecStub: sinon.SinonStub;
let promptStub: sinon.SinonStub;
let lifecycleEmitSpy: sinon.SinonSpy;

oclifConfigStub = fromStub(
Expand All @@ -84,6 +84,8 @@ describe('Doctor Command', () => {
// oclifConfigStub.bin = 'sfdx';
const cmd = new TestDoctor(params, oclifConfigStub);
uxLogStub = stubMethod(sandbox, UX.prototype, 'log');
promptStub = stubMethod(sandbox, UX.prototype, 'prompt').resolves('my new and crazy issue');

uxStyledHeaderStub = stubMethod(sandbox, UX.prototype, 'styledHeader');

return cmd.runIt();
Expand Down Expand Up @@ -334,6 +336,30 @@ describe('Doctor Command', () => {
expect(lifecycleEmitSpy.called).to.be.false;
});

it('runs doctor command with createissue flag', async () => {
fsExistsSyncStub.returns(true);

sandbox.stub(DoctorCmd, 'openUrl').resolves();
const versionDetail = getVersionDetailStub();
Doctor.init(oclifConfigStub, versionDetail);
diagnosticsRunStub.callsFake(() => [Promise.resolve()]);

const result = await runDoctorCmd(['--createissue']);

expect(uxLogStub.called).to.be.true;
expect(promptStub.callCount).to.equal(2);
expect(uxStyledHeaderStub.called).to.be.true;
expect(result).to.have.property('versionDetail', versionDetail);
expect(result).to.have.property('cliConfig');
expect(result.diagnosticResults).to.deep.equal([]);
verifyEnvVars(result);
verifySuggestions(result);
verifyLogFiles(result);
expect(fsExistsSyncStub.args[0][0]).to.equal(process.cwd());
expect(fsMkdirSyncStub.called).to.be.false;
expect(fsWriteFileSyncStub.calledOnce).to.be.true;
});

it('throws with uninstalled plugin flag', async () => {
fsExistsSyncStub.returns(true);
const versionDetail = getVersionDetailStub();
Expand Down
6 changes: 3 additions & 3 deletions test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ describe('Diagnostics', () => {
},
},
plugins: [
{ name: '@salesforce/plugin-org' },
{ name: '@salesforce/plugin-source (link)' },
{ name: 'salesforce-alm' },
{ name: '@salesforce/plugin-org', type: 'core' },
{ name: '@salesforce/plugin-source', type: 'link' },
{ name: 'salesforce-alm', type: 'core' },
],
})
);
Expand Down
Loading

0 comments on commit d554c2b

Please sign in to comment.