Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Testing/Events/Event+FallbackEventHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

private import _TestingInternals
private import _TestingInternals.InteropOnly

extension Event {
/// Attempt to handle an event encoded as JSON as if it had been generated in
Expand Down
52 changes: 52 additions & 0 deletions Sources/_TestingInternals/include/FallbackEventHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !defined(SWT_NO_INTEROP)
#include "Defines.h"
#include "Includes.h"

SWT_ASSUME_NONNULL_BEGIN

/// A type describing a fallback event handler that testing API can invoke as an
/// alternate method of reporting test events to the current test runner.
/// Shadows the type with the same name in _TestingInterop.
///
/// - Parameters:
/// - recordJSONSchemaVersionNumber: The JSON schema version used to encode
/// the event record.
/// - recordJSONBaseAddress: A pointer to the first byte of the encoded event.
/// - recordJSONByteCount: The size of the encoded event in bytes.
/// - reserved: Reserved for future use.
typedef void (* SWTFallbackEventHandler)(const char *recordJSONSchemaVersionNumber,
const void *recordJSONBaseAddress,
size_t recordJSONByteCount,
const void *_Nullable reserved);

/// Set the current fallback event handler if one has not already been set.
///
/// - Parameters:
/// - handler: The handler function to set.
///
/// - Returns: Whether or not `handler` was installed.
///
/// The fallback event handler can only be installed once per process, typically
/// by the first testing library to run. If this function has already been
/// called and the handler set, it does not replace the previous handler.
SWT_EXTERN bool _swift_testing_installFallbackEventHandler(SWTFallbackEventHandler handler);

/// Get the current fallback event handler.
/// Shadows the function with the same name in _TestingInterop.
///
/// - Returns: The currently-set handler function, if any.
SWT_EXTERN SWTFallbackEventHandler _Nullable _swift_testing_getFallbackEventHandler(void);

SWT_ASSUME_NONNULL_END

#endif
37 changes: 0 additions & 37 deletions Sources/_TestingInternals/include/Stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,43 +275,6 @@ static const char *_Nullable swt_getExitCodeName(int exitCode) {
#undef SWT_SYSEXIT_CODE
};

#if !SWT_NO_INTEROP

/// A type describing a fallback event handler that testing API can invoke as an
/// alternate method of reporting test events to the current test runner.
/// Shadows the type with the same name in _TestingInterop.
///
/// - Parameters:
/// - recordJSONSchemaVersionNumber: The JSON schema version used to encode
/// the event record.
/// - recordJSONBaseAddress: A pointer to the first byte of the encoded event.
/// - recordJSONByteCount: The size of the encoded event in bytes.
/// - reserved: Reserved for future use.
typedef void (* SWTFallbackEventHandler)(const char *recordJSONSchemaVersionNumber,
const void *recordJSONBaseAddress,
size_t recordJSONByteCount,
const void *_Nullable reserved);

/// Set the current fallback event handler if one has not already been set.
///
/// - Parameters:
/// - handler: The handler function to set.
///
/// - Returns: Whether or not `handler` was installed.
///
/// The fallback event handler can only be installed once per process, typically
/// by the first testing library to run. If this function has already been
/// called and the handler set, it does not replace the previous handler.
SWT_EXTERN bool _swift_testing_installFallbackEventHandler(SWTFallbackEventHandler handler);

/// Get the current fallback event handler.
/// Shadows the function with the same name in _TestingInterop.
///
/// - Returns: The currently-set handler function, if any.
SWT_EXTERN SWTFallbackEventHandler _Nullable _swift_testing_getFallbackEventHandler(void);

#endif

SWT_ASSUME_NONNULL_END

#endif
5 changes: 5 additions & 0 deletions Sources/_TestingInternals/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ module _TestingInternals {
header "Stubs.h"
export *
}

explicit module InteropOnly {
header "FallbackEventHandler.h"
export *
}
}
15 changes: 12 additions & 3 deletions Sources/_TestingInterop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

include(ModuleABIName)
add_library(_TestingInterop
FallbackEventHandler.swift)
FallbackEventHandler.cpp)
if("${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}" STREQUAL "MSVC" OR
"${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
target_compile_options(_TestingInternals PRIVATE
Comment thread
grynspan marked this conversation as resolved.
Outdated
/EHa-c)
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
target_compile_options(_TestingInternals PRIVATE
-fno-exceptions -fPIC)
else()
target_compile_options(_TestingInternals PRIVATE
-fno-exceptions)
endif()

target_link_libraries(_TestingInterop PRIVATE
_TestingInternals)
if(NOT BUILD_SHARED_LIBS)
# When building a static library, tell clients to autolink the internal
# libraries.
Expand Down
27 changes: 27 additions & 0 deletions Sources/_TestingInterop/FallbackEventHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025–2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

#if !defined(SWT_NO_INTEROP)
#include "../_TestingInternals/include/FallbackEventHandler.h"

#include <atomic>

/// Storage for the fallback event handler.
static std::atomic<SWTFallbackEventHandler> fallbackEventHandler { nullptr };

bool _swift_testing_installFallbackEventHandler(SWTFallbackEventHandler handler) {
SWTFallbackEventHandler nullptrValue = nullptr;
return fallbackEventHandler.compare_exchange_strong(nullptrValue, handler, std::memory_order_seq_cst, std::memory_order_relaxed);
}

SWTFallbackEventHandler _swift_testing_getFallbackEventHandler(void) {
return fallbackEventHandler.load(std::memory_order_seq_cst);
}
#endif
112 changes: 0 additions & 112 deletions Sources/_TestingInterop/FallbackEventHandler.swift

This file was deleted.

1 change: 1 addition & 0 deletions Sources/_TestingInterop/include/Empty.h
Comment thread
grynspan marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion Tests/TestingTests/EventHandlingInteropTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//

@testable @_spi(ForToolsIntegrationOnly) import Testing
private import _TestingInternals
private import _TestingInternals.InteropOnly

#if canImport(Foundation)
import Foundation
Expand Down
Loading