From 5e0550908a7f274743043eb7a881fa40027afece Mon Sep 17 00:00:00 2001 From: Andy Jordan Date: Thu, 17 Nov 2022 14:00:57 -0800 Subject: [PATCH] Refactor to reuse `escapeSingleQuotes` --- src/features/PesterTests.ts | 14 ++------------ src/process.ts | 10 +++------- src/session.ts | 2 +- src/utils.ts | 4 ++++ 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/features/PesterTests.ts b/src/features/PesterTests.ts index 4e4c55bda8..27f474d609 100644 --- a/src/features/PesterTests.ts +++ b/src/features/PesterTests.ts @@ -84,11 +84,6 @@ export class PesterTestsFeature implements vscode.Disposable { outputPath?: string): vscode.DebugConfiguration { const settings = getSettings(); - - // Since we pass the script path to PSES in single quotes to avoid issues with PowerShell - // special chars like & $ @ () [], we do have to double up the interior single quotes. - const scriptPath = fileUri.fsPath.replace(/'/g, "''"); - const launchConfig = { request: "launch", type: "PowerShell", @@ -96,7 +91,7 @@ export class PesterTestsFeature implements vscode.Disposable { script: this.invokePesterStubScriptPath, args: [ "-ScriptPath", - `'${scriptPath}'`, + `'${utils.escapeSingleQuotes(fileUri.fsPath)}'`, ], internalConsoleOptions: "neverOpen", noDebug: (launchType === LaunchType.Run), @@ -106,12 +101,7 @@ export class PesterTestsFeature implements vscode.Disposable { if (lineNum) { launchConfig.args.push("-LineNumber", `${lineNum}`); } else if (testName) { - // Escape single quotes inside double quotes by doubling them up - if (testName.includes("'")) { - testName = testName.replace(/'/g, "''"); - } - - launchConfig.args.push("-TestName", `'${testName}'`); + launchConfig.args.push("-TestName", `'${utils.escapeSingleQuotes(testName)}'`); } else { launchConfig.args.push("-All"); } diff --git a/src/process.ts b/src/process.ts index 5165f6160d..7c113260aa 100644 --- a/src/process.ts +++ b/src/process.ts @@ -10,10 +10,6 @@ import utils = require("./utils"); import { IEditorServicesSessionDetails } from "./session"; export class PowerShellProcess { - public static escapeSingleQuotes(psPath: string): string { - return psPath.replace(new RegExp("'", "g"), "''"); - } - // This is used to warn the user that the extension is taking longer than expected to startup. // After the 15th try we've hit 30 seconds and should warn. private static warnUserThreshold = 15; @@ -51,8 +47,8 @@ export class PowerShellProcess { : ""; this.startPsesArgs += - `-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` + - `-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` + + `-LogPath '${utils.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` + + `-SessionDetailsPath '${utils.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` + `-FeatureFlags @(${featureFlags}) `; if (this.sessionSettings.integratedConsole.useLegacyReadLine) { @@ -78,7 +74,7 @@ export class PowerShellProcess { } const startEditorServices = "Import-Module '" + - PowerShellProcess.escapeSingleQuotes(psesModulePath) + + utils.escapeSingleQuotes(psesModulePath) + "'; Start-EditorServices " + this.startPsesArgs; // On Windows we unfortunately can't Base64 encode the startup command diff --git a/src/session.ts b/src/session.ts index 1f1b05efa1..5eddf147a6 100644 --- a/src/session.ts +++ b/src/session.ts @@ -551,7 +551,7 @@ export class SessionManager implements Middleware { "-HostProfileId 'Microsoft.VSCode' " + `-HostVersion '${this.HostVersion}' ` + "-AdditionalModules @('PowerShellEditorServices.VSCode') " + - `-BundledModulesPath '${PowerShellProcess.escapeSingleQuotes(bundledModulesPath)}' ` + + `-BundledModulesPath '${utils.escapeSingleQuotes(bundledModulesPath)}' ` + "-EnableConsoleRepl "; if (this.sessionSettings.integratedConsole.suppressStartupBanner) { diff --git a/src/utils.ts b/src/utils.ts index 3ca7035ea6..881b5b26c4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -7,6 +7,10 @@ import vscode = require("vscode"); export const PowerShellLanguageId = "powershell"; +export function escapeSingleQuotes(p: string): string { + return p.replace(new RegExp("'", "g"), "''"); +} + export function getPipePath(pipeName: string) { if (os.platform() === "win32") { return "\\\\.\\pipe\\" + pipeName;