From 5c39d3d0d847806f929f233dcbbfe7974d87bc6f Mon Sep 17 00:00:00 2001 From: Vincent Cheung <71457708+vcheung-stripe@users.noreply.github.com> Date: Thu, 28 Jan 2021 14:20:17 -0800 Subject: [PATCH] Prompt user to install CLI or show error message on configuration change (#141) --- src/stripeClient.ts | 14 ++++++++------ src/test/suite/stripeClient.test.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/stripeClient.ts b/src/stripeClient.ts index 8e59cfab..ee593b17 100644 --- a/src/stripeClient.ts +++ b/src/stripeClient.ts @@ -10,12 +10,10 @@ const fs = require('fs'); export class StripeClient { telemetry: Telemetry; - isInstalled: boolean; cliPath: string | null; constructor(telemetry:Telemetry) { this.telemetry = telemetry; - this.isInstalled = false; this.cliPath = null; vscode.workspace.onDidChangeConfiguration(this.handleDidChangeConfiguration, this); } @@ -24,7 +22,6 @@ export class StripeClient { const isInstalled = await this.detectInstalled(); if (!isInstalled) { - this.promptInstall(); return; } @@ -135,6 +132,8 @@ export class StripeClient { `You set a custom installation path for the Stripe CLI, but we couldn't find the executable in '${customInstallPath}'`, ...['Ok'] ); + } else { + this.promptInstall(); } this.cliPath = null; this.telemetry.sendEvent('cli.notInstalled'); @@ -154,9 +153,12 @@ export class StripeClient { private async handleDidChangeConfiguration(e: vscode.ConfigurationChangeEvent) { const shouldHandleConfigurationChange = e.affectsConfiguration('stripe'); if (shouldHandleConfigurationChange) { - const isAuthenticated = await this.isAuthenticated(); - if (!isAuthenticated) { - await this.promptLogin(); + const isInstalled = await this.detectInstalled(); + if (isInstalled) { + const isAuthenticated = await this.isAuthenticated(); + if (!isAuthenticated) { + await this.promptLogin(); + } } } } diff --git a/src/test/suite/stripeClient.test.ts b/src/test/suite/stripeClient.test.ts index 91d4d2a6..88cf6f15 100644 --- a/src/test/suite/stripeClient.test.ts +++ b/src/test/suite/stripeClient.test.ts @@ -62,6 +62,22 @@ suite('stripeClient', () => { }); }); }); + + test('prompts install when CLI is not installed', async () => { + sandbox.stub(fs.promises, 'stat').returns(Promise.resolve({isFile: () => false})); + const showErrorMessageSpy = sandbox.stub(vscode.window, 'showErrorMessage'); + const stripeClient = new StripeClient(new NoOpTelemetry()); + const isInstalled = await stripeClient.detectInstalled(); + assert.strictEqual(isInstalled, false); + assert.deepStrictEqual( + showErrorMessageSpy.args[0], + [ + 'Welcome! Stripe is using the Stripe CLI behind the scenes, and requires it to be installed on your machine', + {}, + 'Read instructions on how to install Stripe CLI', + ] + ); + }); }); suite('with custom CLI install path', () => { @@ -110,6 +126,18 @@ suite('stripeClient', () => { }); }); }); + + test('shows error when CLI is not at that path', async () => { + statStub.returns(Promise.resolve({isFile: () => false})); + const showErrorMessageSpy = sandbox.stub(vscode.window, 'showErrorMessage'); + const stripeClient = new StripeClient(new NoOpTelemetry()); + const isInstalled = await stripeClient.detectInstalled(); + assert.strictEqual(isInstalled, false); + assert.deepStrictEqual( + showErrorMessageSpy.args[0], + ["You set a custom installation path for the Stripe CLI, but we couldn't find the executable in '/foo/bar/baz'", 'Ok'], + ); + }); }); }); });