Skip to content

Fix the bug that cannot parse test cases which contains double quotes #113

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
Feb 10, 2019
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
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;
}