Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions Tests/xcodeinstallTests/API/DownloadDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ struct DownloadDelegateTests {
let log = Logger(label: "DownloadDelegateTests")
let fileManager = FileManager.default

/// Runs `body` with a freshly created temporary directory, removing it on exit.
func withTempDirectory<T>(_ body: (URL) throws -> T) throws -> T {
let tempDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true)
defer { try? fileManager.removeItem(at: tempDir) }
return try body(tempDir)
}

/// Async variant of `withTempDirectory`.
func withTempDirectory<T>(_ body: (URL) async throws -> T) async throws -> T {
let tempDir = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDir, withIntermediateDirectories: true)
defer { try? fileManager.removeItem(at: tempDir) }
return try await body(tempDir)
}

/// Creates a DownloadDelegate backed by an AsyncThrowingStream, returning both.
/// The stream can be consumed to verify what the delegate yielded/threw.
func makeDelegate(
Expand Down Expand Up @@ -61,7 +45,7 @@ extension DownloadDelegateTests {

@Test("Successful file move via didFinishDownloadingTo")
func testDidFinishDownloadingTo_movesFile() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — a source file at a temp location and a destination path
let srcFile = tempDir.appendingPathComponent("source.xip")
let dstFile = tempDir.appendingPathComponent("destination.xip")
Expand Down Expand Up @@ -92,7 +76,7 @@ extension DownloadDelegateTests {

@Test("Error page detection with Error tag in small download")
func testDidFinishDownloadingTo_detectsErrorPage() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — a small file containing an error page marker
let srcFile = tempDir.appendingPathComponent("error_response.xml")
let errorContent = "<Error><Code>AccessDenied</Code></Error>"
Expand Down Expand Up @@ -121,7 +105,7 @@ extension DownloadDelegateTests {

@Test("AccessDenied string triggers error page detection")
func testDidFinishDownloadingTo_detectsAccessDenied() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — a small file containing "AccessDenied"
let srcFile = tempDir.appendingPathComponent("access_denied.txt")
let content = "AccessDenied"
Expand All @@ -147,7 +131,7 @@ extension DownloadDelegateTests {

@Test("Sign in page triggers error page detection")
func testDidFinishDownloadingTo_detectsSignInPage() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — a small file containing Apple sign-in page marker
let srcFile = tempDir.appendingPathComponent("signin_page.html")
let content = "<html>Sign in to your Apple Account</html>"
Expand All @@ -173,7 +157,7 @@ extension DownloadDelegateTests {

@Test("File move failure propagates error through stream")
func testDidFinishDownloadingTo_moveFailure() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — a source file and a file handler that will fail on move
let srcFile = tempDir.appendingPathComponent("source.xip")
let paddedContent = String(repeating: "x", count: 10_001)
Expand Down Expand Up @@ -231,7 +215,7 @@ extension DownloadDelegateTests {

@Test("didCompleteWithError with nil does not throw after successful download")
func testDidCompleteWithError_withNil() async throws {
try await withTempDirectory { tempDir in
try await withTemporaryDirectory { tempDir in
// Given — simulate the scenario where didFinishDownloadingTo already completed the stream
let srcFile = tempDir.appendingPathComponent("source.xip")
let paddedContent = String(repeating: "x", count: 10_001)
Expand Down
27 changes: 27 additions & 0 deletions Tests/xcodeinstallTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ func modifySession(on authenticator: AppleAuthenticator, modifier: (inout AppleS
func getSessionProperty<T>(from authenticator: AppleAuthenticator, accessor: (AppleSession) -> T) -> T {
accessor(authenticator.session)
}

// MARK: - Temporary Directory Helper

/// Creates a temporary directory, executes the body, and cleans up.
/// Returns the temporary directory URL and a cleanup closure.
private func createTemporaryDirectory() throws -> (URL, () -> Void) {
let fileManager = FileManager.default
let tempDirURL = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDirURL, withIntermediateDirectories: true)
let cleanup: () -> Void = { try? fileManager.removeItem(at: tempDirURL) }
return (tempDirURL, cleanup)
}

/// Executes body with a URL to a temporary directory that will be deleted after
/// the closure finishes executing.
func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
let (tempDirURL, cleanup) = try createTemporaryDirectory()
defer { cleanup() }
return try body(tempDirURL)
}

/// Async variant of withTemporaryDirectory.
func withTemporaryDirectory<T>(_ body: (URL) async throws -> T) async throws -> T {
let (tempDirURL, cleanup) = try createTemporaryDirectory()
defer { cleanup() }
return try await body(tempDirURL)
}
11 changes: 0 additions & 11 deletions Tests/xcodeinstallTests/Utilities/ConfigHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ struct ConfigHandlerTests {
self.fileManager = FileManager.default
}

/// Executes body with a URL to a temporary directory that will be deleted after
/// the closure finishes executing.
func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
let tempDirURL = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDirURL, withIntermediateDirectories: true)
defer {
try? fileManager.removeItem(at: tempDirURL)
}
return try body(tempDirURL)
}

@Test("Save and load config")
func testSaveAndLoad() throws {
try withTemporaryDirectory { tempDir in
Expand Down
15 changes: 2 additions & 13 deletions Tests/xcodeinstallTests/Utilities/FileHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@ struct FileHandlerTests {

// MARK: - Helper Methods

/// Executes body with a URL to a temporary directory that will be deleted after
/// the closure finishes executing.
func withTemporaryDirectory<T>(_ body: (URL) throws -> T) throws -> T {
let tempDirURL = fileManager.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try fileManager.createDirectory(at: tempDirURL, withIntermediateDirectories: true)
defer {
try? fileManager.removeItem(at: tempDirURL)
}
return try body(tempDirURL)
}

private func createSrcFile(inDirectory tempDirectory: URL) throws -> URL {
let srcFile: URL = tempDirectory.appendingPathComponent("temp.txt")
let _ = fileManager.createFile(atPath: srcFile.path, contents: test_data.data(using: .utf8))
Expand All @@ -42,7 +31,7 @@ extension FileHandlerTests {

@Test("Test Moving Files Successfully")
func testMoveSucceed() async throws {
try self.withTemporaryDirectory { url in
try withTemporaryDirectory { url in
// Given
let srcFile = try createSrcFile(inDirectory: url)

Expand All @@ -69,7 +58,7 @@ extension FileHandlerTests {

@Test("Test Moving Files When Destination Already Exists")
func testMoveDstExists() async throws {
try self.withTemporaryDirectory { url in
try withTemporaryDirectory { url in
// Given
let test_data2: String = "data already exists"
let srcFile = try createSrcFile(inDirectory: url)
Expand Down