spec/reciper/helpers_spec.rb in reciper-0.0.4 vs spec/reciper/helpers_spec.rb in reciper-0.1.0

- old
+ new

@@ -12,231 +12,386 @@ @operations = [] end describe ".copy" do it "copies the file from the recipe path to the ruby app root path " do - File.exists?("spec/fixtures/ruby_app/file.rb").should_not be + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/file.rb") copy_file("file.rb") - - File.exists?("spec/fixtures/ruby_app/file.rb").should be - - FileUtils.rm("spec/fixtures/ruby_app/file.rb") end it "copies the file from the recipe path to the ruby app" do - File.exists?("spec/fixtures/ruby_app/lib/file.rb").should_not be + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/lib/file.rb") copy_file("file.rb", :to => "lib") - - File.exists?("spec/fixtures/ruby_app/lib/file.rb").should be - - FileUtils.rm("spec/fixtures/ruby_app/lib/file.rb") end it "copies the file with the name as in defined in as" do - File.exists?("spec/fixtures/ruby_app/file.rb").should_not be + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/another_file.rb") copy_file("file.rb", :as => "another_file.rb") - - File.exists?("spec/fixtures/ruby_app/another_file.rb").should be - - FileUtils.rm("spec/fixtures/ruby_app/another_file.rb") end it "if the dir doesn't exists, create it" do - File.exists?("spec/fixtures/ruby_app/lib/file.rb").should_not be + directory = @ruby_app_path + "/my_awesome_dir" + File.should_receive(:directory?).with("spec/fixtures/ruby_app/my_awesome_dir").and_return(false) + FileUtils.should_receive(:mkdir_p).with("spec/fixtures/ruby_app/my_awesome_dir") + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/my_awesome_dir/file.rb") copy_file("file.rb", :to => "my_awesome_dir") - - File.exists?("spec/fixtures/ruby_app/my_awesome_dir/file.rb").should be - - FileUtils.rm_rf("spec/fixtures/ruby_app/my_awesome_dir") end it "adds the operation to @operation array" do + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/file.rb", "spec/fixtures/ruby_app/file.rb") + copy_file("file.rb") - @operations.should include([:copy, "file.rb"]) - - FileUtils.rm("spec/fixtures/ruby_app/file.rb") + @operations.should include([:copy_file, { :destination => "/file.rb" }]) end end describe ".run_tests" do it "returns 0 if all tests pass" do + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + + test_output = <<-EOF + .... + + Finished in 11.29 seconds + 23 examples, 0 failures + EOF + + io = double(:io, :read => test_output) + IO.should_receive(:popen).with("bundle exec rspec spec").and_yield(io) + run_tests.should == 0 end it "returns 1 if there is only one failure" do - copy_file("failing_spec.rb", :to => "spec") + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield - run_tests.should == 1 + test_output = <<-EOF + FE.. - FileUtils.rm("spec/fixtures/ruby_app/spec/failing_spec.rb") + Finished in 11.29 seconds + 4 examples, 2 failures + EOF + + io = double(:io, :read => test_output) + IO.should_receive(:popen).with("bundle exec rspec spec").and_yield(io) + + run_tests.should == 2 end end describe ".run_rake_task" do - it "returns true when the rake task has been run ok" do - run_rake_task("puts_something").should be + it "returns a hash with successful as true when the rake task has been run successfully" do + output = "" + + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + + io = double(:io) + io.stub!(:read).and_return("") + + IO.should_receive(:popen). + with("bundle exec rake puts_something").and_yield(io) + + $?.should_receive(:exitstatus).and_return(0) + + run_rake_task("puts_something").should == { + :response => "", + :successful => true + } end - it "returns false when the rake task hasn't been run ok" do - run_rake_task("idontexists").should_not be + it "returns a hash with successful as false when the rake task hasn't been run successfully" do + output = "" + + io = double(:io) + io.stub!(:read).and_return("") + + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + + IO.should_receive(:popen). + with("bundle exec rake puts_something").and_yield(io) + $?.should_receive(:exitstatus).and_return(1) + + run_rake_task("puts_something").should == { + :response => "", + :successful => false + } end end describe ".copy_line_range" do - it "copies the entire input file to the output line " do - @expected_at_the_beginning = <<-EOF -class MyClass -end + it "copies the entire input file to the output line on a specified line" do + readme = <<-EOF +d +e EOF - File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == @expected_at_the_beginning.chomp - expected_at_the_end = <<-EOF -class MyClass -def my_name - puts self.name -end -end + original = <<-EOF +a +b +c +f EOF - copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2) + Dir.should_receive(:glob).with("spec/fixtures/ruby_app/README.md"). + and_return(["spec/fixtures/ruby_app/README.md"]) - File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == expected_at_the_end.chomp + File.should_receive(:read).with("spec/fixtures/recipe/README"). + and_return(readme) + + File.should_receive(:read).with("spec/fixtures/ruby_app/README.md"). + and_return(original) + + file = double(:file) + file.should_receive(:write).with("a\nb\nc\nd\ne\nf") + File.should_receive(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file) + + copy_line_range("README", "README.md", :to_line => 3) end it "copies only specified lines" do - @expected_at_the_beginning = <<-EOF -class MyClass -end + readme = <<-EOF +a +d +e +f +g EOF - File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == @expected_at_the_beginning.chomp - - expected_at_the_end = <<-EOF -class MyClass - puts self.name -end + original = <<-EOF +a +b +c +f EOF - copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2, :from_lines => (2..2)) + Dir.should_receive(:glob).with("spec/fixtures/ruby_app/README.md"). + and_return(["spec/fixtures/ruby_app/README.md"]) - File.read("spec/fixtures/ruby_app/lib/my_class.rb").should == expected_at_the_end.chomp + File.should_receive(:read).with("spec/fixtures/recipe/README"). + and_return(readme) + + File.should_receive(:read).with("spec/fixtures/ruby_app/README.md"). + and_return(original) + + file = double(:file) + file.should_receive(:write).with("a\nb\nc\nd\ne\nf") + File.should_receive(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file) + + copy_line_range("README", "README.md", :to_line => 3, :lines => (1..2)) end it "adds an entry to operations" do - @expected_at_the_beginning = <<-EOF -class MyClass -end + readme = <<-EOF +a +d +e +f +g EOF - copy_line_range("my_name.rb", "lib/my_class.rb", :to_line => 2) + original = <<-EOF +a +b +c +f +EOF - @operations.should include([:copy_range, "lib/my_class.rb", @expected_at_the_beginning.chomp]) + Dir.stub!(:glob).with("spec/fixtures/ruby_app/README.md"). + and_return(["spec/fixtures/ruby_app/README.md"]) + + File.stub!(:read).with("spec/fixtures/recipe/README"). + and_return(readme) + + File.stub!(:read).with("spec/fixtures/ruby_app/README.md"). + and_return(original) + + file = double(:file) + file.stub!(:write).with("a\nb\nc\nd\ne\nf") + File.stub!(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file) + + copy_line_range("README", "README.md", :to_line => 3, :lines => (1..2)) + + @operations.should include([:copy_line_range, { :original_content => original, :original_file => "spec/fixtures/ruby_app/README.md"}]) end - after do - File.write("spec/fixtures/ruby_app/lib/my_class.rb", @expected_at_the_beginning.chomp) + context "suffix copy" do + it "works with only the suffix of the file when there is only one file" do + readme = "" + original = "" + + Dir.should_receive(:glob).with("spec/fixtures/ruby_app/*.md"). + and_return(["spec/fixtures/ruby_app/README.md"]) + + File.stub!(:read).with("spec/fixtures/recipe/README"). + and_return(readme) + + File.should_receive(:read).with("spec/fixtures/ruby_app/README.md"). + and_return(original) + + file = double(:file) + file.stub!(:write) + File.stub!(:open).with("spec/fixtures/ruby_app/README.md", "w").and_yield(file) + + copy_line_range("README", "*.md", :to_line => 0) + end + + it "raises an exception when given only the suffix of the file when there is more than one file" do + readme = "" + original = "" + + Dir.should_receive(:glob).with("spec/fixtures/ruby_app/*.md"). + and_return(["spec/fixtures/ruby_app/README.md", "spec/fixtures/ruby_app/README2.md"]) + + lambda { + copy_line_range("README", "*.md", :to_line => 0) + }.should raise_error(Reciper::NoFileOrMultipleFilesFound) + end end end describe ".run_command" do - it "runs a command on projects folder and returns true when successful" do - run_command("ls").should be + it "runs a command on projects folder and returns the command hash with the response and true when successful" do + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + + output = <<EOF +a +b +EOF + + io = double(:io) + io.should_receive(:read) do + "a\nb\n" + end + + IO.should_receive(:popen).with("ls").and_yield(io) + $?.should_receive(:exitstatus).and_return(0) + + run_command("ls").should == { + :response => "a\nb\n", + :successful => true + } end - it "runs a command on projects folder and returns not true when failure" do - run_command("cp").should_not be + it "runs a command on projects folder and returns the command hash with the response and false when not successful" do + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + + output = <<EOF +a +b +EOF + + io = double(:io) + io.should_receive(:read) do + "a\nb\n" + end + + IO.should_receive(:popen).with("ls").and_yield(io) + $?.should_receive(:exitstatus).and_return(1) + + run_command("ls").should == { + :response => "a\nb\n", + :successful => false + } end it "receives the rollback command together with the command and store it on @operations array" do run_command("ls", "ls -a") - @operations.should include([:run_command, "ls -a"]) + @operations.should include([:run_command, { :rollback_command => "ls -a" }]) end - end - describe ".rollback" do - it "removes the file when the operation is copy" do - File.open(@ruby_app_path + "/an_added_file.rb", "w") { - |f| f.write("OK") - } + it "doesn't require the rollback command to be informed" do + run_command("ls") - File.exists?("spec/fixtures/ruby_app/an_added_file.rb").should be + @operations.should include([:run_command, { :rollback_command => nil}]) + end + end - @operations = [[:copy, "an_added_file.rb"]] + describe ".override_file" do + it "overrides the file with another file" do + Dir.should_receive(:chdir).with(@ruby_app_path).and_yield + File.should_receive(:exists?).with("README").and_return(true) + FileUtils.should_receive(:mkdir_p).with("/tmp/reciper") - rollback + FileUtils.should_receive(:cp).with("README", "/tmp/reciper/README") + FileUtils.should_receive(:cp).with("spec/fixtures/recipe/README", + "spec/fixtures/ruby_app/README") - File.exists?("spec/fixtures/ruby_app/an_added_file.rb").should_not be + override_file("README", "README") end - it "restores the old file when the operation is copy_range" do - File.open(@ruby_app_path + "/an_added_file.rb", "w") { - |f| f.write("OK") - } + it "raises an error when file doesn't exists" do + Dir.stub!(:chdir).with(@ruby_app_path).and_yield + File.should_receive(:exists?).with("README").and_return(false) - File.read("spec/fixtures/ruby_app/an_added_file.rb").should == "OK" + lambda { + override_file("README", "README") + }.should raise_error(Reciper::NoFileToBeOverriden) + end - @operations = [[:copy_range, "an_added_file.rb", "Not OK"]] + it "adds the operation to operations array" do + Dir.stub!(:chdir).with(@ruby_app_path).and_yield + File.stub!(:exists?).with("README").and_return(true) + FileUtils.stub!(:mkdir_p).with("/tmp/reciper") - rollback + FileUtils.stub!(:cp).with("README", "/tmp/reciper/README") + FileUtils.stub!(:cp).with("spec/fixtures/recipe/README", + "spec/fixtures/ruby_app/README") - File.read("spec/fixtures/ruby_app/an_added_file.rb").should == "Not OK" - - FileUtils.rm("spec/fixtures/ruby_app/an_added_file.rb") + override_file("README", "README") + @operations.should include([:override_file, { :tmp_file => "/tmp/reciper/README", :overriden_file => "README"}]) end + end - it "runs the rollback command when the operation is run_command and we have a rollback command" do - @operations = [[:run_command, "ls"]] + describe ".rollback" do + it "removes the file when the operation is copy_file" do + @operations = [[:copy_file, { :destination => "README" }]] - self.should_receive(:spawn).with("ls") - Process.stub!(:wait) + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield + FileUtils.should_receive(:rm).with("README") + rollback end - it "runs the rollback command when the operation is override_file" do - begin - FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README") - FileUtils.rm("spec/fixtures/ruby_app/README") + it "restores the old file when the operation is copy_range" do + @operations = [[:copy_range, { :original_content => "Not OK", :original_file => "an_added_file.rb"}]] - File.exists?("spec/fixtures/ruby_app/README").should_not be + file = double(:file) + file.should_receive(:write).with("Not OK") + File.should_receive(:open).with("an_added_file.rb", "w").and_yield(file) - @operations = [[:override_file, "/tmp/README", "README"]] - - rollback - - File.exists?("spec/fixtures/ruby_app/README").should be - ensure - FileUtils.cp("/tmp/README", "spec/fixtures/ruby_app/README") unless File.exists?("spec/fixtures/ruby_app/README") - end + rollback end - end - describe ".override_file" do - it "overrides the file with another file" do - FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README") + it "runs the rollback command when the operation is run_command and we have a rollback command" do + @operations = [[:run_command, { :rollback_command => "ls"}]] - File.read("spec/fixtures/ruby_app/README").should == "some content" + Dir.should_receive(:chdir).with("spec/fixtures/ruby_app").and_yield - override_file("README", "README") + output = "" - File.read("spec/fixtures/ruby_app/README").should == "" + io = double(:io) + io.should_receive(:read) do + "" + end - FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README") + IO.should_receive(:popen).with("ls").and_yield(io) + $?.should_receive(:exitstatus).and_return(0) + + rollback end - it "adds the operation to operations array" do - FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README") + it "runs the rollback command when the operation is override_file" do + @operations = [[:override_file, { :tmp_file => "/tmp/reciper/my_file.rb", :overriden_file => "ola.rb"}]] - override_file("README", "README") + FileUtils.should_receive(:cp).with("/tmp/reciper/my_file.rb", "spec/fixtures/ruby_app/ola.rb") - @operations.should include([:override_file, "/tmp/reciper/README", "README"]) - - FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README") + rollback end end end \ No newline at end of file