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

@@ -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>();