Skip to content

Commit

Permalink
[review] make features additive
Browse files Browse the repository at this point in the history
  • Loading branch information
tommilligan committed Sep 13, 2021
1 parent d5eec95 commit 60cf199
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
10 changes: 8 additions & 2 deletions pretty_assertions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ output_vt100 = "0.1.2"
ctor = "0.1.9"

[features]
default = []
no_std = []
default = ["std"]

# Use the Rust standard library.
# Exactly one of `std` and `alloc` is required.
std = []
# Use the `alloc` crate.
# Exactly one of `std` and `alloc` is required.
alloc = []
10 changes: 7 additions & 3 deletions pretty_assertions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@
//!
//! ## Features
//!
//! Features provided by the crate are as follows:
//! Features provided by the crate are:
//!
//! - `std`: Use the Rust standard library. Enabled by default.
//! Exactly one of `std` and `alloc` is required.
//! - `alloc`: Use the `alloc` crate.
//! Exactly one of `std` and `alloc` is required.
//! - `unstable`: opt-in to unstable features that may not follow Semantic Versioning.
//! Implmenetion behind this feature is subject to change without warning between patch versions.

#![cfg_attr(feature = "no_std", no_std)]
#![cfg_attr(not(feature = "std"), no_std)]
#![deny(clippy::all, missing_docs, unsafe_code)]

#[cfg(feature = "no_std")]
#[cfg(feature = "alloc")]
#[macro_use]
extern crate alloc;
pub use ansi_term::Style;
Expand Down
8 changes: 5 additions & 3 deletions pretty_assertions/src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "alloc")]
use alloc::format;
use ansi_term::{
Colour::{Fixed, Green, Red},
Style,
Expand Down Expand Up @@ -210,7 +212,7 @@ fn write_inline_diff<TWrite: fmt::Write>(f: &mut TWrite, left: &str, right: &str
mod test {
use super::*;

#[cfg(feature = "no_std")]
#[cfg(feature = "alloc")]
use alloc::string::String;

// ANSI terminal codes used in our outputs.
Expand All @@ -233,8 +235,8 @@ mod test {
let mut actual = String::new();
printer(&mut actual, left, right).expect("printer function failed");

// Cannot use IO with no_std
#[cfg(not(feature = "no_std"))]
// Cannot use IO without stdlib
#[cfg(feature = "std")]
println!(
"## left ##\n\
{}\n\
Expand Down
12 changes: 6 additions & 6 deletions pretty_assertions/tests/macros.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#![cfg_attr(feature = "no_std", no_std)]
#![cfg_attr(not(feature = "std"), no_std)]
#![no_implicit_prelude]

#[cfg(feature = "no_std")]
#[cfg(feature = "alloc")]
extern crate alloc;

#[allow(clippy::eq_op)]
mod assert_eq {
#[cfg(feature = "no_std")]
#[cfg(feature = "alloc")]
use ::alloc::string::{String, ToString};
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
use ::std::string::{String, ToString};

#[test]
Expand Down Expand Up @@ -100,9 +100,9 @@ mod assert_eq {
}

mod assert_ne {
#[cfg(feature = "no_std")]
#[cfg(feature = "alloc")]
use ::alloc::string::{String, ToString};
#[cfg(not(feature = "no_std"))]
#[cfg(feature = "std")]
use ::std::string::{String, ToString};

#[test]
Expand Down
15 changes: 12 additions & 3 deletions scripts/check
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ cargo audit --deny warnings
eprintln "Linting sources"
cargo clippy --all-targets -- -D warnings

eprintln "Running tests"
# We cd to package directory as it's the only one with tests
# and cargo doesn't support feature flags in virtual workspaces
# https://github.com/rust-lang/cargo/issues/5364
pushd pretty_assertions

eprintln "Running tests (default)"
cargo test
eprintln "Running tests (no_std)"
cargo test --features no_std
eprintln "Running tests (std)"
cargo test --no-default-features --features std
eprintln "Running tests (alloc)"
cargo test --no-default-features --features alloc

popd

eprintln "Running unit tests (unstable)"
cargo +nightly test --manifest-path pretty_assertions/Cargo.toml --all --features unstable
Expand Down

0 comments on commit 60cf199

Please sign in to comment.