Skip to content

Commit 2ea9594

Browse files
committed
Add a workaround for a unit test (testBrewPrefix) that only fails when running the whole test suite. This is because it was dependent on whether package config paths had already been cached. This adds way to clear the cache.
This should really be reworked so that PCFileFinder doesn't use a static variable, especially since it ends up caching the result of the first `brewPrefix` passed into the lookup, without regard for later ones. SwiftPM doesn't currently have a way to change that at runtime, but other clients might.
1 parent 590bc8e commit 2ea9594

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Sources/TSCUtility/PkgConfig.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public struct PCFileFinder {
3333
let diagnostics: DiagnosticsEngine
3434

3535
/// Cached results of locations `pkg-config` will search for `.pc` files
36+
/// FIXME: This shouldn't use a static variable, since the first lookup
37+
/// will cache the result of whatever `brewPrefix` was passed in. It is
38+
/// also not threadsafe.
3639
public private(set) static var pkgConfigPaths: [AbsolutePath]? // FIXME: @testable(internal)
3740
private static var shouldEmitPkgConfigPathsDiagnostic = false
3841

@@ -70,6 +73,14 @@ public struct PCFileFinder {
7073
}
7174
}
7275
}
76+
77+
/// Reset the cached `pkgConfigPaths` property, so that it will be evaluated
78+
/// again when instantiating a `PCFileFinder()`. This is intended only for
79+
/// use by testing. This is a temporary workaround for the use of a static
80+
/// variable by this class.
81+
internal static func resetCachedPkgConfigPaths() {
82+
PCFileFinder.pkgConfigPaths = nil
83+
}
7384

7485
public func locatePCFile(
7586
name: String,

Tests/TSCUtilityTests/PkgConfigParserTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -12,7 +12,7 @@ import XCTest
1212
import TSCBasic
1313
import TSCTestSupport
1414

15-
import TSCUtility
15+
@testable import TSCUtility
1616

1717
final class PkgConfigParserTests: XCTestCase {
1818

@@ -91,6 +91,9 @@ final class PkgConfigParserTests: XCTestCase {
9191

9292
/// Test custom search path get higher priority for locating pc files.
9393
func testCustomPcFileSearchPath() throws {
94+
/// Temporary workaround for PCFileFinder's use of static variables.
95+
PCFileFinder.resetCachedPkgConfigPaths()
96+
9497
let diagnostics = DiagnosticsEngine()
9598

9699
let fs = InMemoryFileSystem(emptyFiles:
@@ -109,6 +112,9 @@ final class PkgConfigParserTests: XCTestCase {
109112
}
110113

111114
func testBrewPrefix() throws {
115+
/// Temporary workaround for PCFileFinder's use of static variables.
116+
PCFileFinder.resetCachedPkgConfigPaths()
117+
112118
try testWithTemporaryDirectory { tmpdir in
113119
let fakePkgConfig = tmpdir.appending(components: "bin", "pkg-config")
114120
try localFileSystem.createDirectory(fakePkgConfig.parentDirectory)

0 commit comments

Comments
 (0)