diff --git a/WordPress/Classes/Utility/Media/MediaFileManager.swift b/WordPress/Classes/Utility/Media/MediaFileManager.swift index 0391ac48874d..a20805cc44e3 100644 --- a/WordPress/Classes/Utility/Media/MediaFileManager.swift +++ b/WordPress/Classes/Utility/Media/MediaFileManager.swift @@ -9,7 +9,10 @@ enum MediaDirectory { /// System Caches directory, for creating discardable media files, such as thumbnails. case cache /// System temporary directory, used for unit testing or temporary media files. - case temporary + case temporary(id: UUID) + + /// Use a new ID for every test scenario to make sure all tests are isolated. + static var temporary: MediaDirectory { .temporary(id: UUID()) } /// Returns the directory URL for the directory type. /// @@ -22,8 +25,8 @@ enum MediaDirectory { parentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first! case .cache: parentDirectory = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first! - case .temporary: - parentDirectory = fileManager.temporaryDirectory + case .temporary(let id): + parentDirectory = fileManager.temporaryDirectory.appendingPathComponent(id.uuidString) } return parentDirectory.appendingPathComponent(MediaFileManager.mediaDirectoryName, isDirectory: true) } diff --git a/WordPress/WordPressTest/ItemProviderMediaExporterTests.swift b/WordPress/WordPressTest/ItemProviderMediaExporterTests.swift index 4361d5fed03e..e01524807055 100644 --- a/WordPress/WordPressTest/ItemProviderMediaExporterTests.swift +++ b/WordPress/WordPressTest/ItemProviderMediaExporterTests.swift @@ -22,6 +22,8 @@ final class ItemProviderMediaExporterTests: XCTestCase { XCTAssertEqual(media.width, 1024.0) XCTAssertEqual(media.height, 772.0) XCTAssertNotNil(UIImage(data: try Data(contentsOf: media.url))) + + MediaExporterTests.cleanUpExportedMedia(atURL: media.url) } func testThatGPSDataIsRemoved() throws { @@ -50,6 +52,8 @@ final class ItemProviderMediaExporterTests: XCTestCase { // THEN but GPS data was removed let properties = try getImageProperties(for: media.url) XCTAssertNil(properties[kCGImagePropertyGPSDictionary]) + + MediaExporterTests.cleanUpExportedMedia(atURL: media.url) } func testThatGIFIsExported() throws { @@ -66,6 +70,8 @@ final class ItemProviderMediaExporterTests: XCTestCase { XCTAssertEqual(media.url.pathExtension, "gif") XCTAssertEqual(media.height, 360) XCTAssertEqual(media.width, 360) + + MediaExporterTests.cleanUpExportedMedia(atURL: media.url) } // MARK: - Video @@ -87,6 +93,8 @@ final class ItemProviderMediaExporterTests: XCTestCase { XCTAssertEqual(media.height, 360) XCTAssertEqual(media.width, 640) XCTAssertEqual(media.duration ?? 0.0, 3.47, accuracy: 0.01) + + MediaExporterTests.cleanUpExportedMedia(atURL: media.url) } // MARK: - Error Handling @@ -104,7 +112,7 @@ final class ItemProviderMediaExporterTests: XCTestCase { exporter.mediaDirectoryType = .temporary do { - let media = try exportedMedia(from: exporter) + let _ = try exportedMedia(from: exporter) XCTFail("Expected the export to fail") } catch { // THEN