2021-05-21 15:17:21 +02:00
|
|
|
const { extractExecutablePath, quotePath, unquotePath, friendlySafeFilename, toFileProtocolPath } = require('./path-utils');
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('pathUtils', () => {
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2022-11-15 12:23:50 +02:00
|
|
|
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should create friendly safe filename', (async () => {
|
2018-11-20 02:42:21 +02:00
|
|
|
const testCases = [
|
|
|
|
['生活', '生活'],
|
|
|
|
['not/good', 'not_good'],
|
|
|
|
['really/not/good', 'really_not_good'],
|
|
|
|
['con', '___'],
|
|
|
|
['no space at the end ', 'no space at the end'],
|
|
|
|
['nor dots...', 'nor dots'],
|
2018-11-21 02:36:23 +02:00
|
|
|
[' no space before either', 'no space before either'],
|
2021-08-10 20:13:16 +02:00
|
|
|
['no\nnewline\n\rplease', 'no_newline__please'],
|
2020-09-07 23:12:51 +02:00
|
|
|
['thatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylong', 'thatsreallylongthatsreallylongthatsreallylongthats'],
|
2018-11-20 02:42:21 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
|
|
|
expect(friendlySafeFilename(t[0])).toBe(t[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(!!friendlySafeFilename('')).toBe(true);
|
|
|
|
expect(!!friendlySafeFilename('...')).toBe(true);
|
2021-08-10 20:13:16 +02:00
|
|
|
|
|
|
|
// Check that it optionally handles filenames with extension
|
2021-08-17 07:57:00 +02:00
|
|
|
expect(friendlySafeFilename('file', null, true)).toBe('file');
|
2021-08-10 20:13:16 +02:00
|
|
|
expect(friendlySafeFilename(' testing.md', null, true)).toBe('testing.md');
|
|
|
|
expect(friendlySafeFilename('testing.safe??ext##', null, true)).toBe('testing.safeext');
|
|
|
|
expect(friendlySafeFilename('thatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylongthatsreallylong.md', null, true)).toBe('thatsreallylongthatsreallylongthatsreallylongthats.md');
|
2019-09-23 23:30:25 +02:00
|
|
|
}));
|
2018-11-20 02:42:21 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should quote and unquote paths', (async () => {
|
2019-01-30 21:19:06 +02:00
|
|
|
const testCases = [
|
|
|
|
['', ''],
|
|
|
|
['/my/path', '/my/path'],
|
|
|
|
['/my/path with spaces', '"/my/path with spaces"'],
|
|
|
|
['/my/weird"path', '"/my/weird\\"path"'],
|
|
|
|
['c:\\Windows\\test.dll', 'c:\\Windows\\test.dll'],
|
|
|
|
['c:\\Windows\\test test.dll', '"c:\\Windows\\test test.dll"'],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
|
|
|
expect(quotePath(t[0])).toBe(t[1]);
|
|
|
|
expect(unquotePath(quotePath(t[0]))).toBe(t[0]);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
2019-09-23 23:30:25 +02:00
|
|
|
}));
|
2019-01-30 21:19:06 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should extract executable path from command', (async () => {
|
2019-01-30 21:19:06 +02:00
|
|
|
const testCases = [
|
|
|
|
['', ''],
|
|
|
|
['/my/cmd -some -args', '/my/cmd'],
|
|
|
|
['"/my/cmd" -some -args', '"/my/cmd"'],
|
|
|
|
['"/my/cmd"', '"/my/cmd"'],
|
|
|
|
['"/my/cmd and space" -some -flags', '"/my/cmd and space"'],
|
|
|
|
['"" -some -flags', '""'],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
|
|
|
expect(extractExecutablePath(t[0])).toBe(t[1]);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
2019-09-23 23:30:25 +02:00
|
|
|
}));
|
2019-01-30 21:19:06 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should create correct fileURL syntax', (async () => {
|
2019-07-29 12:16:47 +02:00
|
|
|
const testCases_win32 = [
|
2021-01-08 20:33:22 +02:00
|
|
|
['C:\\handle\\space test', 'file:///C:/handle/space%20test'],
|
2019-07-29 12:16:47 +02:00
|
|
|
['C:\\escapeplus\\+', 'file:///C:/escapeplus/%2B'],
|
2021-01-08 20:33:22 +02:00
|
|
|
['C:\\handle\\single quote\'', 'file:///C:/handle/single%20quote%27'],
|
2019-07-29 12:16:47 +02:00
|
|
|
];
|
|
|
|
const testCases_unixlike = [
|
2021-01-08 20:33:22 +02:00
|
|
|
['/handle/space test', 'file:///handle/space%20test'],
|
2019-07-29 12:16:47 +02:00
|
|
|
['/escapeplus/+', 'file:///escapeplus/%2B'],
|
2021-01-08 20:33:22 +02:00
|
|
|
['/handle/single quote\'', 'file:///handle/single%20quote%27'],
|
2019-07-29 12:16:47 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases_win32.length; i++) {
|
|
|
|
const t = testCases_win32[i];
|
|
|
|
expect(toFileProtocolPath(t[0], 'win32')).toBe(t[1]);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
2019-07-29 12:16:47 +02:00
|
|
|
for (let i = 0; i < testCases_unixlike.length; i++) {
|
|
|
|
const t = testCases_unixlike[i];
|
|
|
|
expect(toFileProtocolPath(t[0], 'linux')).toBe(t[1]);
|
2019-07-30 09:35:42 +02:00
|
|
|
}
|
2019-09-23 23:30:25 +02:00
|
|
|
}));
|
2019-07-29 12:16:47 +02:00
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|