Migrate from XCTest to Swift Testing (#382)

Migrating `swift-log` from XCTest to Swift Testing

### Motivation

Swift Testing is a modern alternative to XCTest. Swift Server Ecosystem
packages should serve as reference implementations for modern Swift
features and best practices.

### Modifications

- 1-to-1 migration of test suites, tests, and assertions to Swift
Testing syntax with XCTest import removed
- XCTest references removed from documentation
- `.trace` log level added to relevant tests
- `GlobalLoggerTest` suite serialized to handle global `LoggingSystem`
access (XCTest runs tests serially by default; Swift Testing runs in
parallel, so serializing this suite avoids requiring `swift test
--no-parallel` across all CI and development environments)

### Result

Successfully executes 52 tests with `swift test --disable-xctest`.
This commit is contained in:
Vladimir Kukushkin authored and GitHub committed 2025-10-23 13:57:49 +01:00
1 parent 2a4139ef2a
commit a90a3d1c37
10 files changed
+541 -425

No files matched your search

+30 -22
View File
@@ -62,34 +62,42 @@ We require that your commit messages match our template. The easiest way to do t
### Make sure Tests work on Linux
SwiftLog uses XCTest to run tests on both macOS and Linux. While the macOS version of XCTest is able to use the Objective-C runtime to discover tests at execution time, the Linux version is not.
For this reason, whenever you add new tests **you have to run a script** that generates the hooks needed to run those tests on Linux, or our CI will complain that the tests are not all present on Linux. To do this, merely execute `ruby ./scripts/generate_linux_tests.rb` at the root of the package and check the changes it made.
SwiftLog uses Swift Testing to run tests on all supported platforms.
### Run `./scripts/soundness.sh`
### Run CI checks locally
The scripts directory contains a [soundness.sh script](https://github.com/apple/swift-log/blob/main/scripts/soundness.sh)
that enforces additional checks, like license headers and formatting style.
You can run the Github Actions workflows locally using
[act](https://github.com/nektos/act). To run all the jobs that run on a pull
request, use the following command:
Please make sure to `./scripts/soundness.sh` before pushing a change upstream, otherwise it is likely the PR validation will fail
on minor changes such as a missing `self.` or similar formatting issues.
> The script also executes the above mentioned `generate_linux_tests.rb`.
For frequent contributors, we recommend adding the script as a [git pre-push hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks), which you can do via executing the following command in the project root directory:
```bash
cat << EOF > .git/hooks/pre-push
#!/bin/bash
if [[ -f "scripts/soundness.sh" ]]; then
scripts/soundness.sh
fi
EOF
```
% act pull_request
```
Which makes the script execute, and only allow the `git push` to complete if the check has passed.
To run just a single job, use `workflow_call -j <job>`, and specify the inputs
the job expects. For example, to run just shellcheck:
In the case of formatting issues, you can then `git add` the formatting changes, and attempt the push again.
```
% act workflow_call -j soundness --input shell_check_enabled=true
```
To bind-mount the working directory to the container, rather than a copy, use
`--bind`. For example, to run just the formatting, and have the results
reflected in your working directory:
```
% act --bind workflow_call -j soundness --input format_check_enabled=true
```
If you'd like `act` to always run with certain flags, these can be be placed in
an `.actrc` file either in the current working directory or your home
directory, for example:
```
--container-architecture=linux/amd64
--remote-name upstream
--action-offline-mode
```
## How to contribute your work
-9
View File
@@ -26,15 +26,6 @@ components that this product depends on.
-------------------------------------------------------------------------------
This product contains a derivation of the Tony Stone's 'process_test_files.rb'.
* LICENSE (Apache License 2.0):
* https://www.apache.org/licenses/LICENSE-2.0
* HOMEPAGE:
* https://codegists.com/snippet/ruby/generate_xctest_linux_runnerrb_tonystone_ruby
---
This product contains a derivation of the lock implementation and various
scripts from SwiftNIO.
+3 -3
View File
@@ -14,11 +14,11 @@
// Not testable
import Logging
import XCTest
import Testing
final class CompatibilityTest: XCTestCase {
struct CompatibilityTest {
@available(*, deprecated, message: "Testing deprecated functionality")
func testAllLogLevelsWorkWithOldSchoolLogHandlerWorks() {
@Test func allLogLevelsWorkWithOldSchoolLogHandlerWorks() {
let testLogging = OldSchoolTestLogging()
var logger = Logger(label: "\(#function)", factory: { testLogging.make(label: $0) })
+110 -7
View File
@@ -13,18 +13,21 @@
//===----------------------------------------------------------------------===//
import Dispatch
import XCTest
import Testing
@testable import Logging
class GlobalLoggerTest: XCTestCase {
func test1() throws {
/// This is the only test suite allowed to use global LoggingSystem bootstrapping,
/// tests in the suite are executed one by one
@Suite(.serialized)
struct GlobalLoggerTest {
@Test func traceAndAbove() throws {
// bootstrap with our test logging impl
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
// change test logging config to log traces and above
logging.config.set(value: Logger.Level.debug)
// change test logging config to log trace and above
logging.config.set(value: Logger.Level.trace)
// run our program
Struct1().doSomething()
// test results
@@ -40,12 +43,16 @@ class GlobalLoggerTest: XCTestCase {
logging.history.assertExist(level: .debug, message: "Struct3::doSomethingElse::Local", metadata: ["baz": "qux"])
logging.history.assertExist(level: .debug, message: "Struct3::doSomethingElse::end")
logging.history.assertExist(level: .debug, message: "Struct3::doSomething::end")
logging.history.assertExist(level: .trace, message: "Struct3::doSomething::end::lastLine")
logging.history.assertExist(level: .debug, message: "Struct2::doSomethingElse::end")
logging.history.assertExist(level: .trace, message: "Struct2::doSomethingElse::end::lastLine")
logging.history.assertExist(level: .debug, message: "Struct1::doSomethingElse::end")
logging.history.assertExist(level: .debug, message: "Struct1::doSomething::end")
logging.history.assertExist(level: .trace, message: "Struct1::doSomething::end::lastLine")
}
func test2() throws {
@Test func errorAndAbove() throws {
// bootstrap with our test logging impl
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
@@ -75,12 +82,15 @@ class GlobalLoggerTest: XCTestCase {
)
logging.history.assertNotExist(level: .debug, message: "Struct3::doSomethingElse::end")
logging.history.assertNotExist(level: .debug, message: "Struct3::doSomething::end")
logging.history.assertNotExist(level: .trace, message: "Struct3::doSomething::end::lastLine")
logging.history.assertNotExist(level: .debug, message: "Struct2::doSomethingElse::end")
logging.history.assertNotExist(level: .trace, message: "Struct2::doSomethingElse::end::lastLine")
logging.history.assertNotExist(level: .debug, message: "Struct1::doSomethingElse::end")
logging.history.assertNotExist(level: .debug, message: "Struct1::doSomething::end")
logging.history.assertNotExist(level: .trace, message: "Struct1::doSomething::end::lastLine")
}
func test3() throws {
@Test func warningAndAbove() throws {
// bootstrap with our test logging impl
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
@@ -114,6 +124,96 @@ class GlobalLoggerTest: XCTestCase {
}
}
/// MetadataProvider tests relying on the global state
extension GlobalLoggerTest {
@Test func loggingMergesOneOffMetadataWithProvidedMetadataFromExplicitlyPassed() throws {
let logging = TestLogging()
LoggingSystem.bootstrapInternal(
{ logging.makeWithMetadataProvider(label: $0, metadataProvider: $1) },
metadataProvider: .init {
["common": "initial"]
}
)
let logger = Logger(
label: #function,
metadataProvider: .init {
[
"common": "provider",
"provider": "42",
]
}
)
logger.log(level: .info, "test", metadata: ["one-off": "42", "common": "one-off"])
logging.history.assertExist(
level: .info,
message: "test",
metadata: ["common": "one-off", "one-off": "42", "provider": "42"]
)
}
@Test func loggerWithoutFactoryOverrideDefaultsToUsingLoggingSystemMetadataProvider() {
let logging = TestLogging()
LoggingSystem.bootstrapInternal(
{ logging.makeWithMetadataProvider(label: $0, metadataProvider: $1) },
metadataProvider: .init { ["provider": "42"] }
)
let logger = Logger(label: #function)
logger.log(level: .info, "test", metadata: ["one-off": "42"])
logging.history.assertExist(
level: .info,
message: "test",
metadata: ["provider": "42", "one-off": "42"]
)
}
@Test func loggerWithPredefinedLibraryMetadataProvider() {
let logging = TestLogging()
LoggingSystem.bootstrapInternal(
{ logging.makeWithMetadataProvider(label: $0, metadataProvider: $1) },
metadataProvider: .exampleProvider
)
let logger = Logger(label: #function)
logger.log(level: .info, "test", metadata: ["one-off": "42"])
logging.history.assertExist(
level: .info,
message: "test",
metadata: ["example": "example-value", "one-off": "42"]
)
}
@Test func loggerWithFactoryOverrideDefaultsToUsingLoggingSystemMetadataProvider() {
let logging = TestLogging()
LoggingSystem.bootstrapInternal(
{ logging.makeWithMetadataProvider(label: $0, metadataProvider: $1) },
metadataProvider: .init { ["provider": "42"] }
)
let logger = Logger(
label: #function,
factory: { label in
logging.makeWithMetadataProvider(label: label, metadataProvider: LoggingSystem.metadataProvider)
}
)
logger.log(level: .info, "test", metadata: ["one-off": "42"])
logging.history.assertExist(
level: .info,
message: "test",
metadata: ["provider": "42", "one-off": "42"]
)
}
}
private struct Struct1 {
private let logger = Logger(label: "GlobalLoggerTest::Struct1")
@@ -127,6 +227,7 @@ private struct Struct1 {
self.logger.debug("Struct1::doSomethingElse")
Struct2().doSomething()
self.logger.debug("Struct1::doSomethingElse::end")
self.logger.trace("Struct1::doSomething::end::lastLine")
}
}
@@ -143,6 +244,7 @@ private struct Struct2 {
self.logger.info("Struct2::doSomethingElse")
Struct3().doSomething()
self.logger.debug("Struct2::doSomethingElse::end")
self.logger.trace("Struct2::doSomethingElse::end::lastLine")
}
}
@@ -154,6 +256,7 @@ private struct Struct3 {
self.logger.error("Struct3::doSomething")
self.doSomethingElse()
self.logger.debug("Struct3::doSomething::end")
self.logger.trace("Struct3::doSomething::end::lastLine")
}
private func doSomethingElse() {
+36 -18
View File
@@ -13,20 +13,20 @@
//===----------------------------------------------------------------------===//
import Dispatch
import XCTest
import Testing
@testable import Logging
class LocalLoggerTest: XCTestCase {
func test1() throws {
// bootstrap with our test logging impl
struct LocalLoggerTest {
@Test func traceAndAbove() throws {
// create test logging impl, do not bootstrap global LoggingSystem
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
// change test logging config to log traces and above
logging.config.set(value: Logger.Level.debug)
// change global test logging config to log trace and above,
// local logging context will be modified to debug
logging.config.set(value: Logger.Level.trace)
// run our program
let context = Context()
let context = Context { logging.make(label: $0) }
Struct1().doSomething(context: context)
// test results
logging.history.assertExist(level: .debug, message: "Struct1::doSomething", source: "LoggingTests")
@@ -36,29 +36,33 @@ class LocalLoggerTest: XCTestCase {
logging.history.assertExist(level: .error, message: "Struct3::doSomething", metadata: ["bar": "baz"])
logging.history.assertExist(level: .error, message: "Struct3::doSomethingElse", metadata: ["bar": "baz"])
logging.history.assertExist(level: .warning, message: "Struct3::doSomethingElseAsync", metadata: ["bar": "baz"])
logging.history.assertExist(level: .info, message: "TestLibrary::doSomething")
logging.history.assertExist(level: .info, message: "TestLibrary::doSomethingAsync")
logging.history.assertExist(
level: .debug,
message: "Struct3::doSomethingElse::Local",
metadata: ["bar": "baz", "baz": "qux"]
)
logging.history.assertExist(level: .debug, message: "Struct3::doSomethingElse::end", metadata: ["bar": "baz"])
logging.history.assertNotExist(
level: .trace,
message: "Struct3::doSomethingElse::end::lastLine",
metadata: ["bar": "baz"]
)
logging.history.assertExist(level: .debug, message: "Struct3::doSomething::end", metadata: ["bar": "baz"])
logging.history.assertExist(level: .debug, message: "Struct2::doSomethingElse::end")
logging.history.assertNotExist(level: .trace, message: "Struct2::doSomethingElse::end::lastLine")
logging.history.assertExist(level: .debug, message: "Struct1::doSomethingElse::end")
logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse::end::lastLine")
logging.history.assertExist(level: .debug, message: "Struct1::doSomething::end")
}
func test2() throws {
// bootstrap with our test logging impl
@Test func errorAndAbove() throws {
// create test logging impl, do not bootstrap global LoggingSystem
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
// change test logging config to log errors and above
logging.config.set(value: Logger.Level.error)
// run our program
let context = Context()
let context = Context { logging.make(label: $0) }
Struct1().doSomething(context: context)
// test results
// global context
@@ -79,20 +83,27 @@ class LocalLoggerTest: XCTestCase {
logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomething")
// global context
logging.history.assertNotExist(level: .info, message: "TestLibrary::doSomethingAsync")
// hyper local context
// hyper local .debug context
logging.history.assertExist(
level: .debug,
message: "Struct3::doSomethingElse::Local",
metadata: ["bar": "baz", "baz": "qux"]
)
// local context
// local .debug context
logging.history.assertExist(level: .debug, message: "Struct3::doSomethingElse::end", metadata: ["bar": "baz"])
// local context
logging.history.assertNotExist(
level: .trace,
message: "Struct3::doSomethingElse::end::lastLine",
metadata: ["bar": "baz"]
)
// local .debug context
logging.history.assertExist(level: .debug, message: "Struct2::doSomethingElse::end")
logging.history.assertNotExist(level: .trace, message: "Struct2::doSomethingElse::end::lastLine")
// local context
logging.history.assertExist(level: .debug, message: "Struct3::doSomething::end", metadata: ["bar": "baz"])
// global context
logging.history.assertNotExist(level: .debug, message: "Struct1::doSomethingElse::end")
logging.history.assertNotExist(level: .trace, message: "Struct1::doSomethingElse::end::lastLine")
// global context
logging.history.assertNotExist(level: .debug, message: "Struct1::doSomething::end")
}
@@ -100,7 +111,11 @@ class LocalLoggerTest: XCTestCase {
// systems that follow the context pattern need to implement something like this
private struct Context {
var logger = Logger(label: "LocalLoggerTest::ContextLogger")
var logger: Logger
init(_ factory: (String) -> any LogHandler) {
self.logger = Logger(label: "LocalLoggerTest::ContextLogger", factory: factory)
}
// since logger is a value type, we can reuse our copy to manage logLevel
var logLevel: Logger.Level {
@@ -147,6 +162,7 @@ private struct Struct2 {
c.logger.info("Struct2::doSomethingElse")
Struct3().doSomething(context: c)
c.logger.debug("Struct2::doSomethingElse::end")
c.logger.trace("Struct2::doSomethingElse::end::lastLine")
}
}
@@ -159,6 +175,7 @@ private struct Struct3 {
c.logger.error("Struct3::doSomething")
self.doSomethingElse(context: c)
c.logger.debug("Struct3::doSomething::end")
c.logger.trace("Struct3::doSomething::end::lastLine")
}
private func doSomethingElse(context: Context) {
@@ -179,5 +196,6 @@ private struct Struct3 {
l[metadataKey: "baz"] = "qux"
l.debug("Struct3::doSomethingElse::Local")
context.logger.debug("Struct3::doSomethingElse::end")
context.logger.trace("Struct3::doSomethingElse::end::lastLine")
}
}
File diff suppressed because it is too large. Load diff
+8 -12
View File
@@ -12,16 +12,12 @@
//
//===----------------------------------------------------------------------===//
import Dispatch
import XCTest
import Testing
@testable import Logging
class MDCTest: XCTestCase {
func test1() throws {
// bootstrap with our test logger
let logging = TestLogging()
LoggingSystem.bootstrapInternal { logging.make(label: $0) }
struct MDCTest {
@Test func test() throws {
// run the program
MDC.global["foo"] = "bar"
let group = DispatchGroup()
@@ -36,20 +32,20 @@ class MDCTest: XCTestCase {
for i in 0...remove {
MDC.global["key-\(i)"] = nil
}
XCTAssertEqual(add - remove, MDC.global.metadata.count, "expected number of entries to match")
#expect((add - remove) == MDC.global.metadata.count, "expected number of entries to match")
for i in remove + 1...add {
XCTAssertNotNil(MDC.global["key-\(i)"], "expecting value for key-\(i)")
#expect(MDC.global["key-\(i)"] != nil, "expecting value for key-\(i)")
}
for i in 0...remove {
XCTAssertNil(MDC.global["key-\(i)"], "not expecting value for key-\(i)")
#expect(MDC.global["key-\(i)"] == nil, "not expecting value for key-\(i)")
}
MDC.global.clear()
group.leave()
}
}
group.wait()
XCTAssertEqual(MDC.global["foo"], "bar", "expecting to find top items")
#expect(MDC.global["foo"] == "bar", "expecting to find top items")
MDC.global["foo"] = nil
XCTAssertTrue(MDC.global.metadata.isEmpty, "MDC should be empty")
#expect(MDC.global.metadata.isEmpty, "MDC should be empty")
}
}
+5 -33
View File
@@ -12,7 +12,7 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import Testing
@testable import Logging
@@ -26,36 +26,8 @@ import Android
import Glibc
#endif
final class MetadataProviderTest: XCTestCase {
func testLoggingMergesOneOffMetadataWithProvidedMetadataFromExplicitlyPassed() throws {
let logging = TestLogging()
LoggingSystem.bootstrapInternal(
{ logging.makeWithMetadataProvider(label: $0, metadataProvider: $1) },
metadataProvider: .init {
["common": "initial"]
}
)
let logger = Logger(
label: #function,
metadataProvider: .init {
[
"common": "provider",
"provider": "42",
]
}
)
logger.log(level: .info, "test", metadata: ["one-off": "42", "common": "one-off"])
logging.history.assertExist(
level: .info,
message: "test",
metadata: ["common": "one-off", "one-off": "42", "provider": "42"]
)
}
func testLogHandlerThatDidNotImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
struct MetadataProviderTest {
@Test func logHandlerThatDidNotImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
#if DEBUG
// we only emit these warnings in debug mode
let logging = TestLogging()
@@ -72,11 +44,11 @@ final class MetadataProviderTest: XCTestCase {
let countBefore = logging.history.entries.count
handler.metadataProvider = .simpleTestProvider
XCTAssertEqual(countBefore, logging.history.entries.count, "Should only log the warning once")
#expect(countBefore == logging.history.entries.count, "Should only log the warning once")
#endif
}
func testLogHandlerThatDidImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
@Test func logHandlerThatDidImplementProvidersButSomeoneAttemptsToSetOneOnIt() {
#if DEBUG
// we only emit these warnings in debug mode
let logging = TestLogging()
+15 -24
View File
@@ -14,7 +14,7 @@
import Dispatch
import Foundation
import XCTest
import Testing
@testable import Logging
@@ -22,7 +22,7 @@ import XCTest
import WinSDK
#endif
internal struct TestLogging {
struct TestLogging {
private let _config = Config() // shared among loggers
private let recorder = Recorder() // shared among loggers
@@ -233,17 +233,17 @@ extension History {
message: String,
metadata: Logger.Metadata? = nil,
source: String? = nil,
file: StaticString = #filePath,
file: String = #filePath,
fileID: String = #fileID,
line: UInt = #line
line: Int = #line,
column: Int = #column
) {
let source = source ?? Logger.currentModule(fileID: "\(fileID)")
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
XCTAssertNotNil(
entry,
#expect(
entry != nil,
"entry not found: \(level), \(source), \(String(describing: metadata)), \(message)",
file: file,
line: line
sourceLocation: SourceLocation(fileID: fileID, filePath: file, line: line, column: column)
)
}
@@ -252,17 +252,17 @@ extension History {
message: String,
metadata: Logger.Metadata? = nil,
source: String? = nil,
file: StaticString = #filePath,
file: String = #filePath,
fileID: String = #file,
line: UInt = #line
line: Int = #line,
column: Int = #column
) {
let source = source ?? Logger.currentModule(fileID: "\(fileID)")
let entry = self.find(level: level, message: message, metadata: metadata, source: source)
XCTAssertNil(
entry,
#expect(
entry == nil,
"entry was found: \(level), \(source), \(String(describing: metadata)), \(message)",
file: file,
line: line
sourceLocation: SourceLocation(fileID: fileID, filePath: file, line: line, column: column)
)
}
@@ -302,6 +302,7 @@ extension History {
}
}
/// MDC stands for Mapped Diagnostic Context
public class MDC {
private let lock = NSLock()
private var storage = [Int: Logger.Metadata]()
@@ -380,16 +381,6 @@ public class MDC {
}
}
extension NSLock {
func withLock<T>(_ body: () -> T) -> T {
self.lock()
defer {
self.unlock()
}
return body()
}
}
internal struct TestLibrary: Sendable {
private let logger = Logger(label: "TestLibrary")
private let queue = DispatchQueue(label: "TestLibrary")
+9 -5
View File
@@ -12,16 +12,20 @@
//
//===----------------------------------------------------------------------===//
import XCTest
import Testing
@testable import Logging
class SendableTest: XCTestCase {
func testSendableLogger() async {
struct SendableTest {
@Test func sendableLogger() async {
let testLogging = TestLogging()
LoggingSystem.bootstrapInternal { testLogging.make(label: $0) }
let logger = Logger(label: "test")
let logger = Logger(
label: "test",
factory: {
testLogging.make(label: $0)
}
)
let message1 = Logger.Message(stringLiteral: "critical 1")
let message2 = Logger.Message(stringLiteral: "critical 2")
let metadata: Logger.Metadata = ["key": "value"]