asset_cloud 2.7.0 → 2.7.2
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +42 -0
- data/.github/workflows/cla.yml +22 -0
- data/.rubocop.yml +3 -1
- data/Gemfile +5 -3
- data/History.md +8 -0
- data/README.rdoc +3 -4
- data/Rakefile +18 -16
- data/asset_cloud.gemspec +19 -18
- data/dev.yml +1 -1
- data/lib/asset_cloud/asset.rb +17 -13
- data/lib/asset_cloud/asset_extension.rb +27 -15
- data/lib/asset_cloud/base.rb +77 -72
- data/lib/asset_cloud/bucket.rb +5 -2
- data/lib/asset_cloud/buckets/active_record_bucket.rb +16 -14
- data/lib/asset_cloud/buckets/blackhole_bucket.rb +2 -0
- data/lib/asset_cloud/buckets/bucket_chain.rb +38 -31
- data/lib/asset_cloud/buckets/file_system_bucket.rb +14 -15
- data/lib/asset_cloud/buckets/gcs_bucket.rb +6 -8
- data/lib/asset_cloud/buckets/invalid_bucket.rb +9 -6
- data/lib/asset_cloud/buckets/memory_bucket.rb +7 -4
- data/lib/asset_cloud/buckets/s3_bucket.rb +11 -8
- data/lib/asset_cloud/buckets/versioned_memory_bucket.rb +4 -2
- data/lib/asset_cloud/callbacks.rb +9 -5
- data/lib/asset_cloud/free_key_locator.rb +6 -6
- data/lib/asset_cloud/metadata.rb +11 -7
- data/lib/asset_cloud/validations.rb +9 -5
- data/lib/asset_cloud.rb +23 -21
- data/spec/active_record_bucket_spec.rb +27 -26
- data/spec/asset_cloud/metadata_spec.rb +4 -2
- data/spec/asset_extension_spec.rb +17 -16
- data/spec/asset_spec.rb +27 -21
- data/spec/base_spec.rb +93 -92
- data/spec/blackhole_bucket_spec.rb +12 -11
- data/spec/bucket_chain_spec.rb +61 -56
- data/spec/bucket_spec.rb +6 -5
- data/spec/callbacks_spec.rb +52 -32
- data/spec/file_system_spec.rb +25 -24
- data/spec/find_free_key_spec.rb +16 -17
- data/spec/gcs_bucket_remote_spec.rb +23 -22
- data/spec/gcs_bucket_spec.rb +48 -60
- data/spec/memory_bucket_spec.rb +12 -11
- data/spec/mock_s3_interface.rb +17 -6
- data/spec/remote_s3_bucket_spec.rb +31 -28
- data/spec/s3_bucket_spec.rb +19 -17
- data/spec/spec_helper.rb +8 -7
- data/spec/validations_spec.rb +13 -12
- data/spec/versioned_memory_bucket_spec.rb +11 -10
- metadata +10 -33
- data/.github/probots.yml +0 -2
- data/.rubocop_todo.yml +0 -326
- data/.travis.yml +0 -12
data/spec/callbacks_spec.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
class AfterStoreCallback
|
6
|
+
class << self
|
7
|
+
def after_store(*args); end
|
8
|
+
end
|
9
|
+
end
|
3
10
|
|
4
11
|
class CallbackAsset < AssetCloud::Asset
|
5
12
|
before_store :callback_before_store
|
@@ -9,20 +16,22 @@ class CallbackAsset < AssetCloud::Asset
|
|
9
16
|
after_validate :add_spice
|
10
17
|
validate :valid_value
|
11
18
|
|
19
|
+
after_store ::AfterStoreCallback
|
20
|
+
|
12
21
|
private
|
13
22
|
|
14
23
|
def callback_before_delete(*args); end
|
15
24
|
|
16
25
|
def make_value_valid
|
17
|
-
self.value =
|
26
|
+
self.value = "valid"
|
18
27
|
end
|
19
28
|
|
20
29
|
def add_spice
|
21
|
-
self.value +=
|
30
|
+
self.value += " spice"
|
22
31
|
end
|
23
32
|
|
24
33
|
def valid_value
|
25
|
-
add_error('value is not "valid"') unless value ==
|
34
|
+
add_error('value is not "valid"') unless value == "valid"
|
26
35
|
end
|
27
36
|
end
|
28
37
|
|
@@ -52,89 +61,89 @@ class MethodRecordingCloud < AssetCloud::Base
|
|
52
61
|
before_write :callback_before_write
|
53
62
|
after_write :callback_before_write
|
54
63
|
|
55
|
-
def
|
56
|
-
@run_callbacks <<
|
64
|
+
def callback_before_write(*)
|
65
|
+
@run_callbacks << __method__
|
57
66
|
end
|
58
67
|
end
|
59
68
|
|
60
69
|
describe CallbackCloud do
|
61
70
|
before do
|
62
|
-
@fs = CallbackCloud.new(File.dirname(__FILE__) +
|
63
|
-
@fs.write(
|
71
|
+
@fs = CallbackCloud.new(File.dirname(__FILE__) + "/files", "http://assets/")
|
72
|
+
@fs.write("tmp/file.txt", "foo")
|
64
73
|
end
|
65
74
|
|
66
75
|
it "should invoke callbacks after store" do
|
67
|
-
expect(@fs).to(receive(:callback_before_write).with(
|
68
|
-
expect(@fs).to(receive(:callback_after_write).with(
|
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))
|
69
78
|
|
70
|
-
expect(@fs.write(
|
71
|
-
expect(@fs.read(
|
79
|
+
expect(@fs.write("tmp/file.txt", "text")).to(eq(true))
|
80
|
+
expect(@fs.read("tmp/file.txt")).to(eq("text"))
|
72
81
|
end
|
73
82
|
|
74
83
|
it "should invoke callbacks after delete" do
|
75
|
-
expect(@fs).to(receive(:callback_before_delete).with(
|
76
|
-
expect(@fs).to(receive(:callback_after_delete).with(
|
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))
|
77
86
|
|
78
|
-
expect(@fs.delete(
|
87
|
+
expect(@fs.delete("tmp/file.txt")).to(eq("foo"))
|
79
88
|
end
|
80
89
|
|
81
90
|
it "should not invoke other callbacks when a before_ filter returns false" do
|
82
91
|
expect(@fs).to(receive(:callback_before_delete)
|
83
|
-
.with(
|
92
|
+
.with("tmp/file.txt")
|
84
93
|
.and_return(false))
|
85
94
|
expect(@fs).not_to(receive(:callback_after_delete))
|
86
95
|
|
87
|
-
expect(@fs.delete(
|
96
|
+
expect(@fs.delete("tmp/file.txt")).to(eq(nil))
|
88
97
|
end
|
89
98
|
|
90
99
|
it "should invoke callbacks even when constructing a new asset" do
|
91
|
-
expect(@fs).to(receive(:callback_before_write).with(
|
92
|
-
expect(@fs).to(receive(:callback_after_write).with(
|
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))
|
93
102
|
|
94
|
-
asset = @fs.build(
|
95
|
-
asset.value =
|
103
|
+
asset = @fs.build("tmp/file.txt")
|
104
|
+
asset.value = "hello"
|
96
105
|
expect(asset.store).to(eq(true))
|
97
106
|
end
|
98
107
|
end
|
99
108
|
|
100
109
|
describe MethodRecordingCloud do
|
101
110
|
before do
|
102
|
-
@fs = MethodRecordingCloud.new(File.dirname(__FILE__) +
|
111
|
+
@fs = MethodRecordingCloud.new(File.dirname(__FILE__) + "/files", "http://assets/")
|
103
112
|
@fs.run_callbacks = []
|
104
113
|
end
|
105
114
|
|
106
|
-
it
|
107
|
-
@fs.write(
|
115
|
+
it "should record event when invoked" do
|
116
|
+
@fs.write("tmp/file.txt", "random data")
|
108
117
|
expect(@fs.run_callbacks).to(eq([:callback_before_write, :callback_before_write]))
|
109
118
|
end
|
110
119
|
|
111
|
-
it
|
112
|
-
@fs[
|
120
|
+
it "should record event when assignment operator is used" do
|
121
|
+
@fs["tmp/file.txt"] = "random data"
|
113
122
|
expect(@fs.run_callbacks).to(eq([:callback_before_write, :callback_before_write]))
|
114
123
|
end
|
115
124
|
end
|
116
125
|
|
117
126
|
describe CallbackAsset do
|
118
127
|
before(:each) do
|
119
|
-
@fs = BasicCloud.new(File.dirname(__FILE__) +
|
120
|
-
@fs.write(
|
121
|
-
@asset = @fs.asset_at(
|
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")
|
122
131
|
end
|
123
132
|
|
124
133
|
it "should run before_validate, then validate, then after validate, then before_store, then store" do
|
125
134
|
expect(@asset).to(receive(:callback_before_store).and_return(true))
|
126
135
|
expect(@asset).not_to(receive(:callback_after_delete))
|
127
136
|
|
128
|
-
@asset.value =
|
137
|
+
@asset.value = "foo"
|
129
138
|
expect(@asset.store).to(eq(true))
|
130
|
-
expect(@asset.value).to(eq(
|
139
|
+
expect(@asset.value).to(eq("valid spice"))
|
131
140
|
end
|
132
141
|
|
133
142
|
it "should run its after_delete callback after delete is called" do
|
134
143
|
expect(@asset).not_to(receive(:callback_before_store))
|
135
144
|
expect(@asset).to(receive(:callback_after_delete).and_return(true))
|
136
145
|
|
137
|
-
expect(@asset.delete).to(eq(
|
146
|
+
expect(@asset.delete).to(eq("bar"))
|
138
147
|
end
|
139
148
|
|
140
149
|
it "not invoke other callbacks when a before_ filter returns false" do
|
@@ -143,4 +152,15 @@ describe CallbackAsset do
|
|
143
152
|
|
144
153
|
expect(@asset.delete).to(eq(nil))
|
145
154
|
end
|
155
|
+
|
156
|
+
it "should invoke after_store callback defined in separate class" do
|
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
|
+
|
161
|
+
expect(local_asset).to(receive(:callback_before_store).and_return(true))
|
162
|
+
expect(AfterStoreCallback).to(receive(:after_store))
|
163
|
+
|
164
|
+
expect(local_asset.store).to(eq(true))
|
165
|
+
end
|
146
166
|
end
|
data/spec/file_system_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require
|
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__) +
|
13
|
+
directory = File.dirname(__FILE__) + "/files"
|
13
14
|
|
14
15
|
before do
|
15
|
-
@fs = FileSystemCloud.new(directory,
|
16
|
-
FileUtils.mkdir_p(directory +
|
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 +
|
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(
|
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(
|
29
|
-
expect(@fs.bucket_for(
|
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[
|
34
|
-
expect(@fs[
|
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(
|
39
|
-
expect(@fs.stat(
|
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(
|
44
|
+
expect(@fs.ls("products").collect(&:key)).to(eq(["products/key.txt"]))
|
44
45
|
end
|
45
46
|
|
46
|
-
describe
|
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(
|
49
|
+
expect(@fs.buckets[:tmp]).to(receive(:write).with("tmp/new_file.test", "hello world").and_return(true))
|
49
50
|
|
50
|
-
@fs.build(
|
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(
|
55
|
+
@fs.build("tmp/new_file.test", "hello world").store
|
55
56
|
|
56
|
-
expect(@fs.stat(
|
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[
|
61
|
+
@fs["tmp/new_file.test"] = "hello world"
|
61
62
|
|
62
|
-
expect(@fs.stat(
|
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(
|
67
|
+
@fs.build("tmp/new_file.test", "hello world").store
|
67
68
|
|
68
|
-
expect(@fs[
|
69
|
-
expect(@fs[
|
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
|
data/spec/find_free_key_spec.rb
CHANGED
@@ -1,44 +1,43 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
class FindFreeKey
|
6
6
|
extend AssetCloud::FreeKeyLocator
|
7
7
|
end
|
8
8
|
|
9
|
-
describe "FreeFilenameLocator",
|
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(
|
11
|
+
expect(FindFreeKey).to(receive(:exist?).with("free.txt").and_return(false))
|
12
12
|
|
13
|
-
expect(FindFreeKey.find_free_key_like(
|
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(
|
18
|
-
expect(FindFreeKey).to(receive(:exist?).with(
|
19
|
-
expect(FindFreeKey).to(receive(:exist?).with(
|
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(
|
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(
|
26
|
-
expect(FindFreeKey).to(receive(:exist?).with(
|
27
|
-
expect(FindFreeKey).to(receive(:exist?).with(
|
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(
|
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(
|
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(
|
39
|
-
|
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(
|
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
|
4
|
-
require
|
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[
|
12
|
+
gcs_connection.bucket(ENV["GCS_BUCKET"])
|
12
13
|
end
|
13
14
|
end
|
14
15
|
|
15
|
-
describe AssetCloud::GCSBucket, if: ENV[
|
16
|
-
directory = File.dirname(__FILE__) +
|
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,
|
20
|
+
@cloud = RemoteGCSCloud.new(directory, "assets/files")
|
20
21
|
|
21
22
|
@cloud.gcs_connection = Google::Cloud::Storage.new(
|
22
|
-
project_id: ENV[
|
23
|
-
credentials: ENV[
|
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 =
|
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 =
|
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 =
|
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 =
|
70
|
-
acl =
|
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 =
|
79
|
-
content_disposition =
|
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 =
|
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 =
|
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 =
|
102
|
-
key =
|
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 =
|
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 =
|
118
|
-
key =
|
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)
|
data/spec/gcs_bucket_spec.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require
|
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__) +
|
26
|
+
directory = File.dirname(__FILE__) + "/files"
|
21
27
|
|
22
28
|
before(:all) do
|
23
|
-
@cloud = GCSCloud.new(directory,
|
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 =
|
35
|
-
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}")
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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 =
|
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 =
|
84
|
-
acl =
|
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 =
|
97
|
-
content_disposition =
|
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 =
|
109
|
-
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}")
|
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 =
|
119
|
-
key =
|
120
|
-
expect_any_instance_of(MockGCSBucket).to(receive(:file).with("s#{@cloud.url}/#{key}")
|
121
|
-
|
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 =
|
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
|
-
|
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}")
|
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))
|