-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathXcresultBundleFinder.swift
44 lines (34 loc) · 1.47 KB
/
XcresultBundleFinder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Foundation
protocol XcresultBundleFinding {
static func findXcresultFile(derivedDataFolder: String, fileManager: FileManager) throws -> String
}
enum XcresultBundleFinder: XcresultBundleFinding {
enum Errors: LocalizedError {
case xcresultNotFound
var errorDescription: String? {
"Could not find the xcresult file"
}
}
static func findXcresultFile(derivedDataFolder: String, fileManager: FileManager) throws -> String {
let testFolder = derivedDataFolder + "/Logs/Test/"
guard let xcresults = try? fileManager.contentsOfDirectory(atPath: testFolder).filter({ $0.split(separator: ".").last == "xcresult" }),
!xcresults.isEmpty
else {
throw Errors.xcresultNotFound
}
let xcresult = testFolder + xcresults.sorted(by: { left, right -> Bool in
let leftModificationDate = fileManager.modificationDate(forFileAtPath: testFolder + left)?.timeIntervalSince1970 ?? 0
let rightModificationDate = fileManager.modificationDate(forFileAtPath: testFolder + right)?.timeIntervalSince1970 ?? 0
return leftModificationDate > rightModificationDate
}).first!
return xcresult
}
}
extension FileManager {
func modificationDate(forFileAtPath path: String) -> Date? {
guard let attributes = try? attributesOfItem(atPath: path) else {
return nil
}
return attributes[.modificationDate] as? Date
}
}