Skip to content

Commit 1d2e39c

Browse files
authored
Add some WASI-specific branches. (#583)
Adds some WASI-specific branches to platform-specific code (instead of falling back to the "unknown" case with warnings.) ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 7cfebf3 commit 1d2e39c

File tree

8 files changed

+66
-4
lines changed

8 files changed

+66
-4
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ extension Array where Element == PackageDescription.SwiftSetting {
133133
.define("SWT_NO_FILE_IO", .when(platforms: [.wasi])),
134134
.define("SWT_NO_EXIT_TESTS", .when(platforms: [.iOS, .watchOS, .tvOS, .visionOS, .wasi])),
135135
.define("SWT_NO_SNAPSHOT_TYPES", .when(platforms: [.linux, .windows, .wasi])),
136+
.define("SWT_NO_DYNAMIC_LINKING", .when(platforms: [.wasi])),
136137
]
137138
}
138139

Sources/Testing/ABI/EntryPoints/EntryPoint.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ extension Event.ConsoleOutputRecorder.Options {
585585
result.ansiColorBitDepth = 24
586586
} else if _terminalSupports256ColorANSIEscapeCodes {
587587
result.ansiColorBitDepth = 8
588+
} else if _terminalSupports16ColorANSIEscapeCodes {
589+
result.ansiColorBitDepth = 4
588590
}
589591
}
590592

@@ -632,6 +634,28 @@ extension Event.ConsoleOutputRecorder.Options {
632634
return false
633635
}
634636

637+
/// Whether or not the system terminal claims to support 16-color ANSI escape
638+
/// codes.
639+
private static var _terminalSupports16ColorANSIEscapeCodes: Bool {
640+
#if SWT_TARGET_OS_APPLE || os(Linux)
641+
if let termVariable = Environment.variable(named: "TERM") {
642+
return termVariable != "dumb"
643+
}
644+
return false
645+
#elseif os(Windows)
646+
// Windows does not set the "TERM" variable, so assume it supports 16-color
647+
// ANSI escape codes.
648+
true
649+
#elseif os(WASI)
650+
// The "Terminal" under WASI can be assumed to be the browser's JavaScript
651+
// console, which we don't expect supports color escape codes.
652+
false
653+
#else
654+
#warning("Platform-specific implementation missing: terminal colors unavailable")
655+
return false
656+
#endif
657+
}
658+
635659
/// Whether or not the system terminal claims to support 256-color ANSI escape
636660
/// codes.
637661
private static var _terminalSupports256ColorANSIEscapeCodes: Bool {
@@ -644,6 +668,10 @@ extension Event.ConsoleOutputRecorder.Options {
644668
// Windows does not set the "TERM" variable, so assume it supports 256-color
645669
// ANSI escape codes.
646670
true
671+
#elseif os(WASI)
672+
// The "Terminal" under WASI can be assumed to be the browser's JavaScript
673+
// console, which we don't expect supports color escape codes.
674+
false
647675
#else
648676
#warning("Platform-specific implementation missing: terminal colors unavailable")
649677
return false
@@ -662,6 +690,10 @@ extension Event.ConsoleOutputRecorder.Options {
662690
// Windows does not set the "COLORTERM" variable, so assume it supports
663691
// true-color ANSI escape codes. SEE: https://github.com/microsoft/terminal/issues/11057
664692
true
693+
#elseif os(WASI)
694+
// The "Terminal" under WASI can be assumed to be the browser's JavaScript
695+
// console, which we don't expect supports color escape codes.
696+
false
665697
#else
666698
#warning("Platform-specific implementation missing: terminal colors unavailable")
667699
return false

Sources/Testing/ABI/EntryPoints/SwiftPMEntryPoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private import _TestingInternals
2424
///
2525
/// This constant is not part of the public interface of the testing library.
2626
var EXIT_NO_TESTS_FOUND: CInt {
27-
#if SWT_TARGET_OS_APPLE || os(Linux)
27+
#if SWT_TARGET_OS_APPLE || os(Linux) || os(WASI)
2828
EX_UNAVAILABLE
2929
#elseif os(Windows)
3030
ERROR_NOT_FOUND

Sources/Testing/Events/Recorder/Event.ConsoleOutputRecorder.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ extension Event {
3939
///
4040
/// Allowed values are `1` (no color support), `4` (16-color), `8`
4141
/// (256-color), and `24` (true color.) The default value of this property
42-
/// is `4` (16-color.)
42+
/// is `1` (no color support.) When using Swift Testing from the command
43+
/// line with `swift test`, the environment is automatically inspected to
44+
/// determine what color support is available.
4345
///
4446
/// The value of this property is ignored unless the value of
4547
/// ``useANSIEscapeCodes`` is `true`.
46-
public var ansiColorBitDepth: Int8 = 4
48+
public var ansiColorBitDepth: Int8 = 1
4749

4850
#if os(macOS) || (os(iOS) && targetEnvironment(macCatalyst))
4951
/// Whether or not to use [SF Symbols](https://developer.apple.com/sf-symbols/)

Sources/Testing/Support/Additions/CommandLineAdditions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ extension CommandLine {
4848
}
4949
return path
5050
}
51+
#elseif os(WASI)
52+
// WASI does not really have the concept of a file system path to the main
53+
// executable, so simply return the first argument--presumably the program
54+
// name, but as you know this is not guaranteed by the C standard!
55+
return String(cString: arguments[0])
5156
#else
5257
#warning("Platform-specific implementation missing: executable path unavailable")
5358
return ""

Sources/Testing/Support/FileHandle.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ extension FileHandle {
374374
#if SWT_TARGET_OS_APPLE || os(Linux)
375375
// If stderr is a TTY and TERM is set, that's good enough for us.
376376
withUnsafePOSIXFileDescriptor { fd in
377-
if let fd, 0 != isatty(fd), let term = Environment.variable(named: "TERM"), !term.isEmpty && term != "dumb" {
377+
if let fd, 0 != isatty(fd), let term = Environment.variable(named: "TERM"), !term.isEmpty {
378378
return true
379379
}
380380
return false

Sources/Testing/Support/Versions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ let operatingSystemVersion: String = {
8383
return result
8484
}
8585
}
86+
#elseif os(WASI)
87+
if let libcVersion = swt_getWASIVersion().flatMap(String.init(validatingCString:)), !libcVersion.isEmpty {
88+
return "WASI with libc \(libcVersion)"
89+
}
8690
#else
8791
#warning("Platform-specific implementation missing: OS version unavailable")
8892
#endif

Sources/_TestingInternals/include/Stubs.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ static int swt_siginfo_t_si_status(const siginfo_t *siginfo) {
121121
}
122122
#endif
123123

124+
//#if defined(__wasi__)
125+
/// Get the version of the C standard library and runtime used by WASI, if
126+
/// available.
127+
///
128+
/// This function is provided because `WASI_LIBC_VERSION` may or may not be
129+
/// defined and may or may not be a complex macro.
130+
///
131+
/// For more information about the `WASI_LIBC_VERSION` macro, see
132+
/// [wasi-libc-#490](https://github.com/WebAssembly/wasi-libc/issues/490).
133+
static const char *_Nullable swt_getWASIVersion(void) {
134+
#if defined(WASI_LIBC_VERSION)
135+
return WASI_LIBC_VERSION;
136+
#else
137+
return 0;
138+
#endif
139+
}
140+
//#endif
141+
124142
SWT_ASSUME_NONNULL_END
125143

126144
#endif

0 commit comments

Comments
 (0)