Skip to content

Commit 5610731

Browse files
committed
Fix tests
1 parent 9403ad6 commit 5610731

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/fastlane/plugin/wpmreleasetoolkit/actions/common/upload_to_s3.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ def self.available_options
7878
),
7979
FastlaneCore::ConfigItem.new(
8080
key: :key,
81-
description: 'The path to the file within the bucket. If `nil`, will default to the `file`'s basename',
81+
description: 'The path to the file within the bucket. If `nil`, will default to the `file\'s basename',
8282
optional: true,
8383
type: String,
84-
verify_block: proc { |key| UI.user_error!('The provided key must not be empty. Use nil instead if you want to default to the file basename') if key.is_a?(String) && key.empty? }
84+
verify_block: proc { |key|
85+
next if key.is_a?(String) && !key.empty?
86+
UI.user_error!('The provided key must not be empty. Use nil instead if you want to default to the file basename')
87+
}
8588
),
8689
FastlaneCore::ConfigItem.new(
8790
key: :file,

spec/upload_to_s3_spec.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
end
1010

1111
# Stub head_object to return a specific content_length
12-
def stub_s3_file_exists(key, exists)
13-
content_length = exists ? 1 : 0
14-
allow(client).to receive(:head_object)
12+
def stub_s3_response_for_file(key, exists = true)
13+
content_length = exists == true ? 1 : 0
14+
allow(client).to(receive(:head_object))
1515
.with(bucket: test_bucket, key: key)
1616
.and_return(Aws::S3::Types::HeadObjectOutput.new(content_length: content_length))
1717
end
1818

1919
describe 'uploading a file' do
2020
it 'generates a prefix for the key by default' do
2121
expected_key = '939c39398db2405e791e205778ff70f85dff620e/a8c-key1'
22-
stub_s3_file_does_not_exist(expected_key)
22+
stub_s3_response_for_file(expected_key, exists: false)
2323

2424
with_tmp_file_path_for_file_named('input_file_1') do |file_path|
2525
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
@@ -37,7 +37,7 @@ def stub_s3_file_exists(key, exists)
3737

3838
it 'generates a prefix for the key when using auto_prefix:true' do
3939
expected_key = '8bde1a7a04300df27b52f4383dc997e5fbbff180/a8c-key2'
40-
stub_s3_file_does_not_exist(expected_key)
40+
stub_s3_response_for_file(expected_key, exists: false)
4141

4242
with_tmp_file_path_for_file_named('input_file_2') do |file_path|
4343
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
@@ -56,7 +56,7 @@ def stub_s3_file_exists(key, exists)
5656

5757
it 'uses the provided key verbatim when using auto_prefix:false' do
5858
expected_key = 'a8c-key1'
59-
stub_s3_file_does_not_exist(expected_key)
59+
stub_s3_response_for_file(expected_key, exists: false)
6060

6161
with_tmp_file_path do |file_path|
6262
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
@@ -75,7 +75,7 @@ def stub_s3_file_exists(key, exists)
7575

7676
it 'correctly appends the key if it contains subdirectories' do
7777
expected_key = '939c39398db2405e791e205778ff70f85dff620e/subdir/a8c-key1'
78-
stub_s3_file_does_not_exist(expected_key)
78+
stub_s3_response_for_file(expected_key, exists: false)
7979

8080
with_tmp_file_path_for_file_named('input_file_1') do |file_path|
8181
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
@@ -93,7 +93,7 @@ def stub_s3_file_exists(key, exists)
9393

9494
it 'uses the filename as the key if one is not provided' do
9595
expected_key = 'c125bd799c6aad31092b02e440a8fae25b45a2ad/test_file_1'
96-
stub_s3_file_does_not_exist(expected_key)
96+
stub_s3_response_for_file(expected_key, exists: false)
9797

9898
with_tmp_file_path_for_file_named('test_file_1') do |file_path|
9999
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
@@ -129,7 +129,7 @@ def stub_s3_file_exists(key, exists)
129129
file: file_path
130130
)
131131
end
132-
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'You must provide a valid key')
132+
end.to raise_error(FastlaneCore::Interface::FastlaneError, 'The provided key must not be empty. Use nil instead if you want to default to the file basename')
133133
end
134134

135135
it 'fails if local file does not exist' do
@@ -144,7 +144,7 @@ def stub_s3_file_exists(key, exists)
144144

145145
it 'fails if the file already exists on S3' do
146146
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
147-
stub_s3_file_exists(expected_key)
147+
stub_s3_response_for_file(expected_key)
148148

149149
with_tmp_file_path_for_file_named('key') do |file_path|
150150
expect do
@@ -153,7 +153,7 @@ def stub_s3_file_exists(key, exists)
153153
key: 'key',
154154
file: file_path
155155
)
156-
end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists at #{expected_key}")
156+
end.to raise_error(FastlaneCore::Interface::FastlaneError, "File already exists in S3 bucket #{test_bucket} at #{expected_key}")
157157
end
158158
end
159159
end

0 commit comments

Comments
 (0)