Skip to content

Commit

Permalink
Fix the bug that cannot parse test cases which contains double quotes (
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored Feb 10, 2019
1 parent e8da833 commit b7b8e50
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/commands/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { leetCodeExecutor } from "../leetCodeExecutor";
import { leetCodeManager } from "../leetCodeManager";
import { leetCodeResultProvider } from "../leetCodeResultProvider";
import { IQuickItemEx, UserStatus } from "../shared";
import { isWindows, usingCmd } from "../utils/osUtils";
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog } from "../utils/uiUtils";
import { getActiveFilePath } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";

export async function testSolution(uri?: vscode.Uri): Promise<void> {
try {
Expand Down Expand Up @@ -59,15 +61,15 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
ignoreFocusOut: true,
});
if (testString) {
result = await leetCodeExecutor.testSolution(filePath, testString);
result = await leetCodeExecutor.testSolution(filePath, parseTestString(testString));
}
break;
case ":file":
const testFile: vscode.Uri[] | undefined = await showFileSelectDialog();
if (testFile && testFile.length) {
const input: string = await fse.readFile(testFile[0].fsPath, "utf-8");
if (input.trim()) {
result = await leetCodeExecutor.testSolution(filePath, input.replace(/\r?\n/g, "\\n"));
const input: string = (await fse.readFile(testFile[0].fsPath, "utf-8")).trim();
if (input) {
result = await leetCodeExecutor.testSolution(filePath, parseTestString(input.replace(/\r?\n/g, "\\n")));
} else {
vscode.window.showErrorMessage("The selected test file must not be empty.");
}
Expand All @@ -84,3 +86,17 @@ export async function testSolution(uri?: vscode.Uri): Promise<void> {
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
}
}

function parseTestString(test: string): string {
if (wsl.useWsl() || !isWindows()) {
return `'${test}'`;
}

// In windows and not using WSL
if (usingCmd()) {
return `"${test.replace(/"/g, '\\"')}"`;
} else {
// Assume using PowerShell
return `'${test.replace(/"/g, '\\"')}'`;
}
}
2 changes: 1 addition & 1 deletion src/leetCodeExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LeetCodeExecutor {

public async testSolution(filePath: string, testString?: string): Promise<string> {
if (testString) {
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `'${testString}'`]);
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`, "-t", `${testString}`]);
}
return await this.executeCommandWithProgressEx("Submitting to LeetCode...", "node", [await this.getLeetCodeBinaryPath(), "test", `"${filePath}"`]);
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils/osUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.

export function isWindows(): boolean {
return process.platform === "win32";
}

export function usingCmd(): boolean {
const comSpec: string = process.env.ComSpec;
// 'cmd.exe' is used as a fallback if process.env.ComSpec is unavailable.
if (!comSpec) {
return true;
}

if (comSpec.indexOf("cmd.exe") > -1) {
return true;
}
return false;
}

0 comments on commit b7b8e50

Please sign in to comment.