diff --git a/packages/zowe-explorer/__tests__/__unit__/uss/actions.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/uss/actions.unit.test.ts index 3fff27529a..273519df5a 100644 --- a/packages/zowe-explorer/__tests__/__unit__/uss/actions.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/uss/actions.unit.test.ts @@ -205,6 +205,32 @@ describe("USS Action Unit Tests - Function createUSSNode", () => { expect(refreshActions.refreshAll).not.toHaveBeenCalled(); }); + it("returns early if a location was never provided", async () => { + const globalMocks = createGlobalMocks(); + const blockMocks = await createBlockMocks(globalMocks); + + globalMocks.mockShowInputBox.mockResolvedValueOnce(undefined); + const createApiMock = jest.spyOn(blockMocks.ussApi, "create").mockImplementation(); + blockMocks.ussNode.getParent().fullPath = ""; + + await ussNodeActions.createUSSNode(blockMocks.ussNode.getParent(), blockMocks.testUSSTree, "directory"); + expect(createApiMock).not.toHaveBeenCalled(); + createApiMock.mockRestore(); + }); + + it("handles trailing slashes in the location", async () => { + const globalMocks = createGlobalMocks(); + const blockMocks = await createBlockMocks(globalMocks); + + globalMocks.mockShowInputBox.mockResolvedValueOnce("/u/myuser/aDir/"); + globalMocks.mockShowInputBox.mockResolvedValueOnce("testFile.txt"); + const createApiMock = jest.spyOn(blockMocks.ussApi, "create").mockImplementation(); + + await ussNodeActions.createUSSNode(blockMocks.ussNode.getParent(), blockMocks.testUSSTree, "file"); + expect(createApiMock).toHaveBeenCalledWith("/u/myuser/aDir/testFile.txt", "file"); + createApiMock.mockRestore(); + }); + it("Tests that createUSSNode does not execute if node name was not entered", async () => { const globalMocks = createGlobalMocks(); const blockMocks = await createBlockMocks(globalMocks); diff --git a/packages/zowe-explorer/src/uss/actions.ts b/packages/zowe-explorer/src/uss/actions.ts index dd87421699..47014435dd 100644 --- a/packages/zowe-explorer/src/uss/actions.ts +++ b/packages/zowe-explorer/src/uss/actions.ts @@ -64,13 +64,18 @@ export async function createUSSNode( } else { filePath = node.fullPath; } + + if (filePath == null || filePath?.length === 0) { + return; + } + const nameOptions: vscode.InputBoxOptions = { placeHolder: localize("createUSSNode.name", "Name of file or directory"), }; const name = await Gui.showInputBox(nameOptions); if (name && filePath) { try { - filePath = `${filePath}/${name}`; + filePath = path.posix.join(filePath, name); await ZoweExplorerApiRegister.getUssApi(node.getProfile()).create(filePath, nodeType); if (isTopLevel) { await refreshAll(ussFileProvider);