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

Change format of requests to show module sources #12351

Merged
merged 8 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add vscode tests
  • Loading branch information
Stephen Weatherford authored and Stephen Weatherford committed Nov 4, 2023
commit 53dbb4a9ca813607901a79a4f82abb474ba514a1
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import * as vscode from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { Disposable } from "../utils/disposable";
import { bicepExternalSourceRequestType } from "./protocol";
import * as path from "path";
import { Uri } from "vscode";

export const BicepExternalSourceScheme = "bicep-extsrc";
type ExternalSource = {
// The title to display for the document,
// e.g. "br:myregistry.azurecr.io/myrepo/module/main.json:v1/main.json (module:v1)" or similar
// VSCode will display everything after the last slash in the document's tab, and the full string
// on hover.
title: string;
// Full module reference, e.g. "myregistry.azurecr.io/myrepo/module:v1"
moduleReference: string;
// File being requested from the source, relative to the module root.
// e.g. main.bicep or mypath/module.bicep
// This should be undefined to request the compiled JSON file (can't use "main.json" because there
// might actually be a source file called "main.json" in the original module sources).
requestedSourceFile?: string;
};
import {
BicepExternalSourceScheme,
decodeExternalSourceUri,
} from "./decodeExternalSourceUri";

export class BicepExternalSourceContentProvider
extends Disposable
Expand Down Expand Up @@ -60,33 +49,15 @@ export class BicepExternalSourceContentProvider
}

private bicepExternalSourceRequest(uri: vscode.Uri) {
const { moduleReference } = this.decodeExternalSourceUri(uri);
const { moduleReference } = decodeExternalSourceUri(uri);
return {
target: moduleReference,
};
}

// NOTE: This should match the logic in BicepExternalSourceRequestHandler.GetExternalSourceLinkUri and
// also bicep\src\Bicep.LangServer.UnitTests\BicepExternalSourceRequestHandlerTests.cs.DecodeExternalSourceUri
private decodeExternalSourceUri(uri: vscode.Uri): ExternalSource {
// The uri passed in has this format (encoded):
// bicep-extsrc:{title}?{module-reference}[#{source-file-relative-path}]
const title = decodeURIComponent(uri.path);
const moduleReference = decodeURIComponent(uri.query);
let requestedSourceFile: string | undefined = decodeURIComponent(
uri.fragment,
);

if (requestedSourceFile === "") {
requestedSourceFile = undefined;
}

return { title, moduleReference, requestedSourceFile };
}

private getModuleReferenceScheme(uri: Uri): "br" | "ts" {
// e.g. 'br:registry.azurecr.io/module:v3' => 'br'
const { moduleReference } = this.decodeExternalSourceUri(uri);
const { moduleReference } = decodeExternalSourceUri(uri);

const colonIndex = moduleReference.indexOf(":");
if (colonIndex >= 0) {
Expand All @@ -110,9 +81,7 @@ export class BicepExternalSourceContentProvider
// we should try to correct it

const scheme = this.getModuleReferenceScheme(document.uri);
const { requestedSourceFile } = this.decodeExternalSourceUri(
document.uri,
);
const { requestedSourceFile } = decodeExternalSourceUri(document.uri);

// Not necessary to wait for this to finish
void vscode.languages.setTextDocumentLanguage(
Expand Down
38 changes: 38 additions & 0 deletions src/vscode-bicep/src/language/decodeExternalSourceUri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as vscode from "vscode";

export const BicepExternalSourceScheme = "bicep-extsrc";

export type ExternalSource = {
// The title to display for the document,
// e.g. "br:myregistry.azurecr.io/myrepo/module/main.json:v1/main.json (module:v1)" or similar
// VSCode will display everything after the last slash in the document's tab, and the full string
// on hover.
title: string;
// Full module reference, e.g. "myregistry.azurecr.io/myrepo/module:v1"
moduleReference: string;
// File being requested from the source, relative to the module root.
// e.g. main.bicep or mypath/module.bicep
// This should be undefined to request the compiled JSON file (can't use "main.json" because there
// might actually be a source file called "main.json" in the original module sources).
requestedSourceFile?: string;
};

// NOTE: This should match the logic in BicepExternalSourceRequestHandler.GetExternalSourceLinkUri and
// also bicep\src\Bicep.LangServer.UnitTests\BicepExternalSourceRequestHandlerTests.cs.DecodeExternalSourceUri
export function decodeExternalSourceUri(uri: vscode.Uri): ExternalSource {
// The uri passed in has this format (encoded):
// bicep-extsrc:{title}?{module-reference}[#{source-file-relative-path}]
const title = decodeURIComponent(uri.path);
const moduleReference = decodeURIComponent(uri.query);
let requestedSourceFile: string | undefined = decodeURIComponent(
uri.fragment,
);

if (requestedSourceFile === "") {
requestedSourceFile = undefined;
}

return { title, moduleReference, requestedSourceFile };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { Uri } from "vscode";
import { decodeExternalSourceUri } from "../../language/decodeExternalSourceUri";

describe("bicepExternalSourceContentProvider", () => {
it("decodes correctly with source", () => {
const uri =
"bicep-extsrc:br%3Asaw.azurecr.io/complicated%3Av1.0-beta/my%20entrypoint.bicep%20%28complicated%3Av1.0-beta%29?br%3Asaw.azurecr.io%2Fcomplicated%3Av1.0-beta#my%20entrypoint.bicep";
const decoded = decodeExternalSourceUri(Uri.parse(uri));

expect(decoded.moduleReference).toBe(
"br:saw.azurecr.io/complicated:1.0-beta",
);
expect(decoded.requestedSourceFile).toBe("entrypoint.bicep");
expect(decoded.title).toBe(
"br:saw.azurecr.com/complicated:1.0-beta/entrypoint.bicep (complicated:1.0.2)",
);
});

it("decodes correctly without source", () => {
const uri =
"bicep-extsrc:br%3Amcr.microsoft.com/bicep/ai/bing-resource%3A1.0.2/main.json%20%28bing-resource%3A1.0.2%29?br%3Amcr.microsoft.com%2Fbicep%2Fai%2Fbing-resource%3A1.0.2";
const decoded = decodeExternalSourceUri(Uri.parse(uri));

expect(decoded.moduleReference).toBe(
"br:mcr.microsoft.com/bicep/ai/bing-resource:1.0.2",
);
expect(decoded.requestedSourceFile).toBeUndefined();
expect(decoded.title).toBe(
"br:mcr.microsoft.com/bicep/ai/bing-resource:1.0.2/main.json (bing-resource:1.0.2)",
);
});
});
62 changes: 33 additions & 29 deletions src/vscode-bicep/src/test/unit/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,65 @@ jest.mock(
WorkspaceFolder: 3,
},
languages: {
createDiagnosticCollection: jest.fn(),
registerCodeLensProvider: jest.fn(),
createDiagnosticCollection: jest.fn(() => throwNYI()),
registerCodeLensProvider: jest.fn(() => throwNYI()),
},
ProgressLocation: {
Notification: 15,
SourceControl: 1,
Window: 10,
},
StatusBarAlignment: { Left: 1, Right: 2 },
ThemeColor: jest.fn(),
ThemeIcon: jest.fn(),
ThemeColor: jest.fn(() => throwNYI()),
ThemeIcon: jest.fn(() => throwNYI()),
window: {
createStatusBarItem: jest.fn(() => ({
show: jest.fn(),
tooltip: jest.fn(),
show: jest.fn(() => throwNYI()),
tooltip: jest.fn(() => throwNYI()),
})),
showErrorMessage: jest.fn(),
showWarningMessage: jest.fn(),
createTextEditorDecorationType: jest.fn(),
createOutputChannel: jest.fn(),
showWorkspaceFolderPick: jest.fn(),
onDidChangeActiveTextEditor: jest.fn(),
showInformationMessage: jest.fn(),
showErrorMessage: jest.fn(() => throwNYI()),
showWarningMessage: jest.fn(() => throwNYI()),
createTextEditorDecorationType: jest.fn(() => throwNYI()),
createOutputChannel: jest.fn(() => throwNYI()),
showWorkspaceFolderPick: jest.fn(() => throwNYI()),
onDidChangeActiveTextEditor: jest.fn(() => throwNYI()),
showInformationMessage: jest.fn(() => throwNYI()),
},
workspace: {
getConfiguration: jest.fn(),
getConfiguration: jest.fn(() => throwNYI()),
workspaceFolders: [],
getWorkspaceFolder: jest.fn(),
onDidChangeConfiguration: jest.fn(),
onDidChangeTextDocument: jest.fn(),
onDidChangeWorkspaceFolders: jest.fn(),
getWorkspaceFolder: jest.fn(() => throwNYI()),
onDidChangeConfiguration: jest.fn(() => throwNYI()),
onDidChangeTextDocument: jest.fn(() => throwNYI()),
onDidChangeWorkspaceFolders: jest.fn(() => throwNYI()),
},
OverviewRulerLane: {
Left: null,
},
Uri: {
file: jest.fn(),
parse: jest.fn(),
file: jest.fn(() => throwNYI()),
parse: jest.fn(() => throwNYI()),
},
Range: jest.fn(),
Diagnostic: jest.fn(),
Range: jest.fn(() => throwNYI()),
Diagnostic: jest.fn(() => throwNYI()),
DiagnosticSeverity: { Error: 0, Warning: 1, Information: 2, Hint: 3 },
debug: {
onDidTerminateDebugSession: jest.fn(),
startDebugging: jest.fn(),
registerDebugConfigurationProvider: jest.fn(),
onDidTerminateDebugSession: jest.fn(() => throwNYI()),
startDebugging: jest.fn(() => throwNYI()),
registerDebugConfigurationProvider: jest.fn(() => throwNYI()),
},
commands: {
executeCommand: jest.fn(),
registerCommand: jest.fn(),
executeCommand: jest.fn(() => throwNYI()),
registerCommand: jest.fn(() => throwNYI()),
},
CodeLen: jest.fn(),
CodeLen: jest.fn(() => throwNYI()),
l10n: {
t: jest.fn(),
t: jest.fn(() => throwNYI()),
},
}),
{ virtual: true },
);

function throwNYI(): void {
throw new Error("Mock not implemented.");
}
Loading