rak 1.0 → 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/bin/rak +281 -351
  2. data/lib/rak.rb +1 -1
  3. data/spec/help_spec.rb +33 -27
  4. data/spec/rak_spec.rb +435 -363
  5. metadata +31 -61
  6. data/Rakefile +0 -14
data/lib/rak.rb CHANGED
@@ -1,4 +1,4 @@
1
1
 
2
2
  class Rak
3
- VERSION = "1.0"
3
+ VERSION = "1.2"
4
4
  end
@@ -1,50 +1,56 @@
1
+ require "spec_helper"
1
2
 
2
-
3
- require File.dirname(__FILE__) + "/spec_helpers"
3
+ def extract_options_from_help_message(msg)
4
+ msg.scan(/\B--?(?:\[no\]|no)?([a-zA-Z0-9\-]+)/).flatten.sort.uniq
5
+ end
4
6
 
5
7
  describe "Rak", "help and errors" do
6
- before(:all) do
7
- ENV['RAK_TEST'] = "true"
8
- end
9
- after(:all) do
10
- ENV['RAK_TEST'] = "false"
11
- end
12
-
13
8
  it "--version prints version information" do
14
- strip_ansi(%x{rak --version}).should == t=<<END
15
- rak 0.9
9
+ rak("--version").should == <<-END
10
+ rak #{Rak::VERSION}
16
11
 
17
- Copyright 2008 Daniel Lucraft, all rights reserved.
18
- Based on the perl tool 'ack' by Andy Lester.
12
+ Copyright 2008-#{Time.now.year} Daniel Lucraft, all rights reserved.
13
+ Based on the perl tool 'ack' by Andy Lester.
19
14
 
20
- This program is free software; you can redistribute it and/or modify it
21
- under the same terms as Ruby.
22
- END
15
+ This program is free software; you can redistribute it and/or modify it
16
+ under the same terms as Ruby.
17
+ END
23
18
  end
24
19
 
25
20
  it "prints unknown type errors" do
26
- %x{rak Virg --type=pyth}.should == t=<<END
27
- rak: Unknown --type "pyth"
28
- rak: See rak --help types
29
- END
21
+ rak("Virg --type=pyth").should == <<-END
22
+ rak: Unknown --type "pyth"
23
+ rak: See rak --help types
24
+ END
30
25
  end
31
26
 
32
27
  it "--help prints help information" do
33
- %x{rak Virg --help}.split("\n")[0].should == "Usage: rak [OPTION]... PATTERN [FILES]"
28
+ rak("Virg --help").lines[0].should == <<-END
29
+ Usage: rak [OPTION]... PATTERN [FILES]
30
+ END
34
31
  end
35
32
 
36
33
  it "--help types prints type information" do
37
- %x{rak --help types}.split("\n")[2].should == "The following is the list of filetypes supported by rak. You can"
34
+ rak("--help types").lines[2].should == <<-END
35
+ The following is the list of filetypes supported by rak. You can
36
+ END
38
37
  end
39
38
 
40
39
  it "no options or patterns prints the usage info" do
41
- %x{rak}.split("\n")[0].should == "Usage: rak [OPTION]... PATTERN [FILES]"
40
+ rak.lines[0].should == <<-END
41
+ Usage: rak [OPTION]... PATTERN [FILES]
42
+ END
42
43
  end
43
44
 
44
45
  it "prints a nice message for unknown options" do
45
- t=<<END
46
- rak: see rak --help for usage.
47
- END
48
- %x{rak foo --asfdasfd}.include?(t).should be_true
46
+ rak("foo --asfdasfd 2>/dev/null").should include <<-END
47
+ rak: see rak --help for usage.
48
+ END
49
+ end
50
+
51
+ it "should mention every option it supports in help" do
52
+ expected = extract_options_from_help_message(File.readlines(bin_rak).grep(/GetoptLong/).join) - ['color']
53
+ actual = extract_options_from_help_message(rak()) - ['ruby']
54
+ actual.should == expected
49
55
  end
50
56
  end
@@ -1,536 +1,608 @@
1
1
 
2
- require 'fileutils'
3
-
4
- require File.dirname(__FILE__) + "/spec_helpers"
2
+ require "spec_helper"
5
3
 
6
4
  describe "Rak", "with no options" do
7
- before(:all) do
8
- ENV['RAK_TEST'] = "true"
9
- end
10
- before(:each)do
11
- FileUtils.cd(HERE+"/example/")
12
- end
13
- after(:all) do
14
- ENV['RAK_TEST'] = "false"
15
- end
16
5
  it "prints all matches from files in the current directory" do
17
- asterize_ansi(%x{rak Cap.ic}).should == t=<<END
18
- *foo.rb*
19
- 3|foo foo foo *Capric*a foo foo foo
20
- 4|foo *Capsic*um foo foo foo foo foo
21
-
22
- END
6
+ rak("Cap.ic").should == <<-END
7
+ *foo.rb*
8
+ 3|foo foo foo *Capric*a foo foo foo
9
+ 4|foo *Capsic*um foo foo foo foo foo
10
+ END
23
11
  end
24
12
 
25
13
  it "prints all matches correctly" do
26
- strip_ansi(%x{rak foo}).should == t=<<END
27
- foo.rb
28
- 3|foo foo foo Caprica foo foo foo
29
- 4|foo Capsicum foo foo foo foo foo
30
- 6|foo foo foo foo foo Pikon foo foo
31
- 8|foo Pikon foo foo foo foo foo foo
32
- 10|foo foo Six foo foo foo Six foo
33
- 11|foo foo foo foo Six foo foo foo
34
- 13|foo foo foo Gemenon foo foo foo
35
-
36
- END
14
+ rak("foo", :ansi => '').should == <<-END
15
+ foo.rb
16
+ 3|foo foo foo Caprica foo foo foo
17
+ 4|foo Capsicum foo foo foo foo foo
18
+ 6|foo foo foo foo foo Pikon foo foo
19
+ 8|foo Pikon foo foo foo foo foo foo
20
+ 10|foo foo Six foo foo foo Six foo
21
+ 11|foo foo foo foo Six foo foo foo
22
+ 13|foo foo foo Gemenon foo foo foo
23
+ END
37
24
  end
38
25
 
39
26
  it "prints all matches from files in subdirectories" do
40
- asterize_ansi(%x{rak Pikon}).should == t=<<END
41
- *dir1/bar.rb*
42
- 2|bar bar bar bar *Pikon* bar
43
- 9|bar bar *Pikon* bar bar bar
27
+ rak("Pikon").should == <<-END
28
+ *dir1/bar.rb*
29
+ 2|bar bar bar bar *Pikon* bar
30
+ 9|bar bar *Pikon* bar bar bar
44
31
 
45
- *foo.rb*
46
- 6|foo foo foo foo foo *Pikon* foo foo
47
- 8|foo *Pikon* foo foo foo foo foo foo
48
-
49
- END
32
+ *foo.rb*
33
+ 6|foo foo foo foo foo *Pikon* foo foo
34
+ 8|foo *Pikon* foo foo foo foo foo foo
35
+ END
50
36
  end
51
37
 
52
38
  it "prints multiple matches in a line" do
53
- asterize_ansi(%x{rak Six}).should == t=<<END
54
- *foo.rb*
55
- 10|foo foo *Six* foo foo foo *Six* foo
56
- 11|foo foo foo foo *Six* foo foo foo
57
-
58
- END
39
+ rak("Six").should == <<-END
40
+ *foo.rb*
41
+ 10|foo foo *Six* foo foo foo *Six* foo
42
+ 11|foo foo foo foo *Six* foo foo foo
43
+ END
59
44
  end
60
45
 
61
46
  it "skips VC dirs" do
62
- %x{rak Aerelon}.should == ""
47
+ rak("Aerelon").should == ""
63
48
  end
64
49
 
65
50
  it "does not follow symlinks" do
66
- %x{rak Sagitarron}.should == ""
51
+ rak("Sagitarron").should == ""
67
52
  end
68
53
 
69
54
  it "changes defaults when redirected" do
70
- ENV['RAK_TEST'] = "false"
71
- asterize_ansi(%x{rak Six | cat}).should == t=<<END
72
- foo.rb 10|foo foo Six foo foo foo Six foo
73
- foo.rb 11|foo foo foo foo Six foo foo foo
74
- END
75
- ENV['RAK_TEST'] = "true"
55
+ rak("Six | cat", :test_mode => false).should == <<-END
56
+ foo.rb:10:foo foo Six foo foo foo Six foo
57
+ foo.rb:11:foo foo foo foo Six foo foo foo
58
+ END
76
59
  end
77
60
 
78
61
  it "searches shebangs for valid inputs" do
79
- asterize_ansi(%x{rak Aquaria}).should == t=<<END
80
- *shebang*
81
- 3|goo goo goo *Aquaria* goo goo
82
-
83
- END
62
+ rak("Aquaria").should == <<-END
63
+ *shebang*
64
+ 3|goo goo goo *Aquaria* goo goo
65
+ END
84
66
  end
85
67
 
86
68
  it "recognizes Makefiles and Rakefiles" do
87
- asterize_ansi(%x{rak Canceron}).should == t=<<END
88
- *Rakefile*
89
- 1|rakefile rakefile *Canceron* rakefile
90
-
91
- END
69
+ rak("Canceron").should == <<-END
70
+ *Rakefile*
71
+ 1|rakefile rakefile *Canceron* rakefile
72
+ END
92
73
  end
93
74
  end
94
75
 
95
76
  describe "Rak", "with FILE or STDIN inputs" do
96
- before(:all) do
97
- ENV['RAK_TEST'] = "true"
98
- end
99
- after(:all) do
100
- ENV['RAK_TEST'] = "false"
101
- end
102
77
  it "should only search in given files or directories" do
103
- asterize_ansi(%x{rak Pikon foo.rb}).should == t=<<END
104
- 6|foo foo foo foo foo *Pikon* foo foo
105
- 8|foo *Pikon* foo foo foo foo foo foo
106
- END
107
- strip_ansi(%x{rak Pikon dir1/}).should == t=<<END
108
- dir1/bar.rb
109
- 2|bar bar bar bar Pikon bar
110
- 9|bar bar Pikon bar bar bar
111
-
112
- END
78
+ rak("Pikon foo.rb").should == <<-END
79
+ 6|foo foo foo foo foo *Pikon* foo foo
80
+ 8|foo *Pikon* foo foo foo foo foo foo
81
+ END
82
+ rak("Pikon dir1/", :ansi => '').should == <<-END
83
+ dir1/bar.rb
84
+ 2|bar bar bar bar Pikon bar
85
+ 9|bar bar Pikon bar bar bar
86
+ END
113
87
  end
114
88
 
115
89
  it "should search in STDIN by default if no files are specified" do
116
- asterize_ansi(%x{cat _darcs/baz.rb | rak Aere}).should == t=<<END
117
- 2|baz baz baz *Aere*lon baz baz baz
118
- END
90
+ rak("Aere", :pipe => "cat _darcs/baz.rb").should == <<-END
91
+ 2|baz baz baz *Aere*lon baz baz baz
92
+ END
119
93
  end
120
94
 
121
95
  it "only searches STDIN when piped to" do
122
- asterize_ansi(%x{echo asdfCapasdf | rak Cap}).should == t=<<END
123
- 1|asdf*Cap*asdf
124
- END
96
+ rak("Cap", :pipe => "echo asdfCapasdf").should == <<-END
97
+ 1|asdf*Cap*asdf
98
+ END
99
+ end
100
+
101
+ it "should either match on rax x or rak -v x" do
102
+ (rak('"\A.{0,100}\Z" dir1/bar.rb').length + rak('-v "\A.{0,100}\Z" dir1/bar.rb').length).should > 0
125
103
  end
126
104
  end
127
105
 
128
106
  describe "Rak", "options" do
129
- before(:all) do
130
- ENV['RAK_TEST'] = "true"
131
- end
132
- after(:all) do
133
- ENV['RAK_TEST'] = "false"
134
- end
135
107
  it "prints only files with --files" do
136
- t=<<END
137
- Rakefile
138
- quux.py
139
- dir1/bar.rb
140
- foo.rb
141
- shebang
142
- END
143
- sort_lines(%x{rak -f}).should == sort_lines(t)
108
+ t = <<-END
109
+ Rakefile
110
+ quux.py
111
+ dir1/bar.rb
112
+ foo.rb
113
+ shebang
114
+ END
115
+ rak("-f").lines.sort.should == t.lines.sort
144
116
  end
145
117
 
146
118
  it "prints a maximum number of matches if --max-count=x is specified" do
147
- strip_ansi(%x{rak Cap.ic -m 1}).should == t=<<END
148
- foo.rb
149
- 3|foo foo foo Caprica foo foo foo
119
+ rak("Cap.ic -m 1").should == <<-END
120
+ *foo.rb*
121
+ 3|foo foo foo *Capric*a foo foo foo
122
+ END
123
+ end
150
124
 
151
- END
125
+ it "prints context of all matches if -m and -C are both specified, without further highlighting" do
126
+ rak("Cap.ic -C2 -m 1").should == <<-END
127
+ *foo.rb*
128
+ 1|
129
+ 2|
130
+ 3|foo foo foo *Capric*a foo foo foo
131
+ 4|foo Capsicum foo foo foo foo foo
132
+ 5|
133
+ END
152
134
  end
153
135
 
154
136
  it "prints the evaluated output for --output" do
155
- strip_ansi(%x{rak Cap --output='$&'}).should == t=<<END
156
- Cap
157
- Cap
158
- END
137
+ rak("Cap --output='$&'", :ansi => '').should == <<-END
138
+ Cap
139
+ Cap
140
+ END
141
+ end
142
+
143
+ it "-v -c displays correct counts even with multiple matches per line" do
144
+ rak("-v -c bar").lines.sort.join.should == <<-END
145
+ Rakefile:1
146
+ dir1/bar.rb:7
147
+ foo.rb:13
148
+ quux.py:1
149
+ shebang:3
150
+ END
159
151
  end
160
152
 
161
153
  it "-c prints only the number of matches found per file" do
162
- t=<<END
163
- dir1/bar.rb:2
164
- foo.rb:2
165
- END
166
- sort_lines(strip_ansi(%x{rak Pik -c})).should == sort_lines(t)
154
+ rak("Pik -c").lines.sort.join.should == <<-END
155
+ dir1/bar.rb:2
156
+ foo.rb:2
157
+ END
167
158
  end
168
159
 
169
160
  it "-h suppresses filename and line number printing" do
170
- asterize_ansi(%x{rak Pik -h}).should == t=<<END
171
- bar bar bar bar *Pik*on bar
172
- bar bar *Pik*on bar bar bar
173
- foo foo foo foo foo *Pik*on foo foo
174
- foo *Pik*on foo foo foo foo foo foo
175
- END
161
+ rak("Pik -h").should == <<-END
162
+ bar bar bar bar *Pik*on bar
163
+ bar bar *Pik*on bar bar bar
164
+ foo foo foo foo foo *Pik*on foo foo
165
+ foo *Pik*on foo foo foo foo foo foo
166
+ END
176
167
  end
177
168
 
178
169
  it "ignores case with -i" do
179
- strip_ansi(%x{rak six -i}).should == t=<<END
180
- foo.rb
181
- 10|foo foo Six foo foo foo Six foo
182
- 11|foo foo foo foo Six foo foo foo
183
-
184
- END
170
+ rak("six -i").should == <<-END
171
+ *foo.rb*
172
+ 10|foo foo *Six* foo foo foo *Six* foo
173
+ 11|foo foo foo foo *Six* foo foo foo
174
+ END
185
175
  end
186
176
 
187
177
  it "inverts the match with -v" do
188
- t1=<<END
189
- quux.py
190
- 1|quux quux quux quux Virgon quux quux
191
- END
192
- t12=<<END
193
- dir1/bar.rb
194
- 1|
195
- 2|bar bar bar bar Pikon bar
196
- 3|
197
- 4|
198
- 5|
199
- 6|
200
- 7|
201
- 8|
202
- 9|bar bar Pikon bar bar bar
203
- END
204
- t2=<<END
205
- foo.rb
206
- 1|
207
- 2|
208
- 5|
209
- 7|
210
- 9|
211
- 12|
212
- END
213
- t3=<<END
214
- shebang
215
- 1|#!/usr/bin/env ruby
216
- 2|
217
- 3|goo goo goo Aquaria goo goo
218
- END
219
- t4=<<END
220
- Rakefile
221
- 1|rakefile rakefile Canceron rakefile
222
- END
223
- r = strip_ansi(%x{rak foo -v})
224
- r.include?(t1).should be_true
225
- r.include?(t12).should be_true
226
- r.include?(t2).should be_true
227
- r.include?(t3).should be_true
228
- r.include?(t4).should be_true
229
- r.split("\n").length.should == 26
178
+ t1 = <<-END
179
+ quux.py
180
+ 1|quux quux quux quux Virgon quux quux
181
+ END
182
+ t12 = <<-END
183
+ dir1/bar.rb
184
+ 1|
185
+ 2|bar bar bar bar Pikon bar
186
+ 3|
187
+ 4|
188
+ 5|
189
+ 6|
190
+ 7|
191
+ 8|
192
+ 9|bar bar Pikon bar bar bar
193
+ END
194
+ t2 = <<-END
195
+ foo.rb
196
+ 1|
197
+ 2|
198
+ 5|
199
+ 7|
200
+ 9|
201
+ 12|
202
+ END
203
+ t3 = <<-END
204
+ shebang
205
+ 1|#!/usr/bin/env ruby
206
+ 2|
207
+ 3|goo goo goo Aquaria goo goo
208
+ END
209
+ t4 = <<-END
210
+ Rakefile
211
+ 1|rakefile rakefile Canceron rakefile
212
+ END
213
+ r = rak("foo -v", :ansi => '')
214
+ r.should include t1
215
+ r.should include t12
216
+ r.should include t2
217
+ r.should include t3
218
+ r.should include t4
219
+ r.lines.length.should == 29
230
220
  end
231
221
 
232
222
  it "doesn't descend into subdirs with -n" do
233
- strip_ansi(%x{rak Pikon -n}).should == t=<<END
234
- foo.rb
235
- 6|foo foo foo foo foo Pikon foo foo
236
- 8|foo Pikon foo foo foo foo foo foo
237
-
238
- END
223
+ rak("Pikon -n", :ansi => '').should == <<-END
224
+ foo.rb
225
+ 6|foo foo foo foo foo Pikon foo foo
226
+ 8|foo Pikon foo foo foo foo foo foo
227
+ END
239
228
  end
240
229
 
241
230
  it "quotes meta-characters with -Q" do
242
- strip_ansi(%x{rak Cap. -Q}).should == ""
231
+ rak("Cap. -Q").should == ""
243
232
  end
244
233
 
245
234
  it "prints only the matching portion with -o" do
246
- strip_ansi(%x{rak Cap -o}).should == t=<<END
247
- Cap
248
- Cap
249
- END
235
+ rak("Cap -o", :ansi => '').should == <<-END
236
+ Cap
237
+ Cap
238
+ END
250
239
  end
251
240
 
252
241
  it "matches whole words only with -w" do
253
- strip_ansi(%x{rak Cap -w}).should == ""
242
+ rak("'Cap|Hat' -w").should == ""
254
243
  end
255
244
 
256
245
  it "prints the file on each line with --nogroup" do
257
- asterize_ansi(%x{rak Cap --nogroup}).should == t=<<END
258
- *foo.rb* 3|foo foo foo *Cap*rica foo foo foo
259
- *foo.rb* 4|foo *Cap*sicum foo foo foo foo foo
260
- END
246
+ rak("Cap --nogroup").should == <<-END
247
+ *foo.rb*:3:foo foo foo *Cap*rica foo foo foo
248
+ *foo.rb*:4:foo *Cap*sicum foo foo foo foo foo
249
+ END
261
250
  end
262
251
 
263
252
  it "-l means only print filenames with matches" do
264
- asterize_ansi(%x{rak Caprica -l}).should == t=<<END
265
- foo.rb
266
- END
253
+ rak("Caprica -l").should == <<-END
254
+ foo.rb
255
+ END
267
256
  end
268
257
 
269
258
  it "-L means only print filenames without matches" do
270
- t=<<END
271
- quux.py
272
- dir1/bar.rb
273
- shebang
274
- Rakefile
275
- END
276
- sort_lines(asterize_ansi(%x{rak Caprica -L})).should == sort_lines(t)
259
+ t = <<-END
260
+ quux.py
261
+ dir1/bar.rb
262
+ shebang
263
+ Rakefile
264
+ END
265
+ rak("Caprica -L", :ansi => '').lines.sort.should == t.lines.sort
277
266
  end
278
267
 
279
268
  it "--passthru means print all lines whether matching or not" do
280
- t1=<<END
281
- quux quux quux quux Virgon quux quux
282
- END
269
+ t1 = <<-END
270
+ quux quux quux quux Virgon quux quux
271
+ END
283
272
 
284
- t2=<<END
285
- *foo.rb*
286
- 3|foo foo foo *Caprica* foo foo foo
287
- foo Capsicum foo foo foo foo foo
273
+ t2 = <<-END
274
+ *foo.rb*
275
+ 3|foo foo foo *Caprica* foo foo foo
276
+ foo Capsicum foo foo foo foo foo
288
277
 
289
- foo foo foo foo foo Pikon foo foo
278
+ foo foo foo foo foo Pikon foo foo
290
279
 
291
- foo Pikon foo foo foo foo foo foo
280
+ foo Pikon foo foo foo foo foo foo
292
281
 
293
- foo foo Six foo foo foo Six foo
294
- foo foo foo foo Six foo foo foo
282
+ foo foo Six foo foo foo Six foo
283
+ foo foo foo foo Six foo foo foo
295
284
 
296
- foo foo foo Gemenon foo foo foo
297
- END
298
- t3=<<END
299
- #!/usr/bin/env ruby
285
+ foo foo foo Gemenon foo foo foo
286
+ END
287
+ t3 = <<-END
288
+ #!/usr/bin/env ruby
300
289
 
301
- goo goo goo Aquaria goo goo
302
- END
303
- r = asterize_ansi(%x{rak Caprica --passthru -n})
304
- r.include?(t1).should be_true
305
- r.include?(t2).should be_true
306
- r.include?(t3).should be_true
307
- r.split("\n").length.should < (t1+t2+t3).split("\n").length+5
290
+ goo goo goo Aquaria goo goo
291
+ END
292
+ r = rak("Caprica --passthru -n")
293
+ r.should include t1
294
+ r.should include t2
295
+ r.should include t3
296
+ r.lines.length.should < (t1+t2+t3).lines.length+5
308
297
  end
309
298
 
310
299
  it "--nocolour means do not colourize the output" do
311
- asterize_ansi(%x{rak Cap --nocolour}).should == t=<<END
312
- foo.rb
313
- 3|foo foo foo Caprica foo foo foo
314
- 4|foo Capsicum foo foo foo foo foo
315
-
316
- END
300
+ rak("Cap --nocolour").should == <<-END
301
+ foo.rb
302
+ 3|foo foo foo Caprica foo foo foo
303
+ 4|foo Capsicum foo foo foo foo foo
304
+ END
317
305
  end
318
306
 
319
307
  it "-a means to search every file" do
320
- asterize_ansi(%x{rak Libris -a}).should == t=<<END
321
- *qux*
322
- 1|qux qux qux *Libris* qux qux qux
323
-
324
- END
325
-
308
+ rak("Libris -a").should == <<-END
309
+ *qux*
310
+ 1|qux qux qux *Libris* qux qux qux
311
+ END
326
312
  end
327
313
 
328
314
  it "--ruby means only ruby files" do
329
- asterize_ansi(%x{rak Virgon --ruby}).should == ""
315
+ rak("Virgon --ruby").should == ""
330
316
  end
331
317
 
332
318
  it "--python means only python files" do
333
- asterize_ansi(%x{rak Cap --python}).should == ""
319
+ rak("Cap --python").should == ""
334
320
  end
335
321
 
336
322
  it "--noruby means exclude ruby files" do
337
- asterize_ansi(%x{rak Cap --noruby}).should == ""
323
+ rak("Cap --noruby").should == ""
338
324
  end
339
325
 
340
326
  it "--type=ruby means only ruby files" do
341
- asterize_ansi(%x{rak Virgon --type=ruby}).should == ""
327
+ rak("Virgon --type=ruby").should == ""
342
328
  end
343
329
 
344
330
  it "--type=python means only python files" do
345
- asterize_ansi(%x{rak Cap --type=python}).should == ""
331
+ rak("Cap --type=python").should == ""
346
332
  end
347
333
 
348
334
  it "--type=noruby means exclude ruby files" do
349
- asterize_ansi(%x{rak Cap --type=noruby}).should == ""
335
+ rak("Cap --type=noruby").should == ""
350
336
  end
351
337
 
352
338
  it "--sort-files" do
353
- %x{rak -f --sort-files}.should == t=<<END
354
- dir1/bar.rb
355
- foo.rb
356
- quux.py
357
- Rakefile
358
- shebang
359
- END
339
+ rak("-f --sort-files").should == <<-END
340
+ dir1/bar.rb
341
+ foo.rb
342
+ quux.py
343
+ Rakefile
344
+ shebang
345
+ END
360
346
  end
361
347
 
362
348
  it "--follow means follow symlinks" do
363
- strip_ansi(%x{rak Sagitarron --follow}).should == t=<<END
364
- corge.rb
365
- 1|corge corge corge Sagitarron corge
366
-
367
- ln_dir/corge.rb
368
- 1|corge corge corge Sagitarron corge
349
+ rak("Sagitarron --follow", :ansi => '').should == <<-END
350
+ corge.rb
351
+ 1|corge corge corge Sagitarron corge
369
352
 
370
- END
353
+ ln_dir/corge.rb
354
+ 1|corge corge corge Sagitarron corge
355
+ END
371
356
  end
372
357
 
373
358
  it "-A NUM means show NUM lines after" do
374
- strip_ansi(%x{rak Caps -A 2}).should == t=<<END
375
- foo.rb
376
- 4|foo Capsicum foo foo foo foo foo
377
- 5|
378
- 6|foo foo foo foo foo Pikon foo foo
379
-
380
- END
359
+ rak("Caps -A 2", :ansi => '').should == <<-END
360
+ foo.rb
361
+ 4|foo Capsicum foo foo foo foo foo
362
+ 5|
363
+ 6|foo foo foo foo foo Pikon foo foo
364
+ END
381
365
  end
382
366
 
383
367
  it "-A should work when there are matches close together" do
384
- strip_ansi(%x{rak foo -A 2}).should == t=<<END
385
- foo.rb
386
- 3|foo foo foo Caprica foo foo foo
387
- 4|foo Capsicum foo foo foo foo foo
388
- 5|
389
- 6|foo foo foo foo foo Pikon foo foo
390
- 7|
391
- 8|foo Pikon foo foo foo foo foo foo
392
- 9|
393
- 10|foo foo Six foo foo foo Six foo
394
- 11|foo foo foo foo Six foo foo foo
395
- 12|
396
- 13|foo foo foo Gemenon foo foo foo
397
-
398
- END
368
+ rak("foo -A 2", :ansi => '').should == <<-END
369
+ foo.rb
370
+ 3|foo foo foo Caprica foo foo foo
371
+ 4|foo Capsicum foo foo foo foo foo
372
+ 5|
373
+ 6|foo foo foo foo foo Pikon foo foo
374
+ 7|
375
+ 8|foo Pikon foo foo foo foo foo foo
376
+ 9|
377
+ 10|foo foo Six foo foo foo Six foo
378
+ 11|foo foo foo foo Six foo foo foo
379
+ 12|
380
+ 13|foo foo foo Gemenon foo foo foo
381
+ END
399
382
  end
400
383
 
401
384
  it "-B NUM means show NUM lines before" do
402
- strip_ansi(%x{rak Caps -B 2}).should == t=<<END
403
- foo.rb
404
- 2|
405
- 3|foo foo foo Caprica foo foo foo
406
- 4|foo Capsicum foo foo foo foo foo
407
-
408
- END
385
+ rak("Caps -B 2", :ansi => '').should == <<-END
386
+ foo.rb
387
+ 2|
388
+ 3|foo foo foo Caprica foo foo foo
389
+ 4|foo Capsicum foo foo foo foo foo
390
+ END
409
391
  end
410
392
 
411
393
  it "-C means show 2 lines before and after" do
412
- strip_ansi(%x{rak Caps -C}).should == t=<<END
413
- foo.rb
414
- 2|
415
- 3|foo foo foo Caprica foo foo foo
416
- 4|foo Capsicum foo foo foo foo foo
417
- 5|
418
- 6|foo foo foo foo foo Pikon foo foo
419
-
420
- END
394
+ rak("Caps -C", :ansi => '').should == <<-END
395
+ foo.rb
396
+ 2|
397
+ 3|foo foo foo Caprica foo foo foo
398
+ 4|foo Capsicum foo foo foo foo foo
399
+ 5|
400
+ 6|foo foo foo foo foo Pikon foo foo
401
+ END
421
402
  end
422
403
 
423
404
  it "-C 1 means show 1 lines before and after" do
424
- strip_ansi(%x{rak Caps -C 1}).should == t=<<END
425
- foo.rb
426
- 3|foo foo foo Caprica foo foo foo
427
- 4|foo Capsicum foo foo foo foo foo
428
- 5|
429
-
430
- END
405
+ rak("Caps -C 1", :ansi => '').should == <<-END
406
+ foo.rb
407
+ 3|foo foo foo Caprica foo foo foo
408
+ 4|foo Capsicum foo foo foo foo foo
409
+ 5|
410
+ END
431
411
  end
432
412
 
433
413
  it "-C works correctly for nearby results" do
434
- strip_ansi(%x{rak Pik -g foo -C}).should == t=<<END
435
- foo.rb
436
- 4|foo Capsicum foo foo foo foo foo
437
- 5|
438
- 6|foo foo foo foo foo Pikon foo foo
439
- 7|
440
- 8|foo Pikon foo foo foo foo foo foo
441
- 9|
442
- 10|foo foo Six foo foo foo Six foo
443
-
444
- END
414
+ rak("Pik -g foo -C", :ansi => '').should == <<-END
415
+ foo.rb
416
+ 4|foo Capsicum foo foo foo foo foo
417
+ 5|
418
+ 6|foo foo foo foo foo Pikon foo foo
419
+ 7|
420
+ 8|foo Pikon foo foo foo foo foo foo
421
+ 9|
422
+ 10|foo foo Six foo foo foo Six foo
423
+ END
445
424
  end
446
425
 
447
426
  it "-g REGEX only searches in files matching REGEX" do
448
- asterize_ansi(%x{rak Pikon -g f.o}).should == t=<<END
449
- *foo.rb*
450
- 6|foo foo foo foo foo *Pikon* foo foo
451
- 8|foo *Pikon* foo foo foo foo foo foo
452
-
453
- END
454
-
427
+ rak("Pikon -g f.o").should == <<-END
428
+ *foo.rb*
429
+ 6|foo foo foo foo foo *Pikon* foo foo
430
+ 8|foo *Pikon* foo foo foo foo foo foo
431
+ END
455
432
  end
456
433
 
457
434
  it "-k REGEX only searches in files not matching REGEX" do
458
- asterize_ansi(%x{rak Pikon -k f.o}).should == t=<<END
459
- *dir1/bar.rb*
460
- 2|bar bar bar bar *Pikon* bar
461
- 9|bar bar *Pikon* bar bar bar
462
-
463
- END
435
+ rak("Pikon -k f.o").should == <<-END
436
+ *dir1/bar.rb*
437
+ 2|bar bar bar bar *Pikon* bar
438
+ 9|bar bar *Pikon* bar bar bar
439
+ END
464
440
  end
465
441
 
466
442
  it "-x means match only whole lines" do
467
- asterize_ansi(%x{rak Cap -x}).should == ""
468
- asterize_ansi(%x{rak "(foo )+Cap\\w+( foo)+" -x}).should == t=<<END
469
- *foo.rb*
470
- 3|*foo foo foo Caprica foo foo foo*
471
- 4|*foo Capsicum foo foo foo foo foo*
472
-
473
- END
443
+ rak("Cap -x").should == ""
444
+ rak('"foo|goo" -x').should == ""
445
+ rak('"(foo )+Cap\w+( foo)+" -x').should == <<-END
446
+ *foo.rb*
447
+ 3|*foo foo foo Caprica foo foo foo*
448
+ 4|*foo Capsicum foo foo foo foo foo*
449
+ END
474
450
  end
475
451
 
476
452
  it "-s means match only at the start of a line" do
477
- asterize_ansi(%x{rak -s "foo Cap"}).should == t=<<END
478
- *foo.rb*
479
- 4|*foo Cap*sicum foo foo foo foo foo
480
-
481
- END
453
+ rak('-s "foo Cap|Aquaria"').should == <<-END
454
+ *foo.rb*
455
+ 4|*foo Cap*sicum foo foo foo foo foo
456
+ END
482
457
  end
483
458
 
484
459
  it "-e means match only at the end of a line" do
485
- asterize_ansi(%x{rak -e "kon foo foo"}).should == t=<<END
486
- *foo.rb*
487
- 6|foo foo foo foo foo Pi*kon foo foo*
488
-
489
- END
460
+ rak('-e "Aquaria|kon foo foo"').should == <<-END
461
+ *foo.rb*
462
+ 6|foo foo foo foo foo Pi*kon foo foo*
463
+ END
490
464
  end
491
465
 
492
466
  it "should not recurse down '..' when used with . " do
493
- FileUtils.cd(HERE+"/example/dir1/")
494
- asterize_ansi(%x{rak foo .}).should == t=<<END
495
- END
467
+ rak("foo .", :dir=>HERE+"example/dir1").should == ""
496
468
  end
497
469
  end
498
470
 
499
471
  describe "Rak", "with combinations of options" do
500
- before(:all) do
501
- ENV['RAK_TEST'] = "true"
502
- end
503
- before(:each)do
504
- FileUtils.cd(HERE+"/example/")
505
- end
506
- after(:all) do
507
- ENV['RAK_TEST'] = "false"
508
- end
509
-
510
472
  it "should process -c -v " do
511
- t1=<<END
512
- quux.py:1
513
- dir1/bar.rb:7
514
- foo.rb:11
515
- shebang:3
516
- Rakefile:1
517
- END
518
- sort_lines(strip_ansi(%x{rak Pikon -c -v})).should == sort_lines(t1)
473
+ t1=<<-END
474
+ quux.py:1
475
+ dir1/bar.rb:7
476
+ foo.rb:11
477
+ shebang:3
478
+ Rakefile:1
479
+ END
480
+ rak("Pikon -c -v", :ansi => '').lines.sort.should == t1.lines.sort
519
481
  end
520
482
 
521
483
  it "-h and redirection" do
522
- ENV['RAK_TEST'] = "false"
523
- %x{rak Pik -h | cat}.should == t=<<END
524
- bar bar bar bar Pikon bar
525
- bar bar Pikon bar bar bar
526
- foo foo foo foo foo Pikon foo foo
527
- foo Pikon foo foo foo foo foo foo
528
- END
529
- ENV['RAK_TEST'] = "true"
484
+ rak("Pik -h | cat", :test_mode => false).should == <<-END
485
+ bar bar bar bar Pikon bar
486
+ bar bar Pikon bar bar bar
487
+ foo foo foo foo foo Pikon foo foo
488
+ foo Pikon foo foo foo foo foo foo
489
+ END
530
490
  end
531
491
  end
532
492
 
493
+ describe "Rak", "with --eval" do
494
+ it "should support next" do
495
+ rak(%Q[--eval 'next unless $. == $_.split.size']).should == <<-END
496
+ *foo.rb*
497
+ 8|*foo Pikon foo foo foo foo foo foo*
498
+ END
499
+ end
500
+
501
+ it "should support break" do
502
+ rak(%Q[--eval 'break if $. == 2']).should == <<-END
503
+ *dir1/bar.rb*
504
+ 1|*
505
+
506
+ *foo.rb*
507
+ 1|*
508
+
509
+ *quux.py*
510
+ 1|*quux quux quux quux Virgon quux quux*
511
+
512
+ *Rakefile*
513
+ 1|*rakefile rakefile Canceron rakefile*
514
+
515
+ *shebang*
516
+ 1|*#!/usr/bin/env ruby*
517
+ END
518
+ end
519
+
520
+ it "should support highlighting" do
521
+ rak(%Q[--eval 'next unless $_ =~ /Caprica/']).should == <<-END
522
+ *foo.rb*
523
+ 3|foo foo foo *Caprica* foo foo foo
524
+ END
525
+ end
526
+
527
+ it "should support ranges" do
528
+ rak(%Q[--eval 'next unless $_[/Capsicum/]..$_[/Pikon/]']).should == <<-END
529
+ *foo.rb*
530
+ 4|*foo Capsicum foo foo foo foo foo*
531
+ 5|*
532
+ 6|foo foo foo foo foo *Pikon* foo foo
533
+ END
534
+ end
535
+
536
+ it "should support next and contexts" do
537
+ rak(%Q[-C 2 --eval 'next unless $_ =~ /Pikon/']).should == <<-END
538
+ *dir1/bar.rb*
539
+ 1|
540
+ 2|bar bar bar bar *Pikon* bar
541
+ 3|
542
+ 4|
543
+ 7|
544
+ 8|
545
+ 9|bar bar *Pikon* bar bar bar
546
+
547
+ *foo.rb*
548
+ 4|foo Capsicum foo foo foo foo foo
549
+ 5|
550
+ 6|foo foo foo foo foo *Pikon* foo foo
551
+ 7|
552
+ 8|foo *Pikon* foo foo foo foo foo foo
553
+ 9|
554
+ 10|foo foo Six foo foo foo Six foo
555
+ END
556
+ end
557
+
558
+ it "should support break and contexts (and matches past break should get no highlighting)" do
559
+ rak(%Q[-C 2 --eval 'next unless $_[/Pikon/]...(break; nil)']).should == <<-END
560
+ *dir1/bar.rb*
561
+ 1|
562
+ 2|bar bar bar bar *Pikon* bar
563
+ 3|
564
+ 4|
565
+
566
+ *foo.rb*
567
+ 4|foo Capsicum foo foo foo foo foo
568
+ 5|
569
+ 6|foo foo foo foo foo *Pikon* foo foo
570
+ 7|
571
+ 8|foo Pikon foo foo foo foo foo foo
572
+ END
573
+ end
574
+
575
+ it "should support $_ transformations" do
576
+ rak(%Q[--eval '$_=$_.reverse; next unless $_ =~ /oof/']).should == <<-END
577
+ *foo.rb*
578
+ 3|*oof* oof oof acirpaC oof oof oof
579
+ 4|*oof* oof oof oof oof mucispaC oof
580
+ 6|*oof* oof nokiP oof oof oof oof oof
581
+ 8|*oof* oof oof oof oof oof nokiP oof
582
+ 10|*oof* xiS oof oof oof xiS oof oof
583
+ 11|*oof* oof oof xiS oof oof oof oof
584
+ 13|*oof* oof oof nonemeG oof oof oof
585
+ END
586
+ end
587
+
588
+ it "should support multiple matches" do
589
+ rak(%Q[--eval 'next unless $_ =~ /Pikon/; $_.scan(/\\b\\w{3}\\b/){ matches << $~ }']).should == <<-END
590
+ *dir1/bar.rb*
591
+ 2|*bar* *bar* *bar* *bar* Pikon *bar*
592
+ 9|*bar* *bar* Pikon *bar* *bar* *bar*
593
+
594
+ *foo.rb*
595
+ 6|*foo* *foo* *foo* *foo* *foo* Pikon *foo* *foo*
596
+ 8|*foo* Pikon *foo* *foo* *foo* *foo* *foo* *foo*
597
+ END
598
+ end
599
+ end
533
600
 
534
-
535
-
536
-
601
+ ## Other things --eval could support:
602
+ # * per file BEGIN{} END{}
603
+ # * global BEGIN{} END{}
604
+ # * better way to highlight multiple matches
605
+ #
606
+ # There's also redo but I cannot think of any sensible use
607
+ # (especially if $_ won't persist between runs)
608
+ # (also redo in -nl/-pl is broken)