/
checksummer_spec.rb
258 lines (204 loc) · 7.12 KB
/
checksummer_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
# encoding: utf-8
require 'tempfile'
describe Nanoc::Checksummer do
subject { Nanoc::Checksummer }
CHECKSUM_REGEX = /\A[0-9a-zA-Z\/+]+=*\Z/
describe 'for String' do
it 'should checksum strings' do
subject.calc('foo').must_equal('+5k/BWvkYc6T1qhGaSyf3861CyE=')
end
end
describe 'for Array' do
it 'should checksum arrays' do
subject.calc([1, 'a', :a]).must_equal 'YtWOEFUAMQritkY38KXHFZM/n2E='
end
it 'should take order into account when checksumming arrays' do
subject.calc([:a, 'a', 1]).wont_equal(subject.calc([1, 'a', :a]))
end
it 'should checksum non-serializable arrays' do
subject.calc([-> {}]).must_match(CHECKSUM_REGEX)
end
it 'should checksum recursive arrays' do
array = [:a]
array << array
subject.calc(array).must_equal('mR3c98xA5ecazxx+M3h0Ss4/J20=')
end
end
describe 'for Hash' do
it 'should checksum hashes' do
subject.calc({ a: 1, b: 2 }).must_equal 'qY8fW6gWK7F1XQ9MLrx3Gru/RTY='
end
it 'should take order into account when checksumming hashes' do
subject.calc({ a: 1, b: 2 }).wont_equal(subject.calc({ b: 2, a: 1 }))
end
it 'should checksum non-serializable hashes' do
subject.calc({ a: -> {} }).must_match(CHECKSUM_REGEX)
end
it 'should checksum recursive hash keys' do
hash = {}
hash[hash] = 123
subject.calc(hash).must_equal('mKucWMhRtR/FHWNqR/EErF4qgTk=')
end
it 'should checksum recursive hash values' do
hash = {}
hash[123] = hash
subject.calc(hash).must_equal('PBiDX0nWnV+DAYB+w+xM0Kf21ZM=')
end
end
describe 'for Pathname' do
let(:file) { Tempfile.new('foo') }
let(:filename) { file.path }
let(:pathname) { Pathname.new(filename) }
let(:atime) { 1_234_567_890 }
let(:mtime) { 1_234_567_890 }
let(:data) { 'stuffs' }
let(:normal_checksum) { 'THy7Y28oroov/KvPxT6wcMnXr/s=' }
before do
file.write(data)
file.close
File.utime(atime, mtime, filename)
end
after do
file.unlink
end
describe 'does not exist' do
let(:non_existing_filename) { 'askldjfklaslasdfkjsajdf' }
let(:pathname) { Pathname.new(filename) }
it 'should still checksum' do
subject.calc(pathname).must_equal(normal_checksum)
end
end
it 'should get the mtime right' do
stat = File.stat(filename)
stat.mtime.to_i.must_equal(mtime)
end
it 'should get the file size right' do
stat = File.stat(filename)
stat.size.must_equal(6)
end
it 'should checksum binary content' do
subject.calc(pathname).must_equal(normal_checksum)
end
describe 'if the mtime changes' do
let(:mtime) { 1_333_333_333 }
it 'should have a different checksum' do
subject.calc(pathname).must_match(CHECKSUM_REGEX)
subject.calc(pathname).wont_equal(normal_checksum)
end
end
describe 'if the content changes, but not the file size' do
let(:data) { 'STUFF!' }
it 'should have the same checksum' do
subject.calc(pathname).must_equal(normal_checksum)
end
end
describe 'if the file size changes' do
let(:data) { 'stuff and stuff and stuff!!!' }
it 'should have a different checksum' do
subject.calc(pathname).must_match(CHECKSUM_REGEX)
subject.calc(pathname).wont_equal(normal_checksum)
end
end
end
it 'should not have the same checksum for same content but different class'
describe 'for Nanoc::RulesCollection' do
let(:data) { 'STUFF!' }
let(:normal_checksum) { 'r4SwDpCp5saBPeZk2gQOJcipTZU=' }
let(:rules_collection) do
coll = Nanoc::RulesCollection.new(nil)
coll.data = data
coll
end
it 'should calculate' do
subject.calc(rules_collection).must_equal(normal_checksum)
end
describe 'if the content changes' do
let(:data) { 'Other stuff!' }
it 'should have a different checksum' do
subject.calc(rules_collection).must_match(CHECKSUM_REGEX)
subject.calc(rules_collection).wont_equal(normal_checksum)
end
end
end
describe 'for Nanoc::CodeSnippet' do
let(:data) { 'asdf' }
let(:filename) { File.expand_path('bob.txt') }
let(:code_snippet) { Nanoc::CodeSnippet.new(data, filename) }
let(:normal_checksum) { 'ZSo56CFoBcNgiDsWOfLLquH2sF0=' }
it 'should checksum the data' do
subject.calc(code_snippet).must_equal(normal_checksum)
end
describe 'if the filename changes' do
let(:filename) { File.expand_path('george.txt') }
it 'should have the same checksum' do
subject.calc(code_snippet).must_equal(normal_checksum)
end
end
describe 'if the content changes' do
let(:data) { 'Other stuff!' }
it 'should have a different checksum' do
subject.calc(code_snippet).must_match(CHECKSUM_REGEX)
subject.calc(code_snippet).wont_equal(normal_checksum)
end
end
end
describe 'for Nanoc::Configuration' do
let(:wrapped) { { a: 1, b: 2 } }
let(:configuration) { Nanoc::Configuration.new(wrapped) }
let(:normal_checksum) { 'eYYQ74x29njbtXMtuKZX/ogD8JA=' }
it 'should checksum the hash' do
subject.calc(configuration).must_equal('eYYQ74x29njbtXMtuKZX/ogD8JA=')
end
describe 'if the content changes' do
let(:wrapped) { { a: 666, b: 2 } }
it 'should have a different checksum' do
subject.calc(configuration).must_match(CHECKSUM_REGEX)
subject.calc(configuration).wont_equal(normal_checksum)
end
end
end
describe 'for Nanoc::Item' do
let(:content) { 'asdf' }
let(:filename) { File.expand_path('bob.txt') }
let(:attributes) { { a: 1, b: 2 } }
let(:identifier) { '/foo/' }
let(:item) { Nanoc::Item.new(content, attributes, identifier) }
let(:normal_checksum) { 'eTPdmaG7UAuPzH210HM1JvJaWDo=' }
it 'should checksum item' do
subject.calc(item).must_equal(normal_checksum)
end
describe 'with recursive attributes' do
it 'should checksum' do
item.attributes[:a] = item
subject.calc(item).must_match(CHECKSUM_REGEX)
subject.calc(item).wont_equal(normal_checksum)
end
end
describe 'with changed attributes' do
let(:attributes) { { x: 4, y: 5 } }
it 'should have a different checksum' do
subject.calc(item).must_match(CHECKSUM_REGEX)
subject.calc(item).wont_equal(normal_checksum)
end
end
describe 'with changed content' do
let(:content) { 'something drastically different' }
it 'should have a different checksum' do
subject.calc(item).must_match(CHECKSUM_REGEX)
subject.calc(item).wont_equal(normal_checksum)
end
end
end
describe 'for other marshal-able classes' do
let(:obj) { :foobar }
it 'should checksum' do
subject.calc(obj).must_match(CHECKSUM_REGEX)
end
end
describe 'for other non-marshal-able classes' do
let(:obj) { proc {} }
it 'should checksum' do
subject.calc(obj).must_match(CHECKSUM_REGEX)
end
end
end