Blob of spec/spec_helper.rb (raw blob data)

1 require "spec"
2 require "logger"
3 require "piston"
4 require "piston/repository"
5 require "piston/working_copy"
6
7 $logger = Logger.new($stderr)
8 #$logger.level = Logger::INFO unless $DEBUG
9
10 module Piston
11 module SpecHelpers
12 def logger
13 $logger
14 end
15
16 def tmppath(*paths)
17 parts = [File.dirname(__FILE__), "tmp"]
18 parts << (@root_time ||= Time.now.to_i.to_s)
19 parts += paths
20 parts << rand().to_s.split(".").last
21 parts.collect! {|path| path.to_s}
22 path = File.join(*parts)
23 attempts = 10
24 while File.exists?(path)
25 path.succ!
26 attempts -= 1
27 raise "Unable to find a good temp pathname: #{path.inspect}" if attempts.zero?
28 end
29
30 File.expand_path(path)
31 end
32
33 def cleanup
34 path = File.expand_path(File.join(File.dirname(__FILE__), "tmp"))
35 logger.debug {"Removing #{path.inspect}"}
36 FileUtils.rm_rf(path)
37 end
38 end
39 end
40
41 describe "A local repository", :shared => true do
42 include Piston::SpecHelpers
43
44 before do
45 logger.debug {"A local repository..."}
46 @repos_dir = self.tmppath(:repos)
47 @repos = Piston::Repository.new("file://#{@repos_dir}", :logger => self.logger)
48 @repos.create!
49 end
50
51 after do
52 @repos.destroy!
53 end
54 end
55
56 describe "A working copy against a local repository", :shared => true do
57 it_should_behave_like "A local repository"
58
59 before do
60 logger.debug {"A working copy against a local repository..."}
61 @wc_dir = self.tmppath(:wc)
62 @wc = Piston::WorkingCopy.new(@wc_dir, :logger => self.logger)
63 @wc.checkout(@repos.url)
64 end
65
66 after do
67 @wc.destroy!
68 end
69 end
70
71 describe "An upstream repository", :shared => true do
72 include Piston::SpecHelpers
73
74 before(:all) do
75 logger.debug {"An upstream repository..."}
76 @upstream = Piston::Repository.new("file://" + self.tmppath(:repos)).create!
77 @upstream.logger = self.logger
78
79 @upwc_dir = self.tmppath(:wc)
80 @upwc = Piston::WorkingCopy.new(@upwc_dir, :logger => self.logger)
81 @upwc.checkout(@upstream.url)
82 raise "Working copy #{@upwc.dir.inspect} was not checked out properly: directory doesn't exist" unless File.directory?(@upwc.dir)
83 end
84
85 after(:all) do
86 self.cleanup
87 end
88 end
89
90 # Revision 1
91 # A main.c
92 # A CHANGELOG
93 #
94 # Revision 2
95 # M main.c
96 # A LICENSE
97 #
98 # Revision 3
99 # M main.c
100 # M CHANGELOG
101 # A README
102 describe "An upstream repository with no copies/renames", :shared => true do
103 it_should_behave_like "An upstream repository"
104
105 before :all do
106 logger.debug {"An upstream repository with no copies/renames..."}
107 @upwc.create! "main.c", <<EOF
108 #include <stdio.h>
109
110 int main(int argc, char** argv) {
111 return 0;
112 }
113 EOF
114 @upwc.create! "CHANGELOG", <<EOF
115 * Initial revision
116 EOF
117 @upwc.commit # End revision 1
118
119 @upwc.edit! "main.c", <<EOF
120 #include <stdio.h>
121
122 /** Main program file */
123 int main(int argc, char** argv) {
124 return 0;
125 }
126 EOF
127 @upwc.create! "LICENSE", <<EOF
128 Public Domain
129 EOF
130 @upwc.commit # End revision 2
131
132 @upwc.edit! "main.c", <<EOF
133 #include <stdio.h>
134
135 /** Main program file */
136 int main(int argc, char** argv) {
137 return do_work(argc);
138 }
139
140 int do_work(int argc) {
141 return 2*argc;
142 }
143 EOF
144 @upwc.edit! "CHANGELOG", <<EOF
145 * New functionnality
146 * Initial revision
147 EOF
148 @upwc.create! "README", <<EOF
149 = A library to do X
150 EOF
151 @upwc.commit # End revision 3
152 end
153 end