@@ -17,6 +17,23 @@ class TestAppDelegate: AppDelegate {
17
17
18
18
var profile : BrowserProfile
19
19
let launchArguments = ProcessInfo . processInfo. arguments
20
+
21
+ launchArguments. forEach { arg in
22
+ if arg. starts ( with: LaunchArguments . LoadDatabasePrefix) {
23
+ if launchArguments. contains ( LaunchArguments . ClearProfile) {
24
+ fatalError ( " Clearing profile and loading a test database is not a supported combination. " )
25
+ }
26
+
27
+ // Grab the name of file in the bundle's test-fixtures dir, and copy it to the runtime app dir.
28
+ let filename = arg. replacingOccurrences ( of: LaunchArguments . LoadDatabasePrefix, with: " " )
29
+ let input = URL ( fileURLWithPath: Bundle ( for: TestAppDelegate . self) . path ( forResource: filename, ofType: nil , inDirectory: " test-fixtures " ) !)
30
+ let profileDir = " \( appRootDir ( ) ) /profile.testProfile "
31
+ try ? FileManager . default. createDirectory ( atPath: profileDir, withIntermediateDirectories: false , attributes: nil )
32
+ let output = URL ( fileURLWithPath: " \( profileDir) /browser.db " )
33
+ try ! FileManager . default. copyItem ( at: input, to: output)
34
+ }
35
+ }
36
+
20
37
if launchArguments. contains ( LaunchArguments . ClearProfile) {
21
38
// Use a clean profile for each test session.
22
39
log. debug ( " Deleting all files in 'Documents' directory to clear the profile " )
@@ -68,13 +85,7 @@ class TestAppDelegate: AppDelegate {
68
85
}
69
86
70
87
// Clear the documents directory
71
- var rootPath : String = " "
72
- let sharedContainerIdentifier = AppInfo . sharedContainerIdentifier
73
- if let url = FileManager . default. containerURL ( forSecurityApplicationGroupIdentifier: sharedContainerIdentifier) {
74
- rootPath = url. path
75
- } else {
76
- rootPath = ( NSSearchPathForDirectoriesInDomains ( . documentDirectory, . userDomainMask, true ) [ 0 ] )
77
- }
88
+ let rootPath = appRootDir ( )
78
89
let manager = FileManager . default
79
90
let documents = URL ( fileURLWithPath: rootPath)
80
91
let docContents = try ! manager. contentsOfDirectory ( atPath: rootPath)
@@ -93,4 +104,14 @@ class TestAppDelegate: AppDelegate {
93
104
return super. application ( application, didFinishLaunchingWithOptions: launchOptions)
94
105
}
95
106
107
+ func appRootDir( ) -> String {
108
+ var rootPath = " "
109
+ let sharedContainerIdentifier = AppInfo . sharedContainerIdentifier
110
+ if let url = FileManager . default. containerURL ( forSecurityApplicationGroupIdentifier: sharedContainerIdentifier) {
111
+ rootPath = url. path
112
+ } else {
113
+ rootPath = ( NSSearchPathForDirectoriesInDomains ( . documentDirectory, . userDomainMask, true ) [ 0 ] )
114
+ }
115
+ return rootPath
116
+ }
96
117
}
0 commit comments