From 2fbd02ea8830fdc04ed98ee0d3988aec5647d639 Mon Sep 17 00:00:00 2001 From: Trae Yelovich Date: Thu, 14 Nov 2024 15:49:47 -0500 Subject: [PATCH] fix: move 401 check from readFile into stat Signed-off-by: Trae Yelovich --- .../dataset/DatasetFSProvider.unit.test.ts | 27 ++++++------------- .../trees/uss/UssFSProvider.unit.test.ts | 16 +++++++++++ .../src/trees/dataset/DatasetFSProvider.ts | 19 ++++++------- .../src/trees/uss/UssFSProvider.ts | 14 ++++++++-- 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/dataset/DatasetFSProvider.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/dataset/DatasetFSProvider.unit.test.ts index ff18d56f18..03c712093e 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/dataset/DatasetFSProvider.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/dataset/DatasetFSProvider.unit.test.ts @@ -20,6 +20,7 @@ import { FsAbstractUtils, FsDatasetsUtils, Gui, + imperative, PdsEntry, ZoweExplorerApiType, ZoweScheme, @@ -27,6 +28,7 @@ import { import { MockedProperty } from "../../../__mocks__/mockUtils"; import { DatasetFSProvider } from "../../../../src/trees/dataset/DatasetFSProvider"; import { ZoweExplorerApiRegister } from "../../../../src/extending/ZoweExplorerApiRegister"; +import { AuthUtils } from "../../../../src/utils/AuthUtils"; const dayjs = require("dayjs"); const testProfile = createIProfile(); @@ -290,23 +292,6 @@ describe("fetchDatasetAtUri", () => { }); }); describe("readFile", () => { - it("throws an error if the entry does not have a profile", async () => { - const _lookupAsFileMock = jest - .spyOn(DatasetFSProvider.instance as any, "_lookupAsFile") - .mockReturnValueOnce({ ...testEntries.ps, metadata: { profile: null } }); - - let err; - try { - await DatasetFSProvider.instance.readFile(testUris.ps); - } catch (error) { - err = error; - expect(err.code).toBe("FileNotFound"); - } - expect(err).toBeDefined(); - expect(_lookupAsFileMock).toHaveBeenCalledWith(testUris.ps); - _lookupAsFileMock.mockRestore(); - }); - it("throws an error if the entry does not exist and the URI is actually a directory", async () => { const _lookupAsFileMock = jest.spyOn(DatasetFSProvider.instance as any, "_lookupAsFile").mockImplementationOnce((uri) => { throw FileSystemError.FileNotFound(uri as Uri); @@ -709,7 +694,6 @@ describe("stat", () => { lookupParentDirMock.mockRestore(); mvsApiMock.mockRestore(); }); - describe("error handling", () => { it("API response was unsuccessful for remote lookup", async () => { const lookupMock = jest.spyOn(DatasetFSProvider.instance as any, "lookup").mockReturnValue(testEntries.ps); @@ -719,12 +703,17 @@ describe("stat", () => { profileName: "sestest", profile: testEntries.ps.metadata.profile, }); - const exampleError = new Error("Response unsuccessful"); + const exampleError = new imperative.ImperativeError({ + msg: "Response unsuccessful", + errorCode: "401" + }); const dataSetMock = jest.fn().mockRejectedValue(exampleError); + const promptForAuthErrorSpy = jest.spyOn(AuthUtils, "promptForAuthError"); const mvsApiMock = jest.spyOn(ZoweExplorerApiRegister, "getMvsApi").mockReturnValue({ dataSet: dataSetMock, } as any); await expect(DatasetFSProvider.instance.stat(testUris.ps)).rejects.toThrow(); + expect(promptForAuthErrorSpy).toHaveBeenCalled(); mvsApiMock.mockRestore(); getInfoForUriMock.mockRestore(); lookupMock.mockRestore(); diff --git a/packages/zowe-explorer/__tests__/__unit__/trees/uss/UssFSProvider.unit.test.ts b/packages/zowe-explorer/__tests__/__unit__/trees/uss/UssFSProvider.unit.test.ts index 5cd2f0eaf3..3ee086e612 100644 --- a/packages/zowe-explorer/__tests__/__unit__/trees/uss/UssFSProvider.unit.test.ts +++ b/packages/zowe-explorer/__tests__/__unit__/trees/uss/UssFSProvider.unit.test.ts @@ -100,6 +100,21 @@ describe("stat", () => { await expect(UssFSProvider.instance.stat(testUris.file.with({ query: "inDiff=true" }))).resolves.toStrictEqual(testEntries.file); expect(lookupMock).toHaveBeenCalledWith(testUris.file, false); }); + it("throws an error if error encountered", async () => { + lookupMock.mockReturnValueOnce(testEntries.file); + const listFilesMock = jest.spyOn(UssFSProvider.instance, "listFiles").mockRejectedValueOnce( + new imperative.ImperativeError({ + msg: "", + errorCode: "401", + }) + ); + const promptForAuthErrorSpy = jest.spyOn(AuthUtils, "promptForAuthError"); + await expect(UssFSProvider.instance.stat(testUris.file)).rejects.toThrow(); + expect(promptForAuthErrorSpy).toHaveBeenCalled(); + expect(lookupMock).toHaveBeenCalledWith(testUris.file, false); + expect(listFilesMock).toHaveBeenCalled(); + listFilesMock.mockRestore(); + }); }); describe("move", () => { @@ -344,6 +359,7 @@ describe("fetchFileAtUri", () => { expect(autoDetectEncodingMock).toHaveBeenCalledWith(fileEntry); expect(_fireSoonSpy).not.toHaveBeenCalled(); autoDetectEncodingMock.mockRestore(); + lookupAsFileMock.mockRestore(); }); it("calls getContents to get the data for a file entry with encoding", async () => { const fileEntry = { ...testEntries.file }; diff --git a/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts b/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts index c15105bb33..a88e958a73 100644 --- a/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts +++ b/packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts @@ -102,8 +102,10 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem resp = await ZoweExplorerApiRegister.getMvsApi(uriInfo.profile).dataSet(path.parse(uri.path).name, { attributes: true }); } } catch (err) { - if (err instanceof Error) { + if (err instanceof imperative.ImperativeError) { ZoweLogger.error(err.message); + AuthUtils.promptForAuthError(err, entry.metadata.profile); + entry.wasAccessed = false; } throw err; } @@ -429,11 +431,15 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem if (options?.editor) { await this._updateResourceInEditor(uri); } + + if (!options?.isConflict && dsEntry) { + dsEntry.wasAccessed = true; + } return dsEntry; } catch (error) { //Response will error if the file is not found //Callers of fetchDatasetAtUri() do not expect it to throw an error - AuthUtils.promptForAuthError(error, metadata.profile); + dsEntry.wasAccessed = false; return null; } } @@ -474,9 +480,6 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem if (!ds || (!ds.wasAccessed && !urlQuery.has("inDiff")) || isConflict) { //try and fetch its contents from remote ds = (await this.fetchDatasetAtUri(uri, { isConflict })) as DsEntry; - if (!isConflict && ds) { - ds.wasAccessed = true; - } } if (FsAbstractUtils.isDirectoryEntry(ds)) { @@ -488,12 +491,6 @@ export class DatasetFSProvider extends BaseProvider implements vscode.FileSystem throw vscode.FileSystemError.FileNotFound(uri); } - const profInfo = this._getInfoFromUri(uri); - - if (profInfo.profile == null) { - throw vscode.FileSystemError.FileNotFound(vscode.l10n.t("Profile does not exist for this file.")); - } - return isConflict ? ds.conflictData.contents : ds.data; } diff --git a/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts b/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts index fdcf270245..a9ba455e3d 100644 --- a/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts +++ b/packages/zowe-explorer/src/trees/uss/UssFSProvider.ts @@ -84,7 +84,17 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv ZoweLogger.trace(`[UssFSProvider] stat is locating resource ${uri.toString()}`); - const fileResp = await this.listFiles(entry.metadata.profile, uri, true); + let fileResp: IZosFilesResponse; + try { + fileResp = await this.listFiles(entry.metadata.profile, uri, true); + } catch (err) { + if (err instanceof imperative.ImperativeError) { + AuthUtils.promptForAuthError(err, entry.metadata.profile); + entry.wasAccessed = false; + } + throw err; + } + if (fileResp.success) { // Regardless of the resource type, it will be the first item in a successful response. // When listing a folder, the folder's stats will be represented as the "." entry. @@ -295,7 +305,7 @@ export class UssFSProvider extends BaseProvider implements vscode.FileSystemProv if (err instanceof Error) { ZoweLogger.error(err.message); } - AuthUtils.promptForAuthError(err, metadata.profile); + file.wasAccessed = false; return; }