bovem 0.8.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bovem/version.rb CHANGED
@@ -11,13 +11,13 @@ module Bovem
11
11
  # @see http://semver.org
12
12
  module Version
13
13
  # The major version.
14
- MAJOR = 0
14
+ MAJOR = 1
15
15
 
16
16
  # The minor version.
17
- MINOR = 8
17
+ MINOR = 0
18
18
 
19
19
  # The patch version.
20
- PATCH = 1
20
+ PATCH = 0
21
21
 
22
22
  # The current version number of Bovem.
23
23
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
@@ -61,7 +61,7 @@ describe Bovem::Console do
61
61
 
62
62
  describe "#get_screen_width" do
63
63
  it "should execute tput cols" do
64
- ::Bovem::Console.should_receive("execute").with("tput cols")
64
+ ::Bovem::Console.should_receive(:execute).with("tput cols")
65
65
  console.get_screen_width
66
66
  end
67
67
 
@@ -159,7 +159,7 @@ describe Bovem::Console do
159
159
 
160
160
  describe "#replace_markers" do
161
161
  it "should just forwards to .replace_markers" do
162
- ::Bovem::Console.should_receive("replace_markers").with("A", "B")
162
+ ::Bovem::Console.should_receive(:replace_markers).with("A", "B")
163
163
  console.replace_markers("A", "B")
164
164
  end
165
165
  end
@@ -181,7 +181,7 @@ describe Bovem::Console do
181
181
 
182
182
  describe "#write" do
183
183
  it "should call #format" do
184
- console.should_receive("format").with("A", "B", "C", "D", "E")
184
+ console.should_receive(:format).with("A", "B", "C", "D", "E")
185
185
  console.write("A", "B", "C", "D", "E")
186
186
  end
187
187
  end
@@ -199,85 +199,84 @@ describe Bovem::Console do
199
199
 
200
200
  describe "#info" do
201
201
  it "should forward everything to #get_banner" do
202
- console.should_receive("get_banner").with(" INFO", "bright cyan", false).at_least(1).and_return("")
202
+ console.should_receive(:get_banner).with(" INFO", "bright cyan", false).at_least(1).and_return("")
203
203
  console.info("OK", "\n", true, false, false, false, false, false)
204
- console.should_receive("get_banner").with(" INFO", "bright cyan", true).at_least(1).and_return("")
204
+ console.should_receive(:get_banner).with(" INFO", "bright cyan", true).at_least(1).and_return("")
205
205
  console.info("OK", "\n", true, false, false, false, true, false)
206
206
  end
207
207
 
208
208
  it "should forward everything to #write" do
209
- console.should_receive("write").with(/.+/, "B", "C", "D", "E", false)
209
+ console.should_receive(:write).with(/.+/, "B", "C", "D", "E", false)
210
210
  console.info("A", "B", "C", "D", "E", "F", "G", false)
211
211
  end
212
212
  end
213
213
 
214
214
  describe "#begin" do
215
215
  it "should forward everything to #get_banner" do
216
- console.should_receive("get_banner").with("*", "bright green").at_least(1).and_return("")
216
+ console.should_receive(:get_banner).with("*", "bright green").at_least(1).and_return("")
217
217
  console.begin("OK", "\n", true, false, false, false, false, false)
218
218
  end
219
219
 
220
220
  it "should forward everything to #write" do
221
- console.should_receive("write").with(/.+/, "B", "C", "D", "E", false)
221
+ console.should_receive(:write).with(/.+/, "B", "C", "D", "E", false)
222
222
  console.begin("A", "B", "C", "D", "E", "F", "G", false)
223
223
  end
224
224
  end
225
225
 
226
226
  describe "#warn" do
227
227
  it "should forward everything to #get_banner" do
228
- console.should_receive("get_banner").with(" WARN", "bright yellow", false).at_least(1).and_return("")
228
+ console.should_receive(:get_banner).with(" WARN", "bright yellow", false).at_least(1).and_return("")
229
229
  console.warn("OK", "\n", true, false, false, false, false, false)
230
- console.should_receive("get_banner").with(" WARN", "bright yellow", true).at_least(1).and_return("")
230
+ console.should_receive(:get_banner).with(" WARN", "bright yellow", true).at_least(1).and_return("")
231
231
  console.warn("OK", "\n", true, false, false, false, true, false)
232
232
  end
233
233
 
234
234
  it "should forward everything to #write" do
235
- console.should_receive("write").with(/.+/, "B", "C", "D", "E", false)
235
+ console.should_receive(:write).with(/.+/, "B", "C", "D", "E", false)
236
236
  console.warn("A", "B", "C", "D", "E", "F", "G", false)
237
237
  end
238
238
  end
239
239
 
240
240
  describe "#error" do
241
241
  it "should forward everything to #get_banner" do
242
- console.should_receive("get_banner").with("ERROR", "bright red", false).at_least(1).and_return("")
242
+ console.should_receive(:get_banner).with("ERROR", "bright red", false).at_least(1).and_return("")
243
243
  console.error("OK", "\n", true, false, false, false, false, false)
244
- console.should_receive("get_banner").with("ERROR", "bright red", true).at_least(1).and_return("")
244
+ console.should_receive(:get_banner).with("ERROR", "bright red", true).at_least(1).and_return("")
245
245
  console.error("OK", "\n", true, false, false, false, true, false)
246
246
  end
247
247
 
248
248
  it "should forward everything to #write" do
249
- console.should_receive("write").with(/.+/, "B", "C", "D", "E", false)
249
+ console.should_receive(:write).with(/.+/, "B", "C", "D", "E", false)
250
250
  console.error("A", "B", "C", "D", "E", "F", "G", false)
251
251
  end
252
252
  end
253
253
 
254
254
  describe "#fatal" do
255
255
  it "should forward anything to #error" do
256
- Kernel.stub(:abort).and_return(true)
257
- console.should_receive("error").with("A", "B", "C", "D", "E", "F", "G", false)
256
+ Kernel.stub(:exit).and_return(true)
257
+ console.should_receive(:error).with("A", "B", "C", "D", "E", "F", "G", false)
258
258
  console.fatal("A", "B", "C", "D", "E", "F", "G", "H", false)
259
259
  end
260
260
 
261
261
  it "should call abort with the right error code" do
262
- Kernel.stub(:abort).and_return(true)
262
+ Kernel.stub(:exit).and_return(true)
263
263
 
264
- Kernel.should_receive("abort").with(-1).at_least(1)
264
+ Kernel.should_receive(:exit).with(-1).exactly(2)
265
265
  console.fatal("A", "B", "C", "D", "E", "F", "G", -1, false)
266
- Kernel.should_receive("abort").with("H").at_least(1)
267
266
  console.fatal("A", "B", "C", "D", "E", "F", "G", "H", false)
268
267
  end
269
268
  end
270
269
 
271
270
  describe "#debug" do
272
271
  it "should forward everything to #get_banner" do
273
- console.should_receive("get_banner").with("DEBUG", "bright magenta", false).at_least(1).and_return("")
272
+ console.should_receive(:get_banner).with("DEBUG", "bright magenta", false).at_least(1).and_return("")
274
273
  console.debug("OK", "\n", true, false, false, false, false, false)
275
- console.should_receive("get_banner").with("DEBUG", "bright magenta", true).at_least(1).and_return("")
274
+ console.should_receive(:get_banner).with("DEBUG", "bright magenta", true).at_least(1).and_return("")
276
275
  console.debug("OK", "\n", true, false, false, false, true, false)
277
276
  end
278
277
 
279
278
  it "should forward everything to #write" do
280
- console.should_receive("write").with(/.+/, "B", "C", "D", "E", false)
279
+ console.should_receive(:write).with(/.+/, "B", "C", "D", "E", false)
281
280
  console.debug("A", "B", "C", "D", "E", "F", "G", false)
282
281
  end
283
282
  end
@@ -288,29 +287,31 @@ describe Bovem::Console do
288
287
  expect(console.status(:pass, false, true, false)).to eq({:label => "PASS", :color => "bright cyan"})
289
288
  expect(console.status(:warn, false, true, false)).to eq({:label => "WARN", :color => "bright yellow"})
290
289
  expect(console.status(:fail, false, true, false)).to eq({:label => "FAIL", :color => "bright red"})
291
- expect(console.status("NO", false, true, false)).to eq({:label => "PASS", :color => "bright cyan"})
292
- expect(console.status(nil, false, true, false)).to eq({:label => "PASS", :color => "bright cyan"})
290
+ expect(console.status("NO", false, true, false)).to eq({:label => " OK ", :color => "bright green"})
291
+ expect(console.status(nil, false, true, false)).to eq({:label => " OK ", :color => "bright green"})
293
292
  end
294
293
 
295
294
  it "should create the banner" do
296
- console.should_receive("get_banner").with(" OK ", "bright green").and_return("")
295
+ console.should_receive(:get_banner).with(" OK ", "bright green").and_return("")
297
296
  console.status(:ok)
298
297
  end
299
298
 
300
299
  it "should format correctly" do
301
- console.should_receive("format_right").with(/.+/, true, true, false)
300
+ console.should_receive(:format_right).with(/.+/, true, true, false)
302
301
  console.status(:ok, false, true)
303
- console.should_receive("format").with(/.+/, "\n", true, true, false)
302
+ console.should_receive(:format).with(/.+/, "\n", true, true, false)
304
303
  console.status(:ok, false, true, false)
305
304
  end
306
305
  end
307
306
 
308
307
  describe "#read" do
309
308
  it "should show a prompt" do
309
+ Kernel.stub(:gets).and_return("VALUE\n")
310
+
310
311
  prompt = "PROMPT"
311
- Kernel.should_receive("print").with("Please insert a value: ")
312
+ Kernel.should_receive(:print).with("Please insert a value: ")
312
313
  console.read(true)
313
- Kernel.should_receive("print").with(prompt + ": ")
314
+ Kernel.should_receive(:print).with(prompt + ": ")
314
315
  console.read(prompt)
315
316
  Kernel.should_not_receive("print")
316
317
  console.read(nil)
@@ -340,7 +341,7 @@ describe Bovem::Console do
340
341
  end
341
342
  end
342
343
 
343
- console.should_receive("write").with("Sorry, your reply was not understood. Please try again.", false, false).exactly(4)
344
+ console.should_receive(:write).with("Sorry, your reply was not understood. Please try again.", false, false).exactly(4)
344
345
  count = 0
345
346
  console.read(nil, nil, "A")
346
347
  count = 0
@@ -363,25 +364,27 @@ describe Bovem::Console do
363
364
  end
364
365
  end
365
366
 
366
- console.should_receive("write").with("Sorry, your reply was not understood. Please try again.", false, false)
367
+ console.should_receive(:write).with("Sorry, your reply was not understood. Please try again.", false, false)
367
368
  console.read(nil, nil, /[abc]/)
368
369
  end
369
370
 
370
371
  it "should hide echo to the user when the terminal shows echo" do
372
+ Kernel.stub(:gets).and_return("VALUE\n")
371
373
  stty = %x{which stty}.strip
372
374
 
373
- ::Bovem::Console.should_receive("execute").with("which stty").and_return(stty)
374
- ::Bovem::Console.should_receive("execute").with(stty).and_return("speed 9600 baud;\nlflags: echoe echoke echoctl pendin\niflags: iutf8\noflags: -oxtabs\ncflags: cs8 -parenb")
375
- ::Bovem::Console.should_receive("execute").with("#{stty} -echo")
376
- ::Bovem::Console.should_receive("execute").with("#{stty} echo")
375
+ ::Bovem::Console.should_receive(:execute).with("which stty").and_return(stty)
376
+ ::Bovem::Console.should_receive(:execute).with(stty).and_return("speed 9600 baud;\nlflags: echoe echoke echoctl pendin\niflags: iutf8\noflags: -oxtabs\ncflags: cs8 -parenb")
377
+ ::Bovem::Console.should_receive(:execute).with("#{stty} -echo")
378
+ ::Bovem::Console.should_receive(:execute).with("#{stty} echo")
377
379
  console.read(nil, nil, nil, false)
378
380
  end
379
381
 
380
382
  it "shouldn't hide echo again when the terminal already hides it" do
383
+ Kernel.stub(:gets).and_return("VALUE\n")
381
384
  stty = %x{which stty}.strip
382
385
 
383
- ::Bovem::Console.should_receive("execute").with("which stty").and_return(stty)
384
- ::Bovem::Console.should_receive("execute").with(stty).and_return("speed 9600 baud;\nlflags: -echo echoe echoke echoctl pendin\niflags: iutf8\noflags: -oxtabs\ncflags: cs8 -parenb")
386
+ ::Bovem::Console.should_receive(:execute).with("which stty").and_return(stty)
387
+ ::Bovem::Console.should_receive(:execute).with(stty).and_return("speed 9600 baud;\nlflags: -echo echoe echoke echoctl pendin\niflags: iutf8\noflags: -oxtabs\ncflags: cs8 -parenb")
385
388
  ::Bovem::Console.should_not_receive("execute").with("#{stty} -echo")
386
389
  ::Bovem::Console.should_not_receive("execute").with("#{stty} echo")
387
390
  console.read(nil, nil, nil, false)
@@ -395,13 +398,13 @@ describe Bovem::Console do
395
398
  end
396
399
 
397
400
  it "should print the message and indentate correctly" do
398
- console.should_receive("begin").with("A", "B", "C", "D", "E", "F", "G")
399
- console.should_receive("with_indentation").with("H", "I")
401
+ console.should_receive(:begin).with("A", "B", "C", "D", "E", "F", "G")
402
+ console.should_receive(:with_indentation).with("H", "I")
400
403
  console.task("A", "B", "C", "D", "E", "F", "G", "H", "I") { :ok }
401
404
  end
402
405
 
403
406
  it "should execute the given block" do
404
- ::Bovem::Console.should_receive("foo")
407
+ ::Bovem::Console.should_receive(:foo)
405
408
  console.task { ::Bovem::Console.foo }
406
409
  end
407
410
 
@@ -0,0 +1,473 @@
1
+ # encoding: utf-8
2
+ #
3
+ # This file is part of the bovem gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
4
+ # Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
5
+ #
6
+
7
+ require "spec_helper"
8
+
9
+ describe Bovem::Shell do
10
+ let(:shell) { ::Bovem::Shell.new }
11
+ let(:temp_file_1) { "/tmp/bovem-test-1-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
12
+ let(:temp_file_2) { "/tmp/bovem-test-2-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
13
+ let(:temp_file_3) { "/tmp/bovem-test-3-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
14
+ let(:temp_dir_1) { "/tmp/bovem-test-dir-1-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
15
+ let(:temp_dir_2) { "/tmp/bovem-test-dir-2-#{Time.now.strftime("%Y%m%d-%H%M%S")}" }
16
+
17
+ before(:each) do
18
+ Kernel.stub(:puts).and_return(nil)
19
+ end
20
+
21
+ describe ".instance" do
22
+ it "should always return the same instance" do
23
+ instance = ::Bovem::Shell.instance
24
+ expect(::Bovem::Shell.instance).to be(instance)
25
+ end
26
+ end
27
+
28
+ describe "#initialize" do
29
+ it "should correctly set defaults" do
30
+ expect(shell.console).to eq(::Bovem::Console.instance)
31
+ end
32
+ end
33
+
34
+ describe "#run" do
35
+ it "should show a message" do
36
+ shell.console.should_receive("begin").with("MESSAGE")
37
+ shell.run("echo OK", "MESSAGE", true, false)
38
+ shell.console.should_not_receive("begin").with("MESSAGE")
39
+ shell.run("echo OK", nil, true, false)
40
+ end
41
+
42
+ it "should only print the command" do
43
+ shell.console.should_receive("warn").with("Will run command: {mark=bright}\"echo OK\"{/mark}...")
44
+ ::Open4.should_not_receive("open4")
45
+ shell.run("echo OK", nil, false, false)
46
+ end
47
+
48
+ it "should only execute a command" do
49
+ shell.console.should_not_receive("warn").with("Will run command: {mark=bright}\"echo OK\"{/mark}...")
50
+ ::Open4.should_receive("open4").and_return(::OpenStruct.new(:exitstatus => 0))
51
+ shell.run("echo OK", nil, true, false)
52
+ end
53
+
54
+ it "should show a exit message" do
55
+ shell.console.should_receive(:status).with(:ok)
56
+ shell.run("echo OK", nil, true, true)
57
+ shell.console.should_receive(:status).with(:fail)
58
+ shell.run("echo1 OK", nil, true, true, false, false)
59
+ end
60
+
61
+ it "should print output" do
62
+ Kernel.should_receive("print").with("OK\n")
63
+ shell.run("echo OK", nil, true, false, true)
64
+ end
65
+
66
+ it "should raise a exception for failures" do
67
+ expect { shell.run("echo1 OK", nil, true, false, false, false) }.to_not raise_error(SystemExit)
68
+ expect { shell.run("echo1 OK", nil, true, false, false) }.to raise_error(SystemExit)
69
+ end
70
+ end
71
+
72
+ describe "#check" do
73
+ it "executes all tests" do
74
+ expect(shell.check("/", [:read, :dir])).to be_true
75
+ expect(shell.check("/dev/null", :write)).to be_true
76
+ expect(shell.check("/bin/sh", [:execute, :exec])).to be_true
77
+ expect(shell.check("/", [:read, :directory])).to be_true
78
+ expect(shell.check("/", [:writable?, :directory?])).to be_false
79
+ end
80
+
81
+ it "returns false when some tests are invalid" do
82
+ expect(shell.check("/", [:read, :none])).to be_false
83
+ end
84
+ end
85
+
86
+ describe "#delete" do
87
+ it "should delete files" do
88
+ File.unlink(temp_file_1) if File.exists?(temp_file_1)
89
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
90
+
91
+ expect(File.exists?(temp_file_1)).to be_true
92
+ expect(shell.delete(temp_file_1, true, false)).to be_true
93
+ expect(File.exists?(temp_file_1)).to be_false
94
+ File.unlink(temp_file_1) if File.exists?(temp_file_1)
95
+ end
96
+
97
+ it "should only print the list of files" do
98
+ shell.console.should_receive(:warn).with("Will remove file(s):")
99
+ FileUtils.should_not_receive(:rm_r)
100
+ expect(shell.delete(temp_file_1, false)).to be_true
101
+ end
102
+
103
+ it "should complain about non existing files" do
104
+ shell.console.should_receive(:error).with("Cannot remove following non existent file: {mark=bright}#{temp_file_1}{/mark}")
105
+ expect(shell.delete(temp_file_1, true, true, false)).to be_false
106
+ end
107
+
108
+ it "should complain about non writeable files" do
109
+ shell.console.should_receive(:error).with("Cannot remove following non writable file: {mark=bright}/dev/null{/mark}")
110
+ expect(shell.delete("/dev/null", true, true, false)).to be_false
111
+ end
112
+
113
+ it "should complain about other exceptions" do
114
+ FileUtils.stub(:rm_r).and_raise(ArgumentError.new("ERROR"))
115
+ shell.console.should_receive(:error).with("Cannot remove following file(s):")
116
+ shell.console.should_receive(:write).at_least(2)
117
+ expect(shell.delete("/dev/null", true, true, false)).to be_false
118
+ end
119
+
120
+ describe "should exit when requested to" do
121
+ it "by calling :fatal" do
122
+ shell.console.should_receive(:fatal).with("Cannot remove following non writable file: {mark=bright}/dev/null{/mark}")
123
+ expect(shell.delete("/dev/null")).to be_false
124
+ end
125
+
126
+ it "by calling Kernel#exit" do
127
+ FileUtils.stub(:rm_r).and_raise(ArgumentError.new("ERROR"))
128
+ Kernel.should_receive(:exit).with(-1)
129
+ expect(shell.delete("/dev/null", true, true)).to be_false
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "#copy_or_move" do
135
+ before(:each) do
136
+ FileUtils.rm_r(temp_file_1) if File.exists?(temp_file_1)
137
+ FileUtils.rm_r(temp_file_2) if File.exists?(temp_file_2)
138
+ FileUtils.rm_r(temp_file_3) if File.exists?(temp_file_3)
139
+ FileUtils.rm_r(temp_dir_1) if File.exists?(temp_dir_1)
140
+ FileUtils.rm_r(temp_dir_2) if File.exists?(temp_dir_2)
141
+ end
142
+
143
+ after(:each) do
144
+ FileUtils.rm_r(temp_file_1) if File.exists?(temp_file_1)
145
+ FileUtils.rm_r(temp_file_2) if File.exists?(temp_file_2)
146
+ FileUtils.rm_r(temp_file_3) if File.exists?(temp_file_3)
147
+ FileUtils.rm_r(temp_dir_1) if File.exists?(temp_dir_1)
148
+ FileUtils.rm_r(temp_dir_2) if File.exists?(temp_dir_2)
149
+ end
150
+
151
+ it "should copy a file" do
152
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
153
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :copy)).to eq(true)
154
+ expect(File.exists?(temp_file_1)).to be_true
155
+ expect(File.exists?(temp_file_2)).to be_true
156
+ end
157
+
158
+ it "should move a file" do
159
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
160
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :move, true)).to eq(true)
161
+ expect(File.exists?(temp_file_1)).to be_false
162
+ expect(File.exists?(temp_file_2)).to be_true
163
+ end
164
+
165
+ it "should copy multiple entries" do
166
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
167
+ File.open(temp_file_2, "w") {|f| f.write("OK") }
168
+ shell.create_directories(temp_dir_1)
169
+ File.open(temp_dir_1 + "/temp", "w") {|f| f.write("OK") }
170
+
171
+ expect(shell.copy_or_move([temp_file_1, temp_file_2, temp_dir_1], temp_dir_2, :copy)).to be_true
172
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_file_1))).to be_true
173
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_file_2))).to be_true
174
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_dir_1))).to be_true
175
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_dir_1) + "/temp")).to be_true
176
+ end
177
+
178
+ it "should move multiple entries" do
179
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
180
+ File.open(temp_file_2, "w") {|f| f.write("OK") }
181
+ shell.create_directories(temp_dir_1)
182
+ File.open(temp_dir_1 + "/temp", "w") {|f| f.write("OK") }
183
+
184
+ expect(shell.copy_or_move([temp_file_1, temp_file_2, temp_dir_1], temp_dir_2, :move, true)).to be_true
185
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_file_1))).to be_true
186
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_file_2))).to be_true
187
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_dir_1))).to be_true
188
+ expect(File.exists?(temp_dir_2 + "/" + File.basename(temp_dir_1) + "/temp")).to be_true
189
+ expect(File.exists?(temp_file_1)).to be_false
190
+ expect(File.exists?(temp_file_2)).to be_false
191
+ expect(File.exists?(temp_dir_1)).to be_false
192
+ expect(File.exists?(temp_dir_1 + "/temp")).to be_false
193
+ end
194
+
195
+ it "should complain about non existing source" do
196
+ shell.console.should_receive(:error).with("Cannot copy non existent file {mark=bright}#{temp_file_1}{/mark}.")
197
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :copy, true, false, false)).to be_false
198
+
199
+ shell.console.should_receive(:error).with("Cannot move non existent file {mark=bright}#{temp_file_1}{/mark}.")
200
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :move, true, false, false)).to be_false
201
+ end
202
+
203
+ it "should not copy a file to a path which is currently a directory" do
204
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
205
+ shell.create_directories(temp_file_2)
206
+
207
+ shell.console.should_receive(:error).with("Cannot copy file {mark=bright}#{temp_file_1}{/mark} to {mark=bright}#{temp_file_2}{/mark} because it is currently a directory.")
208
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :copy, true, false, false)).to be_false
209
+
210
+ shell.console.should_receive(:error).with("Cannot move file {mark=bright}#{temp_file_1}{/mark} to {mark=bright}#{temp_file_2}{/mark} because it is currently a directory.")
211
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :move, true, false, false)).to be_false
212
+ end
213
+
214
+ it "should create the parent directory if needed" do
215
+ expect(shell.check(temp_dir_1, :dir)).to be_false
216
+
217
+ shell.should_receive(:create_directories).exactly(2)
218
+ expect(shell.copy_or_move(temp_file_1, temp_dir_1 + "/test-1", :copy)).to be_false
219
+ expect(shell.copy_or_move(temp_file_1, temp_dir_1 + "/test-1", :move)).to be_false
220
+ end
221
+
222
+ it "should only print the list of files" do
223
+ FileUtils.should_not_receive(:cp_r)
224
+ FileUtils.should_not_receive(:move)
225
+
226
+ shell.console.should_receive(:warn).with("Will copy a file:")
227
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :copy, false)).to be_true
228
+ shell.console.should_receive(:warn).with("Will copy following entries:")
229
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], temp_dir_1, :copy, false)).to be_true
230
+
231
+ shell.console.should_receive(:warn).with("Will move a file:")
232
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :move, false)).to be_true
233
+ shell.console.should_receive(:warn).with("Will move following entries:")
234
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], temp_dir_1, :move, false)).to be_true
235
+ end
236
+
237
+ it "should complain about non writeable parent directory" do
238
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
239
+
240
+ shell.console.should_receive(:error).with("Cannot copy file {mark=bright}#{temp_file_1}{/mark} to non writable directory {mark=bright}/dev{/mark}.")
241
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :copy, true, false, false)).to be_false
242
+
243
+ shell.console.should_receive(:error).with("Cannot move file {mark=bright}#{temp_file_1}{/mark} to non writable directory {mark=bright}/dev{/mark}.")
244
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :move, true, false, false)).to be_false
245
+ end
246
+
247
+ it "should complain about other exceptions" do
248
+ FileUtils.stub(:cp_r).and_raise(ArgumentError.new("ERROR"))
249
+ FileUtils.stub(:move).and_raise(ArgumentError.new("ERROR"))
250
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
251
+
252
+ shell.console.should_receive(:error).with("Cannot copy file {mark=bright}#{temp_file_1}{/mark} to directory {mark=bright}#{File.dirname(temp_file_2)}{/mark} due to this error: [ArgumentError] ERROR.", "\n", 5)
253
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :copy, true, false, false)).to be_false
254
+
255
+ shell.console.should_receive(:error).with("Cannot move file {mark=bright}#{temp_file_1}{/mark} to directory {mark=bright}#{File.dirname(temp_file_2)}{/mark} due to this error: [ArgumentError] ERROR.", "\n", 5)
256
+ expect(shell.copy_or_move(temp_file_1, temp_file_2, :move, true, false, false)).to be_false
257
+ end
258
+
259
+ describe "should exit when requested to" do
260
+ it "by calling :fatal" do
261
+ FileUtils.stub(:cp_r).and_raise(ArgumentError.new("ERROR"))
262
+ FileUtils.stub(:move).and_raise(ArgumentError.new("ERROR"))
263
+
264
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
265
+ File.open(temp_file_2, "w") {|f| f.write("OK") }
266
+
267
+ shell.console.should_receive(:fatal).with("Cannot copy file {mark=bright}#{temp_file_1}{/mark} to directory {mark=bright}/dev{/mark} due to this error: [ArgumentError] ERROR.", "\n", 5)
268
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :copy, true, false, true)).to be_false
269
+
270
+ shell.console.should_receive(:fatal).with("Cannot move file {mark=bright}#{temp_file_1}{/mark} to directory {mark=bright}/dev{/mark} due to this error: [ArgumentError] ERROR.", "\n", 5)
271
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :move, true, false, true)).to be_false
272
+
273
+ Kernel.stub(:exit).and_return(true)
274
+ shell.console.should_receive(:error).with("Cannot copy following entries to {mark=bright}/dev{/mark}:")
275
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], "/dev", :copy, true, false, true)).to be_false
276
+
277
+ shell.console.should_receive(:error).with("Cannot move following entries to {mark=bright}/dev{/mark}:")
278
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], "/dev", :move, true, false, true)).to be_false
279
+ end
280
+
281
+ it "by calling Kernel#exit" do
282
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
283
+ File.open(temp_file_2, "w") {|f| f.write("OK") }
284
+
285
+ Kernel.should_receive(:exit).with(-1).exactly(4).and_return(true)
286
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :copy, true, false, true)).to be_false
287
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], "/dev", :copy, true, false, true)).to be_false
288
+ expect(shell.copy_or_move(temp_file_1, "/dev/bovem", :move, true, false, true)).to be_false
289
+ expect(shell.copy_or_move([temp_file_1, temp_file_2], "/dev", :move, true, false, true)).to be_false
290
+ end
291
+ end
292
+ end
293
+
294
+ describe "#copy" do
295
+ it "should forward everything to #copy_or_move" do
296
+ shell.should_receive(:copy_or_move).with("A", "B", :copy, "C", "D", "E")
297
+ shell.copy("A", "B", "C", "D", "E")
298
+ end
299
+ end
300
+
301
+ describe "#move" do
302
+ it "should forward everything to #copy_or_move" do
303
+ shell.should_receive(:copy_or_move).with("A", "B", :move, "C", "D", "E")
304
+ shell.move("A", "B", "C", "D", "E")
305
+ end
306
+ end
307
+
308
+ describe "#within_directory" do
309
+ let(:target){ File.expand_path("~") }
310
+
311
+ it "should execute block in other directory and return true" do
312
+ owd = Dir.pwd
313
+ dir = ""
314
+
315
+ shell.within_directory(target) do
316
+ expect(Dir.pwd).to eq(target)
317
+ dir = "OK"
318
+ end
319
+
320
+ expect(shell.within_directory(target) { dir = "OK" }).to be_true
321
+ end
322
+
323
+ it "should change and restore directory" do
324
+ owd = Dir.pwd
325
+
326
+ shell.within_directory(target) do
327
+ expect(Dir.pwd).to eq(target)
328
+ end
329
+
330
+ expect(Dir.pwd).to eq(owd)
331
+ end
332
+
333
+ it "should change but not restore directory" do
334
+ owd = Dir.pwd
335
+
336
+ shell.within_directory(target) do
337
+ expect(Dir.pwd).to eq(target)
338
+ end
339
+
340
+ expect(Dir.pwd).not_to eq(target)
341
+ end
342
+
343
+ it "should show messages" do
344
+ shell.console.should_receive(:info).with(/Moving into directory \{mark=bright\}(.+)\{\/mark\}/)
345
+ shell.within_directory(target, true, true) { "OK" }
346
+ end
347
+
348
+ it "should return false and not execute code in case of invalid directory" do
349
+ dir = ""
350
+
351
+ expect(shell.within_directory("/invalid") { dir = "OK" }).to be_false
352
+ expect(dir).to eq("")
353
+
354
+ Dir.stub(:chdir).and_raise(ArgumentError)
355
+ expect(shell.within_directory("/") { dir = "OK" }).to be_false
356
+
357
+ Dir.unstub(:chdir)
358
+ Dir.stub(:pwd).and_return("/invalid")
359
+ expect(shell.within_directory("/") { dir = "OK" }).to be_false
360
+ end
361
+ end
362
+
363
+ describe "#create_directories" do
364
+ before(:each) do
365
+ FileUtils.rm_r(temp_file_1) if File.exists?(temp_file_1)
366
+ FileUtils.rm_r(temp_file_2) if File.exists?(temp_file_2)
367
+ FileUtils.rm_r(temp_file_3) if File.exists?(temp_file_3)
368
+ FileUtils.rm_r(temp_dir_1) if File.exists?(temp_dir_1)
369
+ FileUtils.rm_r(temp_dir_2) if File.exists?(temp_dir_2)
370
+ end
371
+
372
+ after(:each) do
373
+ FileUtils.rm_r(temp_file_1) if File.exists?(temp_file_1)
374
+ FileUtils.rm_r(temp_file_2) if File.exists?(temp_file_2)
375
+ FileUtils.rm_r(temp_file_3) if File.exists?(temp_file_3)
376
+ FileUtils.rm_r(temp_dir_1) if File.exists?(temp_dir_1)
377
+ FileUtils.rm_r(temp_dir_2) if File.exists?(temp_dir_2)
378
+ end
379
+
380
+ it "should create directory" do
381
+ expect(shell.create_directories([temp_dir_1, temp_dir_2])).to be_true
382
+ expect(shell.check(temp_dir_1, :directory)).to be_true
383
+ expect(shell.check(temp_dir_2, :directory)).to be_true
384
+ end
385
+
386
+ it "should only print the list of files" do
387
+ shell.console.should_receive(:warn).with("Will create directories:")
388
+ FileUtils.should_not_receive(:mkdir_p)
389
+ expect(shell.create_directories(temp_file_1, 0755, false)).to be_true
390
+ end
391
+
392
+ it "should complain about directory already existing" do
393
+ shell.create_directories(temp_dir_1, 0755, true, false, false)
394
+ shell.console.should_receive(:error).with("The directory {mark=bright}#{temp_dir_1}{/mark} already exists.")
395
+ expect(shell.create_directories(temp_dir_1, 0755, true, false, false)).to be_false
396
+ end
397
+
398
+ it "should complain about paths already existing as a file." do
399
+ File.open(temp_file_1, "w") {|f| f.write("OK") }
400
+
401
+ shell.console.should_receive(:error).with("Path {mark=bright}#{temp_file_1}{/mark} is currently a file.")
402
+ expect(shell.create_directories(temp_file_1, 0755, true, false, false)).to be_false
403
+ end
404
+
405
+ it "should complain about non writable parents" do
406
+ shell.console.should_receive(:error).with("Cannot create following directory due to permission denied: {mark=bright}/dev/bovem{/mark}.")
407
+ expect(shell.create_directories("/dev/bovem", 0755, true, false, false)).to be_false
408
+ end
409
+
410
+ it "should complain about other exceptions" do
411
+ FileUtils.stub(:mkdir_p).and_raise(ArgumentError.new("ERROR"))
412
+ shell.console.should_receive(:error).with("Cannot create following directories:")
413
+ shell.console.should_receive(:write).at_least(2)
414
+ expect(shell.create_directories(temp_dir_1, 0755, true, true, false)).to be_false
415
+ end
416
+
417
+ describe "should exit when requested to" do
418
+ it "by calling :fatal" do
419
+ shell.console.should_receive(:fatal).with("Path {mark=bright}/dev/null{/mark} is currently a file.")
420
+ expect(shell.create_directories("/dev/null")).to be_false
421
+ end
422
+
423
+ it "by calling Kernel#exit" do
424
+ FileUtils.stub(:mkdir_p).and_raise(ArgumentError.new("ERROR"))
425
+ Kernel.should_receive(:exit).with(-1)
426
+ expect(shell.create_directories(temp_dir_1, 0755, true, true)).to be_false
427
+ end
428
+ end
429
+ end
430
+
431
+ describe "#find" do
432
+ let(:root) {File.expand_path(File.dirname(__FILE__) + "/../../") }
433
+
434
+ it "it should return [] for invalid or empty directories" do
435
+ expect(shell.find("/invalid", /rb/)).to eq([])
436
+ end
437
+
438
+ it "it should return every file for empty patterns" do
439
+ files = []
440
+
441
+ Find.find(root) do |file|
442
+ files << file
443
+ end
444
+
445
+ expect(shell.find(root, nil)).to eq(files)
446
+ end
447
+
448
+ it "should find files basing on pattern" do
449
+ files = []
450
+
451
+ Find.find(root + "/lib/bovem/") do |file|
452
+ files << file if !File.directory?(file)
453
+ end
454
+
455
+ expect(shell.find(root, /lib\/bovem\/.+rb/)).to eq(files)
456
+ expect(shell.find(root, /lib\/BOVEM\/.+rb/)).to eq(files)
457
+ expect(shell.find(root, "lib\/bovem/")).to eq(files)
458
+ expect(shell.find(root, /lib\/BOVEM\/.+rb/, false, true)).to eq([])
459
+ end
460
+
461
+ it "should find files basing on extension" do
462
+ files = []
463
+
464
+ Find.find(root + "/lib/bovem/") do |file|
465
+ files << file if !File.directory?(file)
466
+ end
467
+
468
+ expect(shell.find(root + "/lib/bovem", /rb/, true)).to eq(files)
469
+ expect(shell.find(root + "/lib/bovem", /bovem/, true)).to eq([])
470
+ expect(shell.find(root + "/lib/bovem", "RB", true, true)).to eq([])
471
+ end
472
+ end
473
+ end