From e91aab5821c59d9d2e6690b441be802d85767be2 Mon Sep 17 00:00:00 2001 From: fortmarek Date: Thu, 15 May 2025 13:08:49 +0200 Subject: [PATCH 1/2] feat: move FileSystemTestingTrait to a new library product FileSystemTesting --- Package.swift | 18 +++++++++++ .../FileSystem/FileSystemTestingTrait.swift | 31 ------------------- .../FileSystemTestingTrait.swift | 29 +++++++++++++++++ .../FileSystemTestingTraitTests.swift | 1 + 4 files changed, 48 insertions(+), 31 deletions(-) delete mode 100644 Sources/FileSystem/FileSystemTestingTrait.swift create mode 100644 Sources/FileSystemTesting/FileSystemTestingTrait.swift rename Tests/{FileSystemTests => FileSystemTestingTests}/FileSystemTestingTraitTests.swift (93%) diff --git a/Package.swift b/Package.swift index 6ab7e08..205178a 100644 --- a/Package.swift +++ b/Package.swift @@ -15,6 +15,11 @@ let package = Package( type: .static, targets: ["FileSystem"] ), + .library( + name: "FileSystemTesting", + type: .static, + targets: ["FileSystemTesting"] + ), ], dependencies: [ .package(url: "https://github.com/tuist/Path", .upToNextMajor(from: "0.3.8")), @@ -42,6 +47,19 @@ let package = Package( "FileSystem", ] ), + .target( + name: "FileSystemTesting", + dependencies: [ + "FileSystem", + ] + ), + .testTarget( + name: "FileSystemTestingTests", + dependencies: [ + "FileSystem", + "FileSystemTesting", + ] + ), .target( name: "Glob", swiftSettings: [ diff --git a/Sources/FileSystem/FileSystemTestingTrait.swift b/Sources/FileSystem/FileSystemTestingTrait.swift deleted file mode 100644 index bed37e6..0000000 --- a/Sources/FileSystem/FileSystemTestingTrait.swift +++ /dev/null @@ -1,31 +0,0 @@ -#if DEBUG && canImport(Testing) && compiler(>=6.1) - import Path - import Testing - - extension FileSystem { - /// It returns the temporary directory created when using the `@Test(.inTemporaryDirectory)`. - /// Note that since the value is only propagated through Swift structured concurrency, if you use DispatchQueue, - /// values won't be propagated so you'll have to make sure they are explicitly passed down. - @TaskLocal public static var temporaryTestDirectory: AbsolutePath? - } - - public struct FileSystemTestingTrait: TestTrait, SuiteTrait, TestScoping { - public func provideScope( - for _: Test, - testCase _: Test.Case?, - performing function: @Sendable () async throws -> Void - ) async throws { - try await FileSystem().runInTemporaryDirectory { temporaryDirectory in - try await FileSystem.$temporaryTestDirectory.withValue(temporaryDirectory) { - try await function() - } - } - } - } - - extension Trait where Self == FileSystemTestingTrait { - /// Creates a temporary directory and scopes its lifecycle to the lifecycle of the test. - public static var inTemporaryDirectory: Self { Self() } - } - -#endif diff --git a/Sources/FileSystemTesting/FileSystemTestingTrait.swift b/Sources/FileSystemTesting/FileSystemTestingTrait.swift new file mode 100644 index 0000000..7bfbff5 --- /dev/null +++ b/Sources/FileSystemTesting/FileSystemTestingTrait.swift @@ -0,0 +1,29 @@ +import FileSystem +import Path +import Testing + +extension FileSystem { + /// It returns the temporary directory created when using the `@Test(.inTemporaryDirectory)`. + /// Note that since the value is only propagated through Swift structured concurrency, if you use DispatchQueue, + /// values won't be propagated so you'll have to make sure they are explicitly passed down. + @TaskLocal public static var temporaryTestDirectory: AbsolutePath? +} + +public struct FileSystemTestingTrait: TestTrait, SuiteTrait, TestScoping { + public func provideScope( + for _: Test, + testCase _: Test.Case?, + performing function: @Sendable () async throws -> Void + ) async throws { + try await FileSystem().runInTemporaryDirectory { temporaryDirectory in + try await FileSystem.$temporaryTestDirectory.withValue(temporaryDirectory) { + try await function() + } + } + } +} + +extension Trait where Self == FileSystemTestingTrait { + /// Creates a temporary directory and scopes its lifecycle to the lifecycle of the test. + public static var inTemporaryDirectory: Self { Self() } +} diff --git a/Tests/FileSystemTests/FileSystemTestingTraitTests.swift b/Tests/FileSystemTestingTests/FileSystemTestingTraitTests.swift similarity index 93% rename from Tests/FileSystemTests/FileSystemTestingTraitTests.swift rename to Tests/FileSystemTestingTests/FileSystemTestingTraitTests.swift index 0589562..9bfa7aa 100644 --- a/Tests/FileSystemTests/FileSystemTestingTraitTests.swift +++ b/Tests/FileSystemTestingTests/FileSystemTestingTraitTests.swift @@ -1,4 +1,5 @@ import FileSystem +import FileSystemTesting import Testing struct FileSystemTestingTraitTests { From 6791748672b9ba37284d620f5d8c23d48a3a579c Mon Sep 17 00:00:00 2001 From: fortmarek Date: Thu, 15 May 2025 14:07:20 +0200 Subject: [PATCH 2/2] Wrap import Testing in a conditional --- .../FileSystemTestingTrait.swift | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/Sources/FileSystemTesting/FileSystemTestingTrait.swift b/Sources/FileSystemTesting/FileSystemTestingTrait.swift index 7bfbff5..4aeaf59 100644 --- a/Sources/FileSystemTesting/FileSystemTestingTrait.swift +++ b/Sources/FileSystemTesting/FileSystemTestingTrait.swift @@ -1,29 +1,31 @@ -import FileSystem -import Path -import Testing +#if canImport(Testing) + import FileSystem + import Path + import Testing -extension FileSystem { - /// It returns the temporary directory created when using the `@Test(.inTemporaryDirectory)`. - /// Note that since the value is only propagated through Swift structured concurrency, if you use DispatchQueue, - /// values won't be propagated so you'll have to make sure they are explicitly passed down. - @TaskLocal public static var temporaryTestDirectory: AbsolutePath? -} + extension FileSystem { + /// It returns the temporary directory created when using the `@Test(.inTemporaryDirectory)`. + /// Note that since the value is only propagated through Swift structured concurrency, if you use DispatchQueue, + /// values won't be propagated so you'll have to make sure they are explicitly passed down. + @TaskLocal public static var temporaryTestDirectory: AbsolutePath? + } -public struct FileSystemTestingTrait: TestTrait, SuiteTrait, TestScoping { - public func provideScope( - for _: Test, - testCase _: Test.Case?, - performing function: @Sendable () async throws -> Void - ) async throws { - try await FileSystem().runInTemporaryDirectory { temporaryDirectory in - try await FileSystem.$temporaryTestDirectory.withValue(temporaryDirectory) { - try await function() + public struct FileSystemTestingTrait: TestTrait, SuiteTrait, TestScoping { + public func provideScope( + for _: Test, + testCase _: Test.Case?, + performing function: @Sendable () async throws -> Void + ) async throws { + try await FileSystem().runInTemporaryDirectory { temporaryDirectory in + try await FileSystem.$temporaryTestDirectory.withValue(temporaryDirectory) { + try await function() + } } } } -} -extension Trait where Self == FileSystemTestingTrait { - /// Creates a temporary directory and scopes its lifecycle to the lifecycle of the test. - public static var inTemporaryDirectory: Self { Self() } -} + extension Trait where Self == FileSystemTestingTrait { + /// Creates a temporary directory and scopes its lifecycle to the lifecycle of the test. + public static var inTemporaryDirectory: Self { Self() } + } +#endif