From d76cb45ed276e398d74e9800f8d2ac2cb95606e2 Mon Sep 17 00:00:00 2001 From: Chunan Ye Date: Mon, 18 Sep 2023 13:59:38 -0700 Subject: [PATCH 1/3] Fix tests. Add most important info to interface --- utils/index.d.ts | 3 + utils/src/AzExtTreeFileSystem.ts | 6 +- utils/test/AzExtTreeFileSystem.test.ts | 80 ++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 utils/test/AzExtTreeFileSystem.test.ts diff --git a/utils/index.d.ts b/utils/index.d.ts index 631b6ccd45..05f312ca42 100644 --- a/utils/index.d.ts +++ b/utils/index.d.ts @@ -1450,6 +1450,9 @@ export type AzExtItemUriParts = { }; export interface AzExtTreeFileSystemItem { + /** + * Warning: the identifier cannot contain plus sign '+'. No matter if it's exactly '+' or if it's URL encoded "%2B". + */ id: string; refresh?(context: IActionContext): Promise; } diff --git a/utils/src/AzExtTreeFileSystem.ts b/utils/src/AzExtTreeFileSystem.ts index 0bd5b18edd..cec61beea7 100644 --- a/utils/src/AzExtTreeFileSystem.ts +++ b/utils/src/AzExtTreeFileSystem.ts @@ -3,8 +3,8 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { parse as parseQuery, ParsedUrlQuery, stringify as stringifyQuery } from "querystring"; -import { Disposable, Event, EventEmitter, FileChangeEvent, FileStat, FileSystemError, FileSystemProvider, FileType, l10n, TextDocumentShowOptions, Uri, window } from "vscode"; +import { ParsedUrlQuery, parse as parseQuery, stringify as stringifyQuery } from "querystring"; +import { Disposable, Event, EventEmitter, FileChangeEvent, FileStat, FileSystemError, FileSystemProvider, FileType, TextDocumentShowOptions, Uri, l10n, window } from "vscode"; import * as types from '../index'; import { callWithTelemetryAndErrorHandling } from "./callWithTelemetryAndErrorHandling"; import { nonNullProp } from "./utils/nonNull"; @@ -127,7 +127,7 @@ export abstract class AzExtTreeFileSystem { + protected async lookup(context: types.IActionContext, uri: Uri): Promise { const item: TItem | undefined = this.findItem(this.getQueryFromUri(uri)); if (!item) { context.telemetry.suppressAll = true; diff --git a/utils/test/AzExtTreeFileSystem.test.ts b/utils/test/AzExtTreeFileSystem.test.ts new file mode 100644 index 0000000000..dfa34afb5c --- /dev/null +++ b/utils/test/AzExtTreeFileSystem.test.ts @@ -0,0 +1,80 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { TestUserInput } from "@microsoft/vscode-azext-dev"; +import * as vscode from 'vscode'; +import { IActionContext } from ".."; +import { AzExtTreeFileSystem } from "../src"; +import assert = require("assert"); + +type MockItemType = { id: string }; + +const mockFilePath = "mock_file_path"; + +class MockAzExtTreeFileSystem extends AzExtTreeFileSystem { + public override scheme = "mockAzExtTreeFileSystem"; + + public override async statImpl(_context: IActionContext, _item: MockItemType, _originalUri): Promise { + throw Error("not implemented"); + } + + public override async readFileImpl(_context: IActionContext, _item: MockItemType, _originalUri): Promise { + throw Error("not implemented"); + } + + public override async writeFileImpl(_context: IActionContext, _item: MockItemType) { + return; + } + + public override getFilePath(_item: MockItemType): string { + return mockFilePath; + } + + public async lookupPublic(context: IActionContext, uri: vscode.Uri) { + return this.lookup(context, uri); + } +} + +suite("AzExtTreeFileSystem", function () { + const mockContext: IActionContext = { errorHandling: { issueProperties: {} }, telemetry: { measurements: {}, properties: {} }, ui: new TestUserInput(vscode), valuesToMask: [] }; + test("lookup doesn't find item not shown", async function () { + const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); + const id = "non_existent"; + const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); + try { + await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); + assert(false, "should have thrown entry not found error"); + } catch (error) { + assert.equal(error.name, "EntryNotFound (FileSystemError)"); + } + }); + + test("lookup finds shown item whose id is a valid query parameter", async function () { + const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); + const id = "mock_id"; + mockAzExtTreeFileSystem.showTextDocument({ id: id }); + const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); + const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); + assert.equal(item?.id, id); + }); + + test("lookup finds shown item whose id contains ?", async function () { + const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); + const id = "ZTA?AMGB????oaUBAAAAAAAAAA???"; + mockAzExtTreeFileSystem.showTextDocument({ id: id }); + const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); + const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); + assert.equal(item?.id, id); + }); + + test("lookup finds shown item whose id contains =", async function () { + const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); + const id = "ZTA=AMGBoaUB===AAAAAAAAAA=="; + mockAzExtTreeFileSystem.showTextDocument({ id: id }); + const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); + const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); + assert.equal(item?.id, id); + }); +}); From be1b953610e84629b0036c3b324aab7417febc4c Mon Sep 17 00:00:00 2001 From: Chunan Ye Date: Mon, 18 Sep 2023 14:14:59 -0700 Subject: [PATCH 2/3] Fix lint --- utils/test/AzExtTreeFileSystem.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/test/AzExtTreeFileSystem.test.ts b/utils/test/AzExtTreeFileSystem.test.ts index dfa34afb5c..5efe747968 100644 --- a/utils/test/AzExtTreeFileSystem.test.ts +++ b/utils/test/AzExtTreeFileSystem.test.ts @@ -54,7 +54,7 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id is a valid query parameter", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "mock_id"; - mockAzExtTreeFileSystem.showTextDocument({ id: id }); + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id); @@ -63,7 +63,7 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id contains ?", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "ZTA?AMGB????oaUBAAAAAAAAAA???"; - mockAzExtTreeFileSystem.showTextDocument({ id: id }); + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id); @@ -72,7 +72,7 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id contains =", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "ZTA=AMGBoaUB===AAAAAAAAAA=="; - mockAzExtTreeFileSystem.showTextDocument({ id: id }); + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id); From e18ca066907685767faaa461b353493e7d271547 Mon Sep 17 00:00:00 2001 From: Chunan Ye Date: Mon, 18 Sep 2023 14:19:13 -0700 Subject: [PATCH 3/3] Fix test --- utils/test/AzExtTreeFileSystem.test.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/utils/test/AzExtTreeFileSystem.test.ts b/utils/test/AzExtTreeFileSystem.test.ts index 5efe747968..85400af8dd 100644 --- a/utils/test/AzExtTreeFileSystem.test.ts +++ b/utils/test/AzExtTreeFileSystem.test.ts @@ -54,7 +54,11 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id is a valid query parameter", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "mock_id"; - await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + try { + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + } catch (error) { + // Ignore error. Only used showTextDocument to add the document to itemsCache. + } const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id); @@ -63,7 +67,11 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id contains ?", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "ZTA?AMGB????oaUBAAAAAAAAAA???"; - await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + try { + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + } catch (error) { + // Ignore error. Only used showTextDocument to add the document to itemsCache. + } const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id); @@ -72,7 +80,11 @@ suite("AzExtTreeFileSystem", function () { test("lookup finds shown item whose id contains =", async function () { const mockAzExtTreeFileSystem = new MockAzExtTreeFileSystem(); const id = "ZTA=AMGBoaUB===AAAAAAAAAA=="; - await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + try { + await mockAzExtTreeFileSystem.showTextDocument({ id: id }); + } catch (error) { + // Ignore error. Only used showTextDocument to add the document to itemsCache. + } const uri: vscode.Uri = vscode.Uri.parse(`${mockAzExtTreeFileSystem.scheme}://mock_file_path?id=${id}`); const item = await mockAzExtTreeFileSystem.lookupPublic(mockContext, uri); assert.equal(item?.id, id);