forked from integrity/integrity
/
git_spec.rb
189 lines (158 loc) · 6.5 KB
/
git_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
184
185
186
187
188
189
require File.dirname(__FILE__) + '/../spec_helper'
describe Integrity::SCM::Git do
before do
Integrity::SCM::Git.class_eval { public :fetch_code, :chdir, :clone, :checkout, :pull, :commit_info, :cloned?, :on_branch? }
@git = Integrity::SCM::Git.new("git://github.com/foca/integrity.git", "master", "/var/integrity/exports/foca-integrity")
end
it "should point to the correct repository" do
@git.uri.should == "git://github.com/foca/integrity.git"
end
it "should track the correct branch" do
@git.branch.should == "master"
end
it "should point to the correct working directory" do
@git.working_directory.should == "/var/integrity/exports/foca-integrity"
end
describe "Determining the state of the code for this project" do
it "should know if a project has been cloned or not by looking at the directories" do
File.stub!(:directory?).with("/var/integrity/exports/foca-integrity/.git").and_return(true)
@git.should be_cloned
end
it "should know if a checkout is on the current branch by asking git the branch" do
@git.stub!(:chdir).and_yield
@git.should_receive(:`).with("git symbolic-ref HEAD").and_return("refs/heads/master\n")
@git.should be_on_branch
end
it "should tell you if it's not in the desired branch" do
@git.stub!(:chdir).and_yield
@git.should_receive(:`).with("git symbolic-ref HEAD").and_return("refs/heads/other_branch\n")
@git.should_not be_on_branch
end
end
describe "Fetching the code from the repo" do
before do
@git.stub!(:on_branch?).and_return(true)
@git.stub!(:cloned?).and_return(true)
@git.stub!(:pull)
end
it "should clone the repository if it hasn't already" do
@git.stub!(:cloned?).and_return(false)
@git.should_receive(:clone)
@git.fetch_code
end
it "should not clone the repository if it has done so before" do
@git.stub!(:cloned?).and_return(true)
@git.should_not_receive(:clone)
@git.fetch_code
end
it "should checkout the branch if it's not on it" do
@git.stub!(:on_branch?).and_return(false)
@git.should_receive(:checkout)
@git.fetch_code
end
it "should not checkout the branch if it's already on it" do
@git.stub!(:on_branch?).and_return(true)
@git.should_not_receive(:checkout)
@git.fetch_code
end
it "should pull the code" do
@git.should_receive(:pull)
@git.fetch_code
end
end
describe "Changing the current directory to that of the checkout" do
it "should let ruby-git handle it by forwarding the call" do
block = lambda { "cuack" }
Dir.should_receive(:chdir).with("/var/integrity/exports/foca-integrity", &block)
@git.chdir(&block)
end
end
describe 'Running code in a specific context using #with_revision' do
before(:each) do
@block = lambda { 'lambda the ultimate' }
@git.stub!(:fetch_code)
@git.stub!(:checkout)
@git.stub!(:chdir).with(&@block)
end
it "should fetch the latest code" do
@git.should_receive(:fetch_code)
@git.with_revision('HEAD', &@block)
end
it 'should checkout the given revision' do
@git.should_receive(:checkout).with('4d0cfafd569ef60d0c578bf8a9d51f9582612f03')
@git.with_revision('4d0cfafd569ef60d0c578bf8a9d51f9582612f03', &@block)
end
it "should run the block in the working copy directory" do
@git.should_receive(:chdir).with(&@block).and_yield
@git.with_revision('4d0cfafd569ef60d0c578bf8a9d51f9582612f03', &@block)
end
it 'should ensure it checkout origin/HEAD' do
@git.should_receive(:checkout).with('origin/HEAD')
@git.with_revision('4d0cfafd569ef60d0c578bf8a9d51f9582612f03', &@block)
end
end
describe "Getting information about a commit" do
before do
@serialized = ["---",
":identifier: 7e4f36231776ea4401b6e385df5f43c11633d59f",
":author: Nicolás Sanguinetti <contacto@nicolassanguinetti.info>",
":message: A beautiful commit"] * "\n"
@git.stub!(:chdir).and_yield
@git.stub!(:`).and_return @serialized
end
it "should switch to the project's directory" do
@git.should_receive(:chdir).and_yield
@git.commit_info("HEAD")
end
it "should ask git for the commit" do
@git.should_receive(:`).with(/^git show -s.*HEAD/).and_return(@serialized)
@git.commit_info("HEAD")
end
it "should ask YAML to interpret the serialized info" do
YAML.should_receive(:load).with(@serialized).and_return({})
@git.commit_info("HEAD")
end
it "should return a hash with all the relevant information" do
@git.commit_info("HEAD").should == {
:identifier => "7e4f36231776ea4401b6e385df5f43c11633d59f",
:author => "Nicolás Sanguinetti <contacto@nicolassanguinetti.info>",
:message => "A beautiful commit"
}
end
end
describe "Getting the HEAD of the repo" do
it "should pass down the request to #commit_info" do
@git.should_receive(:commit_info).with("HEAD")
@git.head
end
end
describe "Doing all the low-level operations on the repo" do
it "should pass the uri and expected working directory to git-clone when cloning" do
@git.should_receive(:system).with("git clone git://github.com/foca/integrity.git /var/integrity/exports/foca-integrity")
@git.clone
end
it "should change dirs to the repo's and checkout the appropiate branch via git-checkout" do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with("git checkout -b master origin/master")
@git.checkout
end
it "should check out a branch that has already been initialized locally without failing" do
pending "it only works the first time you change the branch, need to fix"
end
it "should switch dirs to the repo's and call git-pull when pulling" do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with("git pull")
@git.pull
end
it 'should checkout the given commit' do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with('git checkout 7e4f36231776ea4401b6e385df5f43c11633d59f')
@git.checkout('7e4f36231776ea4401b6e385df5f43c11633d59f')
end
it 'should checkout the given treeish' do
@git.should_receive(:chdir).and_yield
@git.should_receive(:system).with('git checkout origin/HEAD')
@git.checkout('origin/HEAD')
end
end
end