Skip to content

Commit

Permalink
Override_file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Flores committed Apr 11, 2012
1 parent 48a4535 commit 55c512c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
pkg/*
29 changes: 28 additions & 1 deletion lib/reciper/helpers.rb
Expand Up @@ -4,6 +4,9 @@ module Reciper
class NoTestOutput < RuntimeError
end

class NoFileToBeOverriden < RuntimeError
end

module Helpers
def copy_file(filename, options={})
destination_dir = @ruby_app_path + "/" + (options[:to] || "")
Expand All @@ -14,7 +17,13 @@ def copy_file(filename, options={})

FileUtils.cp(@recipe_path + "/" + filename, destination_dir)

@operations << [:copy, (options[:to] || "") + filename]
new_filename = options[:as] || filename

if(options[:as])
FileUtils.mv(destination_dir + "/" + filename, destination_dir + "/" + new_filename)
end

@operations << [:copy, (options[:to] || "") + new_filename]
end

def run_tests(options={})
Expand Down Expand Up @@ -71,6 +80,8 @@ def rollback
spawn(operation[1]) if operation[1]

Process.wait
elsif operation[0] == :override_file
FileUtils.cp(operation[1], @ruby_app_path + "/" + operation[2])
end
end
end
Expand All @@ -89,5 +100,21 @@ def run_command(command, rollback_command=nil)
false
end
end

def override_file(file, file_to_be_overriden)
Dir.chdir(@ruby_app_path) do
fail NoFileToBeOverriden unless File.exists?(file_to_be_overriden)

FileUtils.mkdir_p("/tmp/reciper")
filename = File.basename(file_to_be_overriden)
tmp_file = "/tmp/reciper/#{filename}"

FileUtils.cp(file_to_be_overriden, tmp_file)

@operations << [:override_file, tmp_file, file_to_be_overriden]
end

FileUtils.cp(@recipe_path + "/" + file, @ruby_app_path + "/" + file_to_be_overriden)
end
end
end
Empty file added spec/fixtures/recipe/README
Empty file.
1 change: 1 addition & 0 deletions spec/fixtures/ruby_app/README
@@ -0,0 +1 @@
some content
51 changes: 51 additions & 0 deletions spec/reciper/helpers_spec.rb
Expand Up @@ -33,6 +33,16 @@
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

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

Expand Down Expand Up @@ -187,5 +197,46 @@ class MyClass

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")

File.exists?("spec/fixtures/ruby_app/README").should_not be

@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
end
end

describe ".override_file" do
it "overrides the file with another file" do
FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README")

File.read("spec/fixtures/ruby_app/README").should == "some content"

override_file("README", "README")

File.read("spec/fixtures/ruby_app/README").should == ""

FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README")
end

it "adds the operation to operations array" do
FileUtils.cp("spec/fixtures/ruby_app/README", "/tmp/README")

override_file("README", "README")

@operations.should include([:override_file, "/tmp/reciper/README", "README"])

FileUtils.mv("/tmp/README", "spec/fixtures/ruby_app/README")
end
end
end

0 comments on commit 55c512c

Please sign in to comment.