Skip to content

Commit 22bd44a

Browse files
Refactor to reuse escapeSingleQuotes (#4270)
1 parent 27ee26f commit 22bd44a

File tree

4 files changed

+10
-20
lines changed

4 files changed

+10
-20
lines changed

src/features/PesterTests.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,14 @@ export class PesterTestsFeature implements vscode.Disposable {
8484
outputPath?: string): vscode.DebugConfiguration {
8585

8686
const settings = getSettings();
87-
88-
// Since we pass the script path to PSES in single quotes to avoid issues with PowerShell
89-
// special chars like & $ @ () [], we do have to double up the interior single quotes.
90-
const scriptPath = fileUri.fsPath.replace(/'/g, "''");
91-
9287
const launchConfig = {
9388
request: "launch",
9489
type: "PowerShell",
9590
name: "PowerShell: Launch Pester Tests",
9691
script: this.invokePesterStubScriptPath,
9792
args: [
9893
"-ScriptPath",
99-
`'${scriptPath}'`,
94+
`'${utils.escapeSingleQuotes(fileUri.fsPath)}'`,
10095
],
10196
internalConsoleOptions: "neverOpen",
10297
noDebug: (launchType === LaunchType.Run),
@@ -106,12 +101,7 @@ export class PesterTestsFeature implements vscode.Disposable {
106101
if (lineNum) {
107102
launchConfig.args.push("-LineNumber", `${lineNum}`);
108103
} else if (testName) {
109-
// Escape single quotes inside double quotes by doubling them up
110-
if (testName.includes("'")) {
111-
testName = testName.replace(/'/g, "''");
112-
}
113-
114-
launchConfig.args.push("-TestName", `'${testName}'`);
104+
launchConfig.args.push("-TestName", `'${utils.escapeSingleQuotes(testName)}'`);
115105
} else {
116106
launchConfig.args.push("-All");
117107
}

src/process.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import utils = require("./utils");
1010
import { IEditorServicesSessionDetails } from "./session";
1111

1212
export class PowerShellProcess {
13-
public static escapeSingleQuotes(psPath: string): string {
14-
return psPath.replace(new RegExp("'", "g"), "''");
15-
}
16-
1713
// This is used to warn the user that the extension is taking longer than expected to startup.
1814
// After the 15th try we've hit 30 seconds and should warn.
1915
private static warnUserThreshold = 15;
@@ -51,8 +47,8 @@ export class PowerShellProcess {
5147
: "";
5248

5349
this.startPsesArgs +=
54-
`-LogPath '${PowerShellProcess.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` +
55-
`-SessionDetailsPath '${PowerShellProcess.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` +
50+
`-LogPath '${utils.escapeSingleQuotes(editorServicesLogPath.fsPath)}' ` +
51+
`-SessionDetailsPath '${utils.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` +
5652
`-FeatureFlags @(${featureFlags}) `;
5753

5854
if (this.sessionSettings.integratedConsole.useLegacyReadLine) {
@@ -78,7 +74,7 @@ export class PowerShellProcess {
7874
}
7975

8076
const startEditorServices = "Import-Module '" +
81-
PowerShellProcess.escapeSingleQuotes(psesModulePath) +
77+
utils.escapeSingleQuotes(psesModulePath) +
8278
"'; Start-EditorServices " + this.startPsesArgs;
8379

8480
// On Windows we unfortunately can't Base64 encode the startup command

src/session.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ export class SessionManager implements Middleware {
551551
"-HostProfileId 'Microsoft.VSCode' " +
552552
`-HostVersion '${this.HostVersion}' ` +
553553
"-AdditionalModules @('PowerShellEditorServices.VSCode') " +
554-
`-BundledModulesPath '${PowerShellProcess.escapeSingleQuotes(bundledModulesPath)}' ` +
554+
`-BundledModulesPath '${utils.escapeSingleQuotes(bundledModulesPath)}' ` +
555555
"-EnableConsoleRepl ";
556556

557557
if (this.sessionSettings.integratedConsole.suppressStartupBanner) {

src/utils.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import vscode = require("vscode");
77

88
export const PowerShellLanguageId = "powershell";
99

10+
export function escapeSingleQuotes(p: string): string {
11+
return p.replace(new RegExp("'", "g"), "''");
12+
}
13+
1014
export function getPipePath(pipeName: string) {
1115
if (os.platform() === "win32") {
1216
return "\\\\.\\pipe\\" + pipeName;

0 commit comments

Comments
 (0)