asset_cloud 2.7.1 → 2.7.2

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