carrierwave 0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of carrierwave might be problematic. Click here for more details.

@@ -2,8 +2,6 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe CarrierWave::SanitizedFile do
4
4
 
5
- include SanitizedFileSpecHelper
6
-
7
5
  before do
8
6
  unless File.exists?(file_path('llama.jpg'))
9
7
  FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
@@ -206,15 +204,9 @@ describe CarrierWave::SanitizedFile do
206
204
  @sanitized_file.extension.should == 'png'
207
205
  end
208
206
 
209
- describe "with permissions set" do
210
- before do
211
- @sanitized_file.options[:permissions] = 0755
212
- end
213
-
214
- it "should set the right permissions" do
215
- @sanitized_file.move_to(file_path('gurr.png'))
216
- @sanitized_file.should have_permissions(0755)
217
- end
207
+ it "should set the right permissions" do
208
+ @sanitized_file.move_to(file_path('gurr.png'), 0755)
209
+ @sanitized_file.should have_permissions(0755)
218
210
  end
219
211
 
220
212
  end
@@ -266,15 +258,9 @@ describe CarrierWave::SanitizedFile do
266
258
  new_file.extension.should == 'png'
267
259
  end
268
260
 
269
- describe "with permissions set" do
270
- before do
271
- @sanitized_file.options[:permissions] = 0755
272
- end
273
-
274
- it "should set the right permissions" do
275
- new_file = @sanitized_file.copy_to(file_path('gurr.png'))
276
- new_file.should have_permissions(0755)
277
- end
261
+ it "should set the right permissions" do
262
+ new_file = @sanitized_file.copy_to(file_path('gurr.png'), 0755)
263
+ new_file.should have_permissions(0755)
278
264
  end
279
265
 
280
266
  end
@@ -345,9 +331,9 @@ describe CarrierWave::SanitizedFile do
345
331
  end
346
332
  end
347
333
 
348
- describe '#string?' do
334
+ describe '#is_path?' do
349
335
  it "should be false" do
350
- @sanitized_file.string?.should be_false
336
+ @sanitized_file.is_path?.should be_false
351
337
  end
352
338
  end
353
339
 
@@ -363,9 +349,9 @@ describe CarrierWave::SanitizedFile do
363
349
 
364
350
  it_should_behave_like "all valid sanitized files that are stored on disk"
365
351
 
366
- describe '#string?' do
352
+ describe '#is_path?' do
367
353
  it "should be false" do
368
- @sanitized_file.string?.should be_false
354
+ @sanitized_file.is_path?.should be_false
369
355
  end
370
356
  end
371
357
 
@@ -391,9 +377,9 @@ describe CarrierWave::SanitizedFile do
391
377
  end
392
378
  end
393
379
 
394
- describe '#string?' do
380
+ describe '#is_path?' do
395
381
  it "should be false" do
396
- @sanitized_file.string?.should be_false
382
+ @sanitized_file.is_path?.should be_false
397
383
  end
398
384
  end
399
385
 
@@ -422,9 +408,9 @@ describe CarrierWave::SanitizedFile do
422
408
 
423
409
  it_should_behave_like "all valid sanitized files that are stored on disk"
424
410
 
425
- describe '#string?' do
411
+ describe '#is_path?' do
426
412
  it "should be false" do
427
- @sanitized_file.string?.should be_false
413
+ @sanitized_file.is_path?.should be_false
428
414
  end
429
415
  end
430
416
 
@@ -448,9 +434,9 @@ describe CarrierWave::SanitizedFile do
448
434
 
449
435
  it_should_behave_like "all valid sanitized files that are stored on disk"
450
436
 
451
- describe '#string?' do
437
+ describe '#is_path?' do
452
438
  it "should be true" do
453
- @sanitized_file.string?.should be_true
439
+ @sanitized_file.is_path?.should be_true
454
440
  end
455
441
  end
456
442
 
@@ -474,9 +460,9 @@ describe CarrierWave::SanitizedFile do
474
460
 
475
461
  it_should_behave_like "all valid sanitized files that are stored on disk"
476
462
 
477
- describe '#string?' do
463
+ describe '#is_path?' do
478
464
  it "should be true" do
479
- @sanitized_file.string?.should be_true
465
+ @sanitized_file.is_path?.should be_true
480
466
  end
481
467
  end
482
468
 
@@ -506,9 +492,9 @@ describe CarrierWave::SanitizedFile do
506
492
  end
507
493
  end
508
494
 
509
- describe '#string?' do
495
+ describe '#is_path?' do
510
496
  it "should be false" do
511
- @empty.string?.should be_false
497
+ @empty.is_path?.should be_false
512
498
  end
513
499
  end
514
500
 
@@ -572,9 +558,9 @@ describe CarrierWave::SanitizedFile do
572
558
  end
573
559
  end
574
560
 
575
- describe '#string?' do
561
+ describe '#is_path?' do
576
562
  it "should be false" do
577
- @empty.string?.should be_false
563
+ @empty.is_path?.should be_false
578
564
  end
579
565
  end
580
566
 
data/spec/spec_helper.rb CHANGED
@@ -30,91 +30,55 @@ end
30
30
  CarrierWave.config[:public] = public_path
31
31
  CarrierWave.config[:root] = File.expand_path(File.dirname(__FILE__))
32
32
 
33
- module SanitizedFileSpecHelper
34
- def stub_merb_tempfile(filename)
35
- raise "#{path} file does not exist" unless File.exist?(file_path(filename))
36
-
37
- t = Tempfile.new(filename)
38
- FileUtils.copy_file(file_path(filename), t.path)
33
+ module CarrierWave
34
+ module Test
35
+ module MockFiles
36
+ def stub_merb_tempfile(filename)
37
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
38
+
39
+ t = Tempfile.new(filename)
40
+ FileUtils.copy_file(file_path(filename), t.path)
39
41
 
40
- return t
41
- end
42
+ return t
43
+ end
42
44
 
43
- def stub_tempfile(filename, mime_type=nil, fake_name=nil)
44
- raise "#{path} file does not exist" unless File.exist?(file_path(filename))
45
+ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
46
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
45
47
 
46
- t = Tempfile.new(filename)
47
- FileUtils.copy_file(file_path(filename), t.path)
48
+ t = Tempfile.new(filename)
49
+ FileUtils.copy_file(file_path(filename), t.path)
48
50
 
49
- # This is stupid, but for some reason rspec won't play nice...
50
- eval <<-EOF
51
- def t.original_filename; '#{fake_name || filename}'; end
52
- def t.content_type; '#{mime_type}'; end
53
- def t.local_path; path; end
54
- EOF
55
-
56
- return t
57
- end
58
-
59
- def stub_stringio(filename, mime_type=nil, fake_name=nil)
60
- if filename
61
- t = StringIO.new( IO.read( file_path( filename ) ) )
62
- else
63
- t = StringIO.new
64
- end
65
- t.stub!(:local_path).and_return("")
66
- t.stub!(:original_filename).and_return(filename || fake_name)
67
- t.stub!(:content_type).and_return(mime_type)
68
- return t
69
- end
70
-
71
- def stub_file(filename, mime_type=nil, fake_name=nil)
72
- f = File.open(file_path(filename))
73
- return f
74
- end
75
-
76
- class BeIdenticalTo
77
- def initialize(expected)
78
- @expected = expected
79
- end
80
- def matches?(actual)
81
- @actual = actual
82
- FileUtils.identical?(@actual, @expected)
83
- end
84
- def failure_message
85
- "expected #{@actual.inspect} to be identical to #{@expected.inspect}"
86
- end
87
- def negative_failure_message
88
- "expected #{@actual.inspect} to not be identical to #{@expected.inspect}"
89
- end
90
- end
91
-
92
- def be_identical_to(expected)
93
- BeIdenticalTo.new(expected)
94
- end
95
-
96
- class HavePermissions
97
- def initialize(expected)
98
- @expected = expected
99
- end
100
-
101
- def matches?(actual)
102
- @actual = actual
103
- # Satisfy expectation here. Return false or raise an error if it's not met.
104
- (File.stat(@actual.path).mode & 0777) == @expected
105
- end
106
-
107
- def failure_message
108
- "expected #{@actual.inspect} to have permissions #{@expected.to_s(8)}, but they were #{(File.stat(@actual.path).mode & 0777).to_s(8)}"
109
- end
110
-
111
- def negative_failure_message
112
- "expected #{@actual.inspect} not to have permissions #{@expected.to_s(8)}, but it did"
51
+ # This is stupid, but for some reason rspec won't play nice...
52
+ eval <<-EOF
53
+ def t.original_filename; '#{fake_name || filename}'; end
54
+ def t.content_type; '#{mime_type}'; end
55
+ def t.local_path; path; end
56
+ EOF
57
+
58
+ return t
59
+ end
60
+
61
+ def stub_stringio(filename, mime_type=nil, fake_name=nil)
62
+ if filename
63
+ t = StringIO.new( IO.read( file_path( filename ) ) )
64
+ else
65
+ t = StringIO.new
66
+ end
67
+ t.stub!(:local_path).and_return("")
68
+ t.stub!(:original_filename).and_return(filename || fake_name)
69
+ t.stub!(:content_type).and_return(mime_type)
70
+ return t
71
+ end
72
+
73
+ def stub_file(filename, mime_type=nil, fake_name=nil)
74
+ f = File.open(file_path(filename))
75
+ return f
76
+ end
113
77
  end
114
78
  end
79
+ end
115
80
 
116
- def have_permissions(expected)
117
- HavePermissions.new(expected)
118
- end
119
-
81
+ Spec::Runner.configure do |config|
82
+ config.include CarrierWave::Test::Matchers
83
+ config.include CarrierWave::Test::MockFiles
120
84
  end
@@ -1,9 +1,11 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe CarrierWave::Uploader do
4
-
4
+
5
5
  before do
6
- @uploader_class = Class.new(CarrierWave::Uploader)
6
+ @uploader_class = Class.new do
7
+ include CarrierWave::Uploader
8
+ end
7
9
  @uploader = @uploader_class.new
8
10
  end
9
11
 
@@ -34,10 +36,10 @@ describe CarrierWave::Uploader do
34
36
  @uploader.thumb.version_name.should == :thumb
35
37
  end
36
38
 
37
- it "should set the version name on the class" do
39
+ it "should set the version names on the class" do
38
40
  @uploader_class.version :thumb
39
- @uploader.class.version_name.should == nil
40
- @uploader.thumb.class.version_name.should == :thumb
41
+ @uploader.class.version_names.should == []
42
+ @uploader.thumb.class.version_names.should == [:thumb]
41
43
  end
42
44
 
43
45
  it "should remember mount options" do
@@ -58,6 +60,45 @@ describe CarrierWave::Uploader do
58
60
  @uploader.store_dir.should == 'uploads'
59
61
  @uploader.thumb.store_dir.should == public_path('monkey/apache')
60
62
  end
63
+
64
+ it "should reopen the same class when called multiple times" do
65
+ @uploader_class.version :thumb do
66
+ def self.monkey
67
+ "monkey"
68
+ end
69
+ end
70
+ @uploader_class.version :thumb do
71
+ def self.llama
72
+ "llama"
73
+ end
74
+ end
75
+ @uploader_class.version(:thumb).monkey.should == "monkey"
76
+ @uploader_class.version(:thumb).llama.should == "llama"
77
+ end
78
+
79
+ describe 'with nested versions' do
80
+ before do
81
+ @uploader_class.version :thumb do
82
+ version :mini
83
+ version :micro
84
+ end
85
+ end
86
+
87
+ it "should add an array of version names" do
88
+ @uploader.class.version_names.should == []
89
+ @uploader.thumb.class.version_names.should == [:thumb]
90
+ @uploader.thumb.mini.class.version_names.should == [:thumb, :mini]
91
+ @uploader.thumb.micro.class.version_names.should == [:thumb, :micro]
92
+ end
93
+
94
+ it "should set the version name for the instances" do
95
+ @uploader.version_name.should be_nil
96
+ @uploader.thumb.version_name.should == :thumb
97
+ @uploader.thumb.mini.version_name.should == :thumb_mini
98
+ @uploader.thumb.micro.version_name.should == :thumb_micro
99
+ end
100
+
101
+ end
61
102
 
62
103
  end
63
104
 
@@ -133,7 +174,7 @@ describe CarrierWave::Uploader do
133
174
  end
134
175
  end
135
176
 
136
- describe '#blank' do
177
+ describe '#blank?' do
137
178
  it "should be true when nothing has been done" do
138
179
  @uploader.should be_blank
139
180
  end
@@ -149,6 +190,17 @@ describe CarrierWave::Uploader do
149
190
  end
150
191
  end
151
192
 
193
+ describe '#read' do
194
+ it "should be nil by default" do
195
+ @uploader.read.should be_nil
196
+ end
197
+
198
+ it "should read the contents of a cached file" do
199
+ @uploader.cache!(File.open(file_path('test.jpg')))
200
+ @uploader.read.should == "this is stuff"
201
+ end
202
+ end
203
+
152
204
  describe '#store_dir' do
153
205
  it "should default to the config option" do
154
206
  @uploader.store_dir.should == 'uploads'
@@ -290,6 +342,37 @@ describe CarrierWave::Uploader do
290
342
  it "should do nothing when trying to cache an empty file" do
291
343
  @uploader.cache!(nil)
292
344
  end
345
+
346
+ it "should set permissions if options are given" do
347
+ old_permissions = CarrierWave.config[:permissions]
348
+ CarrierWave.config[:permissions] = 0777
349
+
350
+ @uploader.cache!(File.open(file_path('test.jpg')))
351
+ @uploader.should have_permissions(0777)
352
+
353
+ CarrierWave.config[:permissions] = old_permissions
354
+ end
355
+
356
+ it "should not raise an integiry error if there is no white list" do
357
+ @uploader.stub!(:extension_white_list).and_return(nil)
358
+ running {
359
+ @uploader.cache!(File.open(file_path('test.jpg')))
360
+ }.should_not raise_error(CarrierWave::IntegrityError)
361
+ end
362
+
363
+ it "should not raise an integiry error if there is a white list and the file is on it" do
364
+ @uploader.stub!(:extension_white_list).and_return(%w(jpg gif png))
365
+ running {
366
+ @uploader.cache!(File.open(file_path('test.jpg')))
367
+ }.should_not raise_error(CarrierWave::IntegrityError)
368
+ end
369
+
370
+ it "should raise an integiry error if there is a white list and the file is not on it" do
371
+ @uploader.stub!(:extension_white_list).and_return(%w(txt doc xls))
372
+ running {
373
+ @uploader.cache!(File.open(file_path('test.jpg')))
374
+ }.should raise_error(CarrierWave::IntegrityError)
375
+ end
293
376
  end
294
377
 
295
378
  describe '#retrieve_from_cache!' do
@@ -479,6 +562,7 @@ describe CarrierWave::Uploader do
479
562
  @stored_file.stub!(:path).and_return('/path/to/somewhere')
480
563
  @stored_file.stub!(:url).and_return('http://www.example.com')
481
564
  @stored_file.stub!(:identifier).and_return('this-is-me')
565
+ @stored_file.stub!(:read).and_return('here be content')
482
566
 
483
567
  @uploader_class.storage.stub!(:retrieve!).and_return(@stored_file)
484
568
  end
@@ -497,6 +581,11 @@ describe CarrierWave::Uploader do
497
581
  @uploader.retrieve_from_store('monkey.txt')
498
582
  @uploader.identifier.should == 'this-is-me'
499
583
  end
584
+
585
+ it "should read out the contents" do
586
+ @uploader.retrieve_from_store('monkey.txt')
587
+ @uploader.read.should == 'here be content'
588
+ end
500
589
 
501
590
  it "should instruct the storage engine to retrieve the file and store the result" do
502
591
  @uploader_class.storage.should_receive(:retrieve!).with(@uploader, 'monkey.txt').and_return(@stored_file)
@@ -522,10 +611,11 @@ describe CarrierWave::Uploader do
522
611
  CarrierWave::Uploader.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
523
612
  end
524
613
 
525
- it "should suffix the version's store_dir" do
614
+ it "should set store_path with versions" do
526
615
  @uploader.cache!(File.open(file_path('test.jpg')))
527
- @uploader.store_dir.should == 'uploads'
528
- @uploader.thumb.store_dir.should == 'uploads/thumb'
616
+ @uploader.store_path.should == 'uploads/test.jpg'
617
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
618
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
529
619
  end
530
620
 
531
621
  it "should move it to the tmp dir with the filename prefixed" do
@@ -544,10 +634,11 @@ describe CarrierWave::Uploader do
544
634
  @uploader.thumb.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/thumb_test.jpg')
545
635
  end
546
636
 
547
- it "should suffix the version's store_dir" do
637
+ it "should set store_path with versions" do
548
638
  @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
549
- @uploader.store_dir.should == 'uploads'
550
- @uploader.thumb.store_dir.should == 'uploads/thumb'
639
+ @uploader.store_path.should == 'uploads/test.jpg'
640
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
641
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
551
642
  end
552
643
  end
553
644
 
@@ -579,17 +670,11 @@ describe CarrierWave::Uploader do
579
670
  @uploader.url.should == 'http://www.example.com'
580
671
  end
581
672
 
582
- it "should, if a file is given as argument, suffix the version's store_dir" do
583
- @uploader.store!(@file)
584
- @uploader.store_dir.should == 'uploads'
585
- @uploader.thumb.store_dir.should == 'uploads/thumb'
586
- end
587
-
588
- it "should, if a files is given as an argument and use_cache is false, suffix the version's store_dir" do
589
- CarrierWave.config[:use_cache] = false
673
+ it "should, if a file is given as argument, set the store_path" do
590
674
  @uploader.store!(@file)
591
- @uploader.store_dir.should == 'uploads'
592
- @uploader.thumb.store_dir.should == 'uploads/thumb'
675
+ @uploader.store_path.should == 'uploads/test.jpg'
676
+ @uploader.thumb.store_path.should == 'uploads/thumb_test.jpg'
677
+ @uploader.thumb.store_path('kebab.png').should == 'uploads/thumb_kebab.png'
593
678
  end
594
679
 
595
680
  end