Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move 401 check from readFile into stat #3320

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
FsAbstractUtils,
FsDatasetsUtils,
Gui,
imperative,
PdsEntry,
ZoweExplorerApiType,
ZoweScheme,
} from "@zowe/zowe-explorer-api";
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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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 };
Expand Down
20 changes: 9 additions & 11 deletions packages/zowe-explorer/src/trees/dataset/DatasetFSProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
UriFsInfo,
FileEntry,
ZoweExplorerApiType,
imperative,
} from "@zowe/zowe-explorer-api";
import { IZosFilesResponse } from "@zowe/zos-files-for-zowe-sdk";
import { Profiles } from "../../configuration/Profiles";
Expand Down Expand Up @@ -102,8 +103,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;
}
Expand Down Expand Up @@ -429,11 +432,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;
}
}
Expand Down Expand Up @@ -474,9 +481,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)) {
Expand All @@ -488,12 +492,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;
}

Expand Down
14 changes: 12 additions & 2 deletions packages/zowe-explorer/src/trees/uss/UssFSProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
Loading