require "spec" require "logger" require "piston" require "piston/repository" require "piston/working_copy" $logger = Logger.new($stderr) #$logger.level = Logger::INFO unless $DEBUG module Piston module SpecHelpers def logger $logger end def tmppath(*paths) parts = [File.dirname(__FILE__), "tmp"] parts << (@root_time ||= Time.now.to_i.to_s) parts += paths parts << rand().to_s.split(".").last parts.collect! {|path| path.to_s} path = File.join(*parts) attempts = 10 while File.exists?(path) path.succ! attempts -= 1 raise "Unable to find a good temp pathname: #{path.inspect}" if attempts.zero? end File.expand_path(path) end def cleanup path = File.expand_path(File.join(File.dirname(__FILE__), "tmp")) logger.debug {"Removing #{path.inspect}"} FileUtils.rm_rf(path) end end end describe "A local repository", :shared => true do include Piston::SpecHelpers before do logger.debug {"A local repository..."} @repos_dir = self.tmppath(:repos) @repos = Piston::Repository.new("file://#{@repos_dir}", :logger => self.logger) @repos.create! end after do @repos.destroy! end end describe "A working copy against a local repository", :shared => true do it_should_behave_like "A local repository" before do logger.debug {"A working copy against a local repository..."} @wc_dir = self.tmppath(:wc) @wc = Piston::WorkingCopy.new(@wc_dir, :logger => self.logger) @wc.checkout(@repos.url) end after do @wc.destroy! end end describe "An upstream repository", :shared => true do include Piston::SpecHelpers before(:all) do logger.debug {"An upstream repository..."} @upstream = Piston::Repository.new("file://" + self.tmppath(:repos)).create! @upstream.logger = self.logger @upwc_dir = self.tmppath(:wc) @upwc = Piston::WorkingCopy.new(@upwc_dir, :logger => self.logger) @upwc.checkout(@upstream.url) raise "Working copy #{@upwc.dir.inspect} was not checked out properly: directory doesn't exist" unless File.directory?(@upwc.dir) end after(:all) do self.cleanup end end # Revision 1 # A main.c # A CHANGELOG # # Revision 2 # M main.c # A LICENSE # # Revision 3 # M main.c # M CHANGELOG # A README describe "An upstream repository with no copies/renames", :shared => true do it_should_behave_like "An upstream repository" before :all do logger.debug {"An upstream repository with no copies/renames..."} @upwc.create! "main.c", < int main(int argc, char** argv) { return 0; } EOF @upwc.create! "CHANGELOG", < /** Main program file */ int main(int argc, char** argv) { return 0; } EOF @upwc.create! "LICENSE", < /** Main program file */ int main(int argc, char** argv) { return do_work(argc); } int do_work(int argc) { return 2*argc; } EOF @upwc.edit! "CHANGELOG", <