-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_ls_spec.rb
319 lines (244 loc) · 9.63 KB
/
git_ls_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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# frozen_string_literal: true
RSpec.describe GitLS do
it 'has a version number' do
expect(described_class::VERSION).not_to be_nil
end
shared_examples 'git ls-files' do
context 'with basic files' do
before do
create_file_list 'foo/bar', 'foo/foo', 'bar/foo', 'bar/bar', 'baz'
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'baz', 'foo/bar', 'foo/foo']))
end
it 'can be given the file path exactly' do
expect(described_class.files(Dir.pwd)).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'baz', 'foo/bar', 'foo/foo']))
end
context 'with sparse checkout' do
before do
`git commit -m COMMIT`
`git sparse-checkout init`
end
it 'matches git-ls output' do
expect(File.exist?('bar/bar')).to be false
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'baz', 'foo/bar', 'foo/foo']))
end
end
end
context 'with intent to add file' do
before do
create_file 'x', path: 'foo', git_add: false
`git add -N foo`
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo']))
end
end
context 'with file with newline in name' do
before do
create_file path: "foo\nbar"
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(["foo\nbar"]))
end
end
context 'with file with space in name' do
before do
create_file path: 'foo bar'
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo bar']))
end
end
context 'with a non-ascii file name' do
before do
create_file path: '💖'
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git -c core.quotePath=off ls-files -z`.split("\0"))
.and(eq(['💖']))
end
end
context 'with file with long name' do
before do
create_file_list 'foo/bar', "foo/#{'bar' * 60}", 'foo/baz'
end
it 'matches git-ls output' do
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo/bar', "foo/#{'bar' * 60}", 'foo/baz']))
end
end
end
describe '.files' do
context 'with no git' do
around do |example|
within_temp_dir(git_init: false) { example.run }
end
it 'raises an error' do
expect { described_class.files }.to raise_error(described_class::Error)
end
it 'raises an error when .git/index file is empty' do
create_file '', path: '.git/index'
expect { described_class.files }.to raise_error(described_class::Error)
end
it 'raises an error when .git/index file is a non recognized version' do
create_file "DIRC\0\0\0\x5\0\0\0\0", path: '.git/index'
expect { described_class.files }.to raise_error(described_class::Error, "Unrecognized git index version '5'")
end
end
context 'with git' do
around do |example|
within_temp_dir { example.run }
end
describe 'index version 2' do
before { `git update-index --index-version=2` }
it_behaves_like 'git ls-files'
context 'with split index' do
before { `git update-index --split-index --index-version=2` }
it_behaves_like 'git ls-files'
end
end
describe 'index version 3' do
before { `git update-index --index-version=3` }
it_behaves_like 'git ls-files'
context 'with split index' do
before { `git update-index --split-index --index-version=3` }
it_behaves_like 'git ls-files'
end
end
describe 'index version 4' do
before { `git update-index --index-version=4` }
it_behaves_like 'git ls-files'
context 'with split index' do
before { `git update-index --split-index --index-version=4` }
it_behaves_like 'git ls-files'
end
end
describe 'split index' do
before do
`git config core.splitIndex true`
`git update-index --split-index`
end
it_behaves_like 'git ls-files'
context 'with a deleted file in the middle of a list' do
it 'matches git-ls output' do
create_file_list 'foo/bar', 'foo/foo', 'bar/foo', 'bar/bar', 'baz'
`git update-index --split-index`
`rm ./baz`
`git rm baz`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'foo/bar', 'foo/foo']))
end
end
describe 'compression' do
[63, 64, 65, 90, 127, 128, 129, 300, 600].each do |n|
context "with #{n} files deleted" do
it 'matches git-ls output' do
filenames = Array.new(n).map.with_index { |_, i| "dir/#{i}" }
create_file_list(*filenames, 'foo')
`git update-index --split-index`
`rm -rf ./dir/*`
`git rm dir/*`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo']))
end
end
context "with even of #{n} files deleted" do
it 'matches git-ls output' do
filenames = Array.new(n).map.with_index { |_, i| "dir/#{i}" }
create_file_list(*filenames)
`git update-index --split-index`
`rm -rf ./dir/*2 ./dir/*4 ./dir/*6 ./dir/*8 ./dir/*0`
`git rm dir/*2 dir/*4 dir/*6 dir/*8 dir/*0`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(match_array(filenames.reject { |i| i.sub(%r{^dir/}, '').to_i.even? }))
end
end
context "with #{n} files moved" do
it 'matches git-ls output' do
filenames = Array.new(n).map.with_index { |_, i| "dir/#{i}" }
create_file_list(*filenames)
`git commit --no-verify -m COMMIT`
`git update-index --split-index`
`git mv dir dir2`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(match_array(filenames.map { |f| f.sub('dir', 'dir2') }))
end
end
context "with #{n} files updated" do
it 'matches git-ls output' do
filenames = Array.new(n).map.with_index { |_, i| "dir/#{i}" }
create_file_list(*filenames)
`git update-index --split-index`
sleep 0.1
`touch dir/*`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(match_array(filenames))
end
end
context "with #{n} files in place" do
it 'matches git-ls output' do
filenames = Array.new(n).map.with_index { |_, i| "dir/#{i}" }
create_file_list(*filenames)
`git update-index --split-index`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(match_array(filenames))
end
end
end
end
context 'with a replaced file' do
it 'matches git-ls output' do
create_file_list 'foo/bar', 'foo/foo', 'bar/foo', 'bar/bar', 'baz'
create_file 'CONTENT', path: 'bat/zero'
`git update-index --split-index`
# `git commit --no-verify -m COMMIT`
`git mv bat/zero bat/one`
# `git commit --no-verify -m COMMIT`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'bat/one', 'baz', 'foo/bar', 'foo/foo']))
end
end
context 'with an added file' do
it 'matches git-ls output' do
create_file_list 'foo/bar', 'foo/foo', 'bar/foo', 'bar/bar', 'baz'
`git update-index --split-index`
create_file path: 'bat/zero'
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['bar/bar', 'bar/foo', 'bat/zero', 'baz', 'foo/bar', 'foo/foo']))
end
end
context 'with an invalid unignorable extension' do
it 'raises an error' do
create_file_list 'foo/bar'
File.write('.git/index', File.read('.git/index').gsub!('link', 'beep'))
expect { described_class.files }.to raise_error(GitLS::Error)
end
end
context 'with an ignorable extension' do
it "doesn't raises an error" do
create_file_list 'foo/bar'
`git update-index --force-untracked-cache`
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo/bar']))
end
end
context 'with an unknown but ignorable extension' do
it "doesn't raises an error" do
create_file_list 'foo/bar'
`git update-index --force-untracked-cache`
File.write('.git/index', File.read('.git/index').gsub!('UNTR', 'BEEP'))
expect(described_class.files).to eq(`git ls-files -z`.split("\0"))
.and(eq(['foo/bar']))
end
end
end
end
end
end