reciper 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- reciper (0.0.3)
4
+ reciper (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |t|
7
+ t.rspec_opts = "--order rand -f d"
8
+ end
9
+
10
+ task :default => :spec
@@ -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
- destination_dir = @ruby_app_path + "/" + (options[:to] || "")
28
+ destination_file_name = options[:as] || filename
29
+ destination_dir = options[:to] || ""
13
30
 
14
- unless File.directory?(destination_dir)
15
- FileUtils.mkdir_p(destination_dir)
16
- end
31
+ destination = File.join(destination_dir , destination_file_name)
32
+ global_destination = File.join(@ruby_app_path, destination)
17
33
 
18
- FileUtils.cp(@recipe_path + "/" + filename, destination_dir)
34
+ create_directory_if_not_exists(File.join(@ruby_app_path, destination_dir))
19
35
 
20
- new_filename = options[:as] || filename
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 << [:copy, (options[:to] || "") + new_filename]
38
+ @operations << [:copy_file, { :destination => destination }]
27
39
  end
28
40
 
29
- def run_tests(options={})
30
- Dir.chdir(@ruby_app_path) do
31
- response = `bundle exec rspec spec`
32
-
33
- if response =~ /([.FE*]+)/
34
- $1.split("").reject { |char| char == "." }.size
35
- else
36
- puts "Can't get any test output"
37
- fail NoTestOutput
38
- end
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
- Dir.chdir(@ruby_app_path) do
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
- if options[:from_lines]
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
- from_file_lines = File.open(@recipe_path + "/" + from, "r").readlines
60
- output_lines = File.read(@ruby_app_path + "/" + to).split("\n")
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
- to_output = output_lines.insert(options[:to_line] - 1, from_file_lines.map(&:chomp).slice(from_lines)).flatten!.join("\n")
97
+ new_content = File.read(File.join(@recipe_path, from)).split("\n")
65
98
 
66
- to_file_output.write(to_output)
99
+ range = options[:lines] || (0..-1)
67
100
 
68
- to_file_output.close
101
+ original.insert(options[:to_line], new_content[range])
69
102
 
70
- @operations << [:copy_range, to, original_output.join("\n")]
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
- if operation[0] == :copy
76
- FileUtils.rm(@ruby_app_path + "/" + operation[1])
77
- elsif operation[0] == :copy_range
78
- File.open(@ruby_app_path + "/" + operation[1], "w") { |file| file.write(operation[2]) }
79
- elsif operation[0] == :run_command
80
- spawn(operation[1]) if operation[1]
81
-
82
- Process.wait
83
- elsif operation[0] == :override_file
84
- FileUtils.cp(operation[1], @ruby_app_path + "/" + operation[2])
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
- Dir.chdir(@ruby_app_path) do
91
- spawn("bundle exec #{command}", :out => "/dev/null", :err => "/dev/null")
150
+ response = ""
151
+ successful = ""
92
152
 
93
- Process.wait
94
- end
153
+ run_on_app_path do
154
+ IO.popen(command) do |io|
155
+ response = io.read
156
+ end
95
157
 
96
- if $?.exitstatus == 0
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
- Dir.chdir(@ruby_app_path) do
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 + "/" + file, @ruby_app_path + "/" + file_to_be_overriden)
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
@@ -1,3 +1,3 @@
1
1
  module Reciper
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -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
- File.exists?("spec/fixtures/ruby_app/file.rb").should_not be
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
- File.exists?("spec/fixtures/ruby_app/lib/file.rb").should_not be
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
- File.exists?("spec/fixtures/ruby_app/file.rb").should_not be
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
- File.exists?("spec/fixtures/ruby_app/lib/file.rb").should_not be
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
- copy_file("file.rb")
44
+ FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/file.rb")
58
45
 
59
- @operations.should include([:copy, "file.rb"])
46
+ copy_file("file.rb")
60
47
 
61
- FileUtils.rm("spec/fixtures/ruby_app/file.rb")
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
- copy_file("failing_spec.rb", :to => "spec")
70
+ Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
71
+
72
+ test_output = <<-EOF
73
+ FE..
72
74
 
73
- run_tests.should == 1
75
+ Finished in 11.29 seconds
76
+ 4 examples, 2 failures
77
+ EOF
74
78
 
75
- FileUtils.rm("spec/fixtures/ruby_app/spec/failing_spec.rb")
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 ok" do
81
- run_rake_task("puts_something").should be
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 ok" do
85
- run_rake_task("idontexists").should_not be
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
- @expected_at_the_beginning = <<-EOF
92
- class MyClass
93
- end
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
- File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == @expected_at_the_beginning.chomp
96
-
97
- expected_at_the_end = <<-EOF
98
- class MyClass
99
- def my_name
100
- puts self.name
101
- end
102
- end
131
+
132
+ original = <<-EOF
133
+ a
134
+ b
135
+ c
136
+ f
103
137
  EOF
104
138
 
105
- copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2)
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
- File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == expected_at_the_end.chomp
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
- @expected_at_the_beginning = <<-EOF
112
- class MyClass
113
- end
156
+ readme = <<-EOF
157
+ a
158
+ d
159
+ e
160
+ f
161
+ g
114
162
  EOF
115
163
 
116
- File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == @expected_at_the_beginning.chomp
117
-
118
- expected_at_the_end = <<-EOF
119
- class MyClass
120
- puts self.name
121
- end
164
+ original = <<-EOF
165
+ a
166
+ b
167
+ c
168
+ f
122
169
  EOF
123
170
 
124
- copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2, :from_lines => (2..2))
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/lib/my_class.rb").should == expected_at_the_end.chomp
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
- @expected_at_the_beginning = <<-EOF
131
- class MyClass
132
- end
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
- copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2)
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
- @operations.should include([:copy_range, "lib/my_class.rb", @expected_at_the_beginning.chomp])
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
- after do
141
- File.write("spec/fixtures/ruby_app/lib/my_class.rb", @expected_at_the_beginning.chomp)
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
- run_command("ls").should be
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 not true when failure" do
151
- run_command("cp").should_not be
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
- describe ".rollback" do
162
- it "removes the file when the operation is copy" do
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
- File.exists?("spec/fixtures/ruby_app/an_added_file.rb").should be
310
+ @operations.should include([:run_command, { :rollback_command => nil}])
311
+ end
312
+ end
168
313
 
169
- @operations = [[:copy, "an_added_file.rb"]]
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
- rollback
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
- File.exists?("spec/fixtures/ruby_app/an_added_file.rb").should_not be
324
+ override_file("README", "README")
174
325
  end
175
326
 
176
- it "restores the old file when the operation is copy_range" do
177
- File.open(@ruby_app_path + "/an_added_file.rb", "w") {
178
- |f| f.write("OK")
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
- @operations = [[:copy_range, "an_added_file.rb", "Not OK"]]
331
+ lambda {
332
+ override_file("README", "README")
333
+ }.should raise_error(Reciper::NoFileToBeOverriden)
334
+ end
184
335
 
185
- rollback
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
- File.read("spec/fixtures/ruby_app/an_added_file.rb").should == "Not OK"
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
- FileUtils.rm("spec/fixtures/ruby_app/an_added_file.rb")
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
- it "runs the rollback command when the operation is run_command and we have a rollback command" do
193
- @operations = [[:run_command, "ls"]]
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
- self.should_receive(:spawn).with("ls")
196
- Process.stub!(:wait)
356
+ FileUtils.should_receive(:rm).with("README")
197
357
 
198
358
  rollback
199
359
  end
200
360
 
201
- it "runs the rollback command when the operation is override_file" do
202
- begin
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
- rollback
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
- File.exists?("spec/fixtures/ruby_app/README").should be
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
- describe ".override_file" do
220
- it "overrides the file with another file" do
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
- File.read("spec/fixtures/ruby_app/README").should == "some content"
374
+ Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield
224
375
 
225
- override_file("README", "README")
376
+ output = ""
226
377
 
227
- File.read("spec/fixtures/ruby_app/README").should == ""
378
+ io = double(:io)
379
+ io.should_receive(:read) do
380
+ ""
381
+ end
228
382
 
229
- FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README")
230
- end
383
+ IO.should_receive(:popen).with("ls").and_yield(io)
384
+ $?.should_receive(:exitstatus).and_return(0)
231
385
 
232
- it "adds the operation to operations array" do
233
- FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README")
386
+ rollback
387
+ end
234
388
 
235
- override_file("README", "README")
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
- @operations.should include([:override_file, "/tmp/reciper/README", "README"])
392
+ FileUtils.should_receive(:cp).with("/tmp/reciper/my_file.rb", "spec/fixtures/ruby_app/ola.rb")
238
393
 
239
- FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README")
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
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-11 00:00:00.000000000 Z
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.11
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
File without changes
@@ -1,7 +0,0 @@
1
- require 'rspec'
2
-
3
- describe "true is true" do
4
- it "is true" do
5
- true.should_not be
6
- end
7
- end
File without changes
@@ -1,3 +0,0 @@
1
- def my_name
2
- puts self.name
3
- end
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem "rake"
4
- gem "rspec"
@@ -1,12 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- rake (0.9.2.2)
5
- rspec-core (2.9.0)
6
-
7
- PLATFORMS
8
- ruby
9
-
10
- DEPENDENCIES
11
- rake
12
- rspec-core
@@ -1 +0,0 @@
1
- some content
@@ -1,5 +0,0 @@
1
- task :default => [:puts_something]
2
-
3
- task :puts_something do
4
- sh "echo 'hello'"
5
- end
@@ -1,2 +0,0 @@
1
- class MyClass
2
- end
@@ -1,7 +0,0 @@
1
- require 'rspec'
2
-
3
- describe "true is true" do
4
- it "is true" do
5
- true.should be
6
- end
7
- end