Skip to content

Commit 70fdb1a

Browse files
committed
Use withTestScratchDir instead of withTemporaryDirectory
`withTestScratchDir` respects the SourceKit-LSP test environment variable to keep temporary files.
1 parent 000a566 commit 70fdb1a

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

Sources/SKTestSupport/Utils.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import Foundation
1414
import LanguageServerProtocol
1515
import SKCore
16+
import TSCBasic
1617

1718
extension Language {
1819
var fileExtension: String {
@@ -63,6 +64,25 @@ public func testScratchDir(testName: String = #function) throws -> URL {
6364
return url
6465
}
6566

67+
/// Execute `body` with a path to a temporary scratch directory for the given
68+
/// test name.
69+
///
70+
/// The temporary directory will be deleted at the end of `directory` unless the
71+
/// `SOURCEKITLSP_KEEP_TEST_SCRATCH_DIR` environment variable is set.
72+
public func withTestScratchDir<T>(
73+
_ body: (AbsolutePath) async throws -> T,
74+
testName: String = #function
75+
) async throws -> T {
76+
let scratchDirectory = try testScratchDir(testName: testName)
77+
try FileManager.default.createDirectory(at: scratchDirectory, withIntermediateDirectories: true)
78+
defer {
79+
if cleanScratchDirectories {
80+
try? FileManager.default.removeItem(at: scratchDirectory)
81+
}
82+
}
83+
return try await body(try AbsolutePath(validating: scratchDirectory.path))
84+
}
85+
6686
fileprivate extension URL {
6787
/// Assuming this is a file URL, resolves all symlinks in the path.
6888
///

Tests/SKCoreTests/ToolchainRegistryTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import LSPTestSupport
1414
@_spi(Testing) import SKCore
1515
import SKSupport
16+
import SKTestSupport
1617
import TSCBasic
1718
import XCTest
1819

@@ -341,7 +342,7 @@ final class ToolchainRegistryTests: XCTestCase {
341342
func testFromDirectory() async throws {
342343
// This test uses the real file system because the in-memory system doesn't support marking files executable.
343344
let fs = localFileSystem
344-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
345+
try await withTestScratchDir { tempDir in
345346
let path = tempDir.appending(components: "A.xctoolchain", "usr")
346347
try makeToolchain(
347348
binPath: path.appending(component: "bin"),

Tests/SKSwiftPMWorkspaceTests/SwiftPMWorkspaceTests.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
3131

3232
func testNoPackage() async throws {
3333
let fs = InMemoryFileSystem()
34-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
34+
try await withTestScratchDir { tempDir in
3535
try fs.createFiles(
3636
root: tempDir,
3737
files: [
@@ -53,7 +53,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
5353

5454
func testUnparsablePackage() async throws {
5555
let fs = localFileSystem
56-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
56+
try await withTestScratchDir { tempDir in
5757
try fs.createFiles(
5858
root: tempDir,
5959
files: [
@@ -80,7 +80,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
8080

8181
func testNoToolchain() async throws {
8282
let fs = localFileSystem
83-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
83+
try await withTestScratchDir { tempDir in
8484
try fs.createFiles(
8585
root: tempDir,
8686
files: [
@@ -108,7 +108,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
108108
func testBasicSwiftArgs() async throws {
109109
// FIXME: should be possible to use InMemoryFileSystem.
110110
let fs = localFileSystem
111-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
111+
try await withTestScratchDir { tempDir in
112112
try fs.createFiles(
113113
root: tempDir,
114114
files: [
@@ -168,7 +168,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
168168
func testBuildSetup() async throws {
169169
// FIXME: should be possible to use InMemoryFileSystem.
170170
let fs = localFileSystem
171-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
171+
try await withTestScratchDir { tempDir in
172172
try fs.createFiles(
173173
root: tempDir,
174174
files: [
@@ -214,7 +214,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
214214
func testManifestArgs() async throws {
215215
// FIXME: should be possible to use InMemoryFileSystem.
216216
let fs = localFileSystem
217-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
217+
try await withTestScratchDir { tempDir in
218218
try fs.createFiles(
219219
root: tempDir,
220220
files: [
@@ -247,7 +247,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
247247
func testMultiFileSwift() async throws {
248248
// FIXME: should be possible to use InMemoryFileSystem.
249249
let fs = localFileSystem
250-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
250+
try await withTestScratchDir { tempDir in
251251
try fs.createFiles(
252252
root: tempDir,
253253
files: [
@@ -285,7 +285,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
285285
func testMultiTargetSwift() async throws {
286286
// FIXME: should be possible to use InMemoryFileSystem.
287287
let fs = localFileSystem
288-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
288+
try await withTestScratchDir { tempDir in
289289
try fs.createFiles(
290290
root: tempDir,
291291
files: [
@@ -351,7 +351,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
351351
func testUnknownFile() async throws {
352352
// FIXME: should be possible to use InMemoryFileSystem.
353353
let fs = localFileSystem
354-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
354+
try await withTestScratchDir { tempDir in
355355
try fs.createFiles(
356356
root: tempDir,
357357
files: [
@@ -387,7 +387,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
387387
func testBasicCXXArgs() async throws {
388388
// FIXME: should be possible to use InMemoryFileSystem.
389389
let fs = localFileSystem
390-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
390+
try await withTestScratchDir { tempDir in
391391
try fs.createFiles(
392392
root: tempDir,
393393
files: [
@@ -481,7 +481,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
481481
func testDeploymentTargetSwift() async throws {
482482
// FIXME: should be possible to use InMemoryFileSystem.
483483
let fs = localFileSystem
484-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
484+
try await withTestScratchDir { tempDir in
485485
try fs.createFiles(
486486
root: tempDir,
487487
files: [
@@ -524,7 +524,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
524524
func testSymlinkInWorkspaceSwift() async throws {
525525
// FIXME: should be possible to use InMemoryFileSystem.
526526
let fs = localFileSystem
527-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
527+
try await withTestScratchDir { tempDir in
528528
try fs.createFiles(
529529
root: tempDir,
530530
files: [
@@ -579,7 +579,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
579579
func testSymlinkInWorkspaceCXX() async throws {
580580
// FIXME: should be possible to use InMemoryFileSystem.
581581
let fs = localFileSystem
582-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
582+
try await withTestScratchDir { tempDir in
583583
try fs.createFiles(
584584
root: tempDir,
585585
files: [
@@ -629,7 +629,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
629629
func testSwiftDerivedSources() async throws {
630630
// FIXME: should be possible to use InMemoryFileSystem.
631631
let fs = localFileSystem
632-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
632+
try await withTestScratchDir { tempDir in
633633
try fs.createFiles(
634634
root: tempDir,
635635
files: [
@@ -670,7 +670,7 @@ final class SwiftPMWorkspaceTests: XCTestCase {
670670

671671
func testNestedInvalidPackageSwift() async throws {
672672
let fs = InMemoryFileSystem()
673-
try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDir in
673+
try await withTestScratchDir { tempDir in
674674
try fs.createFiles(
675675
root: tempDir,
676676
files: [

0 commit comments

Comments
 (0)