automateit 0.70923 → 0.70928

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ require File.join(File.dirname(File.expand_path(__FILE__)), "/../spec_helper.rb")
2
+
3
+ describe "AutomateIt::EditManager for files" do
4
+ before :all do
5
+ @a = AutomateIt.new(:verbosity => Logger::WARN)
6
+ end
7
+
8
+ it "should make a backup if editing a file" do
9
+ @a.mktempdircd do
10
+ target = "myfile"
11
+ @a.render(:text => "Whatever", :to => target)
12
+ entries = Dir.entries(".").select{|t| t =~ /^#{target}/}
13
+ entries.size.should == 1
14
+
15
+ @a.edit(:file => target) do
16
+ append "Hello"
17
+ end.should be_true
18
+
19
+ entries = Dir.entries(".").select{|t| t =~ /^#{target}/}
20
+ entries.size.should == 2
21
+ File.size(entries[0]).should_not == File.size(entries[1])
22
+ end
23
+ end
24
+
25
+ it "should not make a backup if not editing a file" do
26
+ @a.mktempdircd do
27
+ target = "myfile"
28
+ @a.render(:text => "Whatever", :to => target)
29
+ entries = Dir.entries(".").select{|t| t =~ /^#{target}/}
30
+ entries.size.should == 1
31
+
32
+ @a.edit(:file => target) do
33
+ end.should be_false
34
+
35
+ entries = Dir.entries(".").select{|t| t =~ /^#{target}/}
36
+ entries.size.should == 1
37
+ end
38
+ end
39
+
40
+ it "should not make a backup for a newly created file" do
41
+ @a.mktempdircd do
42
+ target = "myfile"
43
+
44
+ @a.edit(:file => target, :create => true) do
45
+ append "Hello"
46
+ end.should be_true
47
+
48
+ entries = Dir.entries(".").select{|t| t =~ /^#{target}/}
49
+ entries.size.should == 1
50
+ end
51
+ end
52
+ end
@@ -5,6 +5,10 @@ describe AutomateIt::ShellManager, :shared => true do
5
5
  @a = AutomateIt.new(:verbosity => Logger::WARN)
6
6
  @m = @a.shell_manager
7
7
  end
8
+
9
+ before(:each) do
10
+ @a.preview = false
11
+ end
8
12
  end
9
13
 
10
14
  describe AutomateIt::ShellManager, " with sh and which" do
@@ -42,6 +46,60 @@ end
42
46
  describe AutomateIt::ShellManager, " in general" do
43
47
  it_should_behave_like "AutomateIt::ShellManager"
44
48
 
49
+ it "should backup a file (backup)" do
50
+ @m.mktempdircd do
51
+ source = "myfile"
52
+ @m.touch(source)
53
+
54
+ target = @m.backup(source)
55
+
56
+ target.should_not be_nil
57
+ target.should_not == source
58
+ File.exists?(target).should be_true
59
+ end
60
+ end
61
+
62
+ it "should backup a directory with files (backup)" do
63
+ @m.mktempdircd do
64
+ dir = "mydir"
65
+ file = File.join(dir, "myfile")
66
+ @m.mkdir(dir)
67
+ @m.touch(file)
68
+
69
+ target_dir = @m.backup(dir)
70
+
71
+ target_dir.should_not be_nil
72
+ target_dir.should_not == dir
73
+ File.exists?(target_dir).should be_true
74
+ File.directory?(target_dir).should be_true
75
+
76
+ target_file = File.join(target_dir, File.basename(file))
77
+ File.exists?(target_file).should be_true
78
+ File.directory?(target_file).should be_false
79
+ end
80
+ end
81
+
82
+ it "should backup multiple files and directories (backup)" do
83
+ @m.mktempdircd do
84
+ @m.mkdir_p("foo/bar/baz")
85
+ @m.touch("foo/bar/baz/feh")
86
+ @m.mkdir_p("foo/bar/qux")
87
+ @m.touch("foo/bar/qux/meh")
88
+ @m.mkdir_p("qux/foo/bar")
89
+ @m.touch("qux/foo/bar/bah")
90
+
91
+ results = @m.backup("foo", "qux")
92
+ results.should_not be_blank
93
+
94
+ results[0].should =~ %r{#{File::SEPARATOR}foo\..+\.bak$}
95
+ File.exists?(results[0]+"/bar/baz/feh")
96
+ File.exists?(results[0]+"/bar/baz/meh")
97
+
98
+ results[1].should =~ %r{#{File::SEPARATOR}qux\..+\.bak$}
99
+ File.exists?(results[1]+"/foo/bar/bah")
100
+ end
101
+ end
102
+
45
103
  it "should change directories (cd)" do
46
104
  before = Dir.pwd
47
105
  target = Pathname.new("/").expand_path.to_s
@@ -60,6 +118,38 @@ describe AutomateIt::ShellManager, " in general" do
60
118
  Dir.pwd.should == before
61
119
  end
62
120
 
121
+ it "should change back to a directory when there's an error (cd)" do
122
+ before = Dir.pwd
123
+ target = Pathname.new("/").expand_path.to_s
124
+ lambda {
125
+ @m.cd(target) do
126
+ raise "42"
127
+ end
128
+ }.should raise_error("42")
129
+ Dir.pwd.should == before
130
+ end
131
+
132
+ it "should fail to change to a non-existend directory (cd)" do
133
+ lambda{ @m.cd("qweljkrusfauweqr") }.should raise_error(Errno::ENOENT)
134
+ end
135
+
136
+ it "should not fail to change to a non-existent directory in preview mode (cd)" do
137
+ @m.preview = true
138
+ @m.cd("qweljkrusfauweqr").should_not be_nil
139
+ end
140
+
141
+ it "should return if there's an error when pretending to cd into a non-existent directory in preview mode (cd)" do
142
+ @m.preview = true
143
+ before = Dir.pwd
144
+ lambda {
145
+ @m.cd("qweljkrusfauweqr") do
146
+ Dir.pwd.should == before
147
+ raise "42"
148
+ end
149
+ }.should raise_error("42")
150
+ Dir.pwd.should == before
151
+ end
152
+
63
153
  it "should locate the current directory (pwd)" do
64
154
  @m.pwd.should == Dir.pwd
65
155
  end
@@ -76,6 +166,26 @@ describe AutomateIt::ShellManager, " in general" do
76
166
  end
77
167
  end
78
168
 
169
+ it "should create a directory and cd into it (mkdir)" do
170
+ @m.mktempdircd do
171
+ target = "foo"
172
+ @m.mkdir(target) do |path|
173
+ path.should == target
174
+ File.basename(Dir.pwd).should == target
175
+ end
176
+ end
177
+ end
178
+
179
+ it "should fail to cd into multiple created directories (mkdir)" do
180
+ @m.mktempdircd do
181
+ lambda {
182
+ @m.mkdir(%w(foo bar baz)) do |path|
183
+ violated
184
+ end
185
+ }.should raise_error(ArgumentError)
186
+ end
187
+ end
188
+
79
189
  it "should create nested directories when needed (mkdir_p)" do
80
190
  @m.mktempdircd do
81
191
  target = "foo/bar/baz"
@@ -98,6 +208,20 @@ describe AutomateIt::ShellManager, " in general" do
98
208
  end
99
209
  end
100
210
 
211
+ it "should create a temporary file (mktemp)" do
212
+ @m.mktempdir do
213
+ item = nil
214
+ @m.mktemp do |path|
215
+ item = path
216
+ File.exists?(item).should be_true
217
+ end
218
+ File.exists?(item).should be_false
219
+ end
220
+ end
221
+
222
+ #it "should create a temporary directory (mktempdir)"
223
+ #it "should create a temporary directory and cd into it (mktempdircd)"
224
+
101
225
  it "should install a file to a file (install)" do
102
226
  @m.mktempdircd do
103
227
  source = "foo"
@@ -188,10 +312,6 @@ describe AutomateIt::ShellManager, " in general" do
188
312
  end
189
313
  end
190
314
 
191
- # TODO implement umask spec
192
-
193
- # TODO implement gap
194
-
195
315
  it "should move files (mv)" do
196
316
  @m.mktempdircd do
197
317
  file1 = "foo"
@@ -252,6 +372,25 @@ describe AutomateIt::ShellManager, " in general" do
252
372
  end
253
373
  end
254
374
 
375
+ it "should create files and change their timestamps (touch)" do
376
+ @m.mktempdircd do
377
+ target = "foo"
378
+ File.exists?(target).should be_false
379
+
380
+ @m.touch(target)
381
+ File.exists?(target).should be_true
382
+ before = File.mtime(target)
383
+
384
+ @m.touch(target)
385
+ after = File.mtime(target)
386
+ before.should <= after
387
+ end
388
+ end
389
+ end
390
+
391
+ describe AutomateIt::ShellManager, " when managing modes" do
392
+ it_should_behave_like "AutomateIt::ShellManager"
393
+
255
394
  unless INTERPRETER.shell_manager.provides_mode?
256
395
  puts "NOTE: Can't check permission modes on this platform, #{__FILE__}"
257
396
  else
@@ -288,22 +427,10 @@ describe AutomateIt::ShellManager, " in general" do
288
427
  @m.chmod_R(mode, dir).should be_false
289
428
  end
290
429
  end
291
- end
292
430
 
293
- it "should create files and change their timestamps (touch)" do
294
- @m.mktempdircd do
295
- target = "foo"
296
- File.exists?(target).should be_false
297
-
298
- @m.touch(target)
299
- File.exists?(target).should be_true
300
- before = File.mtime(target)
301
-
302
- @m.touch(target)
303
- after = File.mtime(target)
304
- before.should <= after
305
- end
431
+ #it "should set the default mask (umask)" # TODO
306
432
  end
433
+
307
434
  end
308
435
 
309
436
  describe AutomateIt::ShellManager, " when managing permissions" do
@@ -9,7 +9,11 @@ describe "AutomateIt::TemplateManager::ERB" do
9
9
  @d.available?.should be_true
10
10
  end
11
11
 
12
- it "should set mode when rendering" do
12
+ before(:each) do
13
+ @a.preview = false
14
+ end
15
+
16
+ it "should set file's mode when rendering" do
13
17
  @a.mktempdircd do
14
18
  source = "foo"
15
19
  target = "bar"
@@ -28,4 +32,17 @@ describe "AutomateIt::TemplateManager::ERB" do
28
32
  end
29
33
  end
30
34
  end
35
+
36
+ it "should fail to render non-existent file" do
37
+ @a.mktempdircd do
38
+ lambda { @a.render(:file => "foo", :to => "bar") }.should raise_error(Errno::ENOENT)
39
+ end
40
+ end
41
+
42
+ it "should not raise error with non-existent file in preview mode" do
43
+ @a.mktempdircd do
44
+ @a.preview = true
45
+ @a.render(:file => "foo", :to => "bar").should be_true
46
+ end
47
+ end
31
48
  end
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(File.expand_path(__FILE__)), "/../spec_helper.rb")
2
+
3
+ describe Tempster do
4
+ it "should create a temporary file" do
5
+ result = nil
6
+ begin
7
+ result = Tempster.mktemp(:verbose => false)
8
+ File.exists?(result).should be_true
9
+ ensure
10
+ File.unlink(result) if result
11
+ end
12
+ end
13
+
14
+ it "should create a temporary file and remove it with a block" do
15
+ result = nil
16
+ begin
17
+ Tempster.mktemp(:verbose => false) do |path|
18
+ result = path
19
+ File.exists?(result).should be_true
20
+ end
21
+ File.exists?(result).should be_false
22
+ ensure
23
+ File.unlink(result) if result and File.exists?(result)
24
+ end
25
+ end
26
+
27
+ it "should create a temporary directory" do
28
+ result = nil
29
+ begin
30
+ result = Tempster.mktempdir(:verbose => false)
31
+ File.directory?(result).should be_true
32
+ ensure
33
+ Dir.rmdir(result) if result
34
+ end
35
+ end
36
+
37
+ it "should create a temporary directory and remove it with a block" do
38
+ result = nil
39
+ begin
40
+ Tempster.mktempdir(:verbose => false) do |path|
41
+ result = path
42
+ File.directory?(result).should be_true
43
+ end
44
+ File.directory?(result).should be_false
45
+ ensure
46
+ Dir.rmdir(result) if result and File.directory?(result)
47
+ end
48
+ end
49
+
50
+ it "should create a temporary directory, cd into it and remove it with a block" do
51
+ result = nil
52
+ previous = Dir.pwd
53
+ begin
54
+ Tempster.mktempdircd(:verbose => false) do |path|
55
+ result = path
56
+ File.directory?(result).should be_true
57
+ Dir.pwd.should_not == previous
58
+ end
59
+ File.directory?(result).should be_false
60
+ Dir.pwd.should == previous
61
+ ensure
62
+ Dir.rmdir(result) if result and File.directory?(result)
63
+ end
64
+ end
65
+ end
@@ -21,12 +21,18 @@ describe "AutomateIt::EditManager for strings" do
21
21
  end
22
22
  end
23
23
 
24
- it "should find contained lines" do
24
+ it "should find if buffer contains lines by regexp" do
25
25
  @a.edit(:text => @input) do
26
26
  contains?(/This/).should == true
27
27
  end
28
28
  end
29
29
 
30
+ it "should find if buffer contains lines by string" do
31
+ @a.edit(:text => @input) do
32
+ contains?("This").should == true
33
+ end
34
+ end
35
+
30
36
  it "should prepend lines to the top" do
31
37
  output = @a.edit(:text => @input) do
32
38
  prepend "PREPEND"
@@ -117,7 +123,7 @@ describe "AutomateIt::EditManager for strings" do
117
123
  end
118
124
 
119
125
  it "should raise exceptions for invalid methods" do
120
- lambda {
126
+ lambda {
121
127
  @a.edit(:text => @input) do
122
128
  qwoiuerjzxiuo
123
129
  end
@@ -138,7 +144,7 @@ describe "AutomateIt::EditManager for files" do
138
144
  it "should edit a file" do
139
145
  File.should_receive(:read).with(@filename).and_return(@input)
140
146
  File.should_receive(:open).with(@filename, "w+").and_return(true)
141
- result = @a.edit(:file => @filename) do
147
+ result = @a.edit(:file => @filename, :backup => false) do
142
148
  append "APPEND"
143
149
  end
144
150
  result.should be_true
@@ -146,7 +152,7 @@ describe "AutomateIt::EditManager for files" do
146
152
 
147
153
  it "should not rewrite an unchanged file" do
148
154
  File.should_receive(:read).with(@filename).and_return(@input)
149
- result = @a.edit(:file => @filename) do
155
+ result = @a.edit(:file => @filename, :backup => false) do
150
156
  # Do nothing
151
157
  end
152
158
  result.should be_false
@@ -154,9 +160,47 @@ describe "AutomateIt::EditManager for files" do
154
160
 
155
161
  it "should default to editing a file" do
156
162
  File.should_receive(:read).with(@filename).and_return(@input)
157
- result = @a.edit(@filename) do
163
+ result = @a.edit(@filename, :backup => false) do
164
+ # Do nothing
165
+ end
166
+ result.should be_false
167
+ end
168
+
169
+ it "should fail to editing non-existent file" do
170
+ lambda {
171
+ @a.edit(@filename) do
172
+ # Do nothing
173
+ end
174
+ }.should raise_error(Errno::ENOENT)
175
+ end
176
+ end
177
+
178
+ describe "AutomateIt::EditManager for files in preview mode" do
179
+ before(:all) do
180
+ @a = AutomateIt.new(:verbosity => Logger::WARN)
181
+ @a.preview = true
182
+ @filename = "input"
183
+ end
184
+
185
+ it "should not fail to editing a non-existent file" do
186
+ @a.edit(@filename) do
187
+ # Do nothing
188
+ end
189
+ end
190
+
191
+ it "should not fail when reading a non-existent file" do
192
+ result = @a.edit(@filename, :backup => false) do
158
193
  # Do nothing
159
194
  end
160
195
  result.should be_false
161
196
  end
197
+
198
+ it "should not write changes" do
199
+ File.should_receive(:exists?).any_number_of_times.with(@filename).and_return(true)
200
+ File.should_receive(:read).with(@filename).and_return(@input)
201
+ result = @a.edit(:file => @filename, :backup => false) do
202
+ append "APPEND"
203
+ end
204
+ result.should be_true
205
+ end
162
206
  end