$:.unshift File.join(File.dirname(__FILE__), '..', '..', '..', 'lib') require 'buildmaster/cotta' require 'pathname' def register_system_file_specifications specify 'Log all the shell commands' do @system.shell('my command') @system.executed_commands.length.should_equal(1) @system.executed_commands[0].should_equal 'my command' end specify 'root directory always exists' do @system.dir_exists?(Pathname.new('/')).should_equal true end specify 'current directory always exists' do @system.dir_exists?(Pathname.new('.')).should_equal true end specify 'mkdir should create directory' do pathname = Pathname.new('/one') @system.dir_exists?(pathname).should_equal false @system.mkdir(pathname) @system.dir_exists?(pathname).should_equal true end specify 'mkdir raise error if dir already exists' do pathname = Pathname.new('/one') @system.mkdir(pathname) lambda {@system.mkdir}.should_raise StandardError end specify 'io returns IO handle' do pathname = Pathname.new('file.txt') @system.file_exists?(pathname).should_equal false write_io = load_io(pathname, 'w') write_io.puts 'content' write_io.close @system.file_exists?(pathname).should_equal true read_io = load_io(pathname, 'r') read_io.gets.should_equal "content\n" read_io.close @system.delete_file(pathname) end specify 'file creation should leave file system consistent' do pathname = Pathname.new('dir/sub/file.txt') @system.mkdir(pathname.parent.parent) @system.mkdir(pathname.parent) @system.file_exists?(pathname).should_equal false @system.dir_exists?(pathname.parent).should_equal true load_io(pathname, 'w').close @system.file_exists?(pathname).should_equal true @system.dir_exists?(pathname).should_equal false @system.dir_exists?(pathname.parent).should_equal true children = @system.list(pathname.parent) children.size.should_equal 1 children[0].should_equal pathname.basename.to_s end specify 'directory creation should leave file system consistent' do pathname = Pathname.new('root/dir/sub') @system.dir_exists?(pathname).should_equal false @system.file_exists?(pathname).should_equal false @system.dir_exists?(pathname.parent).should_equal false @system.mkdir(pathname.parent.parent) @system.mkdir(pathname.parent) @system.mkdir(pathname) @system.dir_exists?(pathname).should_equal true @system.file_exists?(pathname).should_equal false @system.dir_exists?(pathname.parent).should_equal true list = @system.list(pathname.parent) list.size.should_equal 1 list[0].should_equal(pathname.basename.to_s) end specify 'read io should raise error if file does not exists' do pathname = Pathname.new('dir/file.txt') Proc.new {@system.io(pathname, 'r')}.should_raise Errno::ENOENT end specify 'delete dir' do pathname = Pathname.new('dir') @system.mkdir(pathname) @system.delete_dir(pathname) @system.dir_exists?(pathname).should_equal false end specify 'deleting dir that does not exist should raise error' do pathname = Pathname.new('dir/dir2') Proc.new {@system.delete_dir(pathname)}.should_raise Errno::ENOENT end specify 'copy file' do pathname = Pathname.new('file1') write_io = load_io(pathname, 'w') write_io.puts 'line' write_io.close target = Pathname.new('target') @system.copy(pathname, target) @system.file_exists?(target).should_equal true read_io = load_io(target, 'r') read_io.gets.should_equal "line\n" read_io.close end specify 'move file' do pathname = Pathname.new('file1') write_io = load_io(pathname, 'w') write_io.puts 'line' write_io.close target = Pathname.new('target') @system.move(pathname, target) @system.file_exists?(target).should_equal true read_io = load_io(target, 'r') read_io.gets.should_equal "line\n" read_io.close @system.file_exists?(pathname).should_equal false end end def load_io(*args) io = @system.io(*args) if (io) @ios.push(io) else raise "IO is null for: #{args}" end return io end