Swapping vscode calls for ApiWrapper for testability (#10267)

* swapping vscode calls for apiwrapper for testability

* Adding mainController tests

* Adding unit tests for input validation

* Adding project controller tests, reorganizing error handling

* Removing commented-out code
This commit is contained in:
Benjin Dubishar
2020-05-06 14:16:27 -07:00
committed by GitHub
parent 0ace033a6f
commit 80901c9a7b
10 changed files with 373 additions and 108 deletions

View File

@@ -3,7 +3,6 @@
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as path from 'path';
import * as constants from '../common/constants';
import * as dataSources from '../models/dataSources/dataSources';
@@ -11,6 +10,8 @@ import * as utils from '../common/utils';
import * as UUID from 'vscode-languageclient/lib/utils/uuid';
import * as templates from '../templates/templates';
import { Uri, QuickPickItem } from 'vscode';
import { ApiWrapper } from '../common/apiWrapper';
import { Project } from '../models/project';
import { SqlDatabaseProjectTreeViewProvider } from './databaseProjectTreeViewProvider';
import { promises as fs } from 'fs';
@@ -26,7 +27,7 @@ export class ProjectsController {
projects: Project[] = [];
constructor(projTreeViewProvider: SqlDatabaseProjectTreeViewProvider) {
constructor(private apiWrapper: ApiWrapper, projTreeViewProvider: SqlDatabaseProjectTreeViewProvider) {
this.projectTreeViewProvider = projTreeViewProvider;
}
@@ -35,29 +36,29 @@ export class ProjectsController {
this.projectTreeViewProvider.load(this.projects);
}
public async openProject(projectFile: vscode.Uri): Promise<Project> {
public async openProject(projectFile: Uri): Promise<Project> {
for (const proj of this.projects) {
if (proj.projectFilePath === projectFile.fsPath) {
vscode.window.showInformationMessage(constants.projectAlreadyOpened(projectFile.fsPath));
this.apiWrapper.showInformationMessage(constants.projectAlreadyOpened(projectFile.fsPath));
return proj;
}
}
// Read project file
const newProject = new Project(projectFile.fsPath);
await newProject.readProjFile();
this.projects.push(newProject);
// Read datasources.json (if present)
const dataSourcesFilePath = path.join(path.dirname(projectFile.fsPath), constants.dataSourcesFileName);
try {
// Read project file
await newProject.readProjFile();
this.projects.push(newProject);
// Read datasources.json (if present)
const dataSourcesFilePath = path.join(path.dirname(projectFile.fsPath), constants.dataSourcesFileName);
newProject.dataSources = await dataSources.load(dataSourcesFilePath);
}
catch (err) {
if (err instanceof dataSources.NoDataSourcesFileError) {
// TODO: prompt to create new datasources.json; for now, swallow
console.log(`No ${constants.dataSourcesFileName} file found.`);
}
else {
throw err;
@@ -69,7 +70,7 @@ export class ProjectsController {
return newProject;
}
public async createNewProject(newProjName: string, folderUri: vscode.Uri, projectGuid?: string): Promise<string> {
public async createNewProject(newProjName: string, folderUri: Uri, projectGuid?: string): Promise<string> {
if (projectGuid && !UUID.isUUID(projectGuid)) {
throw new Error(`Specified GUID is invalid: '${projectGuid}'`);
}
@@ -114,17 +115,17 @@ export class ProjectsController {
public async build(treeNode: BaseProjectTreeItem) {
const project = this.getProjectContextFromTreeNode(treeNode);
await vscode.window.showErrorMessage(`Build not yet implemented: ${project.projectFilePath}`); // TODO
await this.apiWrapper.showErrorMessage(`Build not yet implemented: ${project.projectFilePath}`); // TODO
}
public async deploy(treeNode: BaseProjectTreeItem) {
const project = this.getProjectContextFromTreeNode(treeNode);
await vscode.window.showErrorMessage(`Deploy not yet implemented: ${project.projectFilePath}`); // TODO
await this.apiWrapper.showErrorMessage(`Deploy not yet implemented: ${project.projectFilePath}`); // TODO
}
public async import(treeNode: BaseProjectTreeItem) {
const project = this.getProjectContextFromTreeNode(treeNode);
await vscode.window.showErrorMessage(`Import not yet implemented: ${project.projectFilePath}`); // TODO
await this.apiWrapper.showErrorMessage(`Import not yet implemented: ${project.projectFilePath}`); // TODO
}
public async addFolderPrompt(treeNode: BaseProjectTreeItem) {
@@ -135,26 +136,28 @@ export class ProjectsController {
return; // user cancelled
}
const relativeFolderPath = this.prependContextPath(treeNode, newFolderName);
const relativeFolderPath = path.join(this.getRelativePath(treeNode), newFolderName);
await project.addFolderItem(relativeFolderPath);
this.refreshProjectsTree();
}
public async addItemPrompt(treeNode: BaseProjectTreeItem, itemTypeName?: string) {
const project = this.getProjectContextFromTreeNode(treeNode);
public async addItemPromptFromNode(treeNode: BaseProjectTreeItem, itemTypeName?: string) {
await this.addItemPrompt(this.getProjectContextFromTreeNode(treeNode), this.getRelativePath(treeNode), itemTypeName);
}
public async addItemPrompt(project: Project, relativePath: string, itemTypeName?: string) {
if (!itemTypeName) {
let itemFriendlyNames: string[] = [];
const items: QuickPickItem[] = [];
for (const itemType of templates.projectScriptTypes()) {
itemFriendlyNames.push(itemType.friendlyName);
items.push({ label: itemType.friendlyName });
}
itemTypeName = await vscode.window.showQuickPick(itemFriendlyNames, {
itemTypeName = (await this.apiWrapper.showQuickPick(items, {
canPickMany: false
});
}))?.label;
if (!itemTypeName) {
return; // user cancelled
@@ -162,7 +165,9 @@ export class ProjectsController {
}
const itemType = templates.projectScriptTypeMap()[itemTypeName.toLocaleLowerCase()];
const itemObjectName = await this.promptForNewObjectName(itemType, project);
let itemObjectName = await this.promptForNewObjectName(itemType, project);
itemObjectName = itemObjectName?.trim();
if (!itemObjectName) {
return; // user cancelled
@@ -171,11 +176,11 @@ export class ProjectsController {
// TODO: file already exists?
const newFileText = this.macroExpansion(itemType.templateScript, { 'OBJECT_NAME': itemObjectName });
const relativeFilePath = this.prependContextPath(treeNode, itemObjectName + '.sql');
const relativeFilePath = path.join(relativePath, itemObjectName + '.sql');
const newEntry = await project.addScriptItem(relativeFilePath, newFileText);
vscode.commands.executeCommand('vscode.open', newEntry.fsUri);
this.apiWrapper.executeCommand('vscode.open', newEntry.fsUri);
this.refreshProjectsTree();
}
@@ -216,7 +221,7 @@ export class ProjectsController {
// TODO: ask project for suggested name that doesn't conflict
const suggestedName = itemType.friendlyName.replace(new RegExp('\s', 'g'), '') + '1';
const itemObjectName = await vscode.window.showInputBox({
const itemObjectName = await this.apiWrapper.showInputBox({
prompt: constants.newObjectNamePrompt(itemType.friendlyName),
value: suggestedName,
});
@@ -224,13 +229,8 @@ export class ProjectsController {
return itemObjectName;
}
private prependContextPath(treeNode: BaseProjectTreeItem, objectName: string): string {
if (treeNode instanceof FolderNode) {
return path.join(utils.trimUri(treeNode.root.uri, treeNode.uri), objectName);
}
else {
return objectName;
}
private getRelativePath(treeNode: BaseProjectTreeItem): string {
return treeNode instanceof FolderNode ? utils.trimUri(treeNode.root.uri, treeNode.uri) : '';
}
//#endregion