Skip to content

Commit 6524e41

Browse files
authored
chore(storage): Add tests to cover empty string/file upload (googleapis#19312)
1 parent 3df19bf commit 6524e41

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

.github/workflows/storage-retry-conformance-test-against-emulator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424

2525
steps:
2626
- uses: actions/checkout@v3
27-
- uses: actions/setup-ruby@v1
27+
- uses: ruby/setup-ruby@v1
2828
with:
2929
ruby-version: '2.6'
3030
- run: ruby --version

google-cloud-storage/acceptance/storage/file_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,25 @@
457457
uploaded.delete
458458
end
459459

460+
it "should upload and download an empty file" do
461+
begin
462+
data = ""
463+
file = StringIO.new
464+
465+
uploaded = bucket.create_file file, "uploaded/empty-file.txt"
466+
_(uploaded.name).must_equal "uploaded/empty-file.txt"
467+
468+
downloadio = StringIO.new
469+
downloaded = uploaded.download downloadio
470+
_(downloaded).must_be_kind_of StringIO
471+
472+
downloaded_data = downloaded.string
473+
_(downloaded_data).must_equal data
474+
ensure
475+
uploaded.delete
476+
end
477+
end
478+
460479
it "should download and verify when Content-Encoding gzip response header with skip_decompress" do
461480
bucket = bucket_public
462481
file = bucket.file bucket_public_file_gzip

google-cloud-storage/test/google/cloud/storage/bucket_test.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,38 @@
588588
end
589589
end
590590

591+
it "creates an empty file" do
592+
new_file_name = random_file_path
593+
594+
Tempfile.create ["google-cloud", ".txt"] do |tmpfile|
595+
mock = Minitest::Mock.new
596+
mock.expect :insert_object, create_file_gapi(bucket_user_project.name, new_file_name),
597+
[bucket.name, empty_file_gapi], **insert_object_args(name: new_file_name, upload_source: tmpfile, user_project: "test", options: {retries: 0})
598+
599+
bucket_user_project.service.mocked_service = mock
600+
601+
created = bucket_user_project.create_file tmpfile, new_file_name
602+
_(created.user_project).must_equal true
603+
604+
mock.verify
605+
end
606+
end
607+
608+
it "creates an file with a StringIO" do
609+
new_file_name = random_file_path
610+
new_file_contents = StringIO.new
611+
612+
mock = Minitest::Mock.new
613+
mock.expect :insert_object, create_file_gapi(bucket.name, new_file_name),
614+
[bucket.name, empty_file_gapi], **insert_object_args(name: new_file_name, upload_source: new_file_contents, options: {retries: 0})
615+
616+
bucket.service.mocked_service = mock
617+
618+
bucket.create_file new_file_contents, new_file_name
619+
620+
mock.verify
621+
end
622+
591623
it "raises when given a file that does not exist" do
592624
bad_file_path = "/this/file/does/not/exist.ext"
593625

google-cloud-storage/test/google/cloud/storage/lazy/bucket_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@
336336
end
337337
end
338338

339+
it "creates an empty file" do
340+
new_file_name = random_file_path
341+
342+
Tempfile.create ["google-cloud", ".txt"] do |tmpfile|
343+
mock = Minitest::Mock.new
344+
mock.expect :insert_object, create_file_gapi(bucket_user_project.name, new_file_name),
345+
[bucket.name, empty_file_gapi], **insert_object_args(name: new_file_name, upload_source: tmpfile, user_project: "test", options: {retries: 0})
346+
347+
bucket_user_project.service.mocked_service = mock
348+
349+
created = bucket_user_project.create_file tmpfile, new_file_name
350+
_(created.user_project).must_equal true
351+
352+
mock.verify
353+
end
354+
end
355+
339356
it "raises when given a file that does not exist" do
340357
bad_file_path = "/this/file/does/not/exist.ext"
341358

0 commit comments

Comments
 (0)