test/generator_test.rb in axtro-rubber-1.2.3 vs test/generator_test.rb in axtro-rubber-1.5.7

- old
+ new

@@ -1,11 +1,11 @@ require 'rubygems' gem 'test-unit' require 'test/unit' require 'tempfile' -require 'test_helper' +require File.expand_path(File.join(__FILE__, '..', 'test_helper')) class GeneratorTest < Test::Unit::TestCase include Rubber::Configuration def test_validate @@ -93,10 +93,18 @@ assert ! File.exists?(post_file), "post should not have been executed as dest file unchanged" assert_equal "hello", File.read(out_file.path).strip, "transformed contents are incorrect" FileUtils.rm_f(post_file) gen = Generator.new(nil, nil, nil) + gen.force = true + gen.transform(src) + assert File.exists?(out_file.path), "transform did not generate an output file" + assert File.exists?(post_file), "forced transform did not execute post" + assert_equal "hello", File.read(out_file.path).strip, "transformed contents are incorrect" + + FileUtils.rm_f(post_file) + gen = Generator.new(nil, nil, nil) gen.no_post = true gen.transform(src + "there") assert File.exists?(out_file.path), "transform did not generate an output file" assert ! File.exists?(post_file), "post should not have been generated for no_post option" assert_equal "hello\nthere", File.read(out_file.path).strip, "transformed contents are incorrect" @@ -176,11 +184,11 @@ @write_cmd = 'badcommand' %> hello SRC - assert_raises RuntimeError do + assert_raises do Generator.new(nil, nil, nil).transform(src) end end @@ -222,27 +230,27 @@ assert_equal "common", File.read("#{out_dir}/bar.conf").strip, "transformed contents are incorrect" FileUtils.rm_rf(out_dir) assert ! File.exists?(out_dir) - g = Generator.new("#{File.dirname(__FILE__)}/fixtures/basic", ['role1'], ['host1'], :out_dir => out_dir) + g = Generator.new("#{File.dirname(__FILE__)}/fixtures/basic", ['role1'], 'host1', :out_dir => out_dir) g.run() assert File.directory?(out_dir), "scoped transform did not create dir" assert_equal ['bar.conf', 'foo.conf'], list_dir(out_dir), "scoped transform did not create correct files" assert_equal "host1", File.read("#{out_dir}/foo.conf").strip, "transformed contents are incorrect" assert_equal "common", File.read("#{out_dir}/bar.conf").strip, "transformed contents are incorrect" FileUtils.rm_rf(out_dir) end - def test_ordering + def test_ordering_of_additive out_dir = "#{Dir::tmpdir}/test_rubber_ordering" FileUtils.rm_rf(out_dir) assert ! File.exists?(out_dir) - g = Generator.new("#{File.dirname(__FILE__)}/fixtures/generator_order", ['role2', 'role1'], ['host1'], :out_dir => out_dir) + g = Generator.new("#{File.dirname(__FILE__)}/fixtures/generator_order", ['role2', 'role1'], 'host1', :out_dir => out_dir) g.run() assert File.directory?(out_dir), "transform did not create dir" assert_equal ['out.conf'], list_dir(out_dir), "transform did not create correct file" result = <<DATA # common first start @@ -321,6 +329,42 @@ SRC Generator.new(nil, nil, nil).transform(src) assert ! File.exists?(out_file.path), "transform didn't skip generation of an output file" end + + def test_backup + out_file = Tempfile.new('testbak') + assert ! File.exists?("#{out_file.path}.bak") + File.open(out_file.path, 'w') {|f| f.write("howdy")} + + src = <<-SRC + <% + @path = '#{out_file.path}' + %> + hello <%= Time.now.to_f %> + SRC + + Generator.new(nil, nil, nil).transform(src) + + assert File.exists?("#{out_file.path}.bak"), "transform didn't generate backup" + assert_match /howdy/, File.read("#{out_file.path}.bak"), "transform backup has wrong contents" + end + + def test_no_backup + out_file = Tempfile.new('testnobak') + assert ! File.exists?("#{out_file.path}.bak") + File.open(out_file.path, 'w') {|f| f.write("howdy")} + src = <<-SRC + <% + @path = '#{out_file.path}' + @backup = false + %> + hello <%= Time.now.to_f %> + SRC + + Generator.new(nil, nil, nil).transform(src) + + assert ! File.exists?("#{out_file.path}.bak"), "transform shouldn't generate backup" + end + end