chdir and remove test dir in after call if test fails

This commit is contained in:
Josh Holtrop 2013-06-30 09:59:12 -04:00
parent a732603b59
commit db45548b49

View File

@ -3,16 +3,12 @@ require 'fileutils'
describe Rscons do describe Rscons do
FileUtils.rm_rf('build_test') FileUtils.rm_rf('build_test')
def setup_testdir(files, &blk) def setup_testdir(files)
FileUtils.mkdir_p('build_test')
files.each do |fname| files.each do |fname|
src = "spec/build_items/#{fname}" src = "#{@owd}/spec/build_items/#{fname}"
dst = "build_test/#{fname}" FileUtils.mkdir_p(File.dirname(fname))
FileUtils.mkdir_p(File.dirname(dst)) FileUtils.cp_r(src, fname)
FileUtils.cp_r(src, dst)
end end
Dir.chdir('build_test', &blk)
FileUtils.rm_rf('build_test')
end end
def clear_cache def clear_cache
@ -22,6 +18,14 @@ describe Rscons do
before do before do
$stdout.stub(:puts) { nil } $stdout.stub(:puts) { nil }
clear_cache clear_cache
@owd = Dir.pwd
FileUtils.mkdir_p('build_test')
Dir.chdir('build_test')
end
after do
Dir.chdir(@owd)
FileUtils.rm_rf('build_test')
end end
########################################################################### ###########################################################################
@ -29,72 +33,66 @@ describe Rscons do
########################################################################### ###########################################################################
it 'builds a C program with one source file' do it 'builds a C program with one source file' do
setup_testdir(['simple.c']) do setup_testdir(['simple.c'])
env = Rscons::Environment.new env = Rscons::Environment.new
env.Program('simple', 'simple.c') env.Program('simple', 'simple.c')
File.exist?('simple.o').should be_true File.exist?('simple.o').should be_true
`./simple`.should =~ /This is a simple C program/ `./simple`.should =~ /This is a simple C program/
end
end end
it 'supports short echo mode' do it 'supports short echo mode' do
$stdout.should_receive(:puts).once.with('CC simple.o') $stdout.should_receive(:puts).once.with('CC simple.o')
$stdout.should_receive(:puts).once.with('LD simple') $stdout.should_receive(:puts).once.with('LD simple')
setup_testdir(['simple.c']) do setup_testdir(['simple.c'])
env = Rscons::Environment.new(echo: :short) env = Rscons::Environment.new(echo: :short)
env.Program('simple', 'simple.c') env.Program('simple', 'simple.c')
File.exist?('simple.o').should be_true File.exist?('simple.o').should be_true
`./simple`.should =~ /This is a simple C program/ `./simple`.should =~ /This is a simple C program/
end
end end
it 'does not rebuild the program if no sources changed' do it 'does not rebuild the program if no sources changed' do
$stdout.should_receive(:puts).once.with("gcc -c -o simple.o -MMD -MF simple.mf simple.c") $stdout.should_receive(:puts).once.with("gcc -c -o simple.o -MMD -MF simple.mf simple.c")
$stdout.should_receive(:puts).once.with('gcc -o simple simple.o') $stdout.should_receive(:puts).once.with('gcc -o simple simple.o')
setup_testdir(['simple.c']) do setup_testdir(['simple.c'])
env = Rscons::Environment.new env = Rscons::Environment.new
env.Program('simple', 'simple.c') env.Program('simple', 'simple.c')
env.Program('simple', 'simple.c') env.Program('simple', 'simple.c')
end
end end
it 'rebuilds the application when the source file changes' do it 'rebuilds the application when the source file changes' do
setup_testdir(['simple.c']) do setup_testdir(['simple.c'])
env = Rscons::Environment.new env = Rscons::Environment.new
env.Program('simple', 'simple.c') env.Program('simple', 'simple.c')
`./simple`.should =~ /This is a simple C program/ `./simple`.should =~ /This is a simple C program/
c = File.read('simple.c') c = File.read('simple.c')
File.open('simple.c', 'w') do |fh| File.open('simple.c', 'w') do |fh|
fh.puts c.sub('simple', 'modified') fh.puts c.sub('simple', 'modified')
end
clear_cache
env.Program('simple', 'simple.c')
`./simple`.should =~ /This is a modified C program/
end end
clear_cache
env.Program('simple', 'simple.c')
`./simple`.should =~ /This is a modified C program/
end end
it 'builds a C program with a header file' do it 'builds a C program with a header file' do
setup_testdir(['header.c', 'header.h']) do setup_testdir(['header.c', 'header.h'])
env = Rscons::Environment.new env = Rscons::Environment.new
env.Program('header', 'header.c') env.Program('header', 'header.c')
File.exist?('header.o').should be_true File.exist?('header.o').should be_true
`./header`.should =~ /The value is 33/ `./header`.should =~ /The value is 33/
end
end end
it 'rebuilds a C source when a header it uses changes' do it 'rebuilds a C source when a header it uses changes' do
setup_testdir(['header.c', 'header.h']) do setup_testdir(['header.c', 'header.h'])
env = Rscons::Environment.new env = Rscons::Environment.new
env.Program('header', 'header.c') env.Program('header', 'header.c')
File.exist?('header.o').should be_true File.exist?('header.o').should be_true
`./header`.should =~ /The value is 33/ `./header`.should =~ /The value is 33/
h = File.read('header.h') h = File.read('header.h')
File.open('header.h', 'w') do |fh| File.open('header.h', 'w') do |fh|
fh.puts h.sub('33', '42') fh.puts h.sub('33', '42')
end
clear_cache
env.Program('header', 'header.c')
`./header`.should =~ /The value is 42/
end end
clear_cache
env.Program('header', 'header.c')
`./header`.should =~ /The value is 42/
end end
end end