Skip to content

Commit 99e6ece

Browse files
committed
Combine test helperes
1 parent 920826c commit 99e6ece

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

spec/spec_helper.rb

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,14 @@ def in_tmp_dir
8383
end
8484

8585
# Executes the given block with a temporary file with the given `file_name`
86-
def with_tmp_file_path_for_file_named(file_name)
86+
def with_tmp_file(named: nil, content: '')
8787
in_tmp_dir do |tmp_dir|
88+
file_name = named || ('a'..'z').to_a.sample(8).join # 8-character random file name if nil
8889
file_path = File.join(tmp_dir, file_name)
8990

90-
File.write(file_path, '')
91+
File.write(file_path, content)
9192
yield file_path
9293
ensure
9394
File.delete(file_path)
9495
end
9596
end
96-
97-
# Executes the given block with a temporary file
98-
def with_tmp_file_path
99-
in_tmp_dir do |tmp_dir|
100-
file_name = ('a'..'z').to_a.sample(8).join # 8-character random file name
101-
file_path = File.join(tmp_dir, file_name)
102-
103-
File.write(file_path, '')
104-
yield file_path
105-
File.delete(file_path)
106-
end
107-
end

spec/upload_to_s3_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def stub_s3_response_for_file(key, exists: true)
2121
expected_key = '939c39398db2405e791e205778ff70f85dff620e/a8c-key1'
2222
stub_s3_response_for_file(expected_key, exists: false)
2323

24-
with_tmp_file_path_for_file_named('input_file_1') do |file_path|
24+
with_tmp_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)
2626

2727
return_value = run_described_fastlane_action(
@@ -39,7 +39,7 @@ def stub_s3_response_for_file(key, exists: true)
3939
expected_key = '8bde1a7a04300df27b52f4383dc997e5fbbff180/a8c-key2'
4040
stub_s3_response_for_file(expected_key, exists: false)
4141

42-
with_tmp_file_path_for_file_named('input_file_2') do |file_path|
42+
with_tmp_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)
4444

4545
return_value = run_described_fastlane_action(
@@ -58,7 +58,7 @@ def stub_s3_response_for_file(key, exists: true)
5858
expected_key = 'a8c-key1'
5959
stub_s3_response_for_file(expected_key, exists: false)
6060

61-
with_tmp_file_path do |file_path|
61+
with_tmp_file do |file_path|
6262
expect(client).to receive(:put_object).with(body: file_instance_of(file_path), bucket: test_bucket, key: expected_key)
6363

6464
return_value = run_described_fastlane_action(
@@ -77,7 +77,7 @@ def stub_s3_response_for_file(key, exists: true)
7777
expected_key = '939c39398db2405e791e205778ff70f85dff620e/subdir/a8c-key1'
7878
stub_s3_response_for_file(expected_key, exists: false)
7979

80-
with_tmp_file_path_for_file_named('input_file_1') do |file_path|
80+
with_tmp_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)
8282

8383
return_value = run_described_fastlane_action(
@@ -95,7 +95,7 @@ def stub_s3_response_for_file(key, exists: true)
9595
expected_key = 'c125bd799c6aad31092b02e440a8fae25b45a2ad/test_file_1'
9696
stub_s3_response_for_file(expected_key, exists: false)
9797

98-
with_tmp_file_path_for_file_named('test_file_1') do |file_path|
98+
with_tmp_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)
100100

101101
return_value = run_described_fastlane_action(
@@ -112,7 +112,7 @@ def stub_s3_response_for_file(key, exists: true)
112112
describe 'uploading a file with invalid parameters' do
113113
it 'fails if bucket is empty or nil' do
114114
expect do
115-
with_tmp_file_path do |file_path|
115+
with_tmp_file do |file_path|
116116
run_described_fastlane_action(
117117
bucket: '',
118118
key: 'key',
@@ -124,7 +124,7 @@ def stub_s3_response_for_file(key, exists: true)
124124

125125
it 'fails if an empty key is provided' do
126126
expect do
127-
with_tmp_file_path do |file_path|
127+
with_tmp_file do |file_path|
128128
run_described_fastlane_action(
129129
bucket: test_bucket,
130130
key: '',
@@ -148,7 +148,7 @@ def stub_s3_response_for_file(key, exists: true)
148148
expected_key = 'a62f2225bf70bfaccbc7f1ef2a397836717377de/key'
149149
stub_s3_response_for_file(expected_key)
150150

151-
with_tmp_file_path_for_file_named('key') do |file_path|
151+
with_tmp_file(named: 'key') do |file_path|
152152
expect do
153153
run_described_fastlane_action(
154154
bucket: test_bucket,

0 commit comments

Comments
 (0)