实现最基础的 Windows <-> Unix 风格互转逻辑;添加基础测试用例并通过
This commit is contained in:
		@@ -1,6 +1,60 @@
 | 
				
			|||||||
import { SupportPathFormat } from "./types/SupportPathFormatType";
 | 
					import { SupportPathFormat } from "./types/SupportPathFormatType";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/** / */
 | 
				
			||||||
 | 
					const LEFT_SLASH = '/';
 | 
				
			||||||
 | 
					/** \ */
 | 
				
			||||||
 | 
					const RIGHT_SLASH = '\\';
 | 
				
			||||||
 | 
					/** \\ */
 | 
				
			||||||
 | 
					const DOUBLE_RIGHT_SLASH = '\\\\';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function pathConversion(SupportPathFormat: SupportPathFormat, input: string) {
 | 
					export function pathConversion(targetPathType: SupportPathFormat, input: string): string {
 | 
				
			||||||
    // TODO
 | 
					    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;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -116,8 +116,8 @@ suite('Extension Test: run path convert test case', () => {
 | 
				
			|||||||
				for (const input of inputList) {
 | 
									for (const input of inputList) {
 | 
				
			||||||
					// console.log('input', '->' + input + '<-');
 | 
										// console.log('input', '->' + input + '<-');
 | 
				
			||||||
					// 验证转换
 | 
										// 验证转换
 | 
				
			||||||
					assert.strictEqual(testCase.output.Windows, pathConversion(SupportPathFormat.Windows, input), 'Windows path format test failed.');
 | 
										assert.strictEqual(testCase.output.Windows.unEscape, 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.Unix.unEscape, pathConversion(SupportPathFormat.Unix, input), 'Unix path format test failed.');
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,101 @@
 | 
				
			|||||||
import { PathTestCaseGroup } from "./types/PathTestCaseType";
 | 
					import { PathTestCaseGroup } from "./types/PathTestCaseType";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const pathConvertTestGroups: Array<PathTestCaseGroup> = [
 | 
					export const pathConvertTestGroups: Array<PathTestCaseGroup> = [
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        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: //
 | 
				
			||||||
 | 
					            //                 '',
 | 
				
			||||||
 | 
					            //         },
 | 
				
			||||||
 | 
					            //     },
 | 
				
			||||||
 | 
					            // },
 | 
				
			||||||
 | 
					        ],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
];
 | 
					];
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import exp from "constants";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type PathTestCaseGroup = {
 | 
					export type PathTestCaseGroup = {
 | 
				
			||||||
    group: string
 | 
					    group: string
 | 
				
			||||||
@@ -5,11 +6,16 @@ export type PathTestCaseGroup = {
 | 
				
			|||||||
    cases: Array<PathTestCase>
 | 
					    cases: Array<PathTestCase>
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type PathTestOutputResult = {
 | 
				
			||||||
 | 
					    unEscape: string;
 | 
				
			||||||
 | 
					    escape: string;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type PathTestCase = {
 | 
					export type PathTestCase = {
 | 
				
			||||||
    title: string
 | 
					    title: string
 | 
				
			||||||
    input: string | Array<string>
 | 
					    input: string | Array<string>
 | 
				
			||||||
    output: {
 | 
					    output: {
 | 
				
			||||||
        Windows: string
 | 
					        Windows: PathTestOutputResult
 | 
				
			||||||
        Unix: string
 | 
					        Unix: PathTestOutputResult
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user