File: cache_spec.rb

package info (click to toggle)
ruby-html-proofer 3.19.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,040 kB
  • sloc: ruby: 3,203; sh: 9; makefile: 4; javascript: 1; php: 1
file content (329 lines) | stat: -rw-r--r-- 12,486 bytes parent folder | download | duplicates (2)
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
320
321
322
323
324
325
326
327
328
329
# frozen_string_literal: true

require 'spec_helper'

describe 'Cache test' do
  let(:storage_dir) { File.join(FIXTURES_DIR, '/cache') }
  let(:cache_file) { File.join(storage_dir, cache_file_name) }
  let(:cache_file_name) { HTMLProofer::Cache::DEFAULT_CACHE_FILE_NAME }

  let(:default_cache_options) { { storage_dir: storage_dir } }

  let(:logger) { HTMLProofer::Log.new(:debug) }

  def read_cache(cache_file)
    JSON.parse File.read(cache_file)
  end

  context 'with time parser' do
    it 'understands months' do
      now_time = Time.local(2019, 9, 6, 12, 0, 0)
      Timecop.freeze(now_time)

      cache = HTMLProofer::Cache.new(logger, timeframe: '2M')

      check_time = Time.local(2019, 8, 6, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be true

      check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be false

      Timecop.return
    end

    it 'understands days' do
      now_time = Time.local(2019, 9, 6, 12, 0, 0)
      Timecop.freeze(now_time)

      cache = HTMLProofer::Cache.new(logger, timeframe: '2d')

      check_time = Time.local(2019, 9, 5, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be true

      check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be false

      Timecop.return
    end

    it 'understands weeks' do
      now_time = Time.local(2019, 9, 6, 12, 0, 0)
      Timecop.freeze(now_time)

      cache = HTMLProofer::Cache.new(logger, timeframe: '2w')

      check_time = Time.local(2019, 8, 30, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be true

      check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be false

      Timecop.return
    end

    it 'understands hours' do
      now_time = Time.local(2019, 9, 6, 12, 0, 0)
      Timecop.freeze(now_time)

      cache = HTMLProofer::Cache.new(logger, timeframe: '3h')

      check_time = Time.local(2019, 9, 6, 9, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be true

      check_time = Time.local(2019, 5, 6, 12, 0, 0).to_s

      expect(cache.within_timeframe?(check_time)).to be false

      Timecop.return
    end
  end

  context 'with .htmlproofer.log' do
    let(:cache_file_name) { '.htmlproofer.log' }

    it 'knows how to write to cache' do
      missing_link_href = "#{FIXTURES_DIR}/links/missing_link_href.html"
      expect_any_instance_of(HTMLProofer::Cache).to receive(:write).twice # once for internal, once for external
      run_proofer(missing_link_href, :file, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      log = read_cache(cache_file)
      expect(log.keys.length).to eq(2)
      statuses = log.values.map { |h| h['status'] }
      expect(statuses.count(200)).to eq(1)
      expect(statuses.count(0)).to eq(1)
    end
  end

  it 'fails on an invalid date' do
    file = "#{FIXTURES_DIR}/links/broken_link_external.html"
    expect do
      run_proofer(file, :file, cache: { timeframe: '30x' }.merge(default_cache_options))
    end.to raise_error ArgumentError
  end

  context 'within date' do
    let(:cache_file_name) { '.within_date.log' }
    it 'does not write file if timestamp is within date' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 27, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)

      # we expect no add since we are within the timeframe
      expect_any_instance_of(HTMLProofer::Cache).to_not receive(:add)

      run_proofer(['www.github.com'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  context 'not within date' do
    let(:cache_file_name) { '.within_date.log' }
    it 'does write file if timestamp is not within date' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 27, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)

      # we expect an add since we are mocking outside the timeframe
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('https://www.github.com', nil, 200)

      run_proofer(['https://www.github.com'], :links, cache: { timeframe: '4d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  context 'not within date for internal url' do
    let(:cache_file_name) { '.not_within_date_internal.log' }
    it 'does write file if timestamp is not within date' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 27, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
      root_link = "#{FIXTURES_DIR}/links/root_link/root_link.html"

      # we expect an add since we are mocking outside the timeframe
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('/', ['spec/html-proofer/fixtures/links/root_link/root_link.html'], 200, '')

      run_proofer(root_link, :file, disable_external: true, cache: { timeframe: '4d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  context 'new external url added' do
    let(:cache_file_name) { '.new_external_url.log' }
    it 'does write file if a new URL is added' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 20, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
      # we expect one new link to be added, but github.com can stay...
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('www.google.com', nil, 200)

      # ...because it's within the 30d time frame
      run_proofer(['www.github.com', 'www.google.com'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  context 'new internal url added' do
    let(:cache_file_name) { '.new_internal_url.log' }
    it 'does write file if a new relative URL 200 is added' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 20, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
      root_link = "#{FIXTURES_DIR}/links/root_link/root_link.html"
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('/', ['spec/html-proofer/fixtures/links/root_link/root_link.html'], 200, '')

      # we expect one new link to be added because it's within the 30d time frame
      run_proofer(root_link, :file, disable_external: true, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end

    it 'does write file if a new relative URL 404 is added' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 20, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
      root_link = "#{FIXTURES_DIR}/links/broken_internal_link.html"
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('#noHash', ['spec/html-proofer/fixtures/links/broken_internal_link.html'], 404, '')

      # we expect one new link to be added because it's within the 30d time frame
      run_proofer(root_link, :file, disable_external: true, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end

    it 'does writes file once if a new relative URL 404 hash is detected multiple times' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 20, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)
      root_link = "#{FIXTURES_DIR}/links/broken_internal_hashes"
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add).with('file.html#noHash', ['spec/html-proofer/fixtures/links/broken_internal_hashes/file1.html', 'spec/html-proofer/fixtures/links/broken_internal_hashes/file2.html', 'spec/html-proofer/fixtures/links/broken_internal_hashes/file3.html'], 404, '').once

      # we expect one new link to be added because it's within the 30d time frame
      run_proofer(root_link, :directory, disable_external: true, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  context 'recheck failure' do
    let(:cache_file_name) { '.recheck_failure.log' }
    it 'does recheck failures, regardless of cache' do
      # this is frozen to within 7 days of the log
      new_time = Time.local(2015, 10, 27, 12, 0, 0)
      Timecop.freeze(new_time)

      expect_any_instance_of(HTMLProofer::Cache).to receive(:write)

      # we expect the same link to be re-added, even though we are within the time frame,
      # because `foofoofoo.biz` was a failure
      expect_any_instance_of(HTMLProofer::Cache).to receive(:add)

      run_proofer(['http://www.foofoofoo.biz'], :links, cache: { timeframe: '30d', cache_file: cache_file_name }.merge(default_cache_options))

      Timecop.return
    end
  end

  it 'does recheck failures, regardless of external-only cache' do
    cache_file_name = '.external.log'
    cache_location = File.join(storage_dir, cache_file_name)

    file = "#{FIXTURES_DIR}/cache/external_example.html"

    new_time = Time.local(2021, 0o1, 27, 12, 0, 0)
    Timecop.freeze(new_time)

    File.delete(cache_location) if File.exist?(cache_location)

    run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))

    expect(cache.keys.count).to eq(1)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

    run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))
    expect(cache.keys.count).to eq(1)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

    Timecop.return
  end

  it 'does recheck failures, regardless of external and internal cache' do
    cache_file_name = '.internal_and_external.log'
    cache_location = File.join(storage_dir, cache_file_name)

    file = "#{FIXTURES_DIR}/cache/internal_and_external_example.html"

    new_time = Time.local(2021, 0o1, 27, 12, 0, 0)
    Timecop.freeze(new_time)

    File.delete(cache_location) if File.exist?(cache_location)

    run_proofer(file, :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))

    expect(cache.keys.count).to eq(1)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

    run_proofer(file, :file, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))
    expect(cache.keys.count).to eq(2)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

    Timecop.return
  end

  it 'removes links that do not exist' do
    cache_file_name = '.removed_link.log'
    cache_location = File.join(storage_dir, cache_file_name)

    new_time = Time.local(2021, 0o1, 27, 12, 0, 0)
    Timecop.freeze(new_time)

    File.delete(cache_location) if File.exist?(cache_location)

    run_proofer("#{FIXTURES_DIR}/cache/external_example.html", :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))

    expect(cache.keys.count).to eq(1)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian/html-proofer')

    run_proofer("#{FIXTURES_DIR}/cache/some_link.html", :file, external_only: true, links_only: true, cache: { timeframe: '1d', cache_file: cache_file_name }.merge(default_cache_options))

    cache = JSON.parse(File.read(cache_location))
    expect(cache.keys.count).to eq(1)
    expect(cache.keys[0]).to eq('https://github.com/gjtorikian')

    Timecop.return
  end
end