diff --git a/google-cloud-storage/samples/acceptance/files_test.rb b/google-cloud-storage/samples/acceptance/files_test.rb index d1c6c53c74cf..6cd9a7b1b3bc 100644 --- a/google-cloud-storage/samples/acceptance/files_test.rb +++ b/google-cloud-storage/samples/acceptance/files_test.rb @@ -429,6 +429,52 @@ def mock_cipher.random_key end refute_nil bucket.file remote_file_name + refute_nil bucket.file file_1_name + refute_nil bucket.file file_2_name + end + + it "compose_file with delete_source_objects" do + file_1 = bucket.create_file local_file, file_1_name + file_2 = bucket.create_file local_file, file_2_name + + expected_out = "Composed new file #{remote_file_name} in the bucket #{bucket.name} " \ + "by combining #{file_1.name} and #{file_2.name}\n" \ + "Source objects were deleted\n" + assert_output expected_out do + compose_file bucket_name: bucket.name, + first_file_name: file_1.name, + second_file_name: file_2.name, + destination_file_name: remote_file_name, + delete_source_objects: true + end + + refute_nil bucket.file remote_file_name + assert_nil bucket.file file_1_name + assert_nil bucket.file file_2_name + end + + it "compose_file with delete_source_objects failing does not delete source objects" do + file_1 = bucket.create_file local_file, file_1_name + file_2 = bucket.create_file local_file, file_2_name + + real_storage = Google::Cloud::Storage.new + bucket.stub :compose, ->(*) { raise Google::Cloud::Error, "Mock API error" } do + real_storage.stub :bucket, bucket do + Google::Cloud::Storage.stub :new, real_storage do + assert_raises Google::Cloud::Error do + compose_file bucket_name: bucket.name, + first_file_name: file_1.name, + second_file_name: file_2.name, + destination_file_name: remote_file_name, + delete_source_objects: true + end + end + end + end + + refute_nil bucket.file file_1_name + refute_nil bucket.file file_2_name + assert_nil bucket.file remote_file_name end it "copy_file" do diff --git a/google-cloud-storage/samples/storage_compose_file.rb b/google-cloud-storage/samples/storage_compose_file.rb index 8b10228c2172..5fd1070a2a4c 100644 --- a/google-cloud-storage/samples/storage_compose_file.rb +++ b/google-cloud-storage/samples/storage_compose_file.rb @@ -13,7 +13,7 @@ # limitations under the License. # [START storage_compose_file] -def compose_file bucket_name:, first_file_name:, second_file_name:, destination_file_name: +def compose_file bucket_name:, first_file_name:, second_file_name:, destination_file_name:, delete_source_objects: false # The ID of your GCS bucket # bucket_name = "your-unique-bucket-name" @@ -31,12 +31,13 @@ def compose_file bucket_name:, first_file_name:, second_file_name:, destination_ storage = Google::Cloud::Storage.new bucket = storage.bucket bucket_name, skip_lookup: true - destination = bucket.compose [first_file_name, second_file_name], destination_file_name do |f| + destination = bucket.compose [first_file_name, second_file_name], destination_file_name, delete_source_objects: delete_source_objects do |f| f.content_type = "text/plain" end puts "Composed new file #{destination.name} in the bucket #{bucket_name} " \ "by combining #{first_file_name} and #{second_file_name}" + puts "Source objects were deleted" if delete_source_objects end # [END storage_compose_file] @@ -44,5 +45,6 @@ def compose_file bucket_name:, first_file_name:, second_file_name:, destination_ compose_file bucket_name: ARGV.shift, first_file_name: ARGV.shift, second_file_name: ARGV.shift, - destination_file_name: ARGV.shift + destination_file_name: ARGV.shift, + delete_source_objects: ARGV.shift == "true" end