Skip to content

Commit a76ddce

Browse files
Improve test hygiene in ConvertSubcommandTests (#934)
* unset template environment after testOptionsValidation exits this fixes an issue when tests are run sequentially; testParameterValidationFeatureFlag (added in #897) can fail if run after testOptionsValidation, because it sets `DOCC_HTML_DIR` to a temporary directory, which is deleted after the test exits. by resetting the environment variable in a defer block, we can restore a proper environment for later tests. * reset the environment for all ConvertSubcommandTests * set pwd to a temporary directory this ensures that the tests aren't affected by the machine environment, for example if a gitignore'd path contains a symlink
1 parent 8f1a9d2 commit a76ddce

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

Tests/SwiftDocCUtilitiesTests/ArgumentParsing/ConvertSubcommandTests.swift

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,34 @@ class ConvertSubcommandTests: XCTestCase {
1919

2020
private let testTemplateURL = Bundle.module.url(
2121
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+
2433
// By default, send all warnings to `.none` instead of filling the
2534
// test console output with unrelated messages.
2635
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+
}
2748
}
28-
49+
2950
func testOptionsValidation() throws {
3051
// create source bundle directory
3152
let sourceURL = try createTemporaryDirectory(named: "documentation")
@@ -34,7 +55,7 @@ class ConvertSubcommandTests: XCTestCase {
3455
// create template dir
3556
let rendererTemplateDirectory = try createTemporaryDirectory()
3657
try "".write(to: rendererTemplateDirectory.appendingPathComponent("index.html"), atomically: true, encoding: .utf8)
37-
58+
3859
// Tests a single input.
3960
do {
4061
SetEnvironmentVariable(TemplateOption.environmentVariableKey, rendererTemplateDirectory.path)
@@ -137,8 +158,6 @@ class ConvertSubcommandTests: XCTestCase {
137158
}
138159

139160
func testDefaultCurrentWorkingDirectory() {
140-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
141-
142161
XCTAssertTrue(
143162
FileManager.default.changeCurrentDirectoryPath(testBundleURL.path),
144163
"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 {
159178
// Test throws on non-existing parent folder.
160179
for outputOption in ["-o", "--output-path"] {
161180
for path in ["/tmp/output", "/tmp", "/"] {
162-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
163181
XCTAssertThrowsError(try Docc.Convert.parse([
164182
outputOption, fakeRootPath + path,
165183
testBundleURL.path,
@@ -169,7 +187,6 @@ class ConvertSubcommandTests: XCTestCase {
169187
}
170188

171189
func testAnalyzerIsTurnedOffByDefault() throws {
172-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
173190
let convertOptions = try Docc.Convert.parse([
174191
testBundleURL.path,
175192
])
@@ -178,8 +195,6 @@ class ConvertSubcommandTests: XCTestCase {
178195
}
179196

180197
func testInfoPlistFallbacks() throws {
181-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
182-
183198
// Default to nil when not passed
184199
do {
185200
let convertOptions = try Docc.Convert.parse([
@@ -226,8 +241,6 @@ class ConvertSubcommandTests: XCTestCase {
226241
// Deprecating the test silences the deprecation warning when running the tests. It doesn't skip the test.
227242
@available(*, deprecated)
228243
func testAdditionalSymbolGraphFiles() throws {
229-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
230-
231244
// Default to [] when not passed
232245
do {
233246
let convertOptions = try Docc.Convert.parse([
@@ -291,8 +304,6 @@ class ConvertSubcommandTests: XCTestCase {
291304
}
292305

293306
func testIndex() throws {
294-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
295-
296307
let convertOptions = try Docc.Convert.parse([
297308
testBundleURL.path,
298309
"--index",
@@ -319,8 +330,6 @@ class ConvertSubcommandTests: XCTestCase {
319330
}
320331

321332
func testWithoutBundle() throws {
322-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
323-
324333
let convertOptions = try Docc.Convert.parse([
325334
"--fallback-display-name", "DisplayName",
326335
"--fallback-bundle-identifier", "com.example.test",
@@ -444,10 +453,7 @@ class ConvertSubcommandTests: XCTestCase {
444453
let rendererTemplateDirectory = try createTemporaryDirectory()
445454
try "".write(to: rendererTemplateDirectory.appendingPathComponent("index.html"), atomically: true, encoding: .utf8)
446455
SetEnvironmentVariable(TemplateOption.environmentVariableKey, rendererTemplateDirectory.path)
447-
defer {
448-
UnsetEnvironmentVariable(TemplateOption.environmentVariableKey)
449-
}
450-
456+
451457
let dependencyDir = try createTemporaryDirectory()
452458
.appendingPathComponent("SomeDependency.doccarchive", isDirectory: true)
453459
let fileManager = FileManager.default
@@ -513,7 +519,7 @@ class ConvertSubcommandTests: XCTestCase {
513519

514520
func testTransformForStaticHostingFlagWithoutHTMLTemplate() throws {
515521
UnsetEnvironmentVariable(TemplateOption.environmentVariableKey)
516-
522+
517523
// Since there's no custom template set (and relative HTML template lookup isn't
518524
// supported in the test harness), we expect `transformForStaticHosting` to
519525
// be false in every possible scenario of the flag, even when explicitly requested.
@@ -546,8 +552,6 @@ class ConvertSubcommandTests: XCTestCase {
546552
}
547553

548554
func testTransformForStaticHostingFlagWithHTMLTemplate() throws {
549-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
550-
551555
// Since we've provided an HTML template, we expect `transformForStaticHosting`
552556
// to be true by default, and when explicitly requested. It should only be false
553557
// when `--no-transform-for-static-hosting` is passed.
@@ -580,7 +584,6 @@ class ConvertSubcommandTests: XCTestCase {
580584
}
581585

582586
func testTreatWarningAsError() throws {
583-
SetEnvironmentVariable(TemplateOption.environmentVariableKey, testTemplateURL.path)
584587
do {
585588
// Passing no argument should default to the current working directory.
586589
let convert = try Docc.Convert.parse([])

0 commit comments

Comments
 (0)