Skip to content

Commit

Permalink
Factor out common code from the various skip macros
Browse files Browse the repository at this point in the history
  • Loading branch information
asomers committed May 2, 2020
1 parent ca973ba commit 78d6746
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
51 changes: 18 additions & 33 deletions test/test.rs
Expand Up @@ -14,22 +14,27 @@ extern crate sysctl;
extern crate tempfile;
extern crate semver;

macro_rules! skip {
($($reason: expr),+) => {
use ::std::io::{self, Write};

let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle, $($reason),+).unwrap();
return;
}
}

cfg_if! {
if #[cfg(any(target_os = "android", target_os = "linux"))] {
macro_rules! require_capability {
($capname:ident) => {
use ::caps::{Capability, CapSet, has_cap};
use ::std::io::{self, Write};

if !has_cap(None, CapSet::Effective, Capability::$capname)
.unwrap()
{
let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle,
"Insufficient capabilities. Skipping test.")
.unwrap();
return;
skip!("Insufficient capabilities. Skipping test.");
}
}
}
Expand All @@ -48,12 +53,7 @@ macro_rules! skip_if_jailed {
if let CtlValue::Int(1) = ::sysctl::value("security.jail.jailed")
.unwrap()
{
use ::std::io::Write;
let stderr = ::std::io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "{} cannot run in a jail. Skipping test.", $name)
.unwrap();
return;
skip!("{} cannot run in a jail. Skipping test.", $name);
}
}
}
Expand All @@ -63,11 +63,7 @@ macro_rules! skip_if_not_root {
use nix::unistd::Uid;

if !Uid::current().is_root() {
use ::std::io::Write;
let stderr = ::std::io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "{} requires root privileges. Skipping test.", $name).unwrap();
return;
skip!("{} requires root privileges. Skipping test.", $name);
}
};
}
Expand All @@ -82,13 +78,8 @@ cfg_if! {
if fields.next() == Some("Seccomp:") &&
fields.next() != Some("0")
{
use ::std::io::Write;
let stderr = ::std::io::stderr();
let mut handle = stderr.lock();
writeln!(handle,
"{} cannot be run in Seccomp mode. Skipping test.",
stringify!($name)).unwrap();
return;
skip!("{} cannot be run in Seccomp mode. Skipping test.",
stringify!($name));
}
}
}
Expand All @@ -105,7 +96,6 @@ cfg_if! {
if #[cfg(target_os = "linux")] {
macro_rules! require_kernel_version {
($name:expr, $version_requirement:expr) => {
use ::std::io::Write;
use semver::{Version, VersionReq};

let version_requirement = VersionReq::parse($version_requirement)
Expand All @@ -120,13 +110,8 @@ cfg_if! {
version.build.clear();

if !version_requirement.matches(&version) {
let stderr = ::std::io::stderr();
let mut handle = stderr.lock();

writeln!(handle,
"Skip {} because kernel version `{}` doesn't match the requirement `{}`",
stringify!($name), version, version_requirement).unwrap();
return;
skip!("Skip {} because kernel version `{}` doesn't match the requirement `{}`",
stringify!($name), version, version_requirement);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/test_unistd.rs
Expand Up @@ -872,7 +872,7 @@ fn test_access_file_exists() {
#[test]
fn test_setfsuid() {
use std::os::unix::fs::PermissionsExt;
use std::{fs, thread};
use std::{fs, io, thread};
require_capability!(CAP_SETUID);

// get the UID of the "nobody" user
Expand Down

0 comments on commit 78d6746

Please sign in to comment.