reciper 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +25 -0
- data/Rakefile +9 -1
- data/lib/reciper/helpers.rb +168 -63
- data/lib/reciper/version.rb +1 -1
- data/spec/reciper/helpers_spec.rb +273 -118
- metadata +10 -23
- data/spec/fixtures/recipe/README +0 -0
- data/spec/fixtures/recipe/failing_spec.rb +0 -7
- data/spec/fixtures/recipe/file.rb +0 -0
- data/spec/fixtures/recipe/my_name.rb +0 -3
- data/spec/fixtures/ruby_app/Gemfile +0 -4
- data/spec/fixtures/ruby_app/Gemfile.lock +0 -12
- data/spec/fixtures/ruby_app/README +0 -1
- data/spec/fixtures/ruby_app/Rakefile +0 -5
- data/spec/fixtures/ruby_app/lib/my_class.rb +0 -2
- data/spec/fixtures/ruby_app/spec/true_spec.rb +0 -7
data/Gemfile.lock
CHANGED
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Reciper
|
2
|
+
|
3
|
+
Suppose you're writing a book containing some programming recipes. It would be great to have a test suite that ensures that your snippets really are really working. But why we should write a test suite if RSpec already does an awesome job doing it ?
|
4
|
+
|
5
|
+
Reciper is a collection of helpers that helps you write tests for your book recipes. It should be included on your described class and used whenever you need to copy files, copy line ranges, run tests, overwrite files, among other things.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
|
9
|
+
On your `Gemfile`, just do this:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem "reciper"
|
13
|
+
```
|
14
|
+
|
15
|
+
Run `bundle install` and on your described class just include `Reciper::Helpers`
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
describe "My awesome recipe" do
|
19
|
+
include Reciper::Helpers
|
20
|
+
# (...)
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
|
25
|
+
|
data/Rakefile
CHANGED
data/lib/reciper/helpers.rb
CHANGED
@@ -7,102 +7,177 @@ module Reciper
|
|
7
7
|
class NoFileToBeOverriden < RuntimeError
|
8
8
|
end
|
9
9
|
|
10
|
+
class NoFileOrMultipleFilesFound < RuntimeError
|
11
|
+
end
|
12
|
+
|
10
13
|
module Helpers
|
14
|
+
# Copies the file from recipe to ruby_app. It is reversible.
|
15
|
+
#
|
16
|
+
# filename - The file to be copied relative to the recipe_path
|
17
|
+
# options - The hash options to configure the copy (default: {}):
|
18
|
+
# :as - The copy name (default: the file name).
|
19
|
+
# :to - The destination dir relative to the ruby_app_path. If the directory doesn't exists, it will be created (default: ruby_app_path root path).
|
20
|
+
#
|
21
|
+
# Examples
|
22
|
+
#
|
23
|
+
# copy_file("a.rb")
|
24
|
+
# copy_file("a.rb", :to => "app/models", :as => "person.rb")
|
25
|
+
#
|
26
|
+
# Returns nothing.
|
11
27
|
def copy_file(filename, options={})
|
12
|
-
|
28
|
+
destination_file_name = options[:as] || filename
|
29
|
+
destination_dir = options[:to] || ""
|
13
30
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
31
|
+
destination = File.join(destination_dir , destination_file_name)
|
32
|
+
global_destination = File.join(@ruby_app_path, destination)
|
17
33
|
|
18
|
-
|
34
|
+
create_directory_if_not_exists(File.join(@ruby_app_path, destination_dir))
|
19
35
|
|
20
|
-
|
21
|
-
|
22
|
-
if(options[:as])
|
23
|
-
FileUtils.mv(destination_dir + "/" + filename, destination_dir + "/" + new_filename)
|
24
|
-
end
|
36
|
+
FileUtils.cp(File.join(@recipe_path, filename), global_destination)
|
25
37
|
|
26
|
-
@operations << [:
|
38
|
+
@operations << [:copy_file, { :destination => destination }]
|
27
39
|
end
|
28
40
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
# Run the tests on the ruby app. It is NOT reversible.
|
42
|
+
#
|
43
|
+
# Examples
|
44
|
+
#
|
45
|
+
# run_tests()
|
46
|
+
# # => 2
|
47
|
+
#
|
48
|
+
# Returns the number of failures
|
49
|
+
def run_tests
|
50
|
+
result = run_command("bundle exec rspec spec")
|
51
|
+
|
52
|
+
if result[:response] =~ /([\.FE*]+)/
|
53
|
+
$1.split("").reject { |char| (char == "." || char == "*") }.size
|
54
|
+
else
|
55
|
+
puts "Can't get any test output"
|
56
|
+
fail NoTestOutput
|
39
57
|
end
|
40
58
|
end
|
41
59
|
|
60
|
+
# Run a rake task on the ruby app. It is NOT reversible.
|
61
|
+
#
|
62
|
+
# task - the desired task
|
63
|
+
#
|
64
|
+
# Examples
|
65
|
+
#
|
66
|
+
# rake_task("db:migrate")
|
67
|
+
# # => { :successful => true, :response => ""}
|
68
|
+
# rake_task("db:migrate")
|
69
|
+
# # => { :successful => false, :response => "Couldn't find the specified DB"}
|
70
|
+
#
|
71
|
+
# Returns a command execution hash
|
42
72
|
def run_rake_task(task)
|
43
|
-
|
44
|
-
spawn("bundle exec rake #{task}", :out => "/dev/null", :err => "/dev/null")
|
45
|
-
|
46
|
-
Process.wait
|
47
|
-
end
|
48
|
-
|
49
|
-
$?.exitstatus == 0
|
73
|
+
run_command("bundle exec rake #{task}")
|
50
74
|
end
|
51
75
|
|
76
|
+
# Copies a range of lines from a file on the recipe path to a file on the ruby app path. It is reversible.
|
77
|
+
#
|
78
|
+
# from - the file, relative to the recipe path, that contains the file with the lines that will be copied
|
79
|
+
# to - the file, relative to the ruby app path, that contains the file that will receive the files. You can also specify a suffix like *.rb and if it matches only one file, it will insert the lines to it, otherwise it will raise a NoFileOrMultipleFilesFound exception
|
80
|
+
# options - some options related to the line copy (default: {})
|
81
|
+
# :to_line - The line where the content will be inserted (default: raises an exception)
|
82
|
+
# :lines - A range that specifies the lines that will be copied (default: whole file (0..-1))
|
83
|
+
#
|
84
|
+
# Examples
|
85
|
+
#
|
86
|
+
# copy_line_range("a.rb", "app/models/person.rb", :to_line => 42)
|
87
|
+
# copy_line_range("a.rb", "app/models/person.rb", :to_line => 42, :lines => (4..10))
|
88
|
+
# copy_line_range("a.rb", "db/migrate/*create_users.rb", :to_line => 10, :lines => (1..5))
|
89
|
+
#
|
90
|
+
# Returns nothing.
|
52
91
|
def copy_line_range(from, to, options={})
|
53
|
-
|
54
|
-
from_lines = Range.new(options[:from_lines].first - 1, options[:from_lines].last - 1)
|
55
|
-
else
|
56
|
-
from_lines = (0..-1)
|
57
|
-
end
|
92
|
+
original_file = filename_from_suffix(to)
|
58
93
|
|
59
|
-
|
60
|
-
|
61
|
-
original_output = output_lines.dup
|
62
|
-
to_file_output = File.open(@ruby_app_path + "/" + to, "w")
|
94
|
+
original_content = File.read(original_file)
|
95
|
+
original = original_content.split("\n")
|
63
96
|
|
64
|
-
|
97
|
+
new_content = File.read(File.join(@recipe_path, from)).split("\n")
|
65
98
|
|
66
|
-
|
99
|
+
range = options[:lines] || (0..-1)
|
67
100
|
|
68
|
-
|
101
|
+
original.insert(options[:to_line], new_content[range])
|
69
102
|
|
70
|
-
|
103
|
+
File.open(original_file, "w") do |file|
|
104
|
+
file.write(original.flatten.join("\n"))
|
105
|
+
end
|
106
|
+
|
107
|
+
@operations << [:copy_line_range, { :original_content => original_content, :original_file => original_file }]
|
71
108
|
end
|
72
109
|
|
110
|
+
# Does the rollback on all reversible operations.
|
111
|
+
#
|
112
|
+
# Examples
|
113
|
+
#
|
114
|
+
# rollback
|
115
|
+
#
|
116
|
+
# Returns nothing
|
73
117
|
def rollback
|
74
118
|
@operations.reverse.each do |operation|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
119
|
+
operation_name, arguments = operation
|
120
|
+
|
121
|
+
case operation_name
|
122
|
+
when :copy_file
|
123
|
+
then remove_file(arguments[:destination])
|
124
|
+
when :copy_range
|
125
|
+
then write_content_to_file(arguments[:original_file], arguments[:original_content])
|
126
|
+
when :run_command
|
127
|
+
then run_command(arguments[:rollback_command]) if arguments[:rollback_command]
|
128
|
+
when :override_file
|
129
|
+
then FileUtils.cp(arguments[:tmp_file], File.join(@ruby_app_path, arguments[:overriden_file]))
|
85
130
|
end
|
86
131
|
end
|
87
132
|
end
|
88
133
|
|
134
|
+
# Runs a command using bundle exec on the ruby app path. If you specify a rollback command, it is reversible.
|
135
|
+
#
|
136
|
+
# command - the command. It will run inside a bundle exec.
|
137
|
+
# rollback_command - the command which rollbacks the command (default: nil)
|
138
|
+
#
|
139
|
+
# Examples
|
140
|
+
#
|
141
|
+
# run_command("ls")
|
142
|
+
# # => { :successful => true, response => "file.rb\n\file2.rb" }
|
143
|
+
# run_command("rails g model user", "rails d model user")
|
144
|
+
# # => { :successful => true, response => "model was generated" }
|
145
|
+
# run_command("cp a.rb b.rb")
|
146
|
+
# # => { :successful => false, response => "file a.rb doesn't exists" }
|
147
|
+
#
|
148
|
+
# Returns a command execution hash
|
89
149
|
def run_command(command, rollback_command=nil)
|
90
|
-
|
91
|
-
|
150
|
+
response = ""
|
151
|
+
successful = ""
|
92
152
|
|
93
|
-
|
94
|
-
|
153
|
+
run_on_app_path do
|
154
|
+
IO.popen(command) do |io|
|
155
|
+
response = io.read
|
156
|
+
end
|
95
157
|
|
96
|
-
|
97
|
-
@operations << [:run_command, rollback_command || nil]
|
98
|
-
true
|
99
|
-
else
|
100
|
-
false
|
158
|
+
successful = ($?.exitstatus == 0)
|
101
159
|
end
|
160
|
+
|
161
|
+
@operations << [:run_command, { :rollback_command => rollback_command }]
|
162
|
+
|
163
|
+
{
|
164
|
+
:successful => successful,
|
165
|
+
:response => response
|
166
|
+
}
|
102
167
|
end
|
103
168
|
|
169
|
+
# Overrides a file of the ruby app with a file from the recipe path. It is reversible.
|
170
|
+
#
|
171
|
+
# file - The file that will override relative to the recipe path
|
172
|
+
# file_to_be_overriden - The file that will be overriden
|
173
|
+
#
|
174
|
+
# Examples
|
175
|
+
#
|
176
|
+
# override_file("a.rb", "app/controller/application_controller.rb")
|
177
|
+
#
|
178
|
+
# Returns nothing
|
104
179
|
def override_file(file, file_to_be_overriden)
|
105
|
-
|
180
|
+
run_on_app_path do
|
106
181
|
fail NoFileToBeOverriden unless File.exists?(file_to_be_overriden)
|
107
182
|
|
108
183
|
FileUtils.mkdir_p("/tmp/reciper")
|
@@ -111,10 +186,40 @@ module Reciper
|
|
111
186
|
|
112
187
|
FileUtils.cp(file_to_be_overriden, tmp_file)
|
113
188
|
|
114
|
-
@operations << [:override_file, tmp_file, file_to_be_overriden]
|
189
|
+
@operations << [:override_file, { :tmp_file => tmp_file, :overriden_file => file_to_be_overriden }]
|
115
190
|
end
|
116
191
|
|
117
|
-
FileUtils.cp(@recipe_path
|
192
|
+
FileUtils.cp(File.join(@recipe_path,file), File.join(@ruby_app_path, file_to_be_overriden))
|
193
|
+
end
|
194
|
+
|
195
|
+
private
|
196
|
+
|
197
|
+
def filename_from_suffix(suffix)
|
198
|
+
files = Dir.glob(File.join(@ruby_app_path, suffix))
|
199
|
+
fail NoFileOrMultipleFilesFound if files.size != 1
|
200
|
+
files.first
|
201
|
+
end
|
202
|
+
|
203
|
+
def create_directory_if_not_exists(directory)
|
204
|
+
FileUtils.mkdir_p(directory) unless File.directory?(directory)
|
205
|
+
end
|
206
|
+
|
207
|
+
def run_on_app_path
|
208
|
+
Dir.chdir(@ruby_app_path) do
|
209
|
+
yield
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def remove_file(file)
|
214
|
+
run_on_app_path do
|
215
|
+
FileUtils.rm(file)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
def write_content_to_file(filename, content)
|
220
|
+
File.open(filename, "w") do |file|
|
221
|
+
file.write(content)
|
222
|
+
end
|
118
223
|
end
|
119
224
|
end
|
120
225
|
end
|
data/lib/reciper/version.rb
CHANGED
@@ -14,229 +14,384 @@ describe Reciper::Helpers do
|
|
14
14
|
|
15
15
|
describe ".copy" do
|
16
16
|
it "copies the file from the recipe path to the ruby app root path " do
|
17
|
-
|
17
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/file.rb")
|
18
18
|
|
19
19
|
copy_file("file.rb")
|
20
|
-
|
21
|
-
File.exists?("spec/fixtures/ruby_app/file.rb").should be
|
22
|
-
|
23
|
-
FileUtils.rm("spec/fixtures/ruby_app/file.rb")
|
24
20
|
end
|
25
21
|
|
26
22
|
it "copies the file from the recipe path to the ruby app" do
|
27
|
-
|
23
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/lib/file.rb")
|
28
24
|
|
29
25
|
copy_file("file.rb", :to => "lib")
|
30
|
-
|
31
|
-
File.exists?("spec/fixtures/ruby_app/lib/file.rb").should be
|
32
|
-
|
33
|
-
FileUtils.rm("spec/fixtures/ruby_app/lib/file.rb")
|
34
26
|
end
|
35
27
|
|
36
28
|
it "copies the file with the name as in defined in as" do
|
37
|
-
|
29
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/another_file.rb")
|
38
30
|
|
39
31
|
copy_file("file.rb", :as => "another_file.rb")
|
40
|
-
|
41
|
-
File.exists?("spec/fixtures/ruby_app/another_file.rb").should be
|
42
|
-
|
43
|
-
FileUtils.rm("spec/fixtures/ruby_app/another_file.rb")
|
44
32
|
end
|
45
33
|
|
46
34
|
it "if the dir doesn't exists, create it" do
|
47
|
-
|
35
|
+
directory = @ruby_app_path + "/my_awesome_dir"
|
36
|
+
File.should_receive(:directory?).with("spec/fixtures/ruby_app/my_awesome_dir").and_return(false)
|
37
|
+
FileUtils.should_receive(:mkdir_p).with("spec/fixtures/ruby_app/my_awesome_dir")
|
38
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/my_awesome_dir/file.rb")
|
48
39
|
|
49
40
|
copy_file("file.rb", :to => "my_awesome_dir")
|
50
|
-
|
51
|
-
File.exists?("spec/fixtures/ruby_app/my_awesome_dir/file.rb").should be
|
52
|
-
|
53
|
-
FileUtils.rm_rf("spec/fixtures/ruby_app/my_awesome_dir")
|
54
41
|
end
|
55
42
|
|
56
43
|
it "adds the operation to @operation array" do
|
57
|
-
|
44
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/file.rb")
|
58
45
|
|
59
|
-
|
46
|
+
copy_file("file.rb")
|
60
47
|
|
61
|
-
|
48
|
+
@operations.should include([:copy_file, { :destination => "/file.rb" }])
|
62
49
|
end
|
63
50
|
end
|
64
51
|
|
65
52
|
describe ".run_tests" do
|
66
53
|
it "returns 0 if all tests pass" do
|
54
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
55
|
+
|
56
|
+
test_output = <<-EOF
|
57
|
+
....
|
58
|
+
|
59
|
+
Finished in 11.29 seconds
|
60
|
+
23 examples, 0 failures
|
61
|
+
EOF
|
62
|
+
|
63
|
+
io = double(:io, :read => test_output)
|
64
|
+
IO.should_receive(:popen).with("bundle exec rspec spec").and_yield(io)
|
65
|
+
|
67
66
|
run_tests.should == 0
|
68
67
|
end
|
69
68
|
|
70
69
|
it "returns 1 if there is only one failure" do
|
71
|
-
|
70
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
71
|
+
|
72
|
+
test_output = <<-EOF
|
73
|
+
FE..
|
72
74
|
|
73
|
-
|
75
|
+
Finished in 11.29 seconds
|
76
|
+
4 examples, 2 failures
|
77
|
+
EOF
|
74
78
|
|
75
|
-
|
79
|
+
io = double(:io, :read => test_output)
|
80
|
+
IO.should_receive(:popen).with("bundle exec rspec spec").and_yield(io)
|
81
|
+
|
82
|
+
run_tests.should == 2
|
76
83
|
end
|
77
84
|
end
|
78
85
|
|
79
86
|
describe ".run_rake_task" do
|
80
|
-
it "returns true when the rake task has been run
|
81
|
-
|
87
|
+
it "returns a hash with successful as true when the rake task has been run successfully" do
|
88
|
+
output = ""
|
89
|
+
|
90
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
91
|
+
|
92
|
+
io = double(:io)
|
93
|
+
io.stub!(:read).and_return("")
|
94
|
+
|
95
|
+
IO.should_receive(:popen).
|
96
|
+
with("bundle exec rake puts_something").and_yield(io)
|
97
|
+
|
98
|
+
$?.should_receive(:exitstatus).and_return(0)
|
99
|
+
|
100
|
+
run_rake_task("puts_something").should == {
|
101
|
+
:response => "",
|
102
|
+
:successful => true
|
103
|
+
}
|
82
104
|
end
|
83
105
|
|
84
|
-
it "returns false when the rake task hasn't been run
|
85
|
-
|
106
|
+
it "returns a hash with successful as false when the rake task hasn't been run successfully" do
|
107
|
+
output = ""
|
108
|
+
|
109
|
+
io = double(:io)
|
110
|
+
io.stub!(:read).and_return("")
|
111
|
+
|
112
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
113
|
+
|
114
|
+
IO.should_receive(:popen).
|
115
|
+
with("bundle exec rake puts_something").and_yield(io)
|
116
|
+
$?.should_receive(:exitstatus).and_return(1)
|
117
|
+
|
118
|
+
run_rake_task("puts_something").should == {
|
119
|
+
:response => "",
|
120
|
+
:successful => false
|
121
|
+
}
|
86
122
|
end
|
87
123
|
end
|
88
124
|
|
89
125
|
describe ".copy_line_range" do
|
90
|
-
it "copies the entire input file to the output line " do
|
91
|
-
|
92
|
-
|
93
|
-
|
126
|
+
it "copies the entire input file to the output line on a specified line" do
|
127
|
+
readme = <<-EOF
|
128
|
+
d
|
129
|
+
e
|
94
130
|
EOF
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
end
|
102
|
-
end
|
131
|
+
|
132
|
+
original = <<-EOF
|
133
|
+
a
|
134
|
+
b
|
135
|
+
c
|
136
|
+
f
|
103
137
|
EOF
|
104
138
|
|
105
|
-
|
139
|
+
Dir.should_receive(:glob).with("spec/fixtures/ruby_app/README.md").
|
140
|
+
and_return(["spec/fixtures/ruby_app/README.md"])
|
141
|
+
|
142
|
+
File.should_receive(:read).with("spec/fixtures/recipe/README").
|
143
|
+
and_return(readme)
|
144
|
+
|
145
|
+
File.should_receive(:read).with("spec/fixtures/ruby_app/README.md").
|
146
|
+
and_return(original)
|
106
147
|
|
107
|
-
|
148
|
+
file = double(:file)
|
149
|
+
file.should_receive(:write).with("a\nb\nc\nd\ne\nf")
|
150
|
+
File.should_receive(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file)
|
151
|
+
|
152
|
+
copy_line_range("README", "README.md", :to_line => 3)
|
108
153
|
end
|
109
154
|
|
110
155
|
it "copies only specified lines" do
|
111
|
-
|
112
|
-
|
113
|
-
|
156
|
+
readme = <<-EOF
|
157
|
+
a
|
158
|
+
d
|
159
|
+
e
|
160
|
+
f
|
161
|
+
g
|
114
162
|
EOF
|
115
163
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
end
|
164
|
+
original = <<-EOF
|
165
|
+
a
|
166
|
+
b
|
167
|
+
c
|
168
|
+
f
|
122
169
|
EOF
|
123
170
|
|
124
|
-
|
171
|
+
Dir.should_receive(:glob).with("spec/fixtures/ruby_app/README.md").
|
172
|
+
and_return(["spec/fixtures/ruby_app/README.md"])
|
173
|
+
|
174
|
+
File.should_receive(:read).with("spec/fixtures/recipe/README").
|
175
|
+
and_return(readme)
|
125
176
|
|
126
|
-
File.read("spec/fixtures/ruby_app/
|
177
|
+
File.should_receive(:read).with("spec/fixtures/ruby_app/README.md").
|
178
|
+
and_return(original)
|
179
|
+
|
180
|
+
file = double(:file)
|
181
|
+
file.should_receive(:write).with("a\nb\nc\nd\ne\nf")
|
182
|
+
File.should_receive(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file)
|
183
|
+
|
184
|
+
copy_line_range("README", "README.md", :to_line => 3, :lines => (1..2))
|
127
185
|
end
|
128
186
|
|
129
187
|
it "adds an entry to operations" do
|
130
|
-
|
131
|
-
|
132
|
-
|
188
|
+
readme = <<-EOF
|
189
|
+
a
|
190
|
+
d
|
191
|
+
e
|
192
|
+
f
|
193
|
+
g
|
194
|
+
EOF
|
195
|
+
|
196
|
+
original = <<-EOF
|
197
|
+
a
|
198
|
+
b
|
199
|
+
c
|
200
|
+
f
|
133
201
|
EOF
|
134
202
|
|
135
|
-
|
203
|
+
Dir.stub!(:glob).with("spec/fixtures/ruby_app/README.md").
|
204
|
+
and_return(["spec/fixtures/ruby_app/README.md"])
|
205
|
+
|
206
|
+
File.stub!(:read).with("spec/fixtures/recipe/README").
|
207
|
+
and_return(readme)
|
208
|
+
|
209
|
+
File.stub!(:read).with("spec/fixtures/ruby_app/README.md").
|
210
|
+
and_return(original)
|
136
211
|
|
137
|
-
|
212
|
+
file = double(:file)
|
213
|
+
file.stub!(:write).with("a\nb\nc\nd\ne\nf")
|
214
|
+
File.stub!(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file)
|
215
|
+
|
216
|
+
copy_line_range("README", "README.md", :to_line => 3, :lines => (1..2))
|
217
|
+
|
218
|
+
@operations.should include([:copy_line_range, { :original_content => original, :original_file => "spec/fixtures/ruby_app/README.md"}])
|
138
219
|
end
|
139
220
|
|
140
|
-
|
141
|
-
|
221
|
+
context "suffix copy" do
|
222
|
+
it "works with only the suffix of the file when there is only one file" do
|
223
|
+
readme = ""
|
224
|
+
original = ""
|
225
|
+
|
226
|
+
Dir.should_receive(:glob).with("spec/fixtures/ruby_app/*.md").
|
227
|
+
and_return(["spec/fixtures/ruby_app/README.md"])
|
228
|
+
|
229
|
+
File.stub!(:read).with("spec/fixtures/recipe/README").
|
230
|
+
and_return(readme)
|
231
|
+
|
232
|
+
File.should_receive(:read).with("spec/fixtures/ruby_app/README.md").
|
233
|
+
and_return(original)
|
234
|
+
|
235
|
+
file = double(:file)
|
236
|
+
file.stub!(:write)
|
237
|
+
File.stub!(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file)
|
238
|
+
|
239
|
+
copy_line_range("README", "*.md", :to_line => 0)
|
240
|
+
end
|
241
|
+
|
242
|
+
it "raises an exception when given only the suffix of the file when there is more than one file" do
|
243
|
+
readme = ""
|
244
|
+
original = ""
|
245
|
+
|
246
|
+
Dir.should_receive(:glob).with("spec/fixtures/ruby_app/*.md").
|
247
|
+
and_return(["spec/fixtures/ruby_app/README.md", "spec/fixtures/ruby_app/README2.md"])
|
248
|
+
|
249
|
+
lambda {
|
250
|
+
copy_line_range("README", "*.md", :to_line => 0)
|
251
|
+
}.should raise_error(Reciper::NoFileOrMultipleFilesFound)
|
252
|
+
end
|
142
253
|
end
|
143
254
|
end
|
144
255
|
|
145
256
|
describe ".run_command" do
|
146
|
-
it "runs a command on projects folder and returns true when successful" do
|
147
|
-
|
257
|
+
it "runs a command on projects folder and returns the command hash with the response and true when successful" do
|
258
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
259
|
+
|
260
|
+
output = <<EOF
|
261
|
+
a
|
262
|
+
b
|
263
|
+
EOF
|
264
|
+
|
265
|
+
io = double(:io)
|
266
|
+
io.should_receive(:read) do
|
267
|
+
"a\nb\n"
|
268
|
+
end
|
269
|
+
|
270
|
+
IO.should_receive(:popen).with("ls").and_yield(io)
|
271
|
+
$?.should_receive(:exitstatus).and_return(0)
|
272
|
+
|
273
|
+
run_command("ls").should == {
|
274
|
+
:response => "a\nb\n",
|
275
|
+
:successful => true
|
276
|
+
}
|
148
277
|
end
|
149
278
|
|
150
|
-
it "runs a command on projects folder and returns
|
151
|
-
|
279
|
+
it "runs a command on projects folder and returns the command hash with the response and false when not successful" do
|
280
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
281
|
+
|
282
|
+
output = <<EOF
|
283
|
+
a
|
284
|
+
b
|
285
|
+
EOF
|
286
|
+
|
287
|
+
io = double(:io)
|
288
|
+
io.should_receive(:read) do
|
289
|
+
"a\nb\n"
|
290
|
+
end
|
291
|
+
|
292
|
+
IO.should_receive(:popen).with("ls").and_yield(io)
|
293
|
+
$?.should_receive(:exitstatus).and_return(1)
|
294
|
+
|
295
|
+
run_command("ls").should == {
|
296
|
+
:response => "a\nb\n",
|
297
|
+
:successful => false
|
298
|
+
}
|
152
299
|
end
|
153
300
|
|
154
301
|
it "receives the rollback command together with the command and store it on @operations array" do
|
155
302
|
run_command("ls", "ls -a")
|
156
303
|
|
157
|
-
@operations.should include([:run_command, "ls -a"])
|
304
|
+
@operations.should include([:run_command, { :rollback_command => "ls -a" }])
|
158
305
|
end
|
159
|
-
end
|
160
306
|
|
161
|
-
|
162
|
-
|
163
|
-
File.open(@ruby_app_path + "/an_added_file.rb", "w") {
|
164
|
-
|f| f.write("OK")
|
165
|
-
}
|
307
|
+
it "doesn't require the rollback command to be informed" do
|
308
|
+
run_command("ls")
|
166
309
|
|
167
|
-
|
310
|
+
@operations.should include([:run_command, { :rollback_command => nil}])
|
311
|
+
end
|
312
|
+
end
|
168
313
|
|
169
|
-
|
314
|
+
describe ".override_file" do
|
315
|
+
it "overrides the file with another file" do
|
316
|
+
Dir.should_receive(:chdir).with(@ruby_app_path).and_yield
|
317
|
+
File.should_receive(:exists?).with("README").and_return(true)
|
318
|
+
FileUtils.should_receive(:mkdir_p).with("/tmp/reciper")
|
170
319
|
|
171
|
-
|
320
|
+
FileUtils.should_receive(:cp).with("README", "/tmp/reciper/README")
|
321
|
+
FileUtils.should_receive(:cp).with("spec/fixtures/recipe/README",
|
322
|
+
"spec/fixtures/ruby_app/README")
|
172
323
|
|
173
|
-
|
324
|
+
override_file("README", "README")
|
174
325
|
end
|
175
326
|
|
176
|
-
it "
|
177
|
-
|
178
|
-
|
179
|
-
}
|
180
|
-
|
181
|
-
File.read("spec/fixtures/ruby_app/an_added_file.rb").should == "OK"
|
327
|
+
it "raises an error when file doesn't exists" do
|
328
|
+
Dir.stub!(:chdir).with(@ruby_app_path).and_yield
|
329
|
+
File.should_receive(:exists?).with("README").and_return(false)
|
182
330
|
|
183
|
-
|
331
|
+
lambda {
|
332
|
+
override_file("README", "README")
|
333
|
+
}.should raise_error(Reciper::NoFileToBeOverriden)
|
334
|
+
end
|
184
335
|
|
185
|
-
|
336
|
+
it "adds the operation to operations array" do
|
337
|
+
Dir.stub!(:chdir).with(@ruby_app_path).and_yield
|
338
|
+
File.stub!(:exists?).with("README").and_return(true)
|
339
|
+
FileUtils.stub!(:mkdir_p).with("/tmp/reciper")
|
186
340
|
|
187
|
-
|
341
|
+
FileUtils.stub!(:cp).with("README", "/tmp/reciper/README")
|
342
|
+
FileUtils.stub!(:cp).with("spec/fixtures/recipe/README",
|
343
|
+
"spec/fixtures/ruby_app/README")
|
188
344
|
|
189
|
-
|
345
|
+
override_file("README", "README")
|
346
|
+
@operations.should include([:override_file, { :tmp_file => "/tmp/reciper/README", :overriden_file => "README"}])
|
190
347
|
end
|
348
|
+
end
|
191
349
|
|
192
|
-
|
193
|
-
|
350
|
+
describe ".rollback" do
|
351
|
+
it "removes the file when the operation is copy_file" do
|
352
|
+
@operations = [[:copy_file, { :destination => "README" }]]
|
353
|
+
|
354
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
194
355
|
|
195
|
-
|
196
|
-
Process.stub!(:wait)
|
356
|
+
FileUtils.should_receive(:rm).with("README")
|
197
357
|
|
198
358
|
rollback
|
199
359
|
end
|
200
360
|
|
201
|
-
it "
|
202
|
-
|
203
|
-
FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README")
|
204
|
-
FileUtils.rm("spec/fixtures/ruby_app/README")
|
205
|
-
|
206
|
-
File.exists?("spec/fixtures/ruby_app/README").should_not be
|
207
|
-
|
208
|
-
@operations = [[:override_file, "/tmp/README", "README"]]
|
361
|
+
it "restores the old file when the operation is copy_range" do
|
362
|
+
@operations = [[:copy_range, { :original_content => "Not OK", :original_file => "an_added_file.rb"}]]
|
209
363
|
|
210
|
-
|
364
|
+
file = double(:file)
|
365
|
+
file.should_receive(:write).with("Not OK")
|
366
|
+
File.should_receive(:open).with("an_added_file.rb", "w").and_yield(file)
|
211
367
|
|
212
|
-
|
213
|
-
ensure
|
214
|
-
FileUtils.cp("/tmp/README", "spec/fixtures/ruby_app/README") unless File.exists?("spec/fixtures/ruby_app/README")
|
215
|
-
end
|
368
|
+
rollback
|
216
369
|
end
|
217
|
-
end
|
218
370
|
|
219
|
-
|
220
|
-
|
221
|
-
FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README")
|
371
|
+
it "runs the rollback command when the operation is run_command and we have a rollback command" do
|
372
|
+
@operations = [[:run_command, { :rollback_command => "ls"}]]
|
222
373
|
|
223
|
-
|
374
|
+
Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
|
224
375
|
|
225
|
-
|
376
|
+
output = ""
|
226
377
|
|
227
|
-
|
378
|
+
io = double(:io)
|
379
|
+
io.should_receive(:read) do
|
380
|
+
""
|
381
|
+
end
|
228
382
|
|
229
|
-
|
230
|
-
|
383
|
+
IO.should_receive(:popen).with("ls").and_yield(io)
|
384
|
+
$?.should_receive(:exitstatus).and_return(0)
|
231
385
|
|
232
|
-
|
233
|
-
|
386
|
+
rollback
|
387
|
+
end
|
234
388
|
|
235
|
-
|
389
|
+
it "runs the rollback command when the operation is override_file" do
|
390
|
+
@operations = [[:override_file, { :tmp_file => "/tmp/reciper/my_file.rb", :overriden_file => "ola.rb"}]]
|
236
391
|
|
237
|
-
|
392
|
+
FileUtils.should_receive(:cp).with("/tmp/reciper/my_file.rb", "spec/fixtures/ruby_app/ola.rb")
|
238
393
|
|
239
|
-
|
394
|
+
rollback
|
240
395
|
end
|
241
396
|
end
|
242
397
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reciper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04
|
12
|
+
date: 2012-06-04 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: An awesome way to write recipes for a book chapter
|
15
15
|
email:
|
@@ -22,21 +22,12 @@ files:
|
|
22
22
|
- Gemfile
|
23
23
|
- Gemfile.lock
|
24
24
|
- LICENSE
|
25
|
+
- README.md
|
25
26
|
- Rakefile
|
26
27
|
- lib/reciper.rb
|
27
28
|
- lib/reciper/helpers.rb
|
28
29
|
- lib/reciper/version.rb
|
29
30
|
- reciper.gemspec
|
30
|
-
- spec/fixtures/recipe/README
|
31
|
-
- spec/fixtures/recipe/failing_spec.rb
|
32
|
-
- spec/fixtures/recipe/file.rb
|
33
|
-
- spec/fixtures/recipe/my_name.rb
|
34
|
-
- spec/fixtures/ruby_app/Gemfile
|
35
|
-
- spec/fixtures/ruby_app/Gemfile.lock
|
36
|
-
- spec/fixtures/ruby_app/README
|
37
|
-
- spec/fixtures/ruby_app/Rakefile
|
38
|
-
- spec/fixtures/ruby_app/lib/my_class.rb
|
39
|
-
- spec/fixtures/ruby_app/spec/true_spec.rb
|
40
31
|
- spec/reciper/helpers_spec.rb
|
41
32
|
- spec/spec_helper.rb
|
42
33
|
homepage: http://github.com/rodrigoflores/reciper
|
@@ -51,28 +42,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
42
|
- - ! '>='
|
52
43
|
- !ruby/object:Gem::Version
|
53
44
|
version: '0'
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
hash: -3801167226663698293
|
54
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
49
|
none: false
|
56
50
|
requirements:
|
57
51
|
- - ! '>='
|
58
52
|
- !ruby/object:Gem::Version
|
59
53
|
version: '0'
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
hash: -3801167226663698293
|
60
57
|
requirements: []
|
61
58
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.8.
|
59
|
+
rubygems_version: 1.8.23
|
63
60
|
signing_key:
|
64
61
|
specification_version: 3
|
65
62
|
summary: An awesome way to write recipes for a book chapter
|
66
63
|
test_files:
|
67
|
-
- spec/fixtures/recipe/README
|
68
|
-
- spec/fixtures/recipe/failing_spec.rb
|
69
|
-
- spec/fixtures/recipe/file.rb
|
70
|
-
- spec/fixtures/recipe/my_name.rb
|
71
|
-
- spec/fixtures/ruby_app/Gemfile
|
72
|
-
- spec/fixtures/ruby_app/Gemfile.lock
|
73
|
-
- spec/fixtures/ruby_app/README
|
74
|
-
- spec/fixtures/ruby_app/Rakefile
|
75
|
-
- spec/fixtures/ruby_app/lib/my_class.rb
|
76
|
-
- spec/fixtures/ruby_app/spec/true_spec.rb
|
77
64
|
- spec/reciper/helpers_spec.rb
|
78
65
|
- spec/spec_helper.rb
|
data/spec/fixtures/recipe/README
DELETED
File without changes
|
File without changes
|
@@ -1 +0,0 @@
|
|
1
|
-
some content
|