Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sign: Hide tests behind feature #3100

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/tests/test_gpg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::Permissions;
use std::io::Write;
#[cfg(unix)]
use std::os::unix::prelude::PermissionsExt;
use std::process::Stdio;
use std::process::{Command, Stdio};

use assert_matches::assert_matches;
use insta::assert_debug_snapshot;
Expand Down Expand Up @@ -75,6 +75,15 @@ impl GpgEnvironment {
}
}

macro_rules! gpg_guard {
() => {
if Command::new("gpg").arg("--version").status().is_err() {
eprintln!("Skipping test because gpg is not installed on the system");
return;
}
};
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: alternatively, this could be a declarative macro. I don't know which is better overall. Both approaches have pros and cons.

macro_rules! gpg_guard {
    () => {
        if Command::new("gpg").arg("--version").status().is_err() {
            eprintln!("Skipping because gpg is not installed on the system");
            return;
        }
    };
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Never used macros before. This looks nice to me will use this. Out of interest what would you consider the pros and cons in this context?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closure: + obvious control flow, - nesting
macro: + less noisy, - hidden return statement

fn backend(env: &GpgEnvironment) -> GpgBackend {
// don't really need faked time for current tests,
// but probably will need it for end-to-end cli tests
Expand All @@ -87,6 +96,8 @@ fn backend(env: &GpgEnvironment) -> GpgBackend {

#[test]
fn gpg_signing_roundtrip() {
gpg_guard!();

let env = GpgEnvironment::new().unwrap();
let backend = backend(&env);
let data = b"hello world";
Expand All @@ -111,6 +122,8 @@ fn gpg_signing_roundtrip() {

#[test]
fn gpg_signing_roundtrip_explicit_key() {
gpg_guard!();

let env = GpgEnvironment::new().unwrap();
let backend = backend(&env);
let data = b"hello world";
Expand Down Expand Up @@ -142,6 +155,8 @@ fn gpg_signing_roundtrip_explicit_key() {

#[test]
fn unknown_key() {
gpg_guard!();

let env = GpgEnvironment::new().unwrap();
let backend = backend(&env);
let signature = br"-----BEGIN PGP SIGNATURE-----
Expand Down Expand Up @@ -173,6 +188,8 @@ fn unknown_key() {

#[test]
fn invalid_signature() {
gpg_guard!();

let env = GpgEnvironment::new().unwrap();
let backend = backend(&env);
let signature = br"-----BEGIN PGP SIGNATURE-----
Expand Down