Skip to content

Commit b7b8e50

Browse files
authored
Fix the bug that cannot parse test cases which contains double quotes (#113)
1 parent e8da833 commit b7b8e50

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/commands/test.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import { leetCodeExecutor } from "../leetCodeExecutor";
77
import { leetCodeManager } from "../leetCodeManager";
88
import { leetCodeResultProvider } from "../leetCodeResultProvider";
99
import { IQuickItemEx, UserStatus } from "../shared";
10+
import { isWindows, usingCmd } from "../utils/osUtils";
1011
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog } from "../utils/uiUtils";
1112
import { getActiveFilePath } from "../utils/workspaceUtils";
13+
import * as wsl from "../utils/wslUtils";
1214

1315
export async function testSolution(uri?: vscode.Uri): Promise<void> {
1416
try {
@@ -59,15 +61,15 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
5961
ignoreFocusOut: true,
6062
});
6163
if (testString) {
62-
result = await leetCodeExecutor.testSolution(filePath, testString);
64+
result = await leetCodeExecutor.testSolution(filePath, parseTestString(testString));
6365
}
6466
break;
6567
case ":file":
6668
const testFile: vscode.Uri[] | undefined = await showFileSelectDialog();
6769
if (testFile && testFile.length) {
68-
const input: string = await fse.readFile(testFile[0].fsPath, "utf-8");
69-
if (input.trim()) {
70-
result = await leetCodeExecutor.testSolution(filePath, input.replace(/\r?\n/g, "\\n"));
70+
const input: string = (await fse.readFile(testFile[0].fsPath, "utf-8")).trim();
71+
if (input) {
72+
result = await leetCodeExecutor.testSolution(filePath, parseTestString(input.replace(/\r?\n/g, "\\n")));
7173
} else {
7274
vscode.window.showErrorMessage("The selected test file must not be empty.");
7375
}
@@ -84,3 +86,17 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
8486
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
8587
}
8688
}
89+
90+
function parseTestString(test: string): string {
91+
if (wsl.useWsl() || !isWindows()) {
92+
return `'${test}'`;
93+
}
94+
95+
// In windows and not using WSL
96+
if (usingCmd()) {
97+
return `"${test.replace(/"/g, '\\"')}"`;
98+
} else {
99+
// Assume using PowerShell
100+
return `'${test.replace(/"/g, '\\"')}'`;
101+
}
102+
}

src/leetCodeExecutor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class LeetCodeExecutor {
9696

9797
public async testSolution(filePath: string, testString?: string): Promise<string> {
9898
if (testString) {
99-
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `'${testString}'`]);
99+
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `${testString}`]);
100100
}
101101
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]);
102102
}

src/utils/osUtils.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) jdneo. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
export function isWindows(): boolean {
5+
return process.platform === "win32";
6+
}
7+
8+
export function usingCmd(): boolean {
9+
const comSpec: string = process.env.ComSpec;
10+
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
11+
if (!comSpec) {
12+
return true;
13+
}
14+
15+
if (comSpec.indexOf("cmd.exe") > -1) {
16+
return true;
17+
}
18+
return false;
19+
}

0 commit comments

Comments
 (0)