windows: Fix tests on Windows (#22616)

Release Notes:

- N/A

---------

Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
This commit is contained in:
张小白 2025-02-05 22:30:09 +08:00 committed by GitHub
parent c252b5db16
commit 74c4dbd237
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 1540 additions and 856 deletions

View file

@ -0,0 +1,18 @@
[package]
name = "util_macros"
version = "0.1.0"
edition.workspace = true
publish.workspace = true
license = "GPL-3.0-or-later"
[lints]
workspace = true
[lib]
path = "src/util_macros.rs"
proc-macro = true
doctest = false
[dependencies]
quote.workspace = true
syn.workspace = true

View file

@ -0,0 +1 @@
../../LICENSE-APACHE

View file

@ -0,0 +1,56 @@
#![cfg_attr(not(target_os = "windows"), allow(unused))]
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};
/// This macro replaces the path separator `/` with `\` for Windows.
/// But if the target OS is not Windows, the path is returned as is.
///
/// # Example
/// ```rust
/// # use util_macros::separator;
/// let path = separator!("path/to/file");
/// #[cfg(target_os = "windows")]
/// assert_eq!(path, "path\\to\\file");
/// #[cfg(not(target_os = "windows"))]
/// assert_eq!(path, "path/to/file");
/// ```
#[proc_macro]
pub fn separator(input: TokenStream) -> TokenStream {
let path = parse_macro_input!(input as LitStr);
let path = path.value();
#[cfg(target_os = "windows")]
let path = path.replace("/", "\\");
TokenStream::from(quote! {
#path
})
}
/// This macro replaces the path prefix `file:///` with `file:///C:/` for Windows.
/// But if the target OS is not Windows, the URI is returned as is.
///
/// # Example
/// ```rust
/// use util_macros::uri;
///
/// let uri = uri!("file:///path/to/file");
/// #[cfg(target_os = "windows")]
/// assert_eq!(uri, "file:///C:/path/to/file");
/// #[cfg(not(target_os = "windows"))]
/// assert_eq!(uri, "file:///path/to/file");
/// ```
#[proc_macro]
pub fn uri(input: TokenStream) -> TokenStream {
let uri = parse_macro_input!(input as LitStr);
let uri = uri.value();
#[cfg(target_os = "windows")]
let uri = uri.replace("file:///", "file:///C:/");
TokenStream::from(quote! {
#uri
})
}