From d8af258aecd93cd8604e74ade3c0fe438bac13f8 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Mon, 14 Jun 2021 14:25:41 -0700 Subject: [PATCH] Tests: enable tests on Windows (#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). --- Tests/LinuxMain.swift | 2 +- Tests/LoggingTests/LoggingTest.swift | 20 +++++++++++++++++++- Tests/LoggingTests/TestLogger.swift | 5 +++++ scripts/generate_linux_tests.rb | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 84a6164..16d9ad5 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -22,7 +22,7 @@ import XCTest /// Do NOT edit this file directly as it will be regenerated automatically when needed. /// -#if os(Linux) || os(FreeBSD) +#if os(Linux) || os(FreeBSD) || os(Windows) @testable import LoggingTests XCTMain([ diff --git a/Tests/LoggingTests/LoggingTest.swift b/Tests/LoggingTests/LoggingTest.swift index ec813ad..daa0085 100644 --- a/Tests/LoggingTests/LoggingTest.swift +++ b/Tests/LoggingTests/LoggingTest.swift @@ -16,6 +16,8 @@ import XCTest #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) import Darwin +#elseif os(Windows) +import WinSDK #else import Glibc #endif @@ -769,7 +771,7 @@ class LoggingTest: XCTestCase { let size = read(readFD, readBuffer, 256) - let output = String(decoding: UnsafeRawBufferPointer(start: UnsafeRawPointer(readBuffer), count: size), as: UTF8.self) + let output = String(decoding: UnsafeRawBufferPointer(start: UnsafeRawPointer(readBuffer), count: numericCast(size)), as: UTF8.self) let messageSucceeded = output.trimmingCharacters(in: .whitespacesAndNewlines).hasSuffix(testString) XCTAssertTrue(messageSucceeded) } @@ -806,10 +808,17 @@ class LoggingTest: XCTestCase { func withWriteReadFDsAndReadBuffer(_ body: (UnsafeMutablePointer, CInt, UnsafeMutablePointer) -> Void) { var fds: [Int32] = [-1, -1] + #if os(Windows) + fds.withUnsafeMutableBufferPointer { + let err = _pipe($0.baseAddress, 256, _O_BINARY) + XCTAssertEqual(err, 0, "_pipe failed \(err)") + } + #else fds.withUnsafeMutableBufferPointer { ptr in let err = pipe(ptr.baseAddress!) XCTAssertEqual(err, 0, "pipe failed \(err)") } + #endif let writeFD = fdopen(fds[1], "w") let writeBuffer = UnsafeMutablePointer.allocate(capacity: 256) @@ -822,8 +831,17 @@ class LoggingTest: XCTestCase { XCTAssertEqual(err, 0, "setvbuf failed \(err)") let readFD = fds[0] + #if os(Windows) + let hPipe: HANDLE = HANDLE(bitPattern: _get_osfhandle(readFD))! + XCTAssertFalse(hPipe == INVALID_HANDLE_VALUE) + + var dwMode: DWORD = DWORD(PIPE_NOWAIT) + let bSucceeded = SetNamedPipeHandleState(hPipe, &dwMode, nil, nil) + XCTAssertTrue(bSucceeded) + #else err = fcntl(readFD, F_SETFL, fcntl(readFD, F_GETFL) | O_NONBLOCK) XCTAssertEqual(err, 0, "fcntl failed \(err)") + #endif let readBuffer = UnsafeMutablePointer.allocate(capacity: 256) defer { diff --git a/Tests/LoggingTests/TestLogger.swift b/Tests/LoggingTests/TestLogger.swift index a9cbf82..984649c 100644 --- a/Tests/LoggingTests/TestLogger.swift +++ b/Tests/LoggingTests/TestLogger.swift @@ -14,6 +14,9 @@ import Foundation @testable import Logging import XCTest +#if os(Windows) +import WinSDK +#endif internal struct TestLogging { private let _config = Config() // shared among loggers @@ -299,6 +302,8 @@ public class MDC { private var threadId: Int { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) return Int(pthread_mach_thread_np(pthread_self())) + #elseif os(Windows) + return Int(GetCurrentThreadId()) #else return Int(pthread_self()) #endif diff --git a/scripts/generate_linux_tests.rb b/scripts/generate_linux_tests.rb index b90712e..d43a113 100755 --- a/scripts/generate_linux_tests.rb +++ b/scripts/generate_linux_tests.rb @@ -94,7 +94,7 @@ def createLinuxMain(testsDirectory, allTestSubDirectories, files) file.write header(fileName) file.write "\n" - file.write "#if os(Linux) || os(FreeBSD)\n" + file.write "#if os(Linux) || os(FreeBSD) || os(Windows)\n" for testSubDirectory in allTestSubDirectories.sort { |x, y| x <=> y } file.write '@testable import ' + testSubDirectory + "\n" end