From 22eaa6ca941ac8a426e574e3d78370156e472f31 Mon Sep 17 00:00:00 2001 From: Ruby Lazuli Date: Sat, 14 Aug 2021 10:47:22 -0500 Subject: [PATCH] tests: add some no_run tests --- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 88777c1..4b81eee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,3 +8,49 @@ mod linux; pub use linux::*; #[cfg(not(target_os = "linux"))] compile_error!("pidfds are currently only supported on Linux"); + +#[cfg(any(test, doctest))] +mod tests { + /// Used to allow for `no_run` tests. + /// + /// Using `pidfd`: + /// ```no_run (/usr/bin/foo likely does not exist and might close fd 1 regardless) + /// use pidfd::PidFd; + /// use pidfd_getfd::{GetFdFlags, PidFdExt}; + /// use std::{io, process::Command}; + /// + /// fn main() -> io::Result<()> { + /// let child = Command::new("/usr/bin/foo").spawn().expect("failed to run `foo`"); + /// let pidfd = PidFd::from_std_checked(&child)?; + /// let file_from_child = pidfd.get_file(1, GetFdFlags::empty())?; + /// Ok(()) + /// } + /// ``` + struct _DocTests; + + #[cfg(feature = "nightly")] + /// Used to allow for `no_run` tests. + /// + /// Using `std`'s `PidFd`: + /// ```no_run (/usr/bin/foo likely does not exist and might close fd 1 regardless) + /// #![feature(linux_pidfd)] + /// + /// use pidfd_getfd::{GetFdFlags, PidFdExt}; + /// use std::{ + /// io, + /// os::linux::process::{ChildExt, CommandExt}, + /// process::Command, + /// }; + /// + /// fn main() -> io::Result<()> { + /// let child = Command::new("/usr/bin/foo") + /// .create_pidfd(true) + /// .spawn() + /// .expect("failed to run `foo`"); + /// + /// let file_from_child = child.pidfd()?.get_file(1, GetFdFlags::empty())?; + /// Ok(()) + /// } + /// ``` + struct _NightlyDocTests; +}