Skip to content

Refactor to reuse escapeSingleQuotes #4270

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

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 2 additions & 12 deletions src/features/PesterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,14 @@ 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",
name: "PowerShell: Launch Pester Tests",
script: this.invokePesterStubScriptPath,
args: [
"-ScriptPath",
`'${scriptPath}'`,
`'${utils.escapeSingleQuotes(fileUri.fsPath)}'`,
],
internalConsoleOptions: "neverOpen",
noDebug: (launchType === LaunchType.Run),
Expand All @@ -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");
}
Expand Down
10 changes: 3 additions & 7 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down