From b7b8e501fe7a5ae9390b5fe74eb46225bf7064fb Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Sun, 10 Feb 2019 20:09:08 +0800 Subject: [PATCH] Fix the bug that cannot parse test cases which contains double quotes (#113) --- src/commands/test.ts | 24 ++++++++++++++++++++---- src/leetCodeExecutor.ts | 2 +- src/utils/osUtils.ts | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/utils/osUtils.ts diff --git a/src/commands/test.ts b/src/commands/test.ts index 565c1c1a..b929d9c2 100644 --- a/src/commands/test.ts +++ b/src/commands/test.ts @@ -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 { try { @@ -59,15 +61,15 @@ export async function testSolution(uri?: vscode.Uri): Promise { 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."); } @@ -84,3 +86,17 @@ export async function testSolution(uri?: vscode.Uri): Promise { 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, '\\"')}'`; + } +} diff --git a/src/leetCodeExecutor.ts b/src/leetCodeExecutor.ts index d4cdfea0..b43b02bc 100644 --- a/src/leetCodeExecutor.ts +++ b/src/leetCodeExecutor.ts @@ -96,7 +96,7 @@ class LeetCodeExecutor { public async testSolution(filePath: string, testString?: string): Promise { 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}"`]); } diff --git a/src/utils/osUtils.ts b/src/utils/osUtils.ts new file mode 100644 index 00000000..c2a9c237 --- /dev/null +++ b/src/utils/osUtils.ts @@ -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; +}