asset_cloud 2.5.1 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/.rubocop_todo.yml +326 -0
- data/.travis.yml +6 -0
- data/History.md +10 -0
- data/README.rdoc +20 -0
- data/Rakefile +5 -2
- data/asset_cloud.gemspec +2 -3
- data/lib/asset_cloud/asset.rb +4 -0
- data/lib/asset_cloud/base.rb +11 -1
- data/lib/asset_cloud/buckets/gcs_bucket.rb +1 -1
- data/lib/asset_cloud/metadata.rb +10 -3
- data/spec/active_record_bucket_spec.rb +27 -27
- data/spec/asset_cloud/metadata_spec.rb +31 -0
- data/spec/asset_extension_spec.rb +21 -18
- data/spec/asset_spec.rb +47 -56
- data/spec/base_spec.rb +81 -62
- data/spec/blackhole_bucket_spec.rb +6 -5
- data/spec/bucket_chain_spec.rb +50 -50
- data/spec/bucket_spec.rb +5 -4
- data/spec/callbacks_spec.rb +31 -31
- data/spec/file_system_spec.rb +15 -16
- data/spec/find_free_key_spec.rb +17 -19
- data/spec/gcs_bucket_remote_spec.rb +18 -19
- data/spec/gcs_bucket_spec.rb +50 -36
- data/spec/memory_bucket_spec.rb +8 -9
- data/spec/mock_s3_interface.rb +4 -3
- data/spec/remote_s3_bucket_spec.rb +17 -16
- data/spec/s3_bucket_spec.rb +12 -12
- data/spec/spec_helper.rb +2 -1
- data/spec/validations_spec.rb +18 -16
- data/spec/versioned_memory_bucket_spec.rb +6 -6
- metadata +9 -19
data/spec/bucket_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe AssetCloud::Bucket do
|
@@ -7,19 +8,19 @@ describe AssetCloud::Bucket do
|
|
7
8
|
|
8
9
|
describe "operations not supported" do
|
9
10
|
it "#ls not supported" do
|
10
|
-
expect { @bucket.ls('foo')}.to
|
11
|
+
expect { @bucket.ls('foo') }.to(raise_error(NotImplementedError))
|
11
12
|
end
|
12
13
|
|
13
14
|
it "#read(key) not supported" do
|
14
|
-
expect { @bucket.read('foo')}.to
|
15
|
+
expect { @bucket.read('foo') }.to(raise_error(NotImplementedError))
|
15
16
|
end
|
16
17
|
|
17
18
|
it "#write(key, data) not supported" do
|
18
|
-
expect { @bucket.write('foo', 'bar')}.to
|
19
|
+
expect { @bucket.write('foo', 'bar') }.to(raise_error(NotImplementedError))
|
19
20
|
end
|
20
21
|
|
21
22
|
it "#delete(key) not supported" do
|
22
|
-
expect { @bucket.delete('foo')}.to
|
23
|
+
expect { @bucket.delete('foo') }.to(raise_error(NotImplementedError))
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
data/spec/callbacks_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
class CallbackAsset < AssetCloud::Asset
|
@@ -21,12 +22,12 @@ class CallbackAsset < AssetCloud::Asset
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def valid_value
|
24
|
-
add_error
|
25
|
+
add_error('value is not "valid"') unless value == 'valid'
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
29
|
class BasicCloud < AssetCloud::Base
|
29
|
-
bucket :callback_assets, AssetCloud::MemoryBucket, :
|
30
|
+
bucket :callback_assets, AssetCloud::MemoryBucket, asset_class: CallbackAsset
|
30
31
|
end
|
31
32
|
|
32
33
|
class CallbackCloud < AssetCloud::Base
|
@@ -39,6 +40,7 @@ class CallbackCloud < AssetCloud::Base
|
|
39
40
|
before_write :callback_before_write
|
40
41
|
|
41
42
|
def callback_before_write(*args); end
|
43
|
+
|
42
44
|
def callback_after_write(*args); end
|
43
45
|
end
|
44
46
|
|
@@ -50,7 +52,7 @@ class MethodRecordingCloud < AssetCloud::Base
|
|
50
52
|
before_write :callback_before_write
|
51
53
|
after_write :callback_before_write
|
52
54
|
|
53
|
-
def method_missing(method, *
|
55
|
+
def method_missing(method, *_args)
|
54
56
|
@run_callbacks << method.to_sym
|
55
57
|
end
|
56
58
|
end
|
@@ -62,38 +64,36 @@ describe CallbackCloud do
|
|
62
64
|
end
|
63
65
|
|
64
66
|
it "should invoke callbacks after store" do
|
65
|
-
@fs.
|
66
|
-
@fs.
|
67
|
-
|
67
|
+
expect(@fs).to(receive(:callback_before_write).with('tmp/file.txt', 'text').and_return(true))
|
68
|
+
expect(@fs).to(receive(:callback_after_write).with('tmp/file.txt', 'text').and_return(true))
|
68
69
|
|
69
|
-
@fs.write('tmp/file.txt', 'text').
|
70
|
-
@fs.read('tmp/file.txt').
|
70
|
+
expect(@fs.write('tmp/file.txt', 'text')).to(eq(true))
|
71
|
+
expect(@fs.read('tmp/file.txt')).to(eq('text'))
|
71
72
|
end
|
72
73
|
|
73
74
|
it "should invoke callbacks after delete" do
|
74
|
-
@fs.
|
75
|
-
@fs.
|
75
|
+
expect(@fs).to(receive(:callback_before_delete).with('tmp/file.txt').and_return(true))
|
76
|
+
expect(@fs).to(receive(:callback_after_delete).with('tmp/file.txt').and_return(true))
|
76
77
|
|
77
|
-
@fs.delete('tmp/file.txt').
|
78
|
+
expect(@fs.delete('tmp/file.txt')).to(eq('foo'))
|
78
79
|
end
|
79
80
|
|
80
81
|
it "should not invoke other callbacks when a before_ filter returns false" do
|
81
|
-
@fs.
|
82
|
+
expect(@fs).to(receive(:callback_before_delete)
|
82
83
|
.with('tmp/file.txt')
|
83
|
-
.and_return(false)
|
84
|
-
@fs.
|
84
|
+
.and_return(false))
|
85
|
+
expect(@fs).not_to(receive(:callback_after_delete))
|
85
86
|
|
86
|
-
@fs.delete('tmp/file.txt').
|
87
|
+
expect(@fs.delete('tmp/file.txt')).to(eq(nil))
|
87
88
|
end
|
88
89
|
|
89
90
|
it "should invoke callbacks even when constructing a new asset" do
|
90
|
-
@fs.
|
91
|
-
@fs.
|
92
|
-
|
91
|
+
expect(@fs).to(receive(:callback_before_write).with('tmp/file.txt', 'hello').and_return(true))
|
92
|
+
expect(@fs).to(receive(:callback_after_write).with('tmp/file.txt', 'hello').and_return(true))
|
93
93
|
|
94
94
|
asset = @fs.build('tmp/file.txt')
|
95
95
|
asset.value = 'hello'
|
96
|
-
asset.store.
|
96
|
+
expect(asset.store).to(eq(true))
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -105,12 +105,12 @@ describe MethodRecordingCloud do
|
|
105
105
|
|
106
106
|
it 'should record event when invoked' do
|
107
107
|
@fs.write('tmp/file.txt', 'random data')
|
108
|
-
@fs.run_callbacks.
|
108
|
+
expect(@fs.run_callbacks).to(eq([:callback_before_write, :callback_before_write]))
|
109
109
|
end
|
110
110
|
|
111
111
|
it 'should record event when assignment operator is used' do
|
112
112
|
@fs['tmp/file.txt'] = 'random data'
|
113
|
-
@fs.run_callbacks.
|
113
|
+
expect(@fs.run_callbacks).to(eq([:callback_before_write, :callback_before_write]))
|
114
114
|
end
|
115
115
|
end
|
116
116
|
|
@@ -122,25 +122,25 @@ describe CallbackAsset do
|
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should run before_validate, then validate, then after validate, then before_store, then store" do
|
125
|
-
@asset.
|
126
|
-
@asset.
|
125
|
+
expect(@asset).to(receive(:callback_before_store).and_return(true))
|
126
|
+
expect(@asset).not_to(receive(:callback_after_delete))
|
127
127
|
|
128
128
|
@asset.value = 'foo'
|
129
|
-
@asset.store.
|
130
|
-
@asset.value.
|
129
|
+
expect(@asset.store).to(eq(true))
|
130
|
+
expect(@asset.value).to(eq('valid spice'))
|
131
131
|
end
|
132
132
|
|
133
133
|
it "should run its after_delete callback after delete is called" do
|
134
|
-
@asset.
|
135
|
-
@asset.
|
134
|
+
expect(@asset).not_to(receive(:callback_before_store))
|
135
|
+
expect(@asset).to(receive(:callback_after_delete).and_return(true))
|
136
136
|
|
137
|
-
@asset.delete.
|
137
|
+
expect(@asset.delete).to(eq('bar'))
|
138
138
|
end
|
139
139
|
|
140
140
|
it "not invoke other callbacks when a before_ filter returns false" do
|
141
|
-
@asset.
|
142
|
-
@asset.
|
141
|
+
expect(@asset).to(receive(:callback_before_delete).and_return(false))
|
142
|
+
expect(@asset).not_to(receive(:callback_after_delete))
|
143
143
|
|
144
|
-
@asset.delete.
|
144
|
+
expect(@asset.delete).to(eq(nil))
|
145
145
|
end
|
146
146
|
end
|
data/spec/file_system_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'fileutils'
|
3
4
|
|
@@ -7,12 +8,11 @@ class FileSystemCloud < AssetCloud::Base
|
|
7
8
|
bucket :tmp, AssetCloud::FileSystemBucket
|
8
9
|
end
|
9
10
|
|
10
|
-
|
11
11
|
describe FileSystemCloud do
|
12
12
|
directory = File.dirname(__FILE__) + '/files'
|
13
13
|
|
14
14
|
before do
|
15
|
-
@fs = FileSystemCloud.new(directory
|
15
|
+
@fs = FileSystemCloud.new(directory, 'http://assets/files')
|
16
16
|
FileUtils.mkdir_p(directory + '/tmp')
|
17
17
|
end
|
18
18
|
|
@@ -21,31 +21,31 @@ describe FileSystemCloud do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should use invalid bucket for random directories" do
|
24
|
-
@fs.bucket_for('does-not-exist/file.txt').
|
24
|
+
expect(@fs.bucket_for('does-not-exist/file.txt')).to(be_an_instance_of(AssetCloud::InvalidBucket))
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should use filesystem bucket for products/ and tmp/ directories" do
|
28
|
-
@fs.bucket_for('products/file.txt').
|
29
|
-
@fs.bucket_for('tmp/file.txt').
|
28
|
+
expect(@fs.bucket_for('products/file.txt')).to(be_an_instance_of(AssetCloud::FileSystemBucket))
|
29
|
+
expect(@fs.bucket_for('tmp/file.txt')).to(be_an_instance_of(AssetCloud::FileSystemBucket))
|
30
30
|
end
|
31
31
|
|
32
32
|
it "should return Asset for existing files" do
|
33
|
-
@fs['products/key.txt'].exist
|
34
|
-
@fs['products/key.txt'].
|
33
|
+
expect(@fs['products/key.txt'].exist?).to(eq(true))
|
34
|
+
expect(@fs['products/key.txt']).to(be_an_instance_of(AssetCloud::Asset))
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should be able to test if a file exists or not" do
|
38
|
-
@fs.stat('products/key.txt').exist
|
39
|
-
@fs.stat('products/key2.txt').exist
|
38
|
+
expect(@fs.stat('products/key.txt').exist?).to(eq(true))
|
39
|
+
expect(@fs.stat('products/key2.txt').exist?).to(eq(false))
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should be able to list files" do
|
43
|
-
@fs.ls('products').collect(&:key).
|
43
|
+
expect(@fs.ls('products').collect(&:key)).to(eq(['products/key.txt']))
|
44
44
|
end
|
45
45
|
|
46
46
|
describe 'when modifying file system' do
|
47
47
|
it "should call write after storing an asset" do
|
48
|
-
@fs.buckets[:tmp].
|
48
|
+
expect(@fs.buckets[:tmp]).to(receive(:write).with('tmp/new_file.test', 'hello world').and_return(true))
|
49
49
|
|
50
50
|
@fs.build('tmp/new_file.test', 'hello world').store
|
51
51
|
end
|
@@ -53,21 +53,20 @@ describe FileSystemCloud do
|
|
53
53
|
it "should be able to create new files" do
|
54
54
|
@fs.build('tmp/new_file.test', 'hello world').store
|
55
55
|
|
56
|
-
@fs.stat('tmp/new_file.test').exist.
|
56
|
+
expect(@fs.stat('tmp/new_file.test').exist).to(eq(true))
|
57
57
|
end
|
58
58
|
|
59
59
|
it "should be able to create new files with simple assignment" do
|
60
60
|
@fs['tmp/new_file.test'] = 'hello world'
|
61
61
|
|
62
|
-
@fs.stat('tmp/new_file.test').exist.
|
62
|
+
expect(@fs.stat('tmp/new_file.test').exist).to(eq(true))
|
63
63
|
end
|
64
64
|
|
65
65
|
it "should create directories as needed" do
|
66
66
|
@fs.build('tmp/new_file.test', 'hello world').store
|
67
67
|
|
68
|
-
@fs['tmp/new_file.test'].exist
|
69
|
-
@fs['tmp/new_file.test'].value.
|
68
|
+
expect(@fs['tmp/new_file.test'].exist?).to(eq(true))
|
69
|
+
expect(@fs['tmp/new_file.test'].value).to(eq('hello world'))
|
70
70
|
end
|
71
|
-
|
72
71
|
end
|
73
72
|
end
|
data/spec/find_free_key_spec.rb
CHANGED
@@ -1,46 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
require 'spec_helper'
|
3
4
|
|
4
|
-
|
5
5
|
class FindFreeKey
|
6
6
|
extend AssetCloud::FreeKeyLocator
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "FreeFilenameLocator", 'when asked to return a free key such as the one passed in' do
|
10
|
-
|
11
10
|
it "should simply return the key if it happens to be free" do
|
12
|
-
FindFreeKey.
|
11
|
+
expect(FindFreeKey).to(receive(:exist?).with('free.txt').and_return(false))
|
13
12
|
|
14
|
-
FindFreeKey.find_free_key_like('free.txt').
|
13
|
+
expect(FindFreeKey.find_free_key_like('free.txt')).to(eq('free.txt'))
|
15
14
|
end
|
16
15
|
|
17
16
|
it "should append a UUID to the key before the extension if key is taken" do
|
18
|
-
SecureRandom.
|
19
|
-
FindFreeKey.
|
20
|
-
FindFreeKey.
|
17
|
+
allow(SecureRandom).to(receive(:uuid).and_return('moo'))
|
18
|
+
expect(FindFreeKey).to(receive(:exist?).with('free.txt').and_return(true))
|
19
|
+
expect(FindFreeKey).to(receive(:exist?).with('free_moo.txt').and_return(false))
|
21
20
|
|
22
|
-
FindFreeKey.find_free_key_like('free.txt').
|
21
|
+
expect(FindFreeKey.find_free_key_like('free.txt')).to(eq('free_moo.txt'))
|
23
22
|
end
|
24
23
|
|
25
|
-
|
26
24
|
it "should not strip any directory information from the key" do
|
27
|
-
SecureRandom.
|
28
|
-
FindFreeKey.
|
29
|
-
FindFreeKey.
|
25
|
+
allow(SecureRandom).to(receive(:uuid).and_return('moo'))
|
26
|
+
expect(FindFreeKey).to(receive(:exist?).with('products/images/image.gif').and_return(true))
|
27
|
+
expect(FindFreeKey).to(receive(:exist?).with('products/images/image_moo.gif').and_return(false))
|
30
28
|
|
31
|
-
FindFreeKey.find_free_key_like('products/images/image.gif').
|
29
|
+
expect(FindFreeKey.find_free_key_like('products/images/image.gif')).to(eq('products/images/image_moo.gif'))
|
32
30
|
end
|
33
31
|
|
34
32
|
it "should raise an exception if the randomly chosen value (after 10 attempts) is also taken" do
|
35
|
-
FindFreeKey.
|
36
|
-
|
33
|
+
allow(FindFreeKey).to(receive(:exist?).and_return(true))
|
34
|
+
expect { FindFreeKey.find_free_key_like('free.txt') }.to(raise_error(StandardError))
|
37
35
|
end
|
38
36
|
|
39
37
|
it "should append a UUID to the key before the extensions if the force_uuid option is passed" do
|
40
|
-
FindFreeKey.
|
41
|
-
FindFreeKey.
|
42
|
-
SecureRandom.
|
38
|
+
expect(FindFreeKey).to(receive(:exist?).with('free.txt').and_return(false))
|
39
|
+
expect(FindFreeKey).to(receive(:exist?).with('free_as-in-beer.txt').and_return(false))
|
40
|
+
allow(SecureRandom).to(receive(:uuid).and_return('as-in-beer'))
|
43
41
|
|
44
|
-
FindFreeKey.find_free_key_like('free.txt', :
|
42
|
+
expect(FindFreeKey.find_free_key_like('free.txt', force_uuid: true)).to(eq('free_as-in-beer.txt'))
|
45
43
|
end
|
46
44
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
# rubocop:disable RSpec/FilePath, Lint/MissingCopEnableDirective
|
3
2
|
|
4
3
|
require 'spec_helper'
|
5
4
|
require 'google/cloud/storage'
|
@@ -20,9 +19,9 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
20
19
|
@cloud = RemoteGCSCloud.new(directory, 'assets/files')
|
21
20
|
|
22
21
|
@cloud.gcs_connection = Google::Cloud::Storage.new(
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
project_id: ENV['GCS_PROJECT_ID'],
|
23
|
+
credentials: ENV['GCS_KEY_FILEPATH']
|
24
|
+
)
|
26
25
|
@bucket = @cloud.buckets[:tmp]
|
27
26
|
end
|
28
27
|
|
@@ -31,10 +30,10 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
31
30
|
end
|
32
31
|
|
33
32
|
it "#ls with no arguments returns all files in the bucket" do
|
34
|
-
expect_any_instance_of(Google::Cloud::Storage::Bucket).to
|
33
|
+
expect_any_instance_of(Google::Cloud::Storage::Bucket).to(receive(:files).with(no_args))
|
35
34
|
expect do
|
36
35
|
@bucket.ls
|
37
|
-
end.not_to
|
36
|
+
end.not_to(raise_error)
|
38
37
|
end
|
39
38
|
|
40
39
|
it "#ls with arguments returns the file" do
|
@@ -44,7 +43,7 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
44
43
|
@bucket.write(key, local_path)
|
45
44
|
|
46
45
|
file = @bucket.ls(key)
|
47
|
-
expect(file.name).to
|
46
|
+
expect(file.name).to(eq("s#{@cloud.url}/#{key}"))
|
48
47
|
end
|
49
48
|
|
50
49
|
it "#write writes a file into the bucket" do
|
@@ -58,11 +57,11 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
58
57
|
local_path = "#{directory}/products/key.txt"
|
59
58
|
key = 'test/key.txt'
|
60
59
|
metadata = {
|
61
|
-
"X-Robots-Tag" => "none"
|
60
|
+
"X-Robots-Tag" => "none",
|
62
61
|
}
|
63
62
|
|
64
63
|
file = @bucket.write(key, local_path, metadata: metadata)
|
65
|
-
expect(file.metadata).to
|
64
|
+
expect(file.metadata).to(eq(metadata))
|
66
65
|
end
|
67
66
|
|
68
67
|
it "#write writes a file into the bucket with acl" do
|
@@ -71,7 +70,7 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
71
70
|
acl = 'public'
|
72
71
|
|
73
72
|
file = @bucket.write(key, local_path, acl: acl)
|
74
|
-
expect(file.acl).to
|
73
|
+
expect(file.acl).to(be_truthy)
|
75
74
|
end
|
76
75
|
|
77
76
|
it "#write writes a file into the bucket with content_disposition" do
|
@@ -80,7 +79,7 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
80
79
|
content_disposition = 'attachment'
|
81
80
|
|
82
81
|
file = @bucket.write(key, local_path, content_disposition: content_disposition)
|
83
|
-
expect(file.content_disposition).to
|
82
|
+
expect(file.content_disposition).to(eq(content_disposition))
|
84
83
|
end
|
85
84
|
|
86
85
|
it "#delete removes the file from the bucket" do
|
@@ -88,14 +87,14 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
88
87
|
|
89
88
|
expect do
|
90
89
|
@bucket.delete(key)
|
91
|
-
end.not_to
|
90
|
+
end.not_to(raise_error)
|
92
91
|
end
|
93
92
|
|
94
93
|
it "#delete raises AssetCloud::AssetNotFoundError if the file is not found" do
|
95
94
|
key = 'tmp/not_found.txt'
|
96
95
|
expect do
|
97
96
|
@bucket.delete(key)
|
98
|
-
end.to
|
97
|
+
end.to(raise_error(AssetCloud::AssetNotFoundError))
|
99
98
|
end
|
100
99
|
|
101
100
|
it "#read returns the data of the file" do
|
@@ -104,14 +103,14 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
104
103
|
@bucket.write(key, StringIO.new(value))
|
105
104
|
|
106
105
|
data = @bucket.read(key)
|
107
|
-
data.
|
106
|
+
expect(data).to(eq(value))
|
108
107
|
end
|
109
108
|
|
110
109
|
it "#read raises AssetCloud::AssetNotFoundError if the file is not found" do
|
111
110
|
key = 'tmp/not_found.txt'
|
112
111
|
expect do
|
113
112
|
@bucket.read(key)
|
114
|
-
end.to
|
113
|
+
end.to(raise_error(AssetCloud::AssetNotFoundError))
|
115
114
|
end
|
116
115
|
|
117
116
|
it "#stats returns metadata of the asset" do
|
@@ -121,8 +120,8 @@ describe AssetCloud::GCSBucket, if: ENV['GCS_PROJECT_ID'] && ENV['GCS_KEY_FILEPA
|
|
121
120
|
|
122
121
|
stats = @bucket.stat(key)
|
123
122
|
|
124
|
-
expect(stats.size).not_to
|
125
|
-
expect(stats.created_at).not_to
|
126
|
-
expect(stats.updated_at).not_to
|
123
|
+
expect(stats.size).not_to(be_nil)
|
124
|
+
expect(stats.created_at).not_to(be_nil)
|
125
|
+
expect(stats.updated_at).not_to(be_nil)
|
127
126
|
end
|
128
|
-
end
|
127
|
+
end
|
data/spec/gcs_bucket_spec.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
3
|
require 'google/cloud/storage'
|
3
4
|
|
@@ -24,42 +25,55 @@ describe AssetCloud::GCSBucket do
|
|
24
25
|
end
|
25
26
|
|
26
27
|
it "#ls with no arguments returns all files in the bucket" do
|
27
|
-
expect_any_instance_of(GCSCloud).to
|
28
|
-
expect_any_instance_of(MockGCSBucket).to
|
28
|
+
expect_any_instance_of(GCSCloud).to(receive(:gcs_bucket).and_return(@bucket))
|
29
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:files).with(no_args).and_return(nil))
|
29
30
|
@bucket.ls
|
30
31
|
end
|
31
32
|
|
32
33
|
it "#ls with arguments returns the file" do
|
33
34
|
key = 'test/ls.txt'
|
34
|
-
expect_any_instance_of(MockGCSBucket).to
|
35
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}").and_return(Google::Cloud::Storage::File.new))
|
35
36
|
|
36
37
|
file = @bucket.ls(key)
|
37
|
-
expect(file.class).to
|
38
|
+
expect(file.class).to(eq(Google::Cloud::Storage::File))
|
38
39
|
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
41
|
+
if RUBY_VERSION >= '2.7'
|
42
|
+
it "#write writes a file into the bucket" do
|
43
|
+
local_path = "#{directory}/products/key.txt"
|
44
|
+
key = 'test/key.txt'
|
45
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:create_file).with(
|
46
|
+
local_path,
|
47
|
+
"s#{@cloud.url}/#{key}",
|
48
|
+
))
|
49
|
+
|
50
|
+
@bucket.write(key, local_path)
|
51
|
+
end
|
52
|
+
else
|
53
|
+
it "#write writes a file into the bucket" do
|
54
|
+
local_path = "#{directory}/products/key.txt"
|
55
|
+
key = 'test/key.txt'
|
56
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:create_file).with(
|
57
|
+
local_path,
|
58
|
+
"s#{@cloud.url}/#{key}",
|
59
|
+
{}
|
60
|
+
))
|
61
|
+
|
62
|
+
@bucket.write(key, local_path)
|
63
|
+
end
|
50
64
|
end
|
51
65
|
|
52
66
|
it "#write writes a file into the bucket with metadata" do
|
53
67
|
local_path = "#{directory}/products/key.txt"
|
54
68
|
key = 'test/key.txt'
|
55
69
|
metadata = {
|
56
|
-
"X-Robots-Tag" => "none"
|
70
|
+
"X-Robots-Tag" => "none",
|
57
71
|
}
|
58
|
-
expect_any_instance_of(MockGCSBucket).to
|
72
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:create_file).with(
|
59
73
|
local_path,
|
60
74
|
"s#{@cloud.url}/#{key}",
|
61
75
|
metadata: metadata
|
62
|
-
)
|
76
|
+
))
|
63
77
|
|
64
78
|
@bucket.write(key, local_path, metadata: metadata)
|
65
79
|
end
|
@@ -68,11 +82,11 @@ describe AssetCloud::GCSBucket do
|
|
68
82
|
local_path = "#{directory}/products/key.txt"
|
69
83
|
key = 'test/key.txt'
|
70
84
|
acl = 'public'
|
71
|
-
expect_any_instance_of(MockGCSBucket).to
|
85
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:create_file).with(
|
72
86
|
local_path,
|
73
87
|
"s#{@cloud.url}/#{key}",
|
74
88
|
acl: acl
|
75
|
-
)
|
89
|
+
))
|
76
90
|
|
77
91
|
@bucket.write(key, local_path, acl: acl)
|
78
92
|
end
|
@@ -81,41 +95,41 @@ describe AssetCloud::GCSBucket do
|
|
81
95
|
local_path = "#{directory}/products/key.txt"
|
82
96
|
key = 'test/key.txt'
|
83
97
|
content_disposition = 'attachment'
|
84
|
-
expect_any_instance_of(MockGCSBucket).to
|
98
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:create_file).with(
|
85
99
|
local_path,
|
86
100
|
"s#{@cloud.url}/#{key}",
|
87
101
|
content_disposition: content_disposition
|
88
|
-
)
|
102
|
+
))
|
89
103
|
|
90
104
|
@bucket.write(key, local_path, content_disposition: content_disposition)
|
91
105
|
end
|
92
106
|
|
93
107
|
it "#delete removes the file from the bucket" do
|
94
108
|
key = 'test/key.txt'
|
95
|
-
expect_any_instance_of(MockGCSBucket).to
|
96
|
-
expect_any_instance_of(Google::Cloud::Storage::File).to
|
109
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}").and_return(Google::Cloud::Storage::File.new))
|
110
|
+
expect_any_instance_of(Google::Cloud::Storage::File).to(receive(:delete).with(no_args))
|
97
111
|
|
98
112
|
expect do
|
99
113
|
@bucket.delete(key)
|
100
|
-
end.not_to
|
114
|
+
end.not_to(raise_error)
|
101
115
|
end
|
102
116
|
|
103
117
|
it "#read returns the data of the file" do
|
104
118
|
value = 'hello world'
|
105
119
|
key = 'tmp/new_file.txt'
|
106
|
-
expect_any_instance_of(MockGCSBucket).to
|
107
|
-
expect_any_instance_of(Google::Cloud::Storage::File).to
|
120
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}").and_return(Google::Cloud::Storage::File.new))
|
121
|
+
expect_any_instance_of(Google::Cloud::Storage::File).to(receive(:download).and_return(StringIO.new(value)))
|
108
122
|
|
109
123
|
data = @bucket.read(key)
|
110
|
-
data.
|
124
|
+
expect(data).to(eq(value))
|
111
125
|
end
|
112
126
|
|
113
127
|
it "#read raises AssetCloud::AssetNotFoundError if the file is not found" do
|
114
128
|
key = 'tmp/not_found.txt'
|
115
|
-
expect_any_instance_of(MockGCSBucket).to
|
129
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}").and_return(nil))
|
116
130
|
expect do
|
117
131
|
@bucket.read(key)
|
118
|
-
end.to
|
132
|
+
end.to(raise_error(AssetCloud::AssetNotFoundError))
|
119
133
|
end
|
120
134
|
|
121
135
|
it "#stat returns information on the asset" do
|
@@ -124,14 +138,14 @@ describe AssetCloud::GCSBucket do
|
|
124
138
|
expected_time = Time.now
|
125
139
|
expected_size = 1
|
126
140
|
|
127
|
-
expect_any_instance_of(MockGCSBucket).to
|
128
|
-
expect_any_instance_of(Google::Cloud::Storage::File).to
|
129
|
-
expect_any_instance_of(Google::Cloud::Storage::File).to
|
130
|
-
expect_any_instance_of(Google::Cloud::Storage::File).to
|
141
|
+
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}").and_return(Google::Cloud::Storage::File.new))
|
142
|
+
expect_any_instance_of(Google::Cloud::Storage::File).to(receive(:size).and_return(expected_size))
|
143
|
+
expect_any_instance_of(Google::Cloud::Storage::File).to(receive(:created_at).and_return(expected_time))
|
144
|
+
expect_any_instance_of(Google::Cloud::Storage::File).to(receive(:updated_at).and_return(expected_time))
|
131
145
|
|
132
146
|
stats = @bucket.stat(key)
|
133
|
-
expect(stats.size).to
|
134
|
-
expect(stats.created_at).to
|
135
|
-
expect(stats.updated_at).to
|
147
|
+
expect(stats.size).to(eq(expected_size))
|
148
|
+
expect(stats.created_at).to(eq(expected_time))
|
149
|
+
expect(stats.updated_at).to(eq(expected_time))
|
136
150
|
end
|
137
151
|
end
|