druid-tools 0.4.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +41 -0
  4. data/.rubocop_todo.yml +372 -0
  5. data/.travis.yml +12 -2
  6. data/README.md +36 -16
  7. data/Rakefile +4 -1
  8. data/VERSION +1 -1
  9. data/druid-tools.gemspec +4 -1
  10. data/lib/druid_tools/druid.rb +19 -6
  11. data/spec/druid_spec.rb +470 -0
  12. data/spec/spec_helper.rb +9 -0
  13. metadata +49 -36
  14. data/doc/classes/DruidTools.html +0 -132
  15. data/doc/classes/DruidTools/DifferentContentExistsError.html +0 -111
  16. data/doc/classes/DruidTools/Druid.html +0 -352
  17. data/doc/classes/DruidTools/Druid.src/M000001.html +0 -18
  18. data/doc/classes/DruidTools/Druid.src/M000002.html +0 -18
  19. data/doc/classes/DruidTools/Druid.src/M000003.html +0 -18
  20. data/doc/classes/DruidTools/Druid.src/M000004.html +0 -24
  21. data/doc/classes/DruidTools/Druid.src/M000005.html +0 -18
  22. data/doc/classes/DruidTools/Druid.src/M000006.html +0 -18
  23. data/doc/classes/DruidTools/Druid.src/M000007.html +0 -20
  24. data/doc/classes/DruidTools/Druid.src/M000008.html +0 -25
  25. data/doc/classes/DruidTools/Druid.src/M000009.html +0 -20
  26. data/doc/classes/DruidTools/Druid.src/M000010.html +0 -27
  27. data/doc/classes/DruidTools/Druid.src/M000011.html +0 -24
  28. data/doc/classes/DruidTools/Druid.src/M000012.html +0 -29
  29. data/doc/classes/DruidTools/InvalidDruidError.html +0 -111
  30. data/doc/classes/DruidTools/SameContentExistsError.html +0 -111
  31. data/doc/created.rid +0 -1
  32. data/doc/files/lib/druid-tools_rb.html +0 -108
  33. data/doc/files/lib/druid_tools/druid_rb.html +0 -108
  34. data/doc/files/lib/druid_tools/exceptions_rb.html +0 -101
  35. data/doc/files/lib/druid_tools/version_rb.html +0 -101
  36. data/doc/files/lib/druid_tools_rb.html +0 -110
  37. data/doc/files/spec/druid_tools_spec_rb.html +0 -115
  38. data/doc/files/spec/spec_helper_rb.html +0 -114
  39. data/doc/fr_class_index.html +0 -28
  40. data/doc/fr_file_index.html +0 -30
  41. data/doc/fr_method_index.html +0 -38
  42. data/doc/index.html +0 -24
  43. data/doc/rdoc-style.css +0 -208
  44. data/spec/druid_tools_spec.rb +0 -426
@@ -1,426 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
- #puts $LOAD_PATH.join("\n")
3
- require 'tmpdir'
4
-
5
- describe DruidTools::Druid do
6
- before (:all) do
7
- @fixture_dir = File.expand_path("../fixtures",__FILE__)
8
- FileUtils.rm_rf(File.join(@fixture_dir,'cd'))
9
- @druid_1 = 'druid:cd456ef7890'
10
- @tree_1 = File.join(@fixture_dir,'cd/456/ef/7890/cd456ef7890')
11
- @druid_2 = 'druid:cd456gh1234'
12
- @tree_2 = File.join(@fixture_dir,'cd/456/gh/1234/cd456gh1234')
13
- end
14
-
15
- after(:each) do
16
- FileUtils.rm_rf(File.join(@fixture_dir,'cd'))
17
- end
18
-
19
- it "validate druid strings using the valid? class method" do
20
- tests = [
21
- # Expected Input druid
22
- [true, 'druid:aa000bb0001'],
23
- [true, 'aa000bb0001'],
24
- [false, 'Aa000bb0001'],
25
- [false, "xxx\naa000bb0001"],
26
- [false, 'aaa000bb0001'],
27
- [false, 'druidX:aa000bb0001'],
28
- [false, ':aa000bb0001'],
29
- [true, 'aa123bb1234'],
30
- [false, 'aa12bb1234'],
31
- [false, 'aa1234bb1234'],
32
- [false, 'aa123bb123'],
33
- [false, 'aa123bb12345'],
34
- [false, 'a123bb1234'],
35
- [false, 'aaa123bb1234'],
36
- [false, 'aa123b1234'],
37
- [false, 'aa123bbb1234'],
38
- [false, 'druid:az918AZ9381'.upcase],
39
- [true, 'druid:az918AZ9381'.downcase],
40
- [true, 'druid:zz943vx1492']
41
- ]
42
- tests.each do |exp, dru|
43
- expect(DruidTools::Druid.valid?(dru)).to eq(exp)
44
- end
45
- end
46
-
47
- it "provides the full druid including the prefix" do
48
- expect(DruidTools::Druid.new('druid:cd456ef7890',@fixture_dir).druid).to eq('druid:cd456ef7890')
49
- expect(DruidTools::Druid.new('cd456ef7890',@fixture_dir).druid).to eq('druid:cd456ef7890')
50
- end
51
-
52
- it "extracts the ID from the stem" do
53
- expect(DruidTools::Druid.new('druid:cd456ef7890',@fixture_dir).id).to eq('cd456ef7890')
54
- expect(DruidTools::Druid.new('cd456ef7890',@fixture_dir).id).to eq('cd456ef7890')
55
- end
56
-
57
- it "raises an exception if the druid is invalid" do
58
- expect { DruidTools::Druid.new('nondruid:cd456ef7890',@fixture_dir) }.to raise_error(ArgumentError)
59
- expect { DruidTools::Druid.new('druid:cd4567ef890',@fixture_dir) }.to raise_error(ArgumentError)
60
- end
61
-
62
- it "builds a druid tree from a druid" do
63
- druid = DruidTools::Druid.new(@druid_1,@fixture_dir)
64
- expect(druid.tree).to eq(['cd','456','ef','7890','cd456ef7890'])
65
- expect(druid.path).to eq(@tree_1)
66
- end
67
-
68
- it "creates and destroys druid directories" do
69
- expect(File.exists?(@tree_1)).to eq false
70
- expect(File.exists?(@tree_2)).to eq false
71
-
72
- druid_1 = DruidTools::Druid.new(@druid_1,@fixture_dir)
73
- druid_2 = DruidTools::Druid.new(@druid_2,@fixture_dir)
74
-
75
- druid_1.mkdir
76
- expect(File.exists?(@tree_1)).to eq true
77
- expect(File.exists?(@tree_2)).to eq false
78
-
79
- druid_2.mkdir
80
- expect(File.exists?(@tree_1)).to eq true
81
- expect(File.exists?(@tree_2)).to eq true
82
-
83
- druid_2.rmdir
84
- expect(File.exists?(@tree_1)).to eq true
85
- expect(File.exists?(@tree_2)).to eq false
86
-
87
- druid_1.rmdir
88
- expect(File.exists?(@tree_1)).to eq false
89
- expect(File.exists?(@tree_2)).to eq false
90
- expect(File.exists?(File.join(@fixture_dir,'cd'))).to eq false
91
- end
92
-
93
- describe "alternate prefixes" do
94
- before :all do
95
- DruidTools::Druid.prefix = 'sulair'
96
- end
97
-
98
- after :all do
99
- DruidTools::Druid.prefix = 'druid'
100
- end
101
-
102
- it "handles alternate prefixes" do
103
- expect { DruidTools::Druid.new('druid:cd456ef7890',@fixture_dir) }.to raise_error(ArgumentError)
104
- expect(DruidTools::Druid.new('sulair:cd456ef7890',@fixture_dir).id).to eq('cd456ef7890')
105
- expect(DruidTools::Druid.new('cd456ef7890',@fixture_dir).druid).to eq('sulair:cd456ef7890')
106
- end
107
- end
108
-
109
- describe "content directories" do
110
- it "knows where its content goes" do
111
- druid = DruidTools::Druid.new(@druid_1,@fixture_dir)
112
- expect(druid.content_dir(false)).to eq(File.join(@tree_1,'content'))
113
- expect(druid.metadata_dir(false)).to eq(File.join(@tree_1,'metadata'))
114
- expect(druid.temp_dir(false)).to eq(File.join(@tree_1,'temp'))
115
-
116
- expect(File.exists?(File.join(@tree_1,'content'))).to eq false
117
- expect(File.exists?(File.join(@tree_1,'metadata'))).to eq false
118
- expect(File.exists?(File.join(@tree_1,'temp'))).to eq false
119
- end
120
-
121
- it "creates its content directories on the fly" do
122
- druid = DruidTools::Druid.new(@druid_1,@fixture_dir)
123
- expect(druid.content_dir).to eq(File.join(@tree_1,'content'))
124
- expect(druid.metadata_dir).to eq(File.join(@tree_1,'metadata'))
125
- expect(druid.temp_dir).to eq(File.join(@tree_1,'temp'))
126
-
127
- expect(File.exists?(File.join(@tree_1,'content'))).to eq true
128
- expect(File.exists?(File.join(@tree_1,'metadata'))).to eq true
129
- expect(File.exists?(File.join(@tree_1,'temp'))).to eq true
130
- end
131
-
132
- it "matches glob" do
133
- druid = DruidTools::Druid.new(@druid_1,@fixture_dir)
134
- druid.mkdir
135
- expect(Dir.glob(File.join(File.dirname(druid.path), DruidTools::Druid::glob)).size).to eq(1)
136
- end
137
- end
138
-
139
- describe "content discovery" do
140
- before :all do
141
- @druid = DruidTools::Druid.new(@druid_1,@fixture_dir)
142
- @filelist = %w(1 2 3 4).collect { |num| "someFile#{num}" }
143
- end
144
-
145
- it "finds content in content directories" do
146
- location = @druid.content_dir
147
- File.open(File.join(location,'someContent'),'w') { |f| f.write 'This is the content' }
148
- expect(@druid.find_content('someContent')).to eq(File.join(location,'someContent'))
149
- end
150
-
151
- it "finds content in the root directory" do
152
- location = @druid.path(nil,true)
153
- File.open(File.join(location,'someContent'),'w') { |f| f.write 'This is the content' }
154
- expect(@druid.find_content('someContent')).to eq(File.join(location,'someContent'))
155
- end
156
-
157
- it "finds content in the leaf directory" do
158
- location = File.expand_path('..',@druid.path(nil,true))
159
- File.open(File.join(location,'someContent'),'w') { |f| f.write 'This is the content' }
160
- expect(@druid.find_content('someContent')).to eq(File.join(location,'someContent'))
161
- end
162
-
163
- it "does not find content in the wrong content directory" do
164
- location = @druid.metadata_dir
165
- File.open(File.join(location,'someContent'),'w') { |f| f.write 'This is the content' }
166
- expect(@druid.find_content('someContent')).to be_nil
167
- end
168
-
169
- it "does not find content in a higher-up directory" do
170
- location = File.expand_path('../..',@druid.path(nil,true))
171
- File.open(File.join(location,'someContent'),'w') { |f| f.write 'This is the content' }
172
- expect(@druid.find_content('someContent')).to be_nil
173
- end
174
-
175
- it "finds a filelist in the content directory" do
176
- location = Pathname(@druid.content_dir)
177
- @filelist.each do |filename|
178
- location.join(filename).open('w') { |f| f.write "This is #{filename}" }
179
- end
180
- expect(@druid.find_filelist_parent('content',@filelist)).to eq(location)
181
- end
182
-
183
- it "finds a filelist in the root directory" do
184
- location = Pathname(@druid.path(nil,true))
185
- @filelist.each do |filename|
186
- location.join(filename).open('w') { |f| f.write "This is #{filename}" }
187
- end
188
- expect(@druid.find_filelist_parent('content',@filelist)).to eq(location)
189
- end
190
-
191
- it "finds a filelist in the leaf directory" do
192
- location = Pathname(File.expand_path('..',@druid.path(nil,true)))
193
- @filelist.each do |filename|
194
- location.join(filename).open('w') { |f| f.write "This is #{filename}" }
195
- end
196
- expect(@druid.find_filelist_parent('content',@filelist)).to eq(location)
197
- end
198
-
199
- it "raises an exception if the first file in the filelist is not found" do
200
- location = Pathname(@druid.content_dir)
201
- expect{@druid.find_filelist_parent('content',@filelist)}.to raise_exception(/content dir not found for 'someFile1' when searching/)
202
- end
203
-
204
- it "raises an exception if any other file in the filelist is not found" do
205
- location = Pathname(@druid.content_dir)
206
- location.join(@filelist.first).open('w') { |f| f.write "This is #{@filelist.first}" }
207
- expect{@druid.find_filelist_parent('content',@filelist)}.to raise_exception(/File 'someFile2' not found/)
208
- end
209
-
210
- end
211
-
212
- describe "#mkdir error handling" do
213
- it "raises SameContentExistsError if the directory already exists" do
214
- druid_2 = DruidTools::Druid.new(@druid_2,@fixture_dir)
215
- druid_2.mkdir
216
- expect { druid_2.mkdir }.to raise_error(DruidTools::SameContentExistsError)
217
- end
218
-
219
- it "raises DifferentContentExistsError if a link already exists in the workspace for this druid" do
220
- source_dir = '/tmp/content_dir'
221
- FileUtils.mkdir_p(source_dir)
222
- dr = DruidTools::Druid.new(@druid_2,@fixture_dir)
223
- dr.mkdir_with_final_link(source_dir)
224
- expect { dr.mkdir }.to raise_error(DruidTools::DifferentContentExistsError)
225
- end
226
- end
227
-
228
- describe "#mkdir_with_final_link" do
229
-
230
- before(:each) do
231
- @source_dir = '/tmp/content_dir'
232
- FileUtils.mkdir_p(@source_dir)
233
- @dr = DruidTools::Druid.new(@druid_2,@fixture_dir)
234
- end
235
-
236
- it "creates a druid tree in the workspace with the final directory being a link to the passed in source" do
237
- @dr.mkdir_with_final_link(@source_dir)
238
- expect(File).to be_symlink(@dr.path)
239
- expect(File.readlink(@tree_2)).to eq(@source_dir)
240
- end
241
-
242
- it "does not error out if the link to source already exists" do
243
- @dr.mkdir_with_final_link(@source_dir)
244
- expect(File).to be_symlink(@dr.path)
245
- expect(File.readlink(@tree_2)).to eq(@source_dir)
246
- end
247
-
248
- it "raises DifferentContentExistsError if a directory already exists in the workspace for this druid" do
249
- @dr.mkdir(@fixture_dir)
250
- expect { @dr.mkdir_with_final_link(@source_di) }.to raise_error(DruidTools::DifferentContentExistsError)
251
- end
252
-
253
- end
254
-
255
- describe "#prune!" do
256
-
257
- let(:workspace) { Dir.mktmpdir }
258
-
259
- let(:dr1) { DruidTools::Druid.new @druid_1, workspace }
260
- let(:dr2) { DruidTools::Druid.new @druid_2, workspace }
261
- let(:pathname1) { dr1.pathname }
262
-
263
- after(:each) do
264
- FileUtils.remove_entry workspace
265
- end
266
-
267
- it "throws error on misconfig when base dir cannot be created" do
268
- dir = '/some/dir/that/does/not/exist' # we don't have permissions to create
269
- dr0 = DruidTools::Druid.new @druid_1, dir
270
- expect {dr0.prune!}.to raise_error()
271
- expect(File).to_not exist(dir)
272
- end
273
-
274
- it "does not throw error when base can be created" do
275
- subdir = File.join(Dir.mktmpdir, 'some', 'nonexistant', 'subdir') # but this one *can* be created
276
- dr2 = DruidTools::Druid.new @druid_2, subdir
277
- expect {dr2.prune!}.not_to raise_error()
278
- expect(File).to exist(subdir)
279
- end
280
-
281
- context "shared ancestor" do
282
-
283
- before(:each) do
284
- #Nil the create records for this context because we're in a known read only one
285
- dr1.mkdir
286
- dr2.mkdir
287
- dr1.prune!
288
- end
289
-
290
- it "deletes the outermost directory" do
291
- expect(File).to_not exist(dr1.path)
292
- end
293
-
294
- it "deletes empty ancestor directories" do
295
- expect(File).to_not exist(pathname1.parent)
296
- expect(File).to_not exist(pathname1.parent.parent)
297
- end
298
-
299
- it "stops at ancestor directories that have children" do
300
- # 'cd/456' should still exist because of druid2
301
- shared_ancestor = pathname1.parent.parent.parent
302
- expect(shared_ancestor.to_s).to match(/cd\/456$/)
303
- expect(File).to exist(shared_ancestor)
304
- end
305
- end
306
-
307
- it "removes all directories up to the base path when there are no common ancestors" do
308
- #Make sure a delete record is not present
309
- expect(dr1.deletes_record_exists?).to be_falsey
310
-
311
- #Nil the create records for this test
312
- dr1.mkdir
313
- dr1.prune!
314
- expect(File).to_not exist(File.join(workspace, 'cd'))
315
- expect(File).to exist(workspace)
316
-
317
- #Make sure a delete record was created
318
- expect(dr1.deletes_dir_exists?).to be_truthy
319
- expect(dr1.deletes_record_exists?).to be_truthy
320
- end
321
-
322
- it "removes directories with symlinks" do
323
- #Make sure a delete record is not present
324
- expect(dr2.deletes_record_exists?).to be_falsey
325
-
326
- #Nil the create records for this test
327
- source_dir = File.join workspace, 'src_dir'
328
- FileUtils.mkdir_p(source_dir)
329
- dr2.mkdir_with_final_link(source_dir)
330
- dr2.prune!
331
- expect(File).to_not exist(dr2.path)
332
- expect(File).to_not exist(File.join(workspace, 'cd'))
333
-
334
- #Make sure a delete record was created
335
- expect(dr2.deletes_dir_exists?).to be_truthy
336
- expect(dr2.deletes_record_exists?).to be_truthy
337
- end
338
-
339
- describe "logging deleted druids" do
340
-
341
- #Purge any paths or delete records created in the test
342
- after :each do
343
- #Remove the .deletes dir to clean up
344
- dr2.deletes_delete_record if dr2.deletes_record_exists?
345
- FileUtils.rm_rf dr2.deletes_dir_pathname
346
- end
347
-
348
- it "returns the path to the .deletes directory as a Pathname" do
349
- expect(dr2.deletes_dir_pathname.class).to eq(Pathname)
350
- end
351
-
352
- it "returns the path to the delete record for a druid as a Pathname" do
353
- expect(dr2.deletes_record_pathname.class).to eq(Pathname)
354
- end
355
-
356
- it "returns the path to the delete record for a druid as top_level/.deletes/druid" do
357
- expect(dr2.deletes_record_pathname.to_s).to eq("#{dr2.base}/.deletes/#{dr2.id}")
358
- end
359
-
360
- it "returns false when the .deletes dir is not present on the file system" do
361
- expect(dr2.deletes_dir_exists?).to be_falsey
362
- end
363
-
364
- it "creates the .deletes dir and detect it exists" do
365
-
366
- #Clean the .deletes dir if present
367
- FileUtils.rm_rf dr2.deletes_dir_pathname
368
-
369
- #Test for exists? and create
370
- expect(dr2.deletes_dir_exists?).to be_falsey
371
- dr2.create_deletes_dir
372
- expect(dr2.deletes_dir_exists?).to be_truthy
373
- end
374
-
375
- it "returns false when the .deletes dir does not have a deleted record for a druid" do
376
- expect(dr2.deletes_record_exists?).to be_falsey
377
- end
378
-
379
- it "creates a deleted record with a parent directory that has no .deletes directory and no deleted for the file and successfully create a delete record there" do
380
- #Expect there not to be a .deletes dir or file (the file expectation is redundant I know)
381
- expect(dr2.deletes_dir_exists?).to be_falsey
382
- expect(dr2.deletes_record_exists?).to be_falsey
383
-
384
- #Create the delete record
385
- dr2.creates_delete_record
386
-
387
- #Check to ensure items were created
388
- expect(dr2.deletes_dir_exists?).to be_truthy
389
- expect(dr2.deletes_record_exists?).to be_truthy
390
- end
391
-
392
- it "creates a delete record with a parent directory that has a .deletes directory that does not contain a delete record for this druid" do
393
- #Expect there not to be a .deletes dir or file (the file expectation is redundant I know)
394
- expect(dr2.deletes_dir_exists?).to be_falsey
395
- expect(dr2.deletes_record_exists?).to be_falsey
396
-
397
- #Creates the deletes dir and check
398
- dr2.create_deletes_dir
399
- expect(dr2.deletes_dir_exists?).to be_truthy
400
- expect(dr2.deletes_record_exists?).to be_falsey
401
-
402
- #Create the delete record
403
- dr2.creates_delete_record
404
-
405
- #Check to ensure items were created
406
- expect(dr2.deletes_dir_exists?).to be_truthy
407
- expect(dr2.deletes_record_exists?).to be_truthy
408
- end
409
-
410
- it "creates a delete record with a parent directory that does not have a .deletes directory and contains an older delete record" do
411
- #Expect there not to be a .deletes dir or file (the file expectation is redundant I know)
412
- expect(dr2.deletes_dir_exists?).to be_falsey
413
- expect(dr2.deletes_record_exists?).to be_falsey
414
-
415
- dr2.creates_delete_record
416
- time = Time.now
417
- expect(File.mtime(dr2.deletes_record_pathname)).to be <= time
418
- sleep(1) #force a one second pause in case the machine is fast (as in not some old Commodore64), since mtime only goes down to the second
419
-
420
- dr2.creates_delete_record
421
- #Should have a new newer deleted record
422
- expect(File.mtime(dr2.deletes_record_pathname)).to be > time
423
- end
424
- end
425
- end
426
- end