Permalink
Browse files

Make sure that the test for the install task really does test for .sv…

…n metadata, and .svn folders,

and CVS metadata, and those annoying mac meta data files too.  Refactor a bunch to clean up the test,
and comment to explain what is going on.
  • Loading branch information...
1 parent 71e2255 commit cb99d8585f008e83318b819fa5711903c9771986 rob committed Jul 30, 2007
Showing with 88 additions and 25 deletions.
  1. +4 −1 install.rb
  2. +21 −2 tasks/relevance_extensions_tasks.rake
  3. +63 −22 test/unit/streamlined/relevance_extensions_tasks_test.rb
View
@@ -1,9 +1,12 @@
require 'rake'
-# Install hook code here
+
begin
puts "======================================================================"
puts "Attempting to copy Streamlined required files into your application..."
puts "======================================================================"
+ RAKE_FILE = File.join(File.dirname(__FILE__), '/tasks/relevance_extensions_tasks.rake')
+ load RAKE_FILE
+
Rake::Task['streamlined:install_files'].invoke
puts "======================================================================"
puts "Success!"
@@ -1,3 +1,5 @@
+require 'find'
+
# Handle copying over the default assets, views, and layout that Streamlined depends on.
# We don't do all this in the rake task to make things easier to test.
module Streamlined
@@ -9,9 +11,26 @@ module Streamlined
end
# Copy the files from streamlined into the Rails project
+ # Ignores any files or directories that start with a period (so .svn will get ignored),
+ # also will ignore CVS metadata.
def self.install
- files = Dir.glob("#{source}/*")
- files.each { |file| FileUtils.cp_r(file, destination) }
+ paths = []
+ Find.find(source) do |path|
+ Find.prune if path =~ /\/\..+/
+ Find.prune if path =~ /CVS/
+ paths << path
+ end
+ paths.each do |path|
+ dest_path = path.gsub(source, destination)
+ if File.directory?(path)
+ FileUtils.mkdir_p(dest_path) unless File.exists?(dest_path)
+ else
+ FileUtils.cp(path, dest_path)
+ end
+ end
+ rescue Exception => e
+ puts "Error trying to copy files: #{e.inspect}"
+ raise e
end
end
@@ -1,47 +1,88 @@
require File.join(File.dirname(__FILE__), '../../test_helper')
require 'rake'
+# Test the install process that runs on install that copies over static files required by Streamlined
class Streamlined::RakeTasksTest < Test::Unit::TestCase
RAKE_FILE = File.join(File.dirname(__FILE__), '../../../tasks/relevance_extensions_tasks.rake')
+
+ # We test the install task using real files and real directories, because mocks are to easy to give
+ # us "false passes" when the code really doesn't work for all edge cases.
+
+ # we have a root directory we create in the tmp dir to do all our work in
+ # we have a "source" there which we use as a sandbox to create our test files, and a "destination"
+ # which would correspond to RAILS_ROOT in a real project
+ attr_with_default(:root) { "#{Dir.tmpdir}/streamlined_test" }
+ attr_with_default(:source) { "#{root}/src" }
+ attr_with_default(:destination) { "#{root}/dest" }
+
+ # our stub directories and files that we will create and test against
+ attr_with_default(:source_directories) { ["/CVS", "/images", "/images/.svn/", "/javascripts", "/javascripts/nested_js"] }
+ attr_with_default(:should_be_copied) { %w[readme.txt foo.rb images/logo.png javascripts/foo.js javascripts/nested_js/bar.js] }
+ attr_with_default(:should_not_be_copied) { %w[.svn .DS_STORE /images/.svn/svn_meta_data] }
+
def setup
load RAKE_FILE
- @test_target_directory_root = "#{Dir.tmpdir}/streamlined_test"
-
- @test_target_directory_source = "#{@test_target_directory_root}/src"
- @test_target_directory_destination = "#{@test_target_directory_root}/dest"
+
+ create_directory [source, destination]
+ create_source_directories
+
+ (should_be_copied + should_not_be_copied).each { |path| FileUtils.touch "#{source}/#{path}" }
- @original_source, Streamlined::Assets.source = Streamlined::Assets.source, @test_target_directory_source
- @original_destination, Streamlined::Assets.destination = Streamlined::Assets.destination, @test_target_directory_destination
+ # swap the original and the temp directories we are using for the test
+ @original_source, Streamlined::Assets.source = Streamlined::Assets.source, source
+ @original_destination, Streamlined::Assets.destination = Streamlined::Assets.destination, destination
end
def teardown
+ # replace the source and dest with the original values
Streamlined::Assets.source = @original_source
Streamlined::Assets.destination = @original_destination
-
- FileUtils.rm_r @test_target_directory_root
- assert_false File.exists?(@test_target_directory_root)
+ # clean up after ourselves
+ FileUtils.rm_r root
+ should_not_exist root
end
- def test_install_skips_svn_directories
- create_directory "#{@test_target_directory_source}/images"
- create_directory @test_target_directory_destination
-
- [".svn", "should_get_copied", "images/should_get_copied.png"].each do |path|
- FileUtils.touch "#{@test_target_directory_source}/#{path}"
- end
-
+ def test_install_skips_dot_files_and_CVS_metadata
Streamlined::Assets.install
- assert File.exists?("#{@test_target_directory_destination}/should_get_copied")
- assert File.exists?("#{@test_target_directory_destination}/images/should_get_copied.png")
- assert_false File.exists?("#{@test_target_directory_destination}/.svn")
+ should_be_copied.each { |path| should_exist(path) }
+ should_not_be_copied.each { |path| should_not_exist(path) }
+ end
+
+ # lets make sure things work fine using rake invoke, since thats how install.rb does it
+ def test_should_be_able_to_install_using_rake_invoke
+ Rake::Task['streamlined:install_files'].invoke
+
+ should_be_copied.each { |path| should_exist(path) }
+ should_not_be_copied.each { |path| should_not_exist(path) }
end
private
+
+ def create_source_directories
+ self.source_directories = source_directories.map { |dir| "#{source}/#{dir}"}
+ create_directories source_directories
+ end
+
+ def should_exist(path)
+ assert File.exists?(full_destination_path(path)), "The path '#{full_destination_path(path)}' should exist but does not."
+ end
- def create_directory(path)
- FileUtils.mkdir_p path unless File.exists? path
+ def should_not_exist(path)
+ assert_false File.exists?(full_destination_path(path)), "The path '#{full_destination_path(path)}' should not exist but does."
end
+
+ def full_destination_path(path)
+ File.join(destination, path)
+ end
+
+ # creates 1 to many directories if they don't exist
+ def create_directories(paths)
+ paths = Array(paths)
+ paths.each { |path| FileUtils.mkdir_p path unless File.exists? path }
+ end
+ alias_method :create_directory, :create_directories
+
end

0 comments on commit cb99d85

Please sign in to comment.