Fix unstable data-workspace tests (#13824)

* stub file existing validation

* add error message

* change back to calling dialog.validate()

* move tests to separate dialogbase file and add more error message validation
This commit is contained in:
Kim Santiago
2020-12-17 10:28:42 -08:00
committed by GitHub
parent f88e17a294
commit e3e1ae92b2
5 changed files with 92 additions and 68 deletions

View File

@@ -36,9 +36,7 @@ export abstract class DialogBase {
protected abstract initialize(view: azdata.ModelView): Promise<void>;
protected async validate(): Promise<boolean> {
return Promise.resolve(true);
}
abstract validate(): Promise<boolean>;
public async open(): Promise<void> {
const tab = azdata.window.createTab('');
@@ -77,6 +75,10 @@ export abstract class DialogBase {
};
}
public getErrorMessage(): azdata.window.DialogMessage {
return this._dialogObject.message;
}
protected createHorizontalContainer(view: azdata.ModelView, items: azdata.Component[]): azdata.FlexContainer {
return view.modelBuilder.flexContainer().withItems(items, { CSSStyles: { 'margin-right': '5px', 'margin-bottom': '10px' } }).withLayout({ flexFlow: 'row', alignItems: 'center' }).component();
}
@@ -159,7 +161,7 @@ export abstract class DialogBase {
}
}
protected async validateNewWorkspace(sameFolderAsNewProject: boolean): Promise<boolean> {
public async validateNewWorkspace(sameFolderAsNewProject: boolean): Promise<boolean> {
// workspace file should end in .code-workspace
const workspaceValid = this.workspaceInputBox!.value!.endsWith(constants.WorkspaceFileExtension);
if (!workspaceValid) {
@@ -171,7 +173,7 @@ export abstract class DialogBase {
if (!sameFolderAsNewProject) {
const workspaceParentDirectoryExists = await directoryExist(path.dirname(this.workspaceInputBox!.value!));
if (!workspaceParentDirectoryExists) {
this.showErrorMessage(constants.WorkspaceParentDirectoryNotExistError(this.workspaceInputBox!.value!));
this.showErrorMessage(constants.WorkspaceParentDirectoryNotExistError(path.dirname(this.workspaceInputBox!.value!)));
return false;
}
}

View File

@@ -37,9 +37,7 @@ export class OpenExistingDialog extends DialogBase {
try {
// the selected location should be an existing directory
if (this._targetTypeRadioCardGroup?.selectedCardId === constants.Project) {
const fileExists = await fileExist(this._projectFile);
if (!fileExists) {
this.showErrorMessage(constants.FileNotExistError(constants.Project.toLowerCase(), this._projectFile));
if (!await this.validateFile(this._projectFile, constants.Project.toLowerCase())) {
return false;
}
@@ -47,9 +45,7 @@ export class OpenExistingDialog extends DialogBase {
return false;
}
} else if (this._targetTypeRadioCardGroup?.selectedCardId === constants.Workspace) {
const fileExists = await fileExist(this._workspaceFile);
if (!fileExists) {
this.showErrorMessage(constants.FileNotExistError(constants.Workspace.toLowerCase(), this._workspaceFile));
if (!await this.validateFile(this._workspaceFile, constants.Workspace.toLowerCase())) {
return false;
}
}
@@ -62,6 +58,16 @@ export class OpenExistingDialog extends DialogBase {
}
}
public async validateFile(file: string, fileType: string) {
const fileExists = await fileExist(file);
if (!fileExists) {
this.showErrorMessage(constants.FileNotExistError(fileType, file));
return false;
}
return true;
}
async onComplete(): Promise<void> {
try {
if (this._targetTypeRadioCardGroup?.selectedCardId === constants.Workspace) {

View File

@@ -0,0 +1,69 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import * as TypeMoq from 'typemoq';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import * as utils from '../../common/utils';
import * as constants from '../../common/constants';
import { NewProjectDialog } from '../../dialogs/newProjectDialog';
import { WorkspaceService } from '../../services/workspaceService';
import { testProjectType } from '../testUtils';
suite('DialogBase - workspace validation', function (): void {
// DialogBase is an abstract class, so we'll just use a NewProjectDialog to test the common base class functions
let dialog: NewProjectDialog;
this.beforeEach(async () => {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();
workspaceServiceMock.setup(x => x.getAllProjectTypes()).returns(() => Promise.resolve([testProjectType]));
dialog = new NewProjectDialog(workspaceServiceMock.object);
await dialog.open();
dialog.model.name = `TestProject_${new Date().getTime()}`;
dialog.model.location = os.tmpdir();
});
this.afterEach(() => {
sinon.restore();
});
test('Should validate new workspace location missing file extension', async function (): Promise<void> {
dialog.workspaceInputBox!.value = 'test';
should.equal(await dialog.validateNewWorkspace(false), false, 'Validation should fail because workspace does not end in .code-workspace');
should.equal(dialog.getErrorMessage().text, constants.WorkspaceFileInvalidError(dialog.workspaceInputBox!.value));
});
test('Should validate new workspace location with invalid location', async function (): Promise<void> {
// use invalid folder
dialog.workspaceInputBox!.value = 'invalidLocation/test.code-workspace';
should.equal(await dialog.validateNewWorkspace(false), false, 'Validation should fail because the folder is invalid');
should.equal(dialog.getErrorMessage().text, constants.WorkspaceParentDirectoryNotExistError(path.dirname(dialog.workspaceInputBox!.value)));
});
test('Should validate new workspace location that already exists', async function (): Promise<void> {
// use already existing workspace
const fileExistStub = sinon.stub(utils, 'fileExist');
fileExistStub.resolves(true);
const existingWorkspaceFilePath = path.join(os.tmpdir(), `${dialog.model.name}.code-workspace`);
dialog.workspaceInputBox!.value = existingWorkspaceFilePath;
should.equal(await dialog.validateNewWorkspace(false), false, 'Validation should fail because the selected workspace file already exists');
should.equal(dialog.getErrorMessage().text, constants.WorkspaceFileAlreadyExistsError(existingWorkspaceFilePath));
});
test('Should validate new workspace location that is valid', async function (): Promise<void> {
// same folder as the project should be valid even if the project folder isn't created yet
dialog.workspaceInputBox!.value = path.join(dialog.model.location, dialog.model.name, 'test.code-workspace');
should.equal(await dialog.validateNewWorkspace(true), true, `Validation should pass if the file location is the same folder as the project. Error was: ${dialog.getErrorMessage()?.text}`);
// a workspace not in the same folder as the project should also be valid
dialog.workspaceInputBox!.value = path.join(os.tmpdir(), `TestWorkspace_${new Date().getTime()}.code-workspace`);
should.equal(await dialog.validateNewWorkspace(false), true, `Validation should pass because the parent directory exists, workspace filepath is unique, and the file extension is correct. Error was: ${dialog.getErrorMessage()?.text}`);
});
});

View File

@@ -15,6 +15,10 @@ import { WorkspaceService } from '../../services/workspaceService';
import { testProjectType } from '../testUtils';
suite('New Project Dialog', function (): void {
this.afterEach(() => {
sinon.restore();
});
test('Should validate project location', async function (): Promise<void> {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();
workspaceServiceMock.setup(x => x.getAllProjectTypes()).returns(() => Promise.resolve([testProjectType]));
@@ -38,36 +42,6 @@ suite('New Project Dialog', function (): void {
should.equal(await dialog.validate(), true, 'Validation should pass because name is unique and parent directory exists');
});
test('Should validate new workspace location @UNSTABLE@', async function (): Promise<void> {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();
workspaceServiceMock.setup(x => x.getAllProjectTypes()).returns(() => Promise.resolve([testProjectType]));
const dialog = new NewProjectDialog(workspaceServiceMock.object);
await dialog.open();
dialog.model.name = `TestProject_${new Date().getTime()}`;
dialog.model.location = os.tmpdir();
dialog.workspaceInputBox!.value = 'test';
should.equal(await dialog.validate(), false, 'Validation should fail because workspace does not end in .code-workspace');
// use invalid folder
dialog.workspaceInputBox!.value = 'invalidLocation/test.code-workspace';
should.equal(await dialog.validate(), false, 'Validation should fail because the folder is invalid');
// use already existing workspace
const existingWorkspaceFilePath = path.join(os.tmpdir(), `${dialog.model.name}.code-workspace`);
await fs.writeFile(existingWorkspaceFilePath, '');
dialog.workspaceInputBox!.value = existingWorkspaceFilePath;
should.equal(await dialog.validate(), false, 'Validation should fail because the selected workspace file already exists');
// same folder as the project should be valid even if the project folder isn't created yet
dialog.workspaceInputBox!.value = path.join(dialog.model.location, dialog.model.name, 'test.code-workspace');
should.equal(await dialog.validate(), true, 'Validation should pass if the file location is the same folder as the project');
// change workspace name to something that should pass
dialog.workspaceInputBox!.value = path.join(os.tmpdir(), `TestWorkspace_${new Date().getTime()}.code-workspace`);
should.equal(await dialog.validate(), true, 'Validation should pass because the parent directory exists, workspace filepath is unique, and the file extension is correct');
});
test('Should validate workspace in onComplete', async function (): Promise<void> {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();

View File

@@ -7,8 +7,6 @@ import * as should from 'should';
import * as TypeMoq from 'typemoq';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import * as os from 'os';
import * as path from 'path';
import * as constants from '../../common/constants';
import { promises as fs } from 'fs';
import { WorkspaceService } from '../../services/workspaceService';
@@ -53,31 +51,6 @@ suite('Open Existing Dialog', function (): void {
should.equal(await dialog.validate(), true, 'Validation pass because workspace file exists');
});
test('Should validate new workspace location @UNSTABLE@', async function (): Promise<void> {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();
workspaceServiceMock.setup(x => x.getAllProjectTypes()).returns(() => Promise.resolve([testProjectType]));
const dialog = new OpenExistingDialog(workspaceServiceMock.object, mockExtensionContext.object);
await dialog.open();
dialog._projectFile = await createProjectFile('testproj');
dialog.workspaceInputBox!.value = 'test';
should.equal(await dialog.validate(), false, 'Validation should fail because workspace does not end in code-workspace');
// use invalid folder
dialog.workspaceInputBox!.value = 'invalidLocation/test.code-workspace';
should.equal(await dialog.validate(), false, 'Validation should fail because the folder is invalid');
// use already existing workspace
const existingWorkspaceFilePath = path.join(os.tmpdir(), `test.code-workspace`);
await fs.writeFile(existingWorkspaceFilePath, '');
dialog.workspaceInputBox!.value = existingWorkspaceFilePath;
should.equal(await dialog.validate(), false, 'Validation should fail because the selected workspace file already exists');
// change workspace name to something that should pass
dialog.workspaceInputBox!.value = path.join(os.tmpdir(), `TestWorkspace_${new Date().getTime()}.code-workspace`);
should.equal(await dialog.validate(), true, 'Validation should pass because the parent directory exists, workspace filepath is unique, and the file extension is correct');
});
test('Should validate workspace in onComplete when opening project', async function (): Promise<void> {
const workspaceServiceMock = TypeMoq.Mock.ofType<WorkspaceService>();