-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathworking_repository.rb
More file actions
248 lines (203 loc) · 7.31 KB
/
working_repository.rb
File metadata and controls
248 lines (203 loc) · 7.31 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
RSpec.shared_examples "a git working repository" do
describe "cloning" do
it "creates a working copy of the repo" do
subject.clone(remote)
expect(subject.exist?).to be_truthy
end
describe "using the default branch" do
describe "and the remote HEAD is 'master'" do
it "checks out the default branch" do
subject.clone(remote)
expect(subject.head).to eq "157011a4eaa27f1202a9d94335ee4876b26d377e"
end
end
describe "and the remote HEAD is '0.9.x'" do
before do
File.open(File.join(remote, 'HEAD'), 'w') do |fh|
fh.write('ref: refs/heads/0.9.x')
end
end
after do
clear_remote_path
populate_remote_path
end
it "checks out the default branch" do
subject.clone(remote)
expect(subject.head).to eq "3084373e8d181cf2fea5b4ade2690ba22872bd67"
end
end
end
describe "using an explicit ref" do
it "can check out tags" do
subject.clone(remote, {:ref => '1.0.0'})
expect(subject.head).to eq "14cbb45ae3a5f764320b7e63f1a54a25a1ef6c9c"
end
it "can check out remote branches" do
subject.clone(remote, {:ref => 'origin/0.9.x'})
expect(subject.head).to eq "3084373e8d181cf2fea5b4ade2690ba22872bd67"
end
it "can check out commits" do
subject.clone(remote, {:ref => '14cbb45ae3a5f764320b7e63f1a54a25a1ef6c9c'})
expect(subject.head).to eq "14cbb45ae3a5f764320b7e63f1a54a25a1ef6c9c"
end
end
describe "with a reference repository" do
it "adds the reference repository to the alternates directory" do
subject.clone(remote, {:reference => remote})
alternates = subject.alternates.to_a
expect(alternates.size).to eq 1
expect(alternates[0]).to match_realpath File.join(remote, 'objects')
end
end
context "without a proxy" do
before(:each) do
allow(R10K::Git).to receive(:get_proxy_for_remote).with(remote).and_return(nil)
end
it 'does not change proxy ENV' do
expect(ENV).to_not receive(:[]=)
expect(ENV).to_not receive(:update)
subject.clone(remote)
end
end
context "with a proxy" do
before(:each) do
allow(R10K::Git).to receive(:get_proxy_for_remote).with(remote).and_return('http://proxy.example.com:3128')
end
it "manages proxy-related ENV vars" do
# Sets proxy settings.
['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy'].each do |var|
expect(ENV).to receive(:[]=).with(var, 'http://proxy.example.com:3128')
end
# Resets proxy settings when done.
expect(ENV).to receive(:update).with(hash_including('HTTPS_PROXY' => nil))
subject.clone(remote)
end
end
end
describe "updating the repo" do
let(:tag_090) { subject.git_dir + 'refs' + 'tags' + '0.9.0' }
let(:packed_refs) { subject.git_dir + 'packed-refs' }
before do
subject.clone(remote)
tag_090.delete if tag_090.exist?
packed_refs.delete if packed_refs.exist?
end
it "fetches objects from the remote" do
expect(subject.tags).to_not include('0.9.0')
subject.fetch
expect(subject.tags).to include('0.9.0')
end
context "without a proxy" do
before(:each) do
allow(R10K::Git).to receive(:get_proxy_for_remote).with(remote).and_return(nil)
end
it 'does not change proxy ENV' do
expect(ENV).to_not receive(:[]=)
expect(ENV).to_not receive(:update)
subject.fetch
end
end
context "with a proxy" do
before(:each) do
allow(R10K::Git).to receive(:get_proxy_for_remote).with(remote).and_return('http://proxy.example.com:3128')
end
it "manages proxy-related ENV vars" do
# Sets proxy settings.
['HTTPS_PROXY', 'https_proxy', 'HTTP_PROXY', 'http_proxy'].each do |var|
expect(ENV).to receive(:[]=).with(var, 'http://proxy.example.com:3128')
end
# Resets proxy settings when done.
expect(ENV).to receive(:update).with(hash_including('HTTPS_PROXY' => nil))
subject.fetch
end
end
end
describe "listing branches" do
before do
subject.clone(remote)
end
it "lists the local branches" do
expect(subject.branches).to eq(%w[master])
end
end
describe "listing the origin" do
it "is nil if the remote is not set" do
expect(subject.origin).to be_nil
end
it "is the remote URL when set" do
subject.clone(remote)
expect(subject.origin).to eq remote
end
end
describe "checking out ref" do
before(:each) do
subject.clone(remote)
File.open(File.join(subject.path, 'README.markdown'), 'a') { |f| f.write('local modifications!') }
end
context "with force = true" do
it "should revert changes in managed files" do
subject.checkout(subject.head, {:force => true})
expect(File.read(File.join(subject.path, 'README.markdown')).include?('local modifications!')).to eq false
end
end
context "with force = false" do
it "should not revert changes in managed files" do
subject.checkout(subject.head, {:force => false})
expect(File.read(File.join(subject.path, 'README.markdown')).include?('local modifications!')).to eq true
end
end
end
describe "checking if worktree is dirty" do
before do
subject.clone(remote)
end
context "with no local changes" do
it "reports worktree as not dirty" do
expect(subject.dirty?).to be false
end
end
context "with local changes" do
before(:each) do
File.open(File.join(subject.path, 'README.markdown'), 'a') { |f| f.write('local modifications!') }
File.open(File.join(subject.path, 'CHANGELOG'), 'a') { |f| f.write('local modifications to the changelog too') }
end
it "logs and reports worktree as dirty" do
expect(subject.logger).to receive(:debug).with(/found local modifications in.*README\.markdown/i)
expect(subject.logger).to receive(:debug).with(/found local modifications in.*CHANGELOG/i)
expect(subject.logger).to receive(:debug1).twice
expect(subject.dirty?).to be true
end
end
end
shared_examples "unequal tags" do
it "reports tags as unequal" do
expect(subject.logger).to receive(:debug).with(/found different tags in local and remote in/i)
expect(subject.updatedtags?).to be true
end
end
describe "checking if tags are different" do
let(:tag_090) { subject.git_dir + 'refs' + 'tags' + '0.9.0' }
let(:packed_refs) { subject.git_dir + 'packed-refs' }
before(:each) do
subject.clone(remote)
end
context "with equal tags local and remote" do
it "reports tags as equal" do
expect(subject.updatedtags?).to be false
end
end
context "with missing local tag" do
before do
tag_090.delete if tag_090.exist?
packed_refs.delete if packed_refs.exist?
end
it_behaves_like "unequal tags"
end
context "with additional local tag" do
before(:each) do
File.open(File.join(subject.git_dir, 'packed-refs'), 'a') { |f| f.write('157011a4eaa27f1202a9d94335ee4876b26d377e refs/tags/1.0.2') }
end
it_behaves_like "unequal tags"
end
end
end