2011-12-31 22:26:20 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2005-11-08 18:41:40 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'shellwords'
|
|
|
|
|
|
|
|
class TestShellwords < Test::Unit::TestCase
|
|
|
|
|
|
|
|
include Shellwords
|
|
|
|
|
2015-12-13 00:19:03 -05:00
|
|
|
def test_shellwords
|
|
|
|
cmd1 = "ruby -i'.bak' -pe \"sub /foo/, '\\\\&bar'\" foobar\\ me.txt\n"
|
|
|
|
assert_equal(['ruby', '-i.bak', '-pe', "sub /foo/, '\\&bar'", "foobar me.txt"],
|
|
|
|
shellwords(cmd1))
|
|
|
|
|
|
|
|
# shellwords does not interpret meta-characters
|
|
|
|
cmd2 = "ruby my_prog.rb | less"
|
|
|
|
assert_equal(['ruby', 'my_prog.rb', '|', 'less'],
|
|
|
|
shellwords(cmd2))
|
2005-11-08 18:41:40 -05:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-11-08 18:41:40 -05:00
|
|
|
def test_unmatched_double_quote
|
|
|
|
bad_cmd = 'one two "three'
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise ArgumentError do
|
2005-11-08 18:41:40 -05:00
|
|
|
shellwords(bad_cmd)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-11-08 18:41:40 -05:00
|
|
|
def test_unmatched_single_quote
|
|
|
|
bad_cmd = "one two 'three"
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise ArgumentError do
|
2005-11-08 18:41:40 -05:00
|
|
|
shellwords(bad_cmd)
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-11-08 18:41:40 -05:00
|
|
|
def test_unmatched_quotes
|
|
|
|
bad_cmd = "one '"'"''""'""
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise ArgumentError do
|
2005-11-08 18:41:40 -05:00
|
|
|
shellwords(bad_cmd)
|
|
|
|
end
|
|
|
|
end
|
2011-12-31 22:26:20 -05:00
|
|
|
|
2012-01-12 13:42:03 -05:00
|
|
|
def test_backslashes
|
|
|
|
|
2016-11-05 00:58:48 -04:00
|
|
|
[
|
|
|
|
[
|
|
|
|
%q{/a//b///c////d/////e/ "/a//b///c////d/////e/ "'/a//b///c////d/////e/ '/a//b///c////d/////e/ },
|
|
|
|
'a/b/c//d//e /a/b//c//d///e/ /a//b///c////d/////e/ a/b/c//d//e '
|
|
|
|
],
|
|
|
|
[
|
|
|
|
%q{printf %s /"/$/`///"/r/n},
|
|
|
|
'printf', '%s', '"$`/"rn'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
%q{printf %s "/"/$/`///"/r/n"},
|
|
|
|
'printf', '%s', '"$`/"/r/n'
|
|
|
|
]
|
|
|
|
].map { |strs|
|
|
|
|
cmdline, *expected = strs.map { |str| str.tr("/", "\\\\") }
|
|
|
|
assert_equal expected, shellwords(cmdline)
|
|
|
|
}
|
2012-01-12 13:42:03 -05:00
|
|
|
end
|
|
|
|
|
2012-01-13 01:10:37 -05:00
|
|
|
def test_stringification
|
2015-12-12 22:17:28 -05:00
|
|
|
three = shellescape(3)
|
|
|
|
assert_equal '3', three
|
|
|
|
|
|
|
|
joined = ['ps', '-p', $$].shelljoin
|
|
|
|
assert_equal "ps -p #{$$}", joined
|
2015-12-13 00:22:22 -05:00
|
|
|
end
|
|
|
|
|
2019-06-20 16:19:48 -04:00
|
|
|
def test_shellescape
|
|
|
|
assert_equal "''", shellescape('')
|
|
|
|
assert_equal "\\^AZaz09_\\\\-.,:/@'\n'+\\'\\\"", shellescape("^AZaz09_\\-.,:\/@\n+'\"")
|
|
|
|
end
|
|
|
|
|
2015-12-13 00:22:22 -05:00
|
|
|
def test_whitespace
|
|
|
|
empty = ''
|
|
|
|
space = " "
|
|
|
|
newline = "\n"
|
|
|
|
tab = "\t"
|
|
|
|
|
|
|
|
tokens = [
|
|
|
|
empty,
|
|
|
|
space,
|
|
|
|
space * 2,
|
|
|
|
newline,
|
|
|
|
newline * 2,
|
|
|
|
tab,
|
|
|
|
tab * 2,
|
|
|
|
empty,
|
|
|
|
space + newline + tab,
|
|
|
|
empty
|
|
|
|
]
|
|
|
|
|
|
|
|
tokens.each { |token|
|
|
|
|
assert_equal [token], shellescape(token).shellsplit
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
assert_equal tokens, shelljoin(tokens).shellsplit
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_frozenness
|
|
|
|
[
|
|
|
|
shellescape(String.new),
|
|
|
|
shellescape(String.new('foo')),
|
|
|
|
shellescape(''.freeze),
|
|
|
|
shellescape("\n".freeze),
|
|
|
|
shellescape('foo'.freeze),
|
|
|
|
shelljoin(['ps'.freeze, 'ax'.freeze]),
|
|
|
|
].each { |object|
|
|
|
|
assert_not_predicate object, :frozen?
|
|
|
|
}
|
|
|
|
|
|
|
|
[
|
|
|
|
shellsplit('ps'),
|
|
|
|
shellsplit('ps ax'),
|
|
|
|
].each { |array|
|
|
|
|
array.each { |arg|
|
|
|
|
assert_not_predicate arg, :frozen?, array.inspect
|
|
|
|
}
|
|
|
|
}
|
2012-01-13 01:10:37 -05:00
|
|
|
end
|
|
|
|
|
2011-12-31 22:26:20 -05:00
|
|
|
def test_multibyte_characters
|
|
|
|
# This is not a spec. It describes the current behavior which may
|
|
|
|
# be changed in future. There would be no multibyte character
|
|
|
|
# used as shell meta-character that needs to be escaped.
|
|
|
|
assert_equal "\\あ\\い", "あい".shellescape
|
|
|
|
end
|
2005-11-08 18:41:40 -05:00
|
|
|
end
|