Skip to content

Commit

Permalink
Add support for ${userHome}
Browse files Browse the repository at this point in the history
  • Loading branch information
keraion committed Apr 28, 2024
1 parent b4ea2c4 commit b49d5f6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/features/helper/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ export default class Configuration {
const fileName = editor ? Utilities.normalizePath(editor.document.fileName) : undefined;

const vars: Variables = {
// - the path of the user's home folder
userHome: process.env.HOME || process.env.USERPROFILE,

// - the path of the folder opened in VS Code
workspaceFolder: rootPath,

Expand Down
1 change: 1 addition & 0 deletions src/features/helper/types/variables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type Variables = {
userHome?: string;
fileWorkspaceFolder?: string;
workspaceFolder?: string;
workspaceFolderBasename?: string;
Expand Down
2 changes: 1 addition & 1 deletion src/features/helper/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Utilities {
}

static interpolateString(command: string, variables: Variables): string {
const regex = /\$\{([^\}]+)\}/g; // eslint-disable-line no-useless-escape
const regex = /\$\{([^}]+)\}/g;

const match = command.match(regex);
while (match?.length) {
Expand Down
5 changes: 1 addition & 4 deletions test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ suite("Extension Test Suite", () => {

test("Bad SQL has zero diagnostics after document format", async () => {
const documentUri = Helper.getDocumentUri("/test_sql/format.sql");
const document = await Helper.activate(documentUri);
await Helper.activate(documentUri);
const preFormatDiagnostics = vscode.languages.getDiagnostics(documentUri);
assert.strictEqual(preFormatDiagnostics.length, 1, "Pre-format diagnostics not expected length");

await Helper.format(documentUri);
await Helper.activate(documentUri);

const postFormatDiagnostics = vscode.languages.getDiagnostics(documentUri);
assert.strictEqual(postFormatDiagnostics.length, 0, "Post-format diagnostics not expected length");

Expand Down
7 changes: 1 addition & 6 deletions test/suite/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from "assert";
import * as vscode from "vscode";

export const SLEEP_TIME = 10000;
export const SLEEP_TIME = 2000;

export const activate = async (documentUri: vscode.Uri): Promise<vscode.TextDocument | undefined> => {
// The extensionId is `publisher.name` from package.json
Expand All @@ -13,9 +13,7 @@ export const activate = async (documentUri: vscode.Uri): Promise<vscode.TextDocu
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);
await document.save();

await sleep(SLEEP_TIME); // Wait for server activation

return document;
} catch (e) {
console.error(e);
Expand All @@ -28,11 +26,8 @@ export const format = async (documentUri: vscode.Uri) => {
const document = await vscode.workspace.openTextDocument(documentUri);
await vscode.window.showTextDocument(document);
await vscode.commands.executeCommand("editor.action.formatDocument");
await sleep(SLEEP_TIME);
await document.save();

await sleep(SLEEP_TIME); // Wait for server activation

return document;
} catch (e) {
console.error(e);
Expand Down
25 changes: 25 additions & 0 deletions test/suite/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as assert from "assert";

import Configuration from "../../src/features/helper/configuration";
import Variables from "../../src/features/helper/types/variables";
import Utilities from "../../src/features/helper/utilities";

suite("Utilities Function Test Suite", () => {
const variables = Configuration.variables();
const variablesToCheck = [
"userHome",
"workspaceFolder",
"workspaceFolderBasename",
"execPath",
];
variablesToCheck.forEach((variable) => {
test(`${variable} resolves in interpolation`, function () {
const interpolatedString = Utilities.interpolateString(
`\${${variable}}`,
variables,
);
const expectedValue = variables[variable as keyof Variables];
assert.strictEqual(interpolatedString, expectedValue);
});
});
});

0 comments on commit b49d5f6

Please sign in to comment.