1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| | require 'tempfile'
require 'minitest/autorun'
require 'wordexp'
class TestWordexp < Minitest::Test
include Wordexp
def test_wordexp
assert_equal(%w(HELLO WORLD), wordexp("HELLO WORLD"))
assert_equal(%w(HELLO WORLD), wordexp("HELLO WORLD", 0))
end
def test_constants
consts = %w(WRDE_NOCMD WRDE_SHOWERR WRDE_UNDEF)
consts.each do |c|
val = self.class.const_get(c)
assert_kind_of Integer, val
end
end
def test_showerr
olderr = $stderr.dup
err = Tempfile.new(%w(test_wordexp .err))
$stderr.reopen(err)
res = wordexp("$(sh -c 'echo >&2 hihi')", WRDE_SHOWERR)
err.rewind
assert_equal "hihi\n", err.read
assert_equal [], res
ensure
$stderr.reopen(olderr)
err.close!
end
def test_no_showerr
olderr = $stderr.dup
err = Tempfile.new(%w(test_wordexp .err))
$stderr.reopen(err)
res = wordexp("$(sh -c 'echo >&2 hihi')")
err.rewind
assert_equal "", err.read
assert_equal [], res
ensure
$stderr.reopen(olderr)
err.close!
end
def test_envsub
key = "TEST_WORDEXP_ENVSUB_#$$"
ENV[key] = "echo"
res = wordexp("$#{key}")
assert_equal %w(echo), res
end
def test_syntax
assert_raises(WordexpError::Syntax) do
wordexp("$(sh -c '")
end
end
def test_badval
key = "TEST_WORDEXP_BADVAL_#$$"
ENV.delete(key) # just in case
s = "a$#{key}"
res = wordexp(s)
assert_equal ['a'], res
assert_raises(WordexpError::Badval) do
wordexp(s, WRDE_UNDEF)
end
end
def test_badchar
assert_raises(WordexpError::Badchar) do
wordexp("true|true")
end
assert_equal %w(true|true), wordexp('"true|true"')
end
def test_cmdsub_err
assert_raises(WordexpError::Cmdsub) do
wordexp("$(echo hi)", WRDE_NOCMD)
end
end
def test_cmdsub_nogvl
return if ENV["NO_SLEEP"]
times = []
start = Time.now
stop = nil
thr = Thread.new do
until stop do
times << Time.now.to_f
sleep 0.05
end
end
assert_equal %w(now), wordexp("$(sleep 1 >/dev/null 2>&1) now")
stop = Time.now
assert_in_delta(stop - start, 1, 0.1)
assert_operator times.size, :>, 15
end
def test_arithmetic
assert_equal %w(666), wordexp("$((333 + 333))")
assert_equal %w(8), wordexp("$((010))")
assert_equal %w(1638), wordexp("$((0x666))")
end
def test_shell_funcs
key = "TEST_WORDEXP_SHFUNCS_#$$"
ENV.delete(key) # just in case
assert_equal %w(foo), wordexp("${#{key}-foo}")
ENV[key] = "moo"
assert_equal %w(moo), wordexp("${#{key}-foo}")
assert_equal %w(3), wordexp("${\##{key}}")
assert_equal %w(oo), wordexp("${#{key}#m}")
assert_equal %w(m), wordexp("${#{key}%oo}")
ensure
ENV.delete(key)
end
def test_ifs
key = "TEST_WORDEXP_IFS_#$$"
orig = ENV["IFS"]
ENV["IFS"] = ""
ENV[key] = "a b c"
assert_equal ["a b c"], wordexp("$#{key}")
ENV[key] = "a\nb\tc"
ENV.delete("IFS")
assert_equal %w(a b c), wordexp("$#{key}")
ENV[key] = "a,b,c"
ENV["IFS"] = ","
assert_equal %w(a b c), wordexp("$#{key}")
ensure
ENV["IFS"] = orig
ENV.delete(key)
end
end
|