-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rb
More file actions
469 lines (392 loc) · 13.3 KB
/
Copy pathtest.rb
File metadata and controls
469 lines (392 loc) · 13.3 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
require 'test/unit'
require './zero-files.rb'
require './import-media.rb'
#require 'pry'
#require 'pry-debugger'
class MediaImportTest < Test::Unit::TestCase
TEST_DATA = "./test/data"
SOURCE = "/tmp/root"
@source_files = []
@source_files_valid_media = []
TARGET = "/tmp/imported"
TARGET_UNDATED = "/tmp/imported/undated"
TARGET_FILES = ["#{TARGET}/2008/08/29/IMG_002.JPG",
"#{TARGET}/2001/02/19/sample_.mov",
"#{TARGET}/2005/10/17/sample.mov",
"#{TARGET}/2005/10/28/sample.3g2",
"#{TARGET}/2005/10/28/sample.3gp",
"#{TARGET}/2005/10/28/sample.mp4",
"#{TARGET}/2005/12/20/sample.m4v",
"#{TARGET}/2008/08/29/IMG_002.JPG",
"#{TARGET}/2010/08/20/dateintext.jpg"]
def setup
FileUtils.cp_r "#{TEST_DATA}/.", SOURCE
@source_files = Dir["#{SOURCE}/*"].reject { |f| File.directory? f }.sort
@source_files_valid_media = @source_files.reject { |f| /noexifdate/ =~ f }
FileUtils.mkpath "#{SOURCE}/2015"
FileUtils.mkpath "/tmp/.cache/"
FileUtils.mkpath "#{SOURCE}/.cache/"
FileUtils.mkpath "#{SOURCE}/2015/01/"
FileUtils.mkpath "#{SOURCE}/2017/05/"
FileUtils.touch "#{SOURCE}/zero.JPG"
File.open("#{SOURCE}/text.txt", 'w') { |txt_f| txt_f.write 'dummy content' }
FileUtils.touch "#{SOURCE}/zero.txt"
FileUtils.touch "#{SOURCE}/.cache/IMG_001.JPG"
FileUtils.cp_r "#{TEST_DATA}/.", "#{SOURCE}/.cache/"
FileUtils.touch "#{SOURCE}/.cache/IMG_003.JPG"
FileUtils.mkpath TARGET
end
def teardown
FileUtils.rm_r SOURCE
FileUtils.rm_r TARGET if File.exists?(TARGET)
end
def test_files
valid_files = files(SOURCE)
assert_equal((@source_files + ["#{SOURCE}/text.txt"]).sort, valid_files.sort)
end
=begin
def test_files_commandline
valid_files = `ruby zero-files.rb`
assert(valid_files.empty?)
valid_files = `ruby zero-files.rb #{SOURCE}`
list = valid_files.split("\n").reject { |l| /--/ =~ l || l.empty?}
assert_equal((@source_files + ["#{SOURCE}/text.txt"]).sort, list)
end
=end
def test_media
# check return
assert_equal(@source_files, media_files(SOURCE))
# check yield
media_files(SOURCE) do |m, exif|
assert(@source_files.include?(m))
end
end
def test_date_dir
d = '2009-09-30 11:10:55 UTC'
dir = date_dir(TARGET, Time.parse(d))
assert_equal("#{TARGET}/2009/09/30", dir)
end
def test_import_default
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
import SOURCE, TARGET
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
end
def test_import_copy
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
import SOURCE, TARGET, :copy
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
end
def test_import_move
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
import SOURCE, TARGET, :move
assert(@source_files_valid_media.all? { |f| !File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
end
def test_analyze_yield
source_t = "#{SOURCE}/#{__method__}"
begin
FileUtils.mkpath source_t
FileUtils.cp @source_files_valid_media.first, source_t
target_file = TARGET_FILES.first
source_file = "#{source_t}/#{File.basename(target_file)}"
okay_to_import = false
analyze source_t, TARGET do |s,t,e|
case e
when :okay_to_import
assert_equal(s, source_file)
assert_equal(t, target_file)
okay_to_import = true
end
end
assert(okay_to_import)
duplicate_found = false
FileUtils.cp source_file, "#{target_file}_1"
analyze source_t, TARGET do |s,t,e|
case e
when :duplicate_found
assert_equal(s, source_file)
assert_equal(t, "#{target_file}_1")
duplicate_found = true
end
end
assert(duplicate_found)
File.delete "#{target_file}_1"
name_collision_found = false
FileUtils.touch target_file
analyze source_t, TARGET do |s,t,e|
case e
when :name_collision_found
assert_equal(s, source_file)
assert_equal(t, target_file)
name_collision_found = true
end
end
assert(name_collision_found)
noexifdate = false
FileUtils.rm_r Dir["#{source_t}/*"]
FileUtils.cp "#{SOURCE}/noexifdate.png", source_t
analyze source_t, TARGET do |s,t,e|
case e
when :noexifdate
noexifdate = true
end
end
assert(noexifdate)
ensure
FileUtils.rm_r source_t if File.exists?(source_t)
end
end
def test_import_yield
source_t = "#{SOURCE}/#{__method__}"
begin
FileUtils.mkpath source_t
FileUtils.cp @source_files_valid_media.first, source_t
target_file = TARGET_FILES.first
source_file = "#{source_t}/#{File.basename(target_file)}"
copying = false
import source_t, TARGET, :copy do |s,t,e|
case e
when :copying
assert_equal(s, source_file)
assert_equal(t, target_file)
copying = true
end
end
assert(copying)
File.delete target_file
moving = false
import source_t, TARGET, :move do |s,t,e|
case e
when :moving
assert_equal(s, source_file)
assert_equal(t, target_file)
moving = true
end
end
assert(moving)
duplicate_found = false
FileUtils.mv target_file, source_file
FileUtils.cp source_file, "#{target_file}_1"
analyze source_t, TARGET do |s,t,e|
case e
when :duplicate_found
assert_equal(s, source_file)
assert_equal(t, "#{target_file}_1")
duplicate_found = true
end
end
assert(duplicate_found)
File.delete "#{target_file}_1"
name_collision_found = false
FileUtils.touch target_file
analyze source_t, TARGET do |s,t,e|
case e
when :name_collision_found
assert_equal(s, source_file)
assert_equal(t, target_file)
name_collision_found = true
end
end
assert(name_collision_found)
copying_noexifdate = false
FileUtils.rm_r Dir["#{source_t}/*"]
FileUtils.cp "#{SOURCE}/noexifdate.png", source_t
import source_t, TARGET, :copy do |s,t,e|
case e
when :copying_noexifdate
copying_noexifdate = true
end
end
assert(copying_noexifdate)
moving_noexifdate = false
FileUtils.rm_r Dir["#{source_t}/*"]
FileUtils.rm_r Dir["#{TARGET}/*"]
FileUtils.cp "#{SOURCE}/noexifdate.png", source_t
import source_t, TARGET, :move do |s,t,e|
case e
when :moving_noexifdate
moving_noexifdate = true
end
end
assert(moving_noexifdate)
ensure
FileUtils.rm_r source_t if File.exists?(source_t)
end
end
def test_import_errorhandling
source_t = "#{SOURCE}/#{__method__}"
FileUtils.mkpath source_t
FileUtils.cp @source_files_valid_media.first, source_t
# faking a failure
FileUtils.instance_eval do
alias :original_mkpath :mkpath
@@hit = false
def self.mkpath path
if @@hit
original_mkpath path # so that we don't screw up other tests
else
@@hit = true
raise IOError, 'faking a failure'
end
end
end
error = false
import source_t, TARGET do |s,t,e|
case e
when :error
assert_equal($!.class, IOError)
error = true
end
end
assert(error)
end
def test_import_commandline_error
missing_src_tgt = `ruby import-media.rb`
assert(/Usage:/ =~ missing_src_tgt)
missing_src = `ruby import-media.rb /tmp`
assert(/Usage:/ =~ missing_src)
missing_src = `ruby import-media.rb -mv /tmp /tmp/tgt/ --copy`
assert(/Usage:/ =~ missing_src)
missing_src = `ruby import-media.rb -mv -cp /tmp /tmp/tgt/`
assert(/Usage:/ =~ missing_src)
missing_src = `ruby import-media.rb -cp /tmp /tmp/tgt/ -mv`
assert(/Usage:/ =~ missing_src)
end
def test_import_commandline_help
usage = USAGE.gsub(/([^ ]+)(import-media.rb)/, '\2') # removing path
help = `ruby import-media.rb -h`
assert_equal(usage, help)
help = `ruby import-media.rb --help`
assert_equal(usage, help)
end
def test_import_commandline_move
# move option
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb --move #{SOURCE} #{TARGET}`
assert(@source_files_valid_media.all? { |f| !File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
# mv option
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb -mv #{SOURCE} #{TARGET}`
assert(@source_files_valid_media.all? { |f| !File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
# option at different place
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb #{SOURCE} -mv #{TARGET}`
assert(@source_files_valid_media.all? { |f| !File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb #{SOURCE} #{TARGET} --move`
assert(@source_files_valid_media.all? { |f| !File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
end
def test_import_commandline_copy
# default option is copy
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb #{SOURCE} #{TARGET}`
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
# copy option
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb --copy #{SOURCE} #{TARGET}`
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
# cp option
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb -cp #{SOURCE} #{TARGET}`
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
# option at different place
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb #{SOURCE} -cp #{TARGET}`
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
teardown
setup
TARGET_FILES.each { |f| File.delete(f) if File.exists? f }
`ruby import-media.rb #{SOURCE} #{TARGET} --copy`
assert(@source_files_valid_media.all? { |f| File.exists?(f) })
assert(TARGET_FILES.all? { |f| File.exists?(f) })
end
def test_timestamp
datetime = Time.parse('2014-09-02 06:44:48 +0530')
assert_equal(timestamp(datetime), '2014-09-02-06-44-48')
end
def test_unique_file_name
target_dir = "/tmp/#{__method__}"
begin
file_names = ["IMG_02.JPG", "IMG_02.JPG_1", "IMG_02_1.JPG", "IMG_02_2.JPG"]
FileUtils.mkpath target_dir
file_names.each do |fn|
FileUtils.touch "#{target_dir}/#{fn}"
end
colliding_names = []
unique = unique_file_name(target_dir, "IMG_02.JPG") do |colliding_name, event|
case event
when :name_collision_found
colliding_names << File.basename(colliding_name)
end
end
assert_equal("#{target_dir}/IMG_02_3.JPG", unique)
assert_equal((file_names - ["IMG_02.JPG_1"]).sort, colliding_names.sort)
ensure
FileUtils.rm_r target_dir
end
end
def test_import_noexifdate_copy
source_t = "#{SOURCE}/#{__method__}"
begin
copying_noexifdate = false
FileUtils.mkpath source_t
FileUtils.cp "#{SOURCE}/noexifdate.png", source_t
import source_t, TARGET, :copy do |s,t,e|
case e
when :copying_noexifdate
copying_noexifdate = true
assert_equal("#{source_t}/noexifdate.png", s)
assert_equal("#{TARGET_UNDATED}/noexifdate.png", t)
end
end
assert(copying_noexifdate)
assert(File.exists?("#{TARGET_UNDATED}/noexifdate.png"))
assert(File.exists?("#{TARGET_UNDATED}/noexifdate.png.log"))
ensure
FileUtils.rm_r source_t if File.exists?(source_t)
end
end
def test_import_noexifdate_move
source_t = "#{SOURCE}/#{__method__}"
begin
moving_noexifdate = false
FileUtils.mkpath source_t
FileUtils.cp "#{SOURCE}/noexifdate.png", source_t
import source_t, TARGET, :move do |s,t,e|
case e
when :moving_noexifdate
moving_noexifdate = true
assert_equal("#{source_t}/noexifdate.png", s)
assert_equal("#{TARGET_UNDATED}/noexifdate.png", t)
end
end
assert(moving_noexifdate)
assert(File.exists?("#{TARGET_UNDATED}/noexifdate.png"))
assert(File.exists?("#{TARGET_UNDATED}/noexifdate.png.log"))
ensure
FileUtils.rm_r source_t if File.exists?(source_t)
end
end
end