jnicklas-carrierwave 0.1

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.
@@ -0,0 +1,122 @@
1
+ $TESTING=true
2
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
3
+
4
+ require 'rubygems'
5
+
6
+ if ENV["AS"]
7
+ puts "--> using ActiveSupport"
8
+ require 'activesupport'
9
+ else
10
+ puts "--> using Extlib"
11
+ require 'extlib'
12
+ end
13
+
14
+ require 'tempfile'
15
+ #require 'ruby-debug'
16
+ require 'spec'
17
+
18
+ require 'carrierwave'
19
+
20
+ alias :running :lambda
21
+
22
+ def file_path( *paths )
23
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
24
+ end
25
+
26
+ def public_path( *paths )
27
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
28
+ end
29
+
30
+ CarrierWave.config[:public] = public_path
31
+ CarrierWave.config[:root] = File.expand_path(File.dirname(__FILE__))
32
+ CarrierWave.config[:store_dir] = 'public/uploads'
33
+ CarrierWave.config[:cache_dir] = 'public/uploads/tmp'
34
+
35
+ module SanitizedFileSpecHelper
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)
41
+
42
+ return t
43
+ end
44
+
45
+ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
46
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
47
+
48
+ t = Tempfile.new(filename)
49
+ FileUtils.copy_file(file_path(filename), t.path)
50
+
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
77
+
78
+ class BeIdenticalTo
79
+ def initialize(expected)
80
+ @expected = expected
81
+ end
82
+ def matches?(actual)
83
+ @actual = actual
84
+ FileUtils.identical?(@actual, @expected)
85
+ end
86
+ def failure_message
87
+ "expected #{@actual.inspect} to be identical to #{@expected.inspect}"
88
+ end
89
+ def negative_failure_message
90
+ "expected #{@actual.inspect} to not be identical to #{@expected.inspect}"
91
+ end
92
+ end
93
+
94
+ def be_identical_to(expected)
95
+ BeIdenticalTo.new(expected)
96
+ end
97
+
98
+ class HavePermissions
99
+ def initialize(expected)
100
+ @expected = expected
101
+ end
102
+
103
+ def matches?(actual)
104
+ @actual = actual
105
+ # Satisfy expectation here. Return false or raise an error if it's not met.
106
+ (File.stat(@actual.path).mode & 0777) == @expected
107
+ end
108
+
109
+ def failure_message
110
+ "expected #{@actual.inspect} to have permissions #{@expected.to_s(8)}, but they were #{(File.stat(@actual.path).mode & 0777).to_s(8)}"
111
+ end
112
+
113
+ def negative_failure_message
114
+ "expected #{@actual.inspect} not to have permissions #{@expected.to_s(8)}, but it did"
115
+ end
116
+ end
117
+
118
+ def have_permissions(expected)
119
+ HavePermissions.new(expected)
120
+ end
121
+
122
+ end