require 'spec_helper.rb' describe Temp do before :each do @dir = File.expand_path(File.dirname(__FILE__)) @test_dir = File.join(@dir, 'test') @temp_dir = File.join(@dir, 'template') FileUtils.mkdir(@test_dir) end after :each do FileUtils.rm_r(@test_dir) end describe Temp::Runner do describe 'configuration' do before :each do @conf_file1 = File.join(@dir, 'tempconf.yml') @conf_file2 = File.join(@test_dir, '.tempconf') end it 'should load a configuration file' do runner = Temp::Runner.new(:conf_file => @conf_file1) runner.conf[:test].should == 'test' end it 'should create a configuration file if it is nonexistent' do runner = Temp::Runner.new(:conf_file => @conf_file2) File.file?(@conf_file2) end end end describe Temp::Copier do before :each do @copier = Temp::Copier.new(:template_dir => @temp_dir) @template1_name = 'template1' @template1 = File.join(@temp_dir, @template1_name) @template2_name = 'template.txt' @template2 = File.join(@temp_dir, @template2_name) end describe 'finding files' do before :each do @files = Dir.glob(@template1 + '/**/*').map { |f| f.sub(@template1 + '/', '') }.sort @ignore = ['.tempignore'] | Dir.glob(File.read(File.join(@template1, '.tempignore')).split(?\n)).map { |f| File.expand_path(f).sub(@template1 + '/', '') }.sort end it 'should find all files in a template' do @copier.find_files(@template1_name).sort.should == @files.sort end it 'should find the correct files to ignore from reading .tempignore' do @copier.read_tempignore(@template1_name).sort.should == @ignore end it 'should find all files in a template except for files to ignore' do @copier.find_files(@template1_name, @ignore).sort.should == @files - @ignore end end describe 'project creation' do before :each do @project1_dir = File.join(@test_dir, 'project') @project2_dir = File.join(@test_dir, @template1_name) @project1_file = File.join(@test_dir, 'project.txt') @project2_file = File.join(@test_dir, @template2_name) end describe 'success' do it 'should create a project directory' do @copier.create_project(@project1_dir, @template1_name) File.exist?(@project1_dir).should be_true end it 'should create a project directory, naming it after the template' do pwd = Dir.pwd Dir.chdir(@test_dir) @copier.create_project(@template1_name) File.exist?(@project2_dir).should be_true Dir.chdir(pwd) end it 'should copy all files from a template' do files = @copier.find_files(@template1_name, @copier.read_tempignore(@template1_name)) @copier.create_project(@project1_dir, @template1_name) Dir.glob(File.join(@project1_dir, '**/*')).map { |f| f.sub(@project1_dir + '/', '') }.sort.should == files.sort end it 'should copy all files from a template to a project named after it' do pwd = Dir.pwd Dir.chdir(@test_dir) files = @copier.find_files(@template1_name, @copier.read_tempignore(@template1_name)) @copier.create_project(@template1_name) Dir.glob(File.join(@project2_dir, '**/*')).map { |f| f.sub(@project2_dir + '/', '') }.sort.should == files.sort Dir.chdir(pwd) end it 'should copy a single file template' do @copier.create_project(@project1_file, @template2_name) File.file?(@project1_file).should be_true end it 'should copy a single file template named after the template' do @copier.create_project(@project2_file, @template2_name) File.file?(@project2_file).should be_true end end describe 'failure' do it 'should raise an exception if the project to create already exists' do FileUtils.mkdir(@project1_dir) lambda do @copier.create_project(@project1_dir, @template1_name) end.should raise_error end it 'should raise an exception if the template does not exist' do lambda do @copier.create_project(@project1_dir, 'fake_template') end.should raise_error end end end end end