riel 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README +0 -0
  2. data/lib/riel/ansicolor.rb +93 -0
  3. data/lib/riel/array.rb +20 -0
  4. data/lib/riel/command.rb +30 -0
  5. data/lib/riel/date.rb +16 -0
  6. data/lib/riel/dir.rb +90 -0
  7. data/lib/riel/enumerable.rb +66 -0
  8. data/lib/riel/env.rb +49 -0
  9. data/lib/riel/file.rb +212 -0
  10. data/lib/riel/filetype.rb +189 -0
  11. data/lib/riel/hash.rb +12 -0
  12. data/lib/riel/io.rb +20 -0
  13. data/lib/riel/log.rb +548 -0
  14. data/lib/riel/matchdata.rb +13 -0
  15. data/lib/riel/optproc.rb +369 -0
  16. data/lib/riel/pathname.rb +16 -0
  17. data/lib/riel/rcfile.rb +35 -0
  18. data/lib/riel/regexp.rb +152 -0
  19. data/lib/riel/setdiff.rb +53 -0
  20. data/lib/riel/size_converter.rb +62 -0
  21. data/lib/riel/string.rb +81 -0
  22. data/lib/riel/tempfile.rb +28 -0
  23. data/lib/riel/text.rb +408 -0
  24. data/lib/riel/timer.rb +52 -0
  25. data/lib/riel.rb +13 -0
  26. data/test/riel/array_test.rb +22 -0
  27. data/test/riel/command_test.rb +28 -0
  28. data/test/riel/date_test.rb +17 -0
  29. data/test/riel/dir_test.rb +98 -0
  30. data/test/riel/enumerable_test.rb +27 -0
  31. data/test/riel/env_test.rb +52 -0
  32. data/test/riel/file_test.rb +242 -0
  33. data/test/riel/filetype_test.rb +32 -0
  34. data/test/riel/hash_test.rb +12 -0
  35. data/test/riel/io_test.rb +22 -0
  36. data/test/riel/log_test.rb +184 -0
  37. data/test/riel/matchdata_test.rb +15 -0
  38. data/test/riel/optproc_test.rb +233 -0
  39. data/test/riel/pathname_test.rb +36 -0
  40. data/test/riel/rcfile_test.rb +44 -0
  41. data/test/riel/regexp_test.rb +24 -0
  42. data/test/riel/setdiff_test.rb +26 -0
  43. data/test/riel/size_converter_test.rb +64 -0
  44. data/test/riel/string_test.rb +58 -0
  45. data/test/riel/tempfile_test.rb +16 -0
  46. data/test/riel/text_test.rb +102 -0
  47. data/test/riel/timer_test.rb +43 -0
  48. metadata +134 -0
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'pathname'
6
+ require 'riel/dir'
7
+ require 'riel/file'
8
+
9
+ class DirTestCase < RUNIT::TestCase
10
+
11
+ def test_home
12
+ assert_equal ENV["HOME"], Dir.home
13
+ end
14
+
15
+ def test_remove_if_empty
16
+ # set up a mess of files in /tmp ...
17
+
18
+ tmpdir = Pathname.new('/tmp')
19
+
20
+ # maybe this isn't Linux (poor devils!):
21
+ return unless tmpdir.exist?
22
+
23
+ testdir = tmpdir + 'riel_dir_test'
24
+ if testdir.exist?
25
+ testdir.delete
26
+ end
27
+
28
+ testdir.mkdir
29
+ assert testdir.exist?
30
+
31
+ # create a structure like this:
32
+ layout = [
33
+ :a => [
34
+ :a0 => %w{ a00 a01 a02 }
35
+ ],
36
+ :b => [
37
+ :b0 => %w{ },
38
+ :b1 => %w{ b10 b11 }
39
+ ]
40
+ ]
41
+
42
+ a = testdir + 'a'
43
+ a.mkdir
44
+ (0 .. 5).each do |ai|
45
+ adir = a + "a#{ai}"
46
+ adir.mkdir
47
+ end
48
+
49
+ Dir.remove_if_empty(a, :verbose => false)
50
+
51
+ assert !a.exist?
52
+
53
+ a = testdir + 'a'
54
+ a.mkdir
55
+ (0 .. 5).each do |ai|
56
+ adir = a + "a#{ai}"
57
+ adir.mkdir
58
+ (0 .. 4).each do |aai|
59
+ aafile = adir + "aa#{aai}"
60
+ File.write_file(aafile) do
61
+ (4 .. rand(50)).collect do |li|
62
+ "line #{li}"
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ Dir.remove_if_empty(a)
69
+ assert a.exist?
70
+
71
+ Dir.remove_if_empty(a, :deletable => [ %r{aa\d+} ])
72
+
73
+ assert !a.exist?
74
+
75
+ a = testdir + 'a'
76
+ a.mkdir
77
+ (0 .. 5).each do |ai|
78
+ adir = a + "a#{ai}"
79
+ adir.mkdir
80
+ (0 .. 4).each do |aai|
81
+ aafile = adir + "foo.bar"
82
+ File.write_file(aafile) do
83
+ (4 .. rand(50)).collect do |li|
84
+ "line #{li}"
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ Dir.remove_if_empty(a)
91
+ assert a.exist?
92
+
93
+ Dir.remove_if_empty(a, :deletable => %w{ foo.bar })
94
+ assert !a.exist?
95
+
96
+ testdir.delete
97
+ end
98
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'pathname'
5
+ require 'rubyunit'
6
+ require 'riel/enumerable'
7
+
8
+ class EnumerableTestCase < RUNIT::TestCase
9
+
10
+ def test_collect_with_index
11
+ assert_equals([ 0, 5, 12, 21, 32 ], (4 .. 8).collect_with_index { |val, idx| val * idx })
12
+ assert_equals([ "", "b", "cc", "ddd" ], ('a' .. 'd').collect_with_index { |str, idx| str * idx })
13
+ end
14
+
15
+ def test_select_with_index
16
+ assert_equals([ 5, 6, 7, 8 ], (4 .. 8).select_with_index { |val, idx| val >= 5 })
17
+ assert_equals([ 6 ], (4 .. 8).select_with_index { |val, idx| val * idx == 12 })
18
+ assert_equals([], (4 .. 8).select_with_index { |val, idx| idx - val > 0 })
19
+ end
20
+
21
+ def test_detect_with_index
22
+ assert_equals(5, (4 .. 8).detect_with_index { |val, idx| val >= 5 })
23
+ assert_equals(6, (4 .. 8).detect_with_index { |val, idx| val * idx == 12 })
24
+ assert_equals(nil, (4 .. 8).detect_with_index { |val, idx| idx - val > 0 })
25
+ end
26
+
27
+ end
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/env'
6
+
7
+ class EnvTestCase < RUNIT::TestCase
8
+
9
+ def test_home_directory
10
+ # Unix
11
+
12
+ ENV["HOME"] = "/home/me"
13
+
14
+ assert_equals "/home/me", Env.home_directory
15
+
16
+ # Windows
17
+
18
+ ENV["HOME"] = nil
19
+ ENV["HOMEDRIVE"] = "c:"
20
+
21
+ assert_equals "c:\\", Env.home_directory
22
+
23
+ ENV["HOME"] = nil
24
+ ENV["HOMEDRIVE"] = nil
25
+ ENV["HOMEPATH"] = "\\Program Files\\User"
26
+
27
+ assert_equals "\\Program Files\\User", Env.home_directory
28
+
29
+ ENV["HOME"] = nil
30
+ ENV["HOMEDRIVE"] = "c:"
31
+ ENV["HOMEPATH"] = "\\Program Files\\User"
32
+
33
+ # assert_equals "c:\\Program Files\\User", Env.home_directory
34
+ end
35
+
36
+ def do_split_test(expected, input)
37
+ ENV["FOO"] = input
38
+
39
+ result = Env.split("FOO")
40
+
41
+ assert_equals expected, result
42
+ end
43
+
44
+ def test_split
45
+ do_split_test(%w{ testing }, "testing")
46
+ do_split_test(%w{ this is }, "this is")
47
+ do_split_test(%w{ this is }, '"this" "is"')
48
+ do_split_test(%w{ this is a test }, '"this" "is" \'a\' "test"')
49
+ do_split_test(%w{ this is a tes't }, '"this" "is" \'a\' "tes\'t"')
50
+ end
51
+
52
+ end
@@ -0,0 +1,242 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/file'
6
+ require 'riel/tempfile'
7
+ require 'pathname'
8
+
9
+
10
+ class FileTestCase < RUNIT::TestCase
11
+
12
+ def get_file_rootname
13
+ Pathname.new(__FILE__).rootname
14
+ end
15
+
16
+ def create_text_file
17
+ rootname = Pathname.new(__FILE__).rootname
18
+ Tempfile.open(rootname) do |tf|
19
+ tf.puts "this is some text"
20
+ tf.puts "this is more text"
21
+ tf.puts "even more text"
22
+ tf.puts "and some final text"
23
+ end
24
+ end
25
+
26
+ def create_binary_file
27
+ stringio_so = "/usr/lib/ruby/1.8/i386-linux/stringio.so"
28
+
29
+ tempname = nil
30
+
31
+ if File.exist?(stringio_so)
32
+ rootname = Pathname.new(__FILE__).rootname
33
+ tempname = Tempfile.open(rootname) do |tf|
34
+ tf.write IO.read(stringio_so)
35
+ end
36
+ else
37
+ $stderr.puts "not testing binary file: #{stringio_so} does not exist"
38
+ end
39
+
40
+ tempname
41
+ end
42
+
43
+ def test_file_types
44
+ text_file = create_text_file
45
+ assert(File.text?(text_file))
46
+ assert(!File.binary?(text_file))
47
+
48
+ if binary_file = create_binary_file
49
+ assert(!File.text?(binary_file))
50
+ assert(File.binary?(binary_file))
51
+ end
52
+ end
53
+
54
+ def test_is_file_is_directory
55
+ file = create_text_file
56
+
57
+ dir = Pathname.new(file).dirname
58
+
59
+ assert(File.is_file?(file))
60
+ assert(!File.is_file?(dir))
61
+
62
+ assert(!File.is_directory?(file))
63
+ assert(File.is_directory?(dir))
64
+
65
+ # this should have access only by root:
66
+
67
+ file = "/var/log/httpd/error_log"
68
+ assert(!File.is_file?(file))
69
+ assert(!File.is_directory?(file))
70
+
71
+ end
72
+
73
+ def test_read_write_file
74
+ filename = Tempfile.new(Pathname.new(__FILE__).rootname).path
75
+
76
+ File.write_file(filename) do
77
+ "hello, world"
78
+ end
79
+
80
+ line = nil
81
+ File.read_file(filename) do |ln|
82
+ line = ln
83
+ end
84
+
85
+ assert_equal("hello, world", line)
86
+ end
87
+
88
+ def test_read_put_file
89
+ filename = Tempfile.new(Pathname.new(__FILE__).rootname).path
90
+
91
+ File.put_file(filename) do
92
+ "hello, world"
93
+ end
94
+
95
+ line = nil
96
+ File.read_file(filename) do |ln|
97
+ line = ln
98
+ end
99
+
100
+ assert_equal("hello, world\n", line)
101
+
102
+ File.put_file(filename) do
103
+ [
104
+ "hello, world",
105
+ "this is a test"
106
+ ]
107
+ end
108
+
109
+ lines = Array.new
110
+ File.read_file_lines(filename) do |ln|
111
+ lines << ln
112
+ end
113
+
114
+ assert_equal([ "hello, world\n", "this is a test\n" ] , lines)
115
+ end
116
+
117
+ def test_open_via_temp_file
118
+ fname = "/tmp/test_open_via_temp_file.#{$$}"
119
+
120
+ pn = Pathname.new(fname)
121
+
122
+ assert !pn.exist?
123
+
124
+ File.open_via_temp_file(fname) do |io|
125
+ assert !pn.exist?
126
+ io.puts "this is a test"
127
+ end
128
+
129
+ assert pn.exist?
130
+
131
+ pn.delete
132
+ end
133
+
134
+ def assert_files_existence(expected, files)
135
+ files.each do |file|
136
+ assert_equal expected, file.exist?
137
+ end
138
+ end
139
+
140
+ def test_move_files
141
+ tmp = Pathname.new("/tmp")
142
+
143
+ assert tmp.exist?
144
+
145
+ tgt = tmp + "movetest"
146
+
147
+ fnums = 0 .. 3
148
+
149
+ srcfiles = fnums.collect { |num| Pathname.new(tmp + "movefile#{num}") }
150
+
151
+ # if the user has something there, we shouldn't delete it at the end of
152
+ # these tests.
153
+
154
+ assert !tgt.exist?
155
+
156
+ begin
157
+ tgt.mkdir
158
+
159
+ srcfiles.each do |file|
160
+ assert !file.exist?
161
+
162
+ File.write_file(file) do
163
+ "contents of file"
164
+ end
165
+
166
+ assert file.exist?
167
+ end
168
+
169
+ File.move_files(tgt, srcfiles)
170
+
171
+ assert_files_existence(false, srcfiles)
172
+
173
+ tgtfiles = fnums.collect { |num| tgt + "movefile#{num}" }
174
+
175
+ assert_files_existence(true, tgtfiles)
176
+ ensure
177
+ if tgt && tgt.exist?
178
+ tgt.children.each do |child|
179
+ child.delete
180
+ end
181
+
182
+ tgt.delete if tgt && tgt.exist?
183
+ end
184
+
185
+ srcfiles.each do |file|
186
+ file.delete if file && file.exist?
187
+ end
188
+ end
189
+ end
190
+
191
+ def test_copy_files
192
+ tmp = Pathname.new("/tmp")
193
+
194
+ assert tmp.exist?
195
+
196
+ tgt = tmp + "copytest"
197
+
198
+ fnums = 0 .. 3
199
+
200
+ srcfiles = fnums.collect { |num| Pathname.new(tmp + "copyfile#{num}") }
201
+
202
+ # if the user has something there, we shouldn't delete it at the end of
203
+ # these tests.
204
+
205
+ assert !tgt.exist?
206
+
207
+ begin
208
+ tgt.mkdir
209
+
210
+ srcfiles.each do |file|
211
+ assert !file.exist?
212
+
213
+ File.write_file(file) do
214
+ "contents of file"
215
+ end
216
+
217
+ assert file.exist?
218
+ end
219
+
220
+ File.copy_files(tgt, srcfiles)
221
+
222
+ assert_files_existence(true, srcfiles)
223
+
224
+ tgtfiles = fnums.collect { |num| tgt + "copyfile#{num}" }
225
+
226
+ assert_files_existence(true, tgtfiles)
227
+ ensure
228
+ if tgt && tgt.exist?
229
+ tgt.children.each do |child|
230
+ child.delete
231
+ end
232
+
233
+ tgt.delete if tgt && tgt.exist?
234
+ end
235
+
236
+ srcfiles.each do |file|
237
+ file.delete if file && file.exist?
238
+ end
239
+ end
240
+ end
241
+
242
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/filetype'
6
+
7
+
8
+ class FileTypeTestCase < RUNIT::TestCase
9
+
10
+ def test_default_extensions
11
+ ft = FileType.instance
12
+
13
+ assert(ft.text_extensions.include?("rb"))
14
+
15
+ assert(!ft.text_extensions.include?("rbx"))
16
+ assert(!ft.text_extensions.include?("tar"))
17
+ assert(!ft.text_extensions.include?("jar"))
18
+ assert(!ft.text_extensions.include?("gz"))
19
+
20
+ assert(ft.nontext_extensions.include?("tar"))
21
+ assert(ft.nontext_extensions.include?("jar"))
22
+ assert(ft.nontext_extensions.include?("gz"))
23
+
24
+ ft.set_extensions(true, "rbx")
25
+ assert(ft.text_extensions.include?("rbx"))
26
+
27
+ ft.set_extensions(true, "foo", "bar")
28
+ assert(ft.text_extensions.include?("foo"))
29
+ assert(ft.text_extensions.include?("bar"))
30
+ end
31
+
32
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/hash'
6
+
7
+ class HashTestCase < RUNIT::TestCase
8
+
9
+ def test
10
+ end
11
+
12
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/env'
6
+
7
+ class IOTestCase < RUNIT::TestCase
8
+
9
+ def test_readlines
10
+ orig = $/
11
+
12
+ $/ = nil
13
+
14
+ contents = IO.readlines(__FILE__)
15
+
16
+ assert_not_nil contents
17
+ assert contents.size > 0
18
+
19
+ $/ = orig
20
+ end
21
+
22
+ end
@@ -0,0 +1,184 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ #!/usr/bin/ruby -w
5
+ # -*- ruby -*-
6
+
7
+ require 'pathname'
8
+ require 'rubyunit'
9
+ require 'stringio'
10
+ require 'riel/log'
11
+
12
+ class LogTestCase < RUNIT::TestCase
13
+ include Loggable
14
+
15
+ def test_no_output
16
+ # I could make this an instance variable, but I want to check the method
17
+ # names in the output string.
18
+
19
+ log = Proc.new {
20
+ Log.log "hello, world?"
21
+ Log.debug "hello, world?"
22
+ Log.info "hello, world?"
23
+ Log.warn "EXPECTED OUTPUT TO STDERR: hello, world." # will go to stderr
24
+ Log.error "EXPECTED OUTPUT TO STDERR: hello, world." # will go to stderr
25
+ Log.stack "hello, world?"
26
+ }
27
+
28
+ run_test(@nonverbose_setup, log)
29
+ end
30
+
31
+ def test_output_arg
32
+ log = Proc.new {
33
+ Log.log "hello, world?"
34
+ Log.debug "hello, world?"
35
+ Log.info "hello, world?"
36
+ Log.warn "hello, world?"
37
+ Log.error "hello, world?"
38
+ Log.stack "hello, world?"
39
+ }
40
+
41
+ expected_output = [
42
+ "[log_test.rb : 33] {test_output_arg } hello, world?",
43
+ "[log_test.rb : 34] {test_output_arg } hello, world?",
44
+ "[log_test.rb : 35] {test_output_arg } hello, world?",
45
+ "[log_test.rb : 36] {test_output_arg } hello, world?",
46
+ "[log_test.rb : 37] {test_output_arg } hello, world?",
47
+ "[log_test.rb : 38] {test_output_arg } hello, world?",
48
+ "[log_test.rb : 165] {call } ",
49
+ "[log_test.rb : 165] {run_test } ",
50
+ "[log_test.rb : 71] {test_output_arg } ",
51
+ nil,
52
+ nil,
53
+ nil,
54
+ nil,
55
+ nil,
56
+ nil,
57
+ nil,
58
+ nil,
59
+ nil,
60
+ nil,
61
+ nil,
62
+ nil,
63
+ nil,
64
+ nil,
65
+ nil,
66
+ nil,
67
+ nil,
68
+ nil,
69
+ ]
70
+
71
+ run_test(@verbose_setup, log, *expected_output)
72
+ end
73
+
74
+ def test_output_block
75
+ log = Proc.new {
76
+ Log.log { "hello, world?" }
77
+ }
78
+
79
+ run_test(@verbose_setup, log, "[log_test.rb : 76] {test_output_block } hello, world?")
80
+
81
+ info_setup = Proc.new {
82
+ Log.level = Log::INFO
83
+ Log.output = StringIO.new
84
+ }
85
+
86
+ evaluated = false
87
+ log = Proc.new {
88
+ Log.debug { evaluated = true; "hello, world?" }
89
+ }
90
+
91
+ run_test(info_setup, log)
92
+
93
+ assert_equals false, evaluated
94
+ end
95
+
96
+ def test_instance_log
97
+ log = Proc.new {
98
+ log "hello, world?"
99
+ }
100
+
101
+ Log.set_widths(-15, 4, -40)
102
+ run_test(@verbose_setup, log, "[log_test.rb : 98] {LogTestCase#test_instance_log } hello, world?")
103
+
104
+ Log.set_default_widths
105
+ end
106
+
107
+ def test_colors
108
+ log = Proc.new {
109
+ white "white wedding"
110
+ blue "blue iris"
111
+ on_cyan "red"
112
+ }
113
+
114
+ run_test(@verbose_setup, log,
115
+ "[log_test.rb : 109] {LogTestCase#test_col} \e[37mwhite wedding\e[0m",
116
+ "[log_test.rb : 110] {LogTestCase#test_col} \e[34mblue iris\e[0m",
117
+ "[log_test.rb : 111] {LogTestCase#test_col} \e[46mred\e[0m")
118
+
119
+ Log.set_default_widths
120
+ end
121
+
122
+ def test_format
123
+ log = Proc.new {
124
+ Log.log "hello, world?"
125
+ }
126
+
127
+ Log.set_default_widths
128
+ run_test(@verbose_setup, log, "[log_test.rb : 124] {test_format } hello, world?")
129
+
130
+ # Log.set_widths(file_width, line_width, func_width)
131
+
132
+ run_format_test(log, -25, 8, 30, "[log_test.rb : 124] { test_format} hello, world?")
133
+ run_format_test(log, 25, 8, 30, "[ log_test.rb: 124] { test_format} hello, world?")
134
+ run_format_test(log, 25, "08", 30, "[ log_test.rb:00000124] { test_format} hello, world?")
135
+
136
+ # useless feature of truncating line numbers, but so it goes ...
137
+ run_format_test(log, 4, 2, -10, "[log_:12] {test_forma} hello, world?")
138
+
139
+ Log.set_default_widths
140
+ end
141
+
142
+ def run_format_test(log, file_width, line_width, func_width, expected)
143
+ Log.set_widths(file_width, line_width, func_width)
144
+ run_test(@verbose_setup, log, expected)
145
+ end
146
+
147
+ # the ctor is down here so the lines above are less likely to change.
148
+ def initialize(test, name)
149
+ @nonverbose_setup = Proc.new {
150
+ Log.verbose = false
151
+ Log.output = StringIO.new
152
+ }
153
+
154
+ @verbose_setup = Proc.new {
155
+ Log.verbose = true
156
+ Log.output = StringIO.new
157
+ }
158
+
159
+ super
160
+ end
161
+
162
+ def run_test(setup, log, *expected)
163
+ io = setup.call
164
+
165
+ log.call
166
+
167
+ assert_not_nil(io)
168
+ str = io.string
169
+ assert_not_nil str
170
+
171
+ lines = str.split("\n")
172
+
173
+ assert_equals expected.size, lines.size, "number of lines of output"
174
+
175
+ (0 ... expected.size).each do |idx|
176
+ if expected[idx]
177
+ assert_equals expected[idx], lines[idx], "index: #{idx}"
178
+ end
179
+ end
180
+
181
+ Log.output = io
182
+ end
183
+
184
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/ruby -w
2
+ # -*- ruby -*-
3
+
4
+ require 'rubyunit'
5
+ require 'riel/matchdata'
6
+
7
+
8
+ class MatchDataTestCase < RUNIT::TestCase
9
+
10
+ def test_inspect
11
+ md = %r{(foo)(.*)(bar)}.match("footloose sidebar")
12
+ assert_equals "[\"footloose sidebar\", \"foo\", \"tloose side\", \"bar\"]", md.inspect
13
+ end
14
+
15
+ end