refactor: push isValidPath() into parsePastedPaths() (#18664)

This commit is contained in:
Tommaso Sciortino
2026-02-09 13:19:51 -08:00
committed by GitHub
parent da122b1f8a
commit 415cebe904
15 changed files with 247 additions and 261 deletions
+16 -7
View File
@@ -42,7 +42,11 @@ describe('escapePath', () => {
['double quotes', 'file"name.txt', 'file\\"name.txt'],
['hash symbols', 'file#name.txt', 'file\\#name.txt'],
['exclamation marks', 'file!name.txt', 'file\\!name.txt'],
['tildes', 'file~name.txt', 'file\\~name.txt'],
[
'tildes',
'file~name.txt',
process.platform === 'win32' ? 'file~name.txt' : 'file\\~name.txt',
],
[
'less than and greater than signs',
'file<name>.txt',
@@ -99,11 +103,16 @@ describe('escapePath', () => {
expect(escapePath('')).toBe('');
});
it('should handle paths with only special characters', () => {
expect(escapePath(' ()[]{};&|*?$`\'"#!~<>')).toBe(
'\\ \\(\\)\\[\\]\\{\\}\\;\\&\\|\\*\\?\\$\\`\\\'\\"\\#\\!\\~\\<\\>',
it('should handle paths with multiple special characters', () => {
expect(escapePath(' ()[]{};&|*?$`\'"#!<>')).toBe(
'\\ \\(\\)\\[\\]\\{\\}\\;\\&\\|\\*\\?\\$\\`\\\'\\"\\#\\!\\<\\>',
);
});
it('should handle tildes based on platform', () => {
const expected = process.platform === 'win32' ? '~' : '\\~';
expect(escapePath('~')).toBe(expected);
});
});
describe('unescapePath', () => {
@@ -130,12 +139,12 @@ describe('unescapePath', () => {
);
});
it('should handle all special characters', () => {
it('should handle all special characters but tilda', () => {
expect(
unescapePath(
'\\ \\(\\)\\[\\]\\{\\}\\;\\&\\|\\*\\?\\$\\`\\\'\\"\\#\\!\\~\\<\\>',
'\\ \\(\\)\\[\\]\\{\\}\\;\\&\\|\\*\\?\\$\\`\\\'\\"\\#\\!\\<\\>',
),
).toBe(' ()[]{};&|*?$`\'"#!~<>');
).toBe(' ()[]{};&|*?$`\'"#!<>');
});
it('should be the inverse of escapePath', () => {
+5 -3
View File
@@ -16,10 +16,12 @@ export const GOOGLE_ACCOUNTS_FILENAME = 'google_accounts.json';
/**
* Special characters that need to be escaped in file paths for shell compatibility.
* Includes: spaces, parentheses, brackets, braces, semicolons, ampersands, pipes,
* asterisks, question marks, dollar signs, backticks, quotes, hash, and other shell metacharacters.
* Note that windows doesn't escape tilda.
*/
export const SHELL_SPECIAL_CHARS = /[ \t()[\]{};|*?$`'"#&<>!~]/;
export const SHELL_SPECIAL_CHARS =
process.platform === 'win32'
? /[ \t()[\]{};|*?$`'"#&<>!]/
: /[ \t()[\]{};|*?$`'"#&<>!~]/;
/**
* Returns the home directory.