mock-aws-s3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Joshua Krall
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ = mock-aws-s3
2
+
3
+ This is a simple gem for mocking out the AWS::S3 library, so that no calls to the Amazon S3 service during tests.
4
+
5
+ Usage is simple, just add the gem to your bundle, and require it in your spec_helper.rb:
6
+
7
+ require 'mock-aws-s3'
8
+
9
+ From there on, all (supported) calls to AWS::S3::S3Object will be proxied to file operations in a temp directory on your disk. So these commands:
10
+
11
+ AWS::S3::S3Object.store 'key', 'some data', 'bucket'
12
+ AWS::S3::S3Object.exists?('key', 'bucket')
13
+ AWS::S3::S3Object.delete('key', 'bucket')
14
+
15
+ ... will result in a file being created on disk at <tt>./tmp/mock-aws-s3/bucket/key</tt>, with contents: <tt>"some data"</tt>. A <tt>File.exists?()</tt> check will be performed on that file, and then it will be deleted.
16
+
17
+ If you are using Rails, the <tt>Rails.root</tt> directory will be used: <tt>Rails.root.join('tmp')</tt>.
18
+
19
+
20
+ == Note on Patches/Pull Requests
21
+
22
+ * Fork the project.
23
+ * Make your feature addition or bug fix.
24
+ * Add spec for it. This is important so I don't break it in a
25
+ future version unintentionally.
26
+ * Commit, do not mess with rakefile, version, or history.
27
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
28
+ * Send me a pull request. Bonus points for topic branches.
29
+
30
+ == Copyright
31
+
32
+ Copyright (c) 2010 Joshua Krall. See LICENSE for details.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "mock-aws-s3"
8
+ gem.summary = %Q{A Mock AWS::S3 so you can use it in your tests without hitting the network}
9
+ gem.description = %Q{A Mock AWS::S3 so you can use it in your tests without hitting the network.}
10
+ gem.email = "joshuakrall@pobox.com"
11
+ gem.homepage = "http://github.com/jkrall/mock-aws-s3"
12
+ gem.authors = ["Joshua Krall"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "mock-aws-s3 #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ require 'aws/s3'
2
+
3
+ require 'mock/aws/s3/object'
@@ -0,0 +1,100 @@
1
+ require 'ftools'
2
+ require 'ostruct'
3
+
4
+ module AWS
5
+ module S3
6
+
7
+ class S3Object
8
+ class << self
9
+
10
+ def value(key, bucket = nil, options = {}, &block)
11
+ data = File.open(path!(bucket, key, options)) {|f| f.read}
12
+ Value.new OpenStruct.new(:body=>data)
13
+ end
14
+
15
+ # def stream(key, bucket = nil, options = {}, &block)
16
+ # data = File.open(path!(bucket, key, options)) {|f| f.read}
17
+ # Value.new Response.new(:body=>data)
18
+ # value(key, bucket, options) do |response|
19
+ # response.read_body(&block)
20
+ # end
21
+ # end
22
+
23
+ # # Returns the object whose key is <tt>name</tt> in the specified bucket. If the specified key does not
24
+ # # exist, a NoSuchKey exception will be raised.
25
+ # def find(key, bucket = nil)
26
+ # key = key.remove_extended unless key.valid_utf8?
27
+ # bucket = Bucket.find(bucket_name(bucket), :marker => key.previous, :max_keys => 1)
28
+ # # If our heuristic failed, trigger a NoSuchKey exception
29
+ # if (object = bucket.objects.first) && object.key == key
30
+ # object
31
+ # else
32
+ # raise NoSuchKey.new("No such key `#{key}'", bucket)
33
+ # end
34
+ # end
35
+
36
+
37
+ # Makes a copy of the object with <tt>key</tt> to <tt>copy_key</tt>, preserving the ACL of the existing object if the <tt>:copy_acl</tt> option is true (default false).
38
+ def copy(key, copy_key, bucket = nil, options = {})
39
+ bucket = bucket_name(bucket)
40
+ source_key = path!(bucket, key)
41
+ target_key = path!(bucket, copy_key)
42
+ File.makedirs File.dirname(target_key)
43
+ FileUtils.cp_r source_key, target_key
44
+ end
45
+
46
+ #
47
+ # # Fetch information about the object with <tt>key</tt> from <tt>bucket</tt>. Information includes content type, content length,
48
+ # # last modified time, and others.
49
+ # #
50
+ # # If the specified key does not exist, NoSuchKey is raised.
51
+ # def about(key, bucket = nil, options = {})
52
+ # response = head(path!(bucket, key, options), options)
53
+ # raise NoSuchKey.new("No such key `#{key}'", bucket) if response.code == 404
54
+ # About.new(response.headers)
55
+ # end
56
+ #
57
+
58
+ def exists?(key, bucket = nil)
59
+ File.exists?(path!(bucket, key))
60
+ end
61
+
62
+ def delete(key, bucket = nil, options = {})
63
+ File.unlink path!(bucket, key, options)
64
+ end
65
+
66
+ def store(key, data, bucket = nil, options = {})
67
+ validate_key!(key)
68
+ # Must build path before infering content type in case bucket is being used for options
69
+ path = path!(bucket, key, options)
70
+ infer_content_type!(key, options)
71
+
72
+ File.makedirs File.dirname(path)
73
+ File.open(path, 'wb') do |f|
74
+ f.write data
75
+ end
76
+ end
77
+ alias_method :create, :store
78
+ alias_method :save, :store
79
+
80
+ TEMP_PATH = begin
81
+ if defined?(::Rails)
82
+ Rails.root.to_s
83
+ else
84
+ '.'
85
+ end
86
+ end
87
+
88
+ def path!(bucket, name, options = {}) #:nodoc:
89
+ # We're using the second argument for options
90
+ if bucket.is_a?(Hash)
91
+ options.replace(bucket)
92
+ bucket = nil
93
+ end
94
+ TEMP_PATH.clone << File.join('/tmp', 'mock-aws-s3', bucket_name(bucket), name)
95
+ end
96
+
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mock-aws-s3}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Joshua Krall"]
12
+ s.date = %q{2010-10-01}
13
+ s.description = %q{A Mock AWS::S3 so you can use it in your tests without hitting the network.}
14
+ s.email = %q{joshuakrall@pobox.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/mock-aws-s3.rb",
25
+ "lib/mock/aws/s3/object.rb",
26
+ "mock-aws-s3.gemspec",
27
+ "pkg/mock-aws-s3-0.1.0.gem",
28
+ "spec/mock-aws-s3_spec.rb",
29
+ "spec/spec.opts",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/jkrall/mock-aws-s3}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{A Mock AWS::S3 so you can use it in your tests without hitting the network}
37
+ s.test_files = [
38
+ "spec/mock-aws-s3_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ end
55
+
Binary file
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Mock::AWS::S3" do
4
+
5
+ describe 'when using S3Object' do
6
+ def create_test_file(key, bucket='bucket')
7
+ File.open("./tmp/mock-aws-s3/#{bucket}/#{key}", 'w') {|f| f.write 'test data'}
8
+ end
9
+ def remove_test_file(key, bucket='bucket')
10
+ File.unlink("./tmp/mock-aws-s3/#{bucket}/#{key}")
11
+ end
12
+
13
+ describe '#copy' do
14
+ before(:each) { create_test_file 'from_key' }
15
+ after(:each) do
16
+ remove_test_file 'from_key'
17
+ remove_test_file 'to_key'
18
+ end
19
+ it 'should not do an actual request' do
20
+ AWS::S3::S3Object.should_not_receive(:put)
21
+ AWS::S3::S3Object.copy 'from_key', 'to_key', 'bucket'
22
+ end
23
+ it 'should copy the file' do
24
+ AWS::S3::S3Object.copy 'from_key', 'to_key', 'bucket'
25
+ File.exists?('./tmp/mock-aws-s3/bucket/from_key').should be_true
26
+ File.exists?('./tmp/mock-aws-s3/bucket/to_key').should be_true
27
+ end
28
+ end
29
+ describe '#exists?' do
30
+ it 'should not do an actual request' do
31
+ AWS::S3::S3Object.should_not_receive(:head)
32
+ AWS::S3::S3Object.exists? 'key', 'bucket'
33
+ end
34
+ it 'should return true if the file exists' do
35
+ create_test_file 'key'
36
+ AWS::S3::S3Object.exists?('key', 'bucket').should be_true
37
+ remove_test_file 'key'
38
+ end
39
+ it 'should return false if the file does not exist' do
40
+ remove_test_file 'key' rescue nil
41
+ AWS::S3::S3Object.exists?('key', 'bucket').should be_false
42
+ end
43
+ end
44
+ describe '#delete' do
45
+ before(:each) { create_test_file 'key' }
46
+ it 'should not do an actual request' do
47
+ AWS::S3::Base.should_not_receive(:delete)
48
+ AWS::S3::S3Object.delete('key', 'bucket')
49
+ end
50
+ it 'should delete the file' do
51
+ AWS::S3::S3Object.delete('key', 'bucket')
52
+ File.exists?('./tmp/mock-aws-s3/bucket/key').should be_false
53
+ end
54
+ end
55
+ describe '#store' do
56
+ it 'should not do an actual request' do
57
+ AWS::S3::S3Object.should_not_receive(:put)
58
+ AWS::S3::S3Object.store 'key', 'some data', 'bucket'
59
+ end
60
+ it 'should store the data in the file' do
61
+ AWS::S3::S3Object.store 'key', 'some data', 'bucket'
62
+ File.open('./tmp/mock-aws-s3/bucket/key') do |f|
63
+ f.read.should == 'some data'
64
+ end
65
+ remove_test_file 'key'
66
+ end
67
+ end
68
+ describe '#value' do
69
+ before(:each) { create_test_file 'key' }
70
+ after(:each) { remove_test_file 'key' }
71
+ it 'should not do an actual request' do
72
+ AWS::S3::Base.should_not_receive(:get)
73
+ AWS::S3::S3Object.value('key', 'bucket')
74
+ end
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'mock-aws-s3'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mock-aws-s3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Joshua Krall
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-01 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: A Mock AWS::S3 so you can use it in your tests without hitting the network.
38
+ email: joshuakrall@pobox.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/mock-aws-s3.rb
52
+ - lib/mock/aws/s3/object.rb
53
+ - mock-aws-s3.gemspec
54
+ - pkg/mock-aws-s3-0.1.0.gem
55
+ - spec/mock-aws-s3_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/jkrall/mock-aws-s3
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: A Mock AWS::S3 so you can use it in your tests without hitting the network
92
+ test_files:
93
+ - spec/mock-aws-s3_spec.rb
94
+ - spec/spec_helper.rb