forked from integrity/integrity
/
builder_spec.rb
183 lines (156 loc) · 6.83 KB
/
builder_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require File.dirname(__FILE__) + '/spec_helper'
describe Integrity::Builder do
def mock_project(messages={})
@project ||= begin
uri = Addressable::URI.parse("git://github.com/foca/integrity.git")
messages = { :uri => uri, :command => "rake", :branch => "master", :name => "Integrity" }.merge(messages)
mock("project", messages)
end
end
def mock_build(messages={})
@build ||= begin
messages = {
:project => mock_project,
:commit_metadata= => {},
:commit_identifier= => nil,
:output= => nil,
:output => "",
:error => "",
:successful= => true,
:save => true
}.merge(messages)
mock("build", messages)
end
end
def mock_scm(messages={})
@scm ||= mock("scm", {:with_revision => true}.merge(messages)).tap do |scm|
scm.stub!(:commit_identifier).with('6eba34d94b74fe68b96e35450fadf241113e44fc').and_return('6eba34d94b74fe68b96e35450fadf241113e44fc')
scm.stub!(:commit_metadata).with('6eba34d94b74fe68b96e35450fadf241113e44fc').and_return(
:author => 'Simon Rozet <simon@rozet.name>',
:message => 'A commit message',
:date => Time.parse('Mon Jul 21 15:24:34 2008 +0200')
)
scm.stub!(:working_directory).and_return('/var/integrity/exports/foca-integrity')
scm.stub!(:name).and_return("Integrity::SCM::Git")
end
end
before do
Integrity.stub!(:config).and_return(:export_directory => "/var/integrity/exports")
Integrity::Builder.class_eval { public :export_directory, :run_build_script, :scm_name }
Integrity::SCM.stub!(:working_tree_path).and_return("foca-integrity")
end
describe 'When initializing' do
it 'should instantiate a new Build model' do
Integrity::SCM.stub!(:new).and_return(mock_scm)
Integrity::Build.should_receive(:new).and_return(@build)
Integrity::Builder.new(mock_project)
end
it "should creates a new SCM object using the given URI's and given options and pass it the build" do
Integrity::Build.stub!(:new).and_return(mock_build)
Integrity::SCM.should_receive(:new).with(mock_project.uri, "master", "/var/integrity/exports/foca-integrity-master")
Integrity::Builder.new(mock_project)
end
end
before(:each) do
Integrity::SCM.stub!(:new).and_return(mock_scm)
Integrity::Build.stub!(:new).and_return(mock_build)
@builder = Integrity::Builder.new(mock_project)
end
specify "#scm_name should give the name of the SCM being used" do
@builder.scm_name.should == "Git"
end
describe "Calculating the export directory" do
it "should start with the base export directory set in the global options" do
@builder.export_directory.should =~ %r(^/var/integrity/exports)
end
it "should use the path to the repo in this directory, changing slashes for hyphens" do
@builder.export_directory.should =~ %r(foca-integrity-master$)
end
end
describe "When building a specific commit" do
before { @builder.stub!(:run_build_script) }
it "should log the build" do
Integrity.logger.should_receive(:info).
with("Building 6eba34d94b74fe68b96e35450fadf241113e44fc (master) of Integrity in /var/integrity/exports/foca-integrity-master using Git")
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
end
it "should fetch the latest code from the scm and run the build script" do
mock_scm.should_receive(:with_revision).
with('6eba34d94b74fe68b96e35450fadf241113e44fc').and_yield do
@builder.should_receive(:run_build_script)
end
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
end
it "should assign the head (minus the identifier) of the SCM as the commit metadata in the build" do
metadata = mock_scm.commit_metadata('6eba34d94b74fe68b96e35450fadf241113e44fc')
mock_build.should_receive(:commit_metadata=).with(metadata)
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
end
it "should assign the commit identifier of the SCM's head as the commit identifier in the build" do
mock_build.should_receive(:commit_identifier=).with '6eba34d94b74fe68b96e35450fadf241113e44fc'
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
end
it "should save the build to the database" do
mock_build.should_receive(:save)
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
end
describe "when there's an error" do
before { mock_scm.stub!(:with_revision).and_raise(RuntimeError) }
it "should still save the build" do
lambda {
mock_build.should_receive(:save)
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
}.should raise_error(RuntimeError)
end
it "should still save in what commit this happened" do
lambda {
mock_build.should_receive(:commit_identifier=).with('6eba34d94b74fe68b96e35450fadf241113e44fc')
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
}.should raise_error(RuntimeError)
end
it "should still save the commit metadata" do
lambda {
metadata = mock_scm.commit_metadata('6eba34d94b74fe68b96e35450fadf241113e44fc')
mock_build.should_receive(:commit_metadata=).with(metadata)
@builder.build('6eba34d94b74fe68b96e35450fadf241113e44fc')
}.should raise_error(RuntimeError)
end
end
end
describe "When running the command" do
before do
@pipe = mock("pipe", :read => "output and errors")
IO.stub!(:popen).and_yield(@pipe)
$?.stub!(:success?).and_return(true)
allow_message_expectations_on_nil
end
it "should run the build_script" do
IO.should_receive(:popen).with("(cd /var/integrity/exports/foca-integrity && rake) 2>&1", "r")
@builder.run_build_script
end
it "should write stdout to the build's output log" do
mock_build.should_receive(:output=).with("output and errors")
@builder.run_build_script
end
it "should set the build status to 'successful' if the command executes correctly" do
$?.should_receive(:success?).and_return(true)
mock_build.should_receive(:successful=).with(true)
@builder.run_build_script
end
it "should set the build status to 'failed' if the command fails" do
$?.should_receive(:success?).and_return(false)
mock_build.should_receive(:successful=).with(false)
@builder.run_build_script
end
end
describe "When deleting the code for a project" do
it "should remove the directory from disk" do
FileUtils.should_receive(:rm_r).with("/var/integrity/exports/foca-integrity-master")
@builder.delete_code
end
it "should not complain if the directory isn't there (e.g, a project with no builds)" do
FileUtils.stub!(:rm_r).and_raise(Errno::ENOENT)
lambda { @builder.delete_code }.should_not raise_error
end
end
end