提交 8f370a23 编写于 作者: C Christian Noon

[Issue #1512] Test suite now only writes files to the temp directory.

The test suite also now only cleans up the directory in which all files are written. This fixes the issue on macOS where a user’s Documents and Application Support directories were being deleted when running the test target.
上级 e50160ac
......@@ -29,16 +29,14 @@ import XCTest
class BaseTestCase: XCTestCase {
let timeout: TimeInterval = 30.0
static var testDirectoryURL: URL { return FileManager.temporaryDirectoryURL.appendingPathComponent("org.alamofire.tests") }
var testDirectoryURL: URL { return BaseTestCase.testDirectoryURL }
override func setUp() {
super.setUp()
FileManager.removeAllItemsInsideDirectory(atPath: FileManager.applicationSupportDirectoryPath)
FileManager.removeAllItemsInsideDirectory(atPath: FileManager.cachesDirectoryPath)
FileManager.removeAllItemsInsideDirectory(atPath: FileManager.documentsDirectoryPath)
FileManager.createDirectory(atPath: FileManager.applicationSupportDirectoryPath)
FileManager.createDirectory(atPath: FileManager.cachesDirectoryPath)
FileManager.createDirectory(atPath: FileManager.documentsDirectoryPath)
FileManager.removeAllItemsInsideDirectory(at: testDirectoryURL)
FileManager.createDirectory(at: testDirectoryURL)
}
func url(forResource fileName: String, withExtension ext: String) -> URL {
......
......@@ -27,9 +27,6 @@ import Foundation
import XCTest
class DownloadInitializationTestCase: BaseTestCase {
let searchPathDirectory: FileManager.SearchPathDirectory = .cachesDirectory
let searchPathDomain: FileManager.SearchPathDomainMask = .userDomainMask
func testDownloadClassMethodWithMethodURLAndDestination() {
// Given
let urlString = "https://httpbin.org/"
......@@ -65,7 +62,7 @@ class DownloadInitializationTestCase: BaseTestCase {
class DownloadResponseTestCase: BaseTestCase {
private var randomCachesFileURL: URL {
return FileManager.cachesDirectoryURL.appendingPathComponent("\(UUID().uuidString).json")
return testDirectoryURL.appendingPathComponent("\(UUID().uuidString).json")
}
func testDownloadRequest() {
......@@ -227,7 +224,7 @@ class DownloadResponseTestCase: BaseTestCase {
func testThatDownloadingFileAndMovingToDirectoryThatDoesNotExistThrowsError() {
// Given
let fileURL = FileManager.cachesDirectoryURL.appendingPathComponent("some/random/folder/test_output.json")
let fileURL = testDirectoryURL.appendingPathComponent("some/random/folder/test_output.json")
let expectation = self.expectation(description: "Download request should download data but fail to move file")
var response: DefaultDownloadResponse?
......@@ -258,7 +255,7 @@ class DownloadResponseTestCase: BaseTestCase {
func testThatDownloadOptionsCanCreateIntermediateDirectoriesPriorToMovingFile() {
// Given
let fileURL = FileManager.cachesDirectoryURL.appendingPathComponent("some/random/folder/test_output.json")
let fileURL = testDirectoryURL.appendingPathComponent("some/random/folder/test_output.json")
let expectation = self.expectation(description: "Download request should download data to file: \(fileURL)")
var response: DefaultDownloadResponse?
......@@ -284,8 +281,8 @@ class DownloadResponseTestCase: BaseTestCase {
func testThatDownloadingFileAndMovingToDestinationThatIsOccupiedThrowsError() {
do {
// Given
let directoryURL = FileManager.cachesDirectoryURL.appendingPathComponent("some/random/folder")
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
let directoryURL = testDirectoryURL.appendingPathComponent("some/random/folder")
let directoryCreated = FileManager.createDirectory(at: directoryURL)
let fileURL = directoryURL.appendingPathComponent("test_output.json")
try "random_data".write(to: fileURL, atomically: true, encoding: .utf8)
......@@ -303,6 +300,8 @@ class DownloadResponseTestCase: BaseTestCase {
waitForExpectations(timeout: timeout, handler: nil)
// Then
XCTAssertTrue(directoryCreated)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.temporaryURL)
......@@ -321,35 +320,33 @@ class DownloadResponseTestCase: BaseTestCase {
}
func testThatDownloadOptionsCanRemovePreviousFilePriorToMovingFile() {
do {
// Given
let directoryURL = FileManager.cachesDirectoryURL.appendingPathComponent("some/random/folder")
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
// Given
let directoryURL = testDirectoryURL.appendingPathComponent("some/random/folder")
let directoryCreated = FileManager.createDirectory(at: directoryURL)
let fileURL = directoryURL.appendingPathComponent("test_output.json")
let fileURL = directoryURL.appendingPathComponent("test_output.json")
let expectation = self.expectation(description: "Download should complete and move file to URL: \(fileURL)")
var response: DefaultDownloadResponse?
let expectation = self.expectation(description: "Download should complete and move file to URL: \(fileURL)")
var response: DefaultDownloadResponse?
// When
Alamofire.download("https://httpbin.org/get", to: { _, _ in (fileURL, [.removePreviousFile])})
.response { resp in
response = resp
expectation.fulfill()
}
// When
Alamofire.download("https://httpbin.org/get", to: { _, _ in (fileURL, [.removePreviousFile])})
.response { resp in
response = resp
expectation.fulfill()
}
waitForExpectations(timeout: timeout, handler: nil)
waitForExpectations(timeout: timeout, handler: nil)
// Then
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.temporaryURL)
XCTAssertNotNil(response?.destinationURL)
XCTAssertNil(response?.resumeData)
XCTAssertNil(response?.error)
} catch {
XCTFail("Test encountered unexpected error: \(error)")
}
// Then
XCTAssertTrue(directoryCreated)
XCTAssertNotNil(response?.request)
XCTAssertNotNil(response?.response)
XCTAssertNotNil(response?.temporaryURL)
XCTAssertNotNil(response?.destinationURL)
XCTAssertNil(response?.resumeData)
XCTAssertNil(response?.error)
}
}
......
......@@ -28,38 +28,6 @@ extension FileManager {
// MARK: - Common Directories
static var documentsDirectoryPath: String {
return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
}
static var documentsDirectoryURL: URL {
return URL(fileURLWithPath: FileManager.documentsDirectoryPath, isDirectory: true)
}
static var libraryDirectoryPath: String {
return NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
}
static var libraryDirectoryURL: URL {
return URL(fileURLWithPath: FileManager.libraryDirectoryPath, isDirectory: true)
}
static var applicationSupportDirectoryPath: String {
return NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true)[0]
}
static var applicationSupportDirectoryURL: URL {
return URL(fileURLWithPath: FileManager.applicationSupportDirectoryPath, isDirectory: true)
}
static var cachesDirectoryPath: String {
return NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
}
static var cachesDirectoryURL: URL {
return URL(fileURLWithPath: FileManager.cachesDirectoryPath, isDirectory: true)
}
static var temporaryDirectoryPath: String {
return NSTemporaryDirectory()
}
......@@ -80,6 +48,11 @@ extension FileManager {
}
}
@discardableResult
static func createDirectory(at url: URL) -> Bool {
return createDirectory(atPath: url.path)
}
@discardableResult
static func removeItem(atPath path: String) -> Bool {
do {
......@@ -90,6 +63,11 @@ extension FileManager {
}
}
@discardableResult
static func removeItem(at url: URL) -> Bool {
return removeItem(atPath: url.path)
}
@discardableResult
static func removeAllItemsInsideDirectory(atPath path: String) -> Bool {
let enumerator = FileManager.default.enumerator(atPath: path)
......@@ -102,4 +80,9 @@ extension FileManager {
return result
}
@discardableResult
static func removeAllItemsInsideDirectory(at url: URL) -> Bool {
return removeAllItemsInsideDirectory(atPath: url.path)
}
}
......@@ -58,22 +58,7 @@ struct BoundaryGenerator {
}
}
private func temporaryFileURL() -> URL {
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
let directoryURL = tempDirectoryURL.appendingPathComponent("org.alamofire.test/multipart.form.data")
let fileManager = FileManager.default
do {
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
} catch {
// No-op - will cause tests to fail, not crash
}
let fileName = UUID().uuidString
let fileURL = directoryURL.appendingPathComponent(fileName)
return fileURL
}
private func temporaryFileURL() -> URL { return BaseTestCase.testDirectoryURL.appendingPathComponent(UUID().uuidString) }
// MARK: -
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册