forked from zowe/zowe-explorer-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request zowe#1 from kristinochka/uss-explorer-create-delete
Create and delete Nodes in USS Explorer
- Loading branch information
Showing
7 changed files
with
240 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the * | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at * | ||
* https://www.eclipse.org/legal/epl-v20.html * | ||
* * | ||
* SPDX-License-Identifier: EPL-2.0 * | ||
* * | ||
* Copyright Contributors to the Zowe Project. * | ||
* * | ||
*/ | ||
|
||
import * as vscode from "vscode"; | ||
import { ZoweUSSNode } from "../../src/ZoweUSSNode"; | ||
import * as brtimperative from "@brightside/imperative"; | ||
import * as brightside from "@brightside/core"; | ||
import { createUSSNode, deleteUSSNode, parseUSSPath } from "../../src/uss/ussNodeActions"; | ||
|
||
const Create = jest.fn(); | ||
const Delete = jest.fn(); | ||
const uss = jest.fn(); | ||
const ussFile = jest.fn(); | ||
const mockAddUSSSession = jest.fn(); | ||
const mockUSSRefresh = jest.fn(); | ||
const mockGetUSSChildren = jest.fn(); | ||
const showInputBox = jest.fn(); | ||
const showErrorMessage = jest.fn(); | ||
const showQuickPick = jest.fn(); | ||
|
||
function getUSSNode() { | ||
const ussNode = new ZoweUSSNode("usstest", vscode.TreeItemCollapsibleState.Expanded, null, session, null); | ||
ussNode.contextValue = "uss_session"; | ||
ussNode.fullPath = "/u/myuser"; | ||
return ussNode; | ||
} | ||
|
||
function getUSSTree() { | ||
const ussNode = getUSSNode(); | ||
const USSTree = jest.fn().mockImplementation(() => { | ||
return { | ||
mSessionNodes: [], | ||
addSession: mockAddUSSSession, | ||
refresh: mockUSSRefresh, | ||
getChildren: mockGetUSSChildren, | ||
}; | ||
}); | ||
const testUSSTree = USSTree(); | ||
testUSSTree.mSessionNodes = []; | ||
testUSSTree.mSessionNodes.push(ussNode); | ||
return testUSSTree; | ||
} | ||
|
||
const session = new brtimperative.Session({ | ||
user: "fake", | ||
password: "fake", | ||
hostname: "fake", | ||
protocol: "https", | ||
type: "basic", | ||
}); | ||
|
||
const ussNode = getUSSNode(); | ||
const testUSSTree = getUSSTree(); | ||
|
||
Object.defineProperty(brightside, "Create", {value: Create}); | ||
Object.defineProperty(brightside, "Delete", {value: Delete}); | ||
Object.defineProperty(Create, "uss", { value: uss }); | ||
Object.defineProperty(Delete, "ussFile", { value: ussFile }); | ||
Object.defineProperty(vscode.window, "showInputBox", {value: showInputBox}); | ||
Object.defineProperty(vscode.window, "showErrorMessage", {value: showErrorMessage}); | ||
Object.defineProperty(vscode.window, "showQuickPick", {value: showQuickPick}); | ||
|
||
describe("ussNodeActions", async () => { | ||
beforeEach(() => { | ||
showErrorMessage.mockReset(); | ||
testUSSTree.refresh.mockReset(); | ||
showQuickPick.mockReset(); | ||
}); | ||
describe("createUSSNode", () => { | ||
it("createUSSNode is executed successfully", async () => { | ||
showInputBox.mockReset(); | ||
showInputBox.mockReturnValueOnce("USSFolder"); | ||
await createUSSNode(ussNode, testUSSTree, "file"); | ||
expect(testUSSTree.refresh).toHaveBeenCalled(); | ||
expect(showErrorMessage.mock.calls.length).toBe(0); | ||
}); | ||
it("createUSSNode does not execute if node name was not entered", async () => { | ||
showInputBox.mockReset(); | ||
showInputBox.mockReturnValueOnce(""); | ||
await createUSSNode(ussNode, testUSSTree, "file"); | ||
expect(testUSSTree.refresh).not.toHaveBeenCalled(); | ||
expect(showErrorMessage.mock.calls.length).toBe(0); | ||
}); | ||
}) | ||
describe("deleteUSSNode", () => { | ||
it("deleteUSSNode is executed successfully", async () => { | ||
showQuickPick.mockResolvedValueOnce("Yes"); | ||
await deleteUSSNode(ussNode, testUSSTree, ""); | ||
expect(testUSSTree.refresh).toHaveBeenCalled(); | ||
}); | ||
it("should not delete node if user did not verify", async () => { | ||
showQuickPick.mockResolvedValueOnce("No"); | ||
await deleteUSSNode(ussNode, testUSSTree, ""); | ||
expect(testUSSTree.refresh).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
describe("parseUSSPath", () => { | ||
it("should append slash", () => { | ||
expect(parseUSSPath("/u/user")).toEqual("//u/user"); | ||
expect(parseUSSPath("/test")).toEqual("//test"); | ||
expect(parseUSSPath("/u/kri/dir/dir2")).toEqual("//u/kri/dir/dir2"); | ||
}); | ||
it("should not append slash", () => { | ||
expect(parseUSSPath("//u/user")).toEqual("//u/user"); | ||
expect(parseUSSPath("//")).toEqual("//"); | ||
expect(parseUSSPath("///")).toEqual("///"); | ||
expect(parseUSSPath("/")).toEqual("/"); | ||
expect(parseUSSPath("")).toEqual(""); | ||
expect(parseUSSPath(undefined)).toEqual(undefined); | ||
expect(parseUSSPath("//u/kri/dir/dir2")).toEqual("//u/kri/dir/dir2"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the * | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at * | ||
* https://www.eclipse.org/legal/epl-v20.html * | ||
* * | ||
* SPDX-License-Identifier: EPL-2.0 * | ||
* * | ||
* Copyright Contributors to the Zowe Project. * | ||
* * | ||
*/ | ||
|
||
import { USSTree } from "../USSTree"; | ||
import { ZoweUSSNode } from "../ZoweUSSNode"; | ||
import * as vscode from "vscode"; | ||
import * as zowe from "@brightside/core"; | ||
import * as fs from "fs"; | ||
|
||
/** | ||
* Prompts the user for a path, and populates the [TreeView]{@link vscode.TreeView} based on the path | ||
* | ||
* @param {ZoweUSSNode} node - The session node | ||
* @param {ussTree} ussFileProvider - Current ussTree used to populate the TreeView | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function createUSSNode(node: ZoweUSSNode, ussFileProvider: USSTree, nodeType: string) { | ||
const name = await vscode.window.showInputBox({placeHolder: "Name of Member"}); | ||
if (name) { | ||
try { | ||
const filePath = `${node.fullPath}/${name}`; | ||
await zowe.Create.uss(node.getSession(), filePath, nodeType); | ||
ussFileProvider.refresh(); | ||
} catch (err) { | ||
vscode.window.showErrorMessage(`Unable to create node: ${err.message}`); | ||
throw (err); | ||
} | ||
} | ||
} | ||
|
||
export async function deleteUSSNode(node: ZoweUSSNode, ussFileProvider: USSTree, filePath: string) { | ||
const nodePath = node.fullPath; | ||
const quickPickOptions: vscode.QuickPickOptions = { | ||
placeHolder: `Are you sure you want to delete ${node.label}`, | ||
ignoreFocusOut: true, | ||
canPickMany: false | ||
}; | ||
if (await vscode.window.showQuickPick(["Yes", "No"], quickPickOptions) === "No") { | ||
return; | ||
} | ||
try { | ||
const isRecursive = node.contextValue === "directory" ? true : false; | ||
await zowe.Delete.ussFile(node.getSession(), nodePath, isRecursive); | ||
ussFileProvider.refresh(); | ||
deleteFromDisk(node, filePath); | ||
} catch (err) { | ||
vscode.window.showErrorMessage(`Unable to delete node: ${err.message}`); | ||
throw (err); | ||
} | ||
} | ||
|
||
export function parseUSSPath(path: string) { | ||
if (path && path.match('^\/[^\/]')) { | ||
return `/${path}`; | ||
} | ||
return path; | ||
} | ||
|
||
/** | ||
* Marks file as deleted from disk | ||
* | ||
* @param {ZoweUSSNode} node | ||
*/ | ||
export async function deleteFromDisk(node: ZoweUSSNode, filePath: string) { | ||
try { | ||
if (fs.existsSync(filePath)) { | ||
fs.unlinkSync(filePath); | ||
} | ||
} | ||
catch (err) {} | ||
} |