@@ -19,13 +19,34 @@ class ConvertSubcommandTests: XCTestCase {
19
19
20
20
private let testTemplateURL = Bundle . module. url (
21
21
forResource: " Test Template " , withExtension: nil , subdirectory: " Test Resources " ) !
22
-
23
- override func setUp( ) {
22
+
23
+ override func setUpWithError( ) throws {
24
+ // By default, run all tests in a temporary directory to ensure that they are not affected
25
+ // by the machine environment.
26
+ let priorWorkingDirectory = FileManager . default. currentDirectoryPath
27
+ let temporaryDirectory = try createTemporaryDirectory ( )
28
+ FileManager . default. changeCurrentDirectoryPath ( temporaryDirectory. path)
29
+ addTeardownBlock {
30
+ FileManager . default. changeCurrentDirectoryPath ( priorWorkingDirectory)
31
+ }
32
+
24
33
// By default, send all warnings to `.none` instead of filling the
25
34
// test console output with unrelated messages.
26
35
Docc . Convert. _errorLogHandle = . none
36
+
37
+ // Set the documentation template to a well-defined default so that options parsing isn't
38
+ // affected by other tests' changing it.
39
+ let existingTemplate = ProcessInfo . processInfo. environment [ TemplateOption . environmentVariableKey]
40
+ SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
41
+ addTeardownBlock {
42
+ if let existingTemplate = existingTemplate {
43
+ SetEnvironmentVariable ( TemplateOption . environmentVariableKey, existingTemplate)
44
+ } else {
45
+ UnsetEnvironmentVariable ( TemplateOption . environmentVariableKey)
46
+ }
47
+ }
27
48
}
28
-
49
+
29
50
func testOptionsValidation( ) throws {
30
51
// create source bundle directory
31
52
let sourceURL = try createTemporaryDirectory ( named: " documentation " )
@@ -34,7 +55,7 @@ class ConvertSubcommandTests: XCTestCase {
34
55
// create template dir
35
56
let rendererTemplateDirectory = try createTemporaryDirectory ( )
36
57
try " " . write ( to: rendererTemplateDirectory. appendingPathComponent ( " index.html " ) , atomically: true , encoding: . utf8)
37
-
58
+
38
59
// Tests a single input.
39
60
do {
40
61
SetEnvironmentVariable ( TemplateOption . environmentVariableKey, rendererTemplateDirectory. path)
@@ -137,8 +158,6 @@ class ConvertSubcommandTests: XCTestCase {
137
158
}
138
159
139
160
func testDefaultCurrentWorkingDirectory( ) {
140
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
141
-
142
161
XCTAssertTrue (
143
162
FileManager . default. changeCurrentDirectoryPath ( testBundleURL. path) ,
144
163
" The test env is invalid if the current working directory is not set to the current working directory "
@@ -159,7 +178,6 @@ class ConvertSubcommandTests: XCTestCase {
159
178
// Test throws on non-existing parent folder.
160
179
for outputOption in [ " -o " , " --output-path " ] {
161
180
for path in [ " /tmp/output " , " /tmp " , " / " ] {
162
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
163
181
XCTAssertThrowsError ( try Docc . Convert. parse ( [
164
182
outputOption, fakeRootPath + path,
165
183
testBundleURL. path,
@@ -169,7 +187,6 @@ class ConvertSubcommandTests: XCTestCase {
169
187
}
170
188
171
189
func testAnalyzerIsTurnedOffByDefault( ) throws {
172
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
173
190
let convertOptions = try Docc . Convert. parse ( [
174
191
testBundleURL. path,
175
192
] )
@@ -178,8 +195,6 @@ class ConvertSubcommandTests: XCTestCase {
178
195
}
179
196
180
197
func testInfoPlistFallbacks( ) throws {
181
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
182
-
183
198
// Default to nil when not passed
184
199
do {
185
200
let convertOptions = try Docc . Convert. parse ( [
@@ -226,8 +241,6 @@ class ConvertSubcommandTests: XCTestCase {
226
241
// Deprecating the test silences the deprecation warning when running the tests. It doesn't skip the test.
227
242
@available ( * , deprecated)
228
243
func testAdditionalSymbolGraphFiles( ) throws {
229
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
230
-
231
244
// Default to [] when not passed
232
245
do {
233
246
let convertOptions = try Docc . Convert. parse ( [
@@ -291,8 +304,6 @@ class ConvertSubcommandTests: XCTestCase {
291
304
}
292
305
293
306
func testIndex( ) throws {
294
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
295
-
296
307
let convertOptions = try Docc . Convert. parse ( [
297
308
testBundleURL. path,
298
309
" --index " ,
@@ -319,8 +330,6 @@ class ConvertSubcommandTests: XCTestCase {
319
330
}
320
331
321
332
func testWithoutBundle( ) throws {
322
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
323
-
324
333
let convertOptions = try Docc . Convert. parse ( [
325
334
" --fallback-display-name " , " DisplayName " ,
326
335
" --fallback-bundle-identifier " , " com.example.test " ,
@@ -444,10 +453,7 @@ class ConvertSubcommandTests: XCTestCase {
444
453
let rendererTemplateDirectory = try createTemporaryDirectory ( )
445
454
try " " . write ( to: rendererTemplateDirectory. appendingPathComponent ( " index.html " ) , atomically: true , encoding: . utf8)
446
455
SetEnvironmentVariable ( TemplateOption . environmentVariableKey, rendererTemplateDirectory. path)
447
- defer {
448
- UnsetEnvironmentVariable ( TemplateOption . environmentVariableKey)
449
- }
450
-
456
+
451
457
let dependencyDir = try createTemporaryDirectory ( )
452
458
. appendingPathComponent ( " SomeDependency.doccarchive " , isDirectory: true )
453
459
let fileManager = FileManager . default
@@ -513,7 +519,7 @@ class ConvertSubcommandTests: XCTestCase {
513
519
514
520
func testTransformForStaticHostingFlagWithoutHTMLTemplate( ) throws {
515
521
UnsetEnvironmentVariable ( TemplateOption . environmentVariableKey)
516
-
522
+
517
523
// Since there's no custom template set (and relative HTML template lookup isn't
518
524
// supported in the test harness), we expect `transformForStaticHosting` to
519
525
// be false in every possible scenario of the flag, even when explicitly requested.
@@ -546,8 +552,6 @@ class ConvertSubcommandTests: XCTestCase {
546
552
}
547
553
548
554
func testTransformForStaticHostingFlagWithHTMLTemplate( ) throws {
549
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
550
-
551
555
// Since we've provided an HTML template, we expect `transformForStaticHosting`
552
556
// to be true by default, and when explicitly requested. It should only be false
553
557
// when `--no-transform-for-static-hosting` is passed.
@@ -580,7 +584,6 @@ class ConvertSubcommandTests: XCTestCase {
580
584
}
581
585
582
586
func testTreatWarningAsError( ) throws {
583
- SetEnvironmentVariable ( TemplateOption . environmentVariableKey, testTemplateURL. path)
584
587
do {
585
588
// Passing no argument should default to the current working directory.
586
589
let convert = try Docc . Convert. parse ( [ ] )
0 commit comments