Skip to content

Commit

Permalink
on success action buttons fixes (#217)
Browse files Browse the repository at this point in the history
* fix workspace buttons

* increase coverage

* update version
  • Loading branch information
slavik-lvovsky authored Apr 7, 2020
1 parent a9e2438 commit 83c9cca
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
8 changes: 4 additions & 4 deletions backend/.nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"temp-dir": "./reports/.nyc_output",
"report-dir": "./reports/coverage",
"check-coverage": true,
"branches": 68,
"lines": 69.8,
"functions": 62.4,
"statements": 69.7
"branches": 68.8,
"lines": 69.9,
"functions": 62.8,
"statements": 69.8
}
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "Apache 2.0",
"description": "Provide rich user experience for Yeoman generators using VSCode extension or the browser",
"repository": "https://github.com/SAP/yeoman-ui",
"version": "0.0.70",
"version": "0.0.71",
"engines": {
"vscode": "^1.39.2"
},
Expand Down
17 changes: 10 additions & 7 deletions backend/src/vscode-youi-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,21 @@ export class VSCodeYouiEvents implements YouiEvents {
const openInNewWorkspace: any = "Open in New Workspace";
const items: string[] = [];

const uriTargetFolder = vscode.Uri.file(targetPath);
const targetFolderUri: vscode.Uri = vscode.Uri.file(targetPath);

if (this.genFilter.type !== GeneratorType.module) {
const targetWorkspaceFolder: vscode.WorkspaceFolder = vscode.workspace.getWorkspaceFolder(uriTargetFolder);
const workspacePath = _.get(vscode, "workspace.workspaceFolders[0].uri.fsPath");
// 1. target workspace folder should not already contain target generator folder
// 2. Theia bug: vscode.workspace.workspaceFolders should not be undefined
if (!targetWorkspaceFolder && (_.size(vscode.workspace.workspaceFolders) > 0)) {
const foundInWorkspace = _.find(vscode.workspace.workspaceFolders, (wsFolder: vscode.WorkspaceFolder) => {
return targetFolderUri.fsPath === wsFolder.uri.fsPath;
});
// 2. Theia bug: vscode.workspace.workspaceFolders should not be undefined or empty
if (!foundInWorkspace && workspacePath) {
items.push(addToWorkspace);
}

// target workspace path should not be equal to target generator folder path
if (_.get(targetWorkspaceFolder, "uri.fsPath") !== _.get(uriTargetFolder, "fsPath")) {
if (workspacePath !== targetFolderUri.fsPath) {
items.push(openInNewWorkspace);
}
}
Expand All @@ -83,10 +86,10 @@ export class VSCodeYouiEvents implements YouiEvents {

return vscode.window.showInformationMessage(`${successInfoMessage}\nWhat would you like to do with it?`, ...items).then(selection => {
if (selection === openInNewWorkspace) {
return vscode.commands.executeCommand("vscode.openFolder", uriTargetFolder);
return vscode.commands.executeCommand("vscode.openFolder", targetFolderUri);
} else if (selection === addToWorkspace) {
const wsFoldersQuantity = _.size(vscode.workspace.workspaceFolders);
return vscode.workspace.updateWorkspaceFolders(wsFoldersQuantity, null, { uri: uriTargetFolder});
return vscode.workspace.updateWorkspaceFolders(wsFoldersQuantity, null, { uri: targetFolderUri});
}
});
}
Expand Down
42 changes: 32 additions & 10 deletions backend/tests/vscode-youi-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ describe('vscode-youi-events unit test', () => {
before(() => {
sandbox = sinon.createSandbox();
_.set(vscode, "ProgressLocation.Notification", 15);
_.set(vscode, "Uri.file", (): any => undefined);
_.set(vscode, "Uri.file", (path: string) => {
return {
fsPath: path
};
});
_.set(vscode, "window.showInformationMessage", () => {return Promise.resolve("");});
_.set(vscode, "window.showErrorMessage", () => {return Promise.resolve("");});
_.set(vscode, "workspace.workspaceFolders", []);
_.set(vscode, "workspace.getWorkspaceFolder", (): any => undefined);
_.set(vscode, "workspace.updateWorkspaceFolders", (): any => undefined);
_.set(vscode, "commands.executeCommand", (): any => undefined);
});
Expand Down Expand Up @@ -55,28 +58,47 @@ describe('vscode-youi-events unit test', () => {
});

describe("doGeneratorDone", () => {
it("on success, add to workspace button is visible", () => {
it("on success, add to workspace button and open in new workspace button are visible", () => {
eventsMock.expects("doClose");
const actionName = 'Add to Workspace';
_.set(vscode, "workspace.workspaceFolders", [{}]);
const actionName1 = 'Add to Workspace';
const actionName2 = 'Open in New Workspace';
_.set(vscode, "workspace.workspaceFolders", [{uri: {fsPath: "rootFolderPath"}}]);
windowMock.expects("showInformationMessage").
withExactArgs('The project has been generated.\nWhat would you like to do with it?', actionName).resolves(actionName);
workspaceMock.expects("updateWorkspaceFolders").withArgs(1, null).resolves();
withExactArgs('The project has been generated.\nWhat would you like to do with it?', actionName1, actionName2).resolves();
return events.doGeneratorDone(true, "success message", "testDestinationRoot");
});

it("on success, open in new workspace button is visible", () => {
eventsMock.expects("doClose");
_.set(vscode, "workspace.workspaceFolders", undefined);
_.set(vscode, "Uri.file", (path: string) => {return {uri: path};});
_.set(vscode, "workspace.getWorkspaceFolder", (): any => {return {uri: {fsPath: "testDestinationRoot"}};});
_.set(vscode, "workspace.workspaceFolders", [{uri: {fsPath: "rootFolderPath"}}, {uri: {fsPath: "testDestinationRoot"}}]);
const actionName = 'Open in New Workspace';
windowMock.expects("showInformationMessage").
withExactArgs('The project has been generated.\nWhat would you like to do with it?', actionName).resolves(actionName);
commandsMock.expects("executeCommand").withArgs("vscode.openFolder").resolves();
return events.doGeneratorDone(true, "success message", "testDestinationRoot");
});

it("on success, add to workspace button is pressed", () => {
eventsMock.expects("doClose");
_.set(vscode, "workspace.workspaceFolders", [{uri: {fsPath: "rootFolderPath"}}, {uri: {fsPath: "testRoot"}}]);
const actionName1 = 'Add to Workspace';
const actionName2 = 'Open in New Workspace';
windowMock.expects("showInformationMessage").
withExactArgs('The project has been generated.\nWhat would you like to do with it?', actionName1, actionName2).resolves(actionName1);
workspaceMock.expects("updateWorkspaceFolders").withArgs(2, null).resolves();
return events.doGeneratorDone(true, "success message", "testDestinationRoot");
});

it("on success, no buttons are displayed", () => {
eventsMock.expects("doClose");
_.set(vscode, "workspace.workspaceFolders", [{uri: {fsPath: "testDestinationRoot"}}]);
const actionName1 = 'Add to Workspace';
const actionName2 = 'Open in New Workspace';
windowMock.expects("showInformationMessage").
withExactArgs('The project has been generated.').resolves();
return events.doGeneratorDone(true, "success message", "testDestinationRoot");
});

it("on failure", () => {
eventsMock.expects("doClose");
windowMock.expects("showErrorMessage").withExactArgs("error message");
Expand Down

0 comments on commit 83c9cca

Please sign in to comment.