Prompts user for desired action when autorest is not installed (#17305)

* Giving user option to install autorest globally or run via npx

* Adds message for when no files are generated (no models in spec)

* Adding test

* Reinstating the rest of the test suite

* PR feedback

* Fixing tests to stub new prompt when test runner doesn't have autorest installed

* PR feedback

* fix typo

* fix typo
This commit is contained in:
Benjin Dubishar
2021-10-15 13:26:58 -07:00
committed by GitHub
parent 7b66acd58b
commit 901b90317c
5 changed files with 62 additions and 14 deletions

View File

@@ -11,6 +11,8 @@ import * as path from 'path';
import { TestContext, createContext } from './testContext';
import { AutorestHelper } from '../tools/autorestHelper';
import { promises as fs } from 'fs';
import { window } from 'vscode';
import { runViaNpx } from '../common/constants';
let testContext: TestContext;
@@ -24,12 +26,16 @@ describe('Autorest tests', function (): void {
});
it('Should detect autorest', async function (): Promise<void> {
sinon.stub(window, 'showInformationMessage').returns(<any>Promise.resolve(runViaNpx)); // stub a selection in case test runner doesn't have autorest installed
const autorestHelper = new AutorestHelper(testContext.outputChannel);
const executable = await autorestHelper.detectInstallation();
should(executable === 'autorest' || executable === 'npx autorest').equal(true, 'autorest command should be found in default path during unit tests');
});
it('Should run an autorest command successfully', async function (): Promise<void> {
sinon.stub(window, 'showInformationMessage').returns(<any>Promise.resolve(runViaNpx)); // stub a selection in case test runner doesn't have autorest installed
const autorestHelper = new AutorestHelper(testContext.outputChannel);
const dummyFile = path.join(await testUtils.generateTestFolderPath(), 'testoutput.log');
sinon.stub(autorestHelper, 'constructAutorestCommand').returns(`${await autorestHelper.detectInstallation()} --version > ${dummyFile}`);
@@ -47,12 +53,27 @@ describe('Autorest tests', function (): void {
});
it('Should construct a correct autorest command for project generation', async function (): Promise<void> {
const autorestHelper = new AutorestHelper(testContext.outputChannel);
sinon.stub(window, 'showInformationMessage').returns(<any>Promise.resolve(runViaNpx)); // stub a selection in case test runner doesn't have autorest installed
sinon.stub(autorestHelper, 'detectInstallation').returns(Promise.resolve('autorest'));
const expectedOutput = 'autorest --use:autorest-sql-testing@latest --input-file="/some/path/test.yaml" --output-folder="/some/output/path" --clear-output-folder --verbose';
const autorestHelper = new AutorestHelper(testContext.outputChannel);
const constructedCommand = autorestHelper.constructAutorestCommand((await autorestHelper.detectInstallation())!, '/some/path/test.yaml', '/some/output/path');
// depending on whether the machine running the test has autorest installed or just node, the expected output may differ by just the prefix, hence matching against two options
should(constructedCommand === expectedOutput || constructedCommand === `npx ${expectedOutput}`).equal(true, `Constructed autorest command not formatting as expected:\nActual: ${constructedCommand}\nExpected: [npx ]${expectedOutput}`);
should(constructedCommand === expectedOutput).equal(true, `Constructed autorest command not formatting as expected:\nActual:\n\t${constructedCommand}\nExpected:\n\t${expectedOutput}`);
});
it('Should prompt user for action when autorest not found', async function (): Promise<void> {
const promptStub = sinon.stub(window, 'showInformationMessage').returns(<any>Promise.resolve());
const detectStub = sinon.stub(utils, 'detectCommandInstallation');
detectStub.withArgs('autorest').returns(Promise.resolve(false));
detectStub.withArgs('npx').returns(Promise.resolve(true));
const autorestHelper = new AutorestHelper(testContext.outputChannel);
await autorestHelper.detectInstallation();
should(promptStub.calledOnce).be.true('User should have been prompted for how to run autorest because it wasn\'t found.');
});
});

View File

@@ -704,6 +704,7 @@ describe('ProjectsController', function (): void {
projController.setup(x => x.generateAutorestFiles(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(async () => {
await testUtils.createDummyFileStructure(true, fileList, newProjFolder);
await testUtils.createTestFile('SELECT \'This is a post-deployment script\'', constants.autorestPostDeploymentScriptName, newProjFolder);
return 'some dummy console output';
});
projController.setup(x => x.openProjectInWorkspace(TypeMoq.It.isAny())).returns(async () => { });