Provide shell and escape options when formatting matching files

This commit is contained in:
Michal Dorner 2020-12-13 21:07:47 +01:00
parent ada1eee648
commit e4d886f503
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
7 changed files with 124 additions and 47 deletions

View file

@ -1,21 +1,57 @@
import shellEscape from '../src/shell-escape'
import {escape, shellEscape} from '../src/shell-escape'
test('simple filename should not be modified', () => {
expect(shellEscape('file.txt')).toBe('file.txt')
describe('escape() backslash escapes every character except subset of definitely safe characters', () => {
test('simple filename should not be modified', () => {
expect(escape('file.txt')).toBe('file.txt')
})
test('directory separator should be preserved and not escaped', () => {
expect(escape('path/to/file.txt')).toBe('path/to/file.txt')
})
test('spaces should be escaped with backslash', () => {
expect(escape('file with space')).toBe('file\\ with\\ space')
})
test('quotes should be escaped with backslash', () => {
expect(escape('file\'with quote"')).toBe('file\\\'with\\ quote\\"')
})
test('$variables should be escaped', () => {
expect(escape('$var')).toBe('\\$var')
})
})
test('directory separator should be preserved and not escaped', () => {
expect(shellEscape('path/to/file.txt')).toBe('path/to/file.txt')
})
describe('shellEscape() returns human readable filenames with as few escaping applied as possible', () => {
test('simple filename should not be modified', () => {
expect(shellEscape('file.txt')).toBe('file.txt')
})
test('spaces should be escaped with backslash', () => {
expect(shellEscape('file with space')).toBe('file\\ with\\ space')
})
test('directory separator should be preserved and not escaped', () => {
expect(shellEscape('path/to/file.txt')).toBe('path/to/file.txt')
})
test('quotes should be escaped with backslash', () => {
expect(shellEscape('file\'with quote"')).toBe('file\\\'with\\ quote\\"')
})
test('filename with spaces should be quoted', () => {
expect(shellEscape('file with space')).toBe("'file with space'")
})
test('$variables sould be escaped', () => {
expect(shellEscape('$var')).toBe('\\$var')
test('filename with spaces should be quoted', () => {
expect(shellEscape('file with space')).toBe("'file with space'")
})
test('filename with $ should be quoted', () => {
expect(shellEscape('$var')).toBe("'$var'")
})
test('filename with " should be quoted', () => {
expect(shellEscape('file"name')).toBe("'file\"name'")
})
test('filename with single quote should be wrapped in double quotes', () => {
expect(shellEscape("file'with quote")).toBe('"file\'with quote"')
})
test('filename with single quote and special characters is split and quoted/escaped as needed', () => {
expect(shellEscape("file'with $quote")).toBe("file\\''with $quote'")
})
})