From 6fce88f88a051aca01e96bba0166adb2d4d85e26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=8D=9A=E5=87=AF?= Date: Sat, 14 Dec 2024 22:00:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=9C=80=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E7=9A=84=20Windows=20<->=20Unix=20=E9=A3=8E=E6=A0=BC=E4=BA=92?= =?UTF-8?q?=E8=BD=AC=E9=80=BB=E8=BE=91=EF=BC=9B=E6=B7=BB=E5=8A=A0=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E5=B9=B6=E9=80=9A?= =?UTF-8?q?=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/path-convert/conversion.ts | 58 +++++++++++- src/test/extension.test.ts | 4 +- src/test/test-case/path-convert-test-case.ts | 97 ++++++++++++++++++++ src/test/test-case/types/PathTestCaseType.ts | 10 +- 4 files changed, 163 insertions(+), 6 deletions(-) diff --git a/src/core/path-convert/conversion.ts b/src/core/path-convert/conversion.ts index 33f986a..b5b6c79 100644 --- a/src/core/path-convert/conversion.ts +++ b/src/core/path-convert/conversion.ts @@ -1,6 +1,60 @@ import { SupportPathFormat } from "./types/SupportPathFormatType"; +/** / */ +const LEFT_SLASH = '/'; +/** \ */ +const RIGHT_SLASH = '\\'; +/** \\ */ +const DOUBLE_RIGHT_SLASH = '\\\\'; -export function pathConversion(SupportPathFormat: SupportPathFormat, input: string) { - // TODO +export function pathConversion(targetPathType: SupportPathFormat, input: string): string { + let resultPath; + + let isSeperator = false; + switch (targetPathType) { + case SupportPathFormat.Windows: + // 将其中的 / 替换为 \ + resultPath = Array.from(input).map((char: string) => { + if (char !== LEFT_SLASH) { + // 当前字符不是 / + isSeperator = false; + return char; + } else { + // 当前字符是 / + if (!isSeperator) { + // 上一字符不是 / + isSeperator = true; + return RIGHT_SLASH; // 替换成 \ + } + // 上一字符是 / + return ''; + } + }).join(''); + break; + case SupportPathFormat.Unix: + // 将其中的 \\ 和 \ 替换为 / + resultPath = Array.from(input).map((char: string) => { + if (char !== RIGHT_SLASH) { + // 当前字符不是 \ + isSeperator = false; + return char; + } else { + // 当前字符是 \ + if (!isSeperator) { + // 上一字符不是 \ + isSeperator = true; + return LEFT_SLASH; // 替换成 / + } + // 上一字符是 \ + return ''; + } + }).join(''); + break; + // case SupportPathFormat.WindowsGitBash: + // break; + default: + return input; + } + + return resultPath; } diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 29d37fb..1e2fc19 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -116,8 +116,8 @@ suite('Extension Test: run path convert test case', () => { for (const input of inputList) { // console.log('input', '->' + input + '<-'); // 验证转换 - assert.strictEqual(testCase.output.Windows, pathConversion(SupportPathFormat.Windows, input), 'Windows path format test failed.'); - assert.strictEqual(testCase.output.Unix, pathConversion(SupportPathFormat.Unix, input), 'Unix path format test failed.'); + assert.strictEqual(testCase.output.Windows.unEscape, pathConversion(SupportPathFormat.Windows, input), 'Windows path format test failed.'); + assert.strictEqual(testCase.output.Unix.unEscape, pathConversion(SupportPathFormat.Unix, input), 'Unix path format test failed.'); } }); } diff --git a/src/test/test-case/path-convert-test-case.ts b/src/test/test-case/path-convert-test-case.ts index 71d4a31..3a96e20 100644 --- a/src/test/test-case/path-convert-test-case.ts +++ b/src/test/test-case/path-convert-test-case.ts @@ -1,4 +1,101 @@ import { PathTestCaseGroup } from "./types/PathTestCaseType"; export const pathConvertTestGroups: Array = [ + { + group: 'Normal Path Format Convert', + testTitle: 'Normal Path Format Convert (常规路径风格转换)', + cases: [ + { + title: 'Windows 风格', + input: // E:\Project\variable-conversion-vscode-extension + 'E:\\Project\\variable-conversion-vscode-extension', + output: { + Windows: { + unEscape: // E:\Project\variable-conversion-vscode-extension + 'E:\\Project\\variable-conversion-vscode-extension', + + escape: // E:\\Project\\variable-conversion-vscode-extension + 'E:\\\\Project\\\\variable-conversion-vscode-extension', + }, + Unix: { + unEscape: + 'E:/Project/variable-conversion-vscode-extension', + escape: // E:\/Project\/variable-conversion-vscode-extension + 'E:\\/Project\\/variable-conversion-vscode-extension', + }, + }, + }, + { + title: 'Unix 风格', + input: '/home/user/file.txt', + output: { + Windows: { + unEscape: // \home\user\file.txt + '\\home\\user\\file.txt', + escape: // \\home\\user\\file.txt + '\\\\home\\\\user\\\\file.txt', + }, + Unix: { + unEscape: + '/home/user/file.txt', + escape: // \/home\/user\/file.txt + '\\/home\\/user\\/file.txt', + }, + }, + }, + { + title: 'Windows (Git Bash) 风格', + input: '/c/Users/test/file.txt', + output: { + Windows: { + unEscape: // \c\Users\test/file.txt + '\\c\\Users\\test\\file.txt', + escape: // \\c\\Users\\test\\file.txt + '\\\\c\\\\Users\\\\test\\\\file.txt', + // TODO need to transform to ↓ + /* + unEscape: // C:\Users\test\file.txt + 'C:\\Users\\test\\file.txt', + escape: // C:\\Users\\test\\file.txt + 'C:\\\\Users\\\\test\\\\file.txt', + */ + }, + Unix: { + unEscape: + '/c/Users/test/file.txt', + escape: // \/c\/Users\/test\/file.txt + '\\/c\\/Users\\/test\\/file.txt', + }, + }, + }, + + // TODO + // Windows 局域网主机名风格 + // \\ComputerName + // 路径带空格 + // /home/user/hello world.txt + // /home/user/hello world.txt + // and more ... + + // { + // title: '', + // input: // + // '', + // output: { + // Windows: { + // unEscape: // + // '', + // escape: // + // '', + // }, + // Unix: { + // unEscape: // + // '', + // escape: // + // '', + // }, + // }, + // }, + ], + }, ]; diff --git a/src/test/test-case/types/PathTestCaseType.ts b/src/test/test-case/types/PathTestCaseType.ts index a3e136d..1b39ed0 100644 --- a/src/test/test-case/types/PathTestCaseType.ts +++ b/src/test/test-case/types/PathTestCaseType.ts @@ -1,3 +1,4 @@ +import exp from "constants"; export type PathTestCaseGroup = { group: string @@ -5,11 +6,16 @@ export type PathTestCaseGroup = { cases: Array }; +export type PathTestOutputResult = { + unEscape: string; + escape: string; +}; + export type PathTestCase = { title: string input: string | Array output: { - Windows: string - Unix: string + Windows: PathTestOutputResult + Unix: PathTestOutputResult } };