require 'spec_helper' describe Penchant::Gemfile do include FakeFS::SpecHelpers let(:dir) { File.expand_path(Dir.pwd) } let(:gemfile) { described_class.new(dir) } let(:gemfile_path) { File.join(dir, 'Gemfile') } let(:gemfile_erb_path) { File.join(dir, 'Gemfile.erb') } def write_file(path, content = nil) File.open(path, 'wb') do |fh| content = yield if block_given? fh.print content end end subject { gemfile } context 'with no gemfile' do it { should_not have_gemfile } it { should_not have_gemfile_erb } end context 'with gemfile' do let(:data) { "whatever" } before do write_file(gemfile_path) { data } end describe 'existence' do it { should have_gemfile } it { should_not have_gemfile_erb } end describe '#environment' do context 'not defined' do its(:environment) { should be_nil } end context 'defined' do let(:environment) { 'test' } let(:data) { <<-GEMFILE } # generated by penchant, environment: #{environment} GEMFILE its(:environment) { should == environment } end end describe '#switch_to!' do it 'should raise an exception' do expect { subject.switch_to!(:whatever) }.to raise_error(Errno::ENOENT) end end end context 'with gemfile.erb' do let(:erb_data) { 'whatever' } before do write_file(gemfile_erb_path) { erb_data } end it { should_not have_gemfile } it { should have_gemfile_erb } describe '#switch_to!' do let(:erb_data) { <<-ERB } <% env :test do %> test <% end %> <% env :not do %> not <% end %> all ERB it 'should render test data' do subject.switch_to!(:test) File.read('Gemfile').should include('test') File.read('Gemfile').should_not include('not') File.read('Gemfile').should include('all') end it 'should not render test data' do subject.switch_to!(:not) File.read('Gemfile').should_not include('test') File.read('Gemfile').should include('not') File.read('Gemfile').should include('all') end it 'should not render either' do subject.switch_to! File.read('Gemfile').should_not include('test') File.read('Gemfile').should_not include('not') File.read('Gemfile').should include('all') end it { should_not have_dot_penchant } context 'with .penchant' do before do File.open('.penchant', 'wb') end it { should have_dot_penchant } it 'should process the file' do subject.switch_to!(:not) end end end end end