Skip to content

Commit

Permalink
Replace windows with windows-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake-Shadle committed Dec 20, 2023
1 parent 3a61f35 commit f225800
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 49 deletions.
23 changes: 2 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nextest-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ nix = { version = "0.27.1", default-features = false, features = ["signal"] }
# https://docs.rs/winapi/0.3.9/src/winapi/lib.rs.html#35-37
# Otherwise nextest-runner runs into compilation issues with win32job.
winapi = { version = "0.3.9", features = ["std"] }
windows = { version = "0.52.0", features = [
windows-sys = { version = "0.52.0", features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_System_Console",
Expand Down
10 changes: 8 additions & 2 deletions nextest-runner/src/cargo_config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn relative_dir_for(config_path: &Utf8Path) -> Option<&Utf8Path> {
mod imp {
use super::*;
use std::{borrow::Borrow, cmp, ffi::OsStr, os::windows::prelude::OsStrExt};
use windows::Win32::Globalization::{
use windows_sys::Win32::Globalization::{
CompareStringOrdinal, CSTR_EQUAL, CSTR_GREATER_THAN, CSTR_LESS_THAN,
};

Expand Down Expand Up @@ -203,7 +203,13 @@ mod imp {
impl Ord for EnvKey {
fn cmp(&self, other: &Self) -> cmp::Ordering {
unsafe {
let result = CompareStringOrdinal(&self.utf16, &other.utf16, true);
let result = CompareStringOrdinal(
self.utf16.as_ptr(),
self.utf16.len() as _,
other.utf16.as_ptr(),
other.utf16.len() as _,
1, /* ignore case */
);
match result {
CSTR_LESS_THAN => cmp::Ordering::Less,
CSTR_EQUAL => cmp::Ordering::Equal,
Expand Down
2 changes: 1 addition & 1 deletion nextest-runner/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ pub enum ConfigureHandleInheritanceError {
/// An error occurred. This can only happen on Windows.
#[cfg(windows)]
#[error("error configuring handle inheritance")]
WindowsError(#[from] windows::core::Error),
WindowsError(#[from] std::io::Error),
}

/// An error that occurs while building the test runner.
Expand Down
12 changes: 5 additions & 7 deletions nextest-runner/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ pub(crate) fn extract_abort_status(exit_status: ExitStatus) -> Option<AbortStatu
exit_status.signal().map(AbortStatus::UnixSignal)
} else if #[cfg(windows)] {
exit_status.code().and_then(|code| {
let exception = windows::Win32::Foundation::NTSTATUS(code);
exception.is_err().then(|| AbortStatus::WindowsNtStatus(exception))
(code < 0).then(|| AbortStatus::WindowsNtStatus(code))
})
} else {
None
Expand Down Expand Up @@ -226,19 +225,18 @@ pub(crate) fn signal_str(signal: i32) -> Option<&'static str> {
}

#[cfg(windows)]
pub(crate) fn display_nt_status(nt_status: windows::Win32::Foundation::NTSTATUS) -> String {
pub(crate) fn display_nt_status(nt_status: windows_sys::Win32::Foundation::NTSTATUS) -> String {
// Convert the NTSTATUS to a Win32 error code.
let win32_code = unsafe { windows::Win32::Foundation::RtlNtStatusToDosError(nt_status) };
let win32_code = unsafe { windows_sys::Win32::Foundation::RtlNtStatusToDosError(nt_status) };

if win32_code == windows::Win32::Foundation::ERROR_MR_MID_NOT_FOUND.0 {
if win32_code == windows_sys::Win32::Foundation::ERROR_MR_MID_NOT_FOUND {
// The Win32 code was not found.
let nt_status = nt_status.0;
return format!("{nt_status:#x} ({nt_status})");
}

return format!(
"{:#x}: {}",
nt_status.0,
nt_status,
io::Error::from_raw_os_error(win32_code as i32)
);
}
Expand Down
2 changes: 1 addition & 1 deletion nextest-runner/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ impl<'a> TestReporterImpl<'a> {
#[cfg(windows)]
fn write_windows_message_line(
&self,
nt_status: windows::Win32::Foundation::NTSTATUS,
nt_status: windows_sys::Win32::Foundation::NTSTATUS,
writer: &mut dyn Write,
) -> io::Result<()> {
write!(writer, "{:>12} ", "Message".style(self.styles.fail))?;
Expand Down
35 changes: 19 additions & 16 deletions nextest-runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,8 +1170,7 @@ fn create_execution_result(exit_status: ExitStatus, leaked: bool) -> ExecutionRe
let abort_status = exit_status.signal().map(AbortStatus::UnixSignal);
} else if #[cfg(windows)] {
let abort_status = exit_status.code().and_then(|code| {
let exception = windows::Win32::Foundation::NTSTATUS(code);
exception.is_err().then(|| AbortStatus::WindowsNtStatus(exception))
(code < 0).then(|| AbortStatus::WindowsNtStatus(code))
});
} else {
let abort_status = None;
Expand Down Expand Up @@ -2084,7 +2083,7 @@ pub enum AbortStatus {

/// The test was determined to have aborted because the high bit was set on Windows.
#[cfg(windows)]
WindowsNtStatus(windows::Win32::Foundation::NTSTATUS),
WindowsNtStatus(windows_sys::Win32::Foundation::NTSTATUS),
}

/// Configures stdout, stdin and stderr inheritance by test processes on Windows.
Expand All @@ -2110,8 +2109,8 @@ mod imp {
use super::*;
pub(super) use win32job::Job;
use win32job::JobError;
use windows::Win32::{
Foundation::{SetHandleInformation, HANDLE, HANDLE_FLAGS, HANDLE_FLAG_INHERIT},
use windows_sys::Win32::{
Foundation::{SetHandleInformation, HANDLE_FLAG_INHERIT, INVALID_HANDLE_VALUE},
System::{
Console::{GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE},
JobObjects::TerminateJobObject,
Expand All @@ -2121,22 +2120,26 @@ mod imp {
pub(super) fn configure_handle_inheritance_impl(
no_capture: bool,
) -> Result<(), ConfigureHandleInheritanceError> {
fn set_handle_inherit(handle: HANDLE, inherit: bool) -> windows::core::Result<()> {
let flags = if inherit { HANDLE_FLAG_INHERIT.0 } else { 0 };
unsafe { SetHandleInformation(handle, HANDLE_FLAG_INHERIT.0, HANDLE_FLAGS(flags)) }
unsafe fn set_handle_inherit(handle: u32, inherit: bool) -> std::io::Result<()> {
let handle = GetStdHandle(handle);
if handle == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error());
}
let flags = if inherit { HANDLE_FLAG_INHERIT } else { 0 };
if SetHandleInformation(handle, HANDLE_FLAG_INHERIT, flags) == 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}

unsafe {
let stdin = GetStdHandle(STD_INPUT_HANDLE)?;
// Never inherit stdin.
set_handle_inherit(stdin, false)?;
set_handle_inherit(STD_INPUT_HANDLE, false)?;

// Inherit stdout and stderr if and only if no_capture is true.

let stdout = GetStdHandle(STD_OUTPUT_HANDLE)?;
set_handle_inherit(stdout, no_capture)?;
let stderr = GetStdHandle(STD_ERROR_HANDLE)?;
set_handle_inherit(stderr, no_capture)?;
set_handle_inherit(STD_OUTPUT_HANDLE, no_capture)?;
set_handle_inherit(STD_ERROR_HANDLE, no_capture)?;
}

Ok(())
Expand Down Expand Up @@ -2184,7 +2187,7 @@ mod imp {
unsafe {
// Ignore the error here -- it's likely due to the process exiting.
// Note: 1 is the exit code returned by Windows.
_ = TerminateJobObject(HANDLE(handle as isize), 1);
_ = TerminateJobObject(handle as _, 1);
}
}
// Start killing the process directly for good measure.
Expand Down

0 comments on commit f225800

Please sign in to comment.