require_relative '../spec_helper' require 'fakefs/spec_helpers' describe 'Template' do include FakeFS::SpecHelpers let(:template_dir) { "/tmp/template/sample" } let(:project_dir) { "/tmp/output/new_project" } context 'with all fields specified' do let(:template) { XCBootstrap::Template.new({"from" => "some/path/from","to" => "some/path/to"}, template_dir, project_dir) } it 'should have the from field inside the template dir' do template.from.should == "/tmp/template/sample/some/path/from" end it 'should have the to field inside the output project dir' do template.to.should == "/tmp/output/new_project/some/path/to" end end context 'without the to field specified' do let(:template) { XCBootstrap::Template.new({"from" => "some/path/from"}, template_dir, project_dir) } it 'should use the from field as the to' do template.to.should == "/tmp/output/new_project/some/path/from" end end context 'with the template name in the from path' do let(:template) { XCBootstrap::Template.new({"from" => "some/path/including/sample/in/path"}, template_dir, project_dir) } it 'should use the from field with the template name replaces with the project name' do template.to.should == "/tmp/output/new_project/some/path/including/new_project/in/path" end end context 'when processing the file' do let(:template) { XCBootstrap::Template.new({"from" => "path/to/file"}, template_dir, project_dir) } before(:each) do File.stub(:binary?).and_return(false) FileUtils.mkdir_p "/tmp/template/sample/path/to/" File.open "/tmp/template/sample/path/to/file", "w" do |from_file| from_file.puts "content including sample name" end end it 'should create any intermediate directories to the destination' do template.process File.exists?("/tmp/output/new_project/path/to/").should be_true File.directory?("/tmp/output/new_project/path/to/").should be_true end it 'should copy the file to the destination' do template.process File.exists?("/tmp/output/new_project/path/to/file").should be_true File.directory?("/tmp/output/new_project/path/to/file").should be_false end it 'should replace occurances of the template name with the new project name' do template.process File.read("/tmp/output/new_project/path/to/file").should_not include("sample") File.read("/tmp/output/new_project/path/to/file").should include("new_project") end end context 'when processing a binary file' do let(:template) { XCBootstrap::Template.new({"from" => "path/to/binary/file"}, template_dir, project_dir) } before(:each) do File.stub(:binary?).and_return(true) FileUtils.mkdir_p "/tmp/template/sample/path/to/binary" File.open "/tmp/template/sample/path/to/binary/file", "wb" do |from_file| from_file.puts "content including sample name" end end it 'should not replace occurances of the template name' do template.process File.read("/tmp/output/new_project/path/to/binary/file").should include("sample") end end end