From 52f1e290a031f716f49e4d171339a8186bbff850 Mon Sep 17 00:00:00 2001 From: Matt Comi Date: Wed, 20 Mar 2024 12:13:24 +0800 Subject: [PATCH] Removed commented code and added tests. --- .../Sources/StorageReference.swift | 26 --------------- .../Tests/Integration/StorageAsyncAwait.swift | 33 +++++++++++++++++++ .../Integration/StorageIntegration.swift | 4 +-- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/FirebaseStorage/Sources/StorageReference.swift b/FirebaseStorage/Sources/StorageReference.swift index 2047708e31d9..f07d1a42aae2 100644 --- a/FirebaseStorage/Sources/StorageReference.swift +++ b/FirebaseStorage/Sources/StorageReference.swift @@ -237,32 +237,6 @@ import Foundation return task } - /* - open func putFileHandle(_ fileHandle: FileHandle, metadata: StorageMetadata? = nil) -> StorageUploadTask { - return putFileHandle(fileHandle, metadata: metadata, completion: nil) - } - - open func putFileHandle(_ fileHandle: FileHandle, - metadata: StorageMetadata? = nil, - completion: ((_: StorageMetadata?, _: Error?) -> Void)?) -> StorageUploadTask { - print("called this one") - let putMetadata: StorageMetadata = metadata ?? StorageMetadata() - - if let path = path.object { - putMetadata.path = path - putMetadata.name = (path as NSString).lastPathComponent as String - } - - let task = StorageUploadTask(reference: self, - service: storage.fetcherServiceForApp, - queue: storage.dispatchQueue, - fileHandle: fileHandle, - metadata: putMetadata) - startAndObserveUploadTask(task: task, completion: completion) - return task - } - */ - // MARK: - Downloads /// Asynchronously downloads the object at the `StorageReference` to a `Data` instance in memory. diff --git a/FirebaseStorage/Tests/Integration/StorageAsyncAwait.swift b/FirebaseStorage/Tests/Integration/StorageAsyncAwait.swift index fafdb21906ce..1f774c1b05d5 100644 --- a/FirebaseStorage/Tests/Integration/StorageAsyncAwait.swift +++ b/FirebaseStorage/Tests/Integration/StorageAsyncAwait.swift @@ -177,6 +177,19 @@ class StorageAsyncAwait: StorageIntegrationCommon { XCTAssertNotNil(result) } + func testSimplePutFileHandleNoMetadata() async throws { + let ref = storage.reference(withPath: "ios/public/testSimplePutFileHandleNoMetadata") + let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") + let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) + let fileURL = tmpDirURL.appendingPathComponent(#function + "hello.txt") + try data.write(to: fileURL, options: .atomicWrite) + + let fileHandle = try XCTUnwrap(FileHandle(forReadingFrom: fileURL), "FileHandle init failed") + + let result = try await ref.putFileHandleAsync(fileHandle) + XCTAssertNotNil(result) + } + func testSimplePutFileWithAsyncProgress() async throws { var checkedProgress = false let ref = storage.reference(withPath: "ios/public/testSimplePutFile") @@ -196,6 +209,26 @@ class StorageAsyncAwait: StorageIntegrationCommon { XCTAssertTrue(checkedProgress) } + func testSimplePutFileHandleWithAsyncProgress() async throws { + var checkedProgress = false + let ref = storage.reference(withPath: "ios/public/testSimplePutFileHandle") + let data = try XCTUnwrap("Hello Swift World".data(using: .utf8), "Data construction failed") + let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory()) + let fileURL = tmpDirURL.appendingPathComponent(#function + "hello.txt") + try data.write(to: fileURL, options: .atomicWrite) + let fileHandle = try XCTUnwrap(FileHandle(forReadingFrom: fileURL), "FileHandle init failed") + var uploadedBytes: Int64 = -1 + let successMetadata = try await ref.putFileHandleAsync(fileHandle) { progress in + if let completed = progress?.completedUnitCount { + checkedProgress = true + XCTAssertGreaterThanOrEqual(completed, uploadedBytes) + uploadedBytes = completed + } + } + XCTAssertEqual(successMetadata.size, 17) + XCTAssertTrue(checkedProgress) + } + func testSimpleGetData() async throws { let ref = storage.reference(withPath: "ios/public/1mb2") let result = try await ref.data(maxSize: 1024 * 1024) diff --git a/FirebaseStorage/Tests/Integration/StorageIntegration.swift b/FirebaseStorage/Tests/Integration/StorageIntegration.swift index 14629c0f9cc9..a7f7ee64e1a9 100644 --- a/FirebaseStorage/Tests/Integration/StorageIntegration.swift +++ b/FirebaseStorage/Tests/Integration/StorageIntegration.swift @@ -376,9 +376,9 @@ class StorageResultTests: StorageIntegrationCommon { let fileHandle = try XCTUnwrap(FileHandle(forReadingFrom: fileURL), "FileHandle init failed") - let task = ref.putFileHandle(from: fileURL) { result in + let task = ref.putFileHandle(fileHandle) { result in self.assertResultSuccess(result) - putFileExpectation.fulfill() + putFileHandleExpectation.fulfill() } task.observe(StorageTaskStatus.success) { snapshot in