Skip to content

Commit d8af258

Browse files
authored
Tests: enable tests on Windows (apple#198)
This adds support for running the tests on Windows. However, running the tests on Windows requires SPM currently. While it is possible to enable executing tests with CMake, the expectation is that SPM builds would be the most common and CMake is largely used for bootstrapping. The changes here require alterations to remove the POSIX threading to get the thread ID. Additionally, pipes on Windows are not similar to pipes on Unix platforms. Finally, we must add Windows to the platform list, which is temporary (it would be better to use test discovery in SPM).
1 parent 02770d9 commit d8af258

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

Tests/LinuxMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import XCTest
2222
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
2323
///
2424

25-
#if os(Linux) || os(FreeBSD)
25+
#if os(Linux) || os(FreeBSD) || os(Windows)
2626
@testable import LoggingTests
2727

2828
XCTMain([

Tests/LoggingTests/LoggingTest.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import XCTest
1616

1717
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
1818
import Darwin
19+
#elseif os(Windows)
20+
import WinSDK
1921
#else
2022
import Glibc
2123
#endif
@@ -769,7 +771,7 @@ class LoggingTest: XCTestCase {
769771

770772
let size = read(readFD, readBuffer, 256)
771773

772-
let output = String(decoding: UnsafeRawBufferPointer(start: UnsafeRawPointer(readBuffer), count: size), as: UTF8.self)
774+
let output = String(decoding: UnsafeRawBufferPointer(start: UnsafeRawPointer(readBuffer), count: numericCast(size)), as: UTF8.self)
773775
let messageSucceeded = output.trimmingCharacters(in: .whitespacesAndNewlines).hasSuffix(testString)
774776
XCTAssertTrue(messageSucceeded)
775777
}
@@ -806,10 +808,17 @@ class LoggingTest: XCTestCase {
806808

807809
func withWriteReadFDsAndReadBuffer(_ body: (UnsafeMutablePointer<FILE>, CInt, UnsafeMutablePointer<Int8>) -> Void) {
808810
var fds: [Int32] = [-1, -1]
811+
#if os(Windows)
812+
fds.withUnsafeMutableBufferPointer {
813+
let err = _pipe($0.baseAddress, 256, _O_BINARY)
814+
XCTAssertEqual(err, 0, "_pipe failed \(err)")
815+
}
816+
#else
809817
fds.withUnsafeMutableBufferPointer { ptr in
810818
let err = pipe(ptr.baseAddress!)
811819
XCTAssertEqual(err, 0, "pipe failed \(err)")
812820
}
821+
#endif
813822

814823
let writeFD = fdopen(fds[1], "w")
815824
let writeBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
@@ -822,8 +831,17 @@ class LoggingTest: XCTestCase {
822831
XCTAssertEqual(err, 0, "setvbuf failed \(err)")
823832

824833
let readFD = fds[0]
834+
#if os(Windows)
835+
let hPipe: HANDLE = HANDLE(bitPattern: _get_osfhandle(readFD))!
836+
XCTAssertFalse(hPipe == INVALID_HANDLE_VALUE)
837+
838+
var dwMode: DWORD = DWORD(PIPE_NOWAIT)
839+
let bSucceeded = SetNamedPipeHandleState(hPipe, &dwMode, nil, nil)
840+
XCTAssertTrue(bSucceeded)
841+
#else
825842
err = fcntl(readFD, F_SETFL, fcntl(readFD, F_GETFL) | O_NONBLOCK)
826843
XCTAssertEqual(err, 0, "fcntl failed \(err)")
844+
#endif
827845

828846
let readBuffer = UnsafeMutablePointer<Int8>.allocate(capacity: 256)
829847
defer {

Tests/LoggingTests/TestLogger.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import Foundation
1515
@testable import Logging
1616
import XCTest
17+
#if os(Windows)
18+
import WinSDK
19+
#endif
1720

1821
internal struct TestLogging {
1922
private let _config = Config() // shared among loggers
@@ -299,6 +302,8 @@ public class MDC {
299302
private var threadId: Int {
300303
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
301304
return Int(pthread_mach_thread_np(pthread_self()))
305+
#elseif os(Windows)
306+
return Int(GetCurrentThreadId())
302307
#else
303308
return Int(pthread_self())
304309
#endif

scripts/generate_linux_tests.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files)
9494
file.write header(fileName)
9595
file.write "\n"
9696

97-
file.write "#if os(Linux) || os(FreeBSD)\n"
97+
file.write "#if os(Linux) || os(FreeBSD) || os(Windows)\n"
9898
for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y }
9999
file.write '@testable import ' + testSubDirectory + "\n"
100100
end

0 commit comments

Comments
 (0)