test_quote.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* eslint-env mocha */
  2. 'use strict'
  3. const { assert } = require('chai')
  4. const shlex = require('../shlex')
  5. describe('shlex.quote()', function () {
  6. const safeUnquoted = 'abcdefghijklmnopqrstuvwxyz' +
  7. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
  8. '0123456789' +
  9. '@%_-+=:,./'
  10. const unicodeSample = '\xe9\xe0\xdf' // e + acute accent, a + grave, sharp s
  11. const unsafe = '"`$\\!' + unicodeSample
  12. it('should escape the empty string', function () {
  13. assert.equal(shlex.quote(''), '\'\'')
  14. })
  15. it('should not escape safe strings', function () {
  16. assert.equal(shlex.quote(safeUnquoted), safeUnquoted)
  17. })
  18. it('should escape strings containing spaces', function () {
  19. assert.equal(shlex.quote('test file name'), "'test file name'")
  20. })
  21. it('should escape unsafe characters', function () {
  22. for (var char of unsafe) {
  23. const input = 'test' + char + 'file'
  24. const expected = '\'' + input + '\''
  25. assert.equal(shlex.quote(input), expected)
  26. }
  27. })
  28. it('should escape single quotes', function () {
  29. assert.equal(shlex.quote('test\'file'), '\'test\'"\'"\'file\'')
  30. })
  31. })