Skip to content

Commit

Permalink
Change test dir structure for better coverage and update tests accord…
Browse files Browse the repository at this point in the history
…ingly
  • Loading branch information
emilevr committed Nov 10, 2023
1 parent 61e0bc4 commit 5c7fbae
Show file tree
Hide file tree
Showing 21 changed files with 610 additions and 224 deletions.
103 changes: 103 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ bumpalo-herd = "^0.1.2"
criterion = "^0.5.1"
id-arena = { version = "^2.2.1", features = ["rayon"] }
memory-stats = "^1.1.0"
mockall = "0.11.4"
regex = "^1.9.5"
rstest = "^0.18.2"
uuid = { version = "^1.4.1", features = ["v4"] }
52 changes: 17 additions & 35 deletions buildit/src/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ pub struct CoverageCommandArgs {
#[arg(long = "package")]
pub package: Option<String>,

/// Optionally run only the ignored tests
#[arg(long = "ignored", default_value_t = false)]
pub ignored: bool,
#[arg(long = "ignore-file-globs")]
pub ignore_file_globs: Option<Vec<String>>,

/// Optionally include the ignored tests
#[arg(long = "include-ignored", default_value_t = false)]
Expand All @@ -51,7 +50,6 @@ pub struct CoverageCommandArgs {
pub struct CoverageCommand {
output_types: Option<Vec<CoverageReportType>>,
package: Option<String>,
ignored: bool,
include_ignored: bool,
}

Expand All @@ -60,7 +58,6 @@ impl CoverageCommand {
CoverageCommand {
output_types: args.output_types,
package: args.package,
ignored: args.ignored,
include_ignored: args.include_ignored,
}
}
Expand Down Expand Up @@ -90,13 +87,9 @@ impl BuildItCommand for CoverageCommand {
args.push(package);
}
// Now the test params, i.e. after a '--' in the params.
if self.include_ignored || self.ignored {
if self.include_ignored {
args.push("--");
if self.ignored {
args.push("--ignored");
} else if self.include_ignored {
args.push("--include-ignored");
}
args.push("--include-ignored");
}
cmd("cargo", args)
.env("RUSTFLAGS", "-Cinstrument-coverage")
Expand All @@ -115,17 +108,13 @@ impl BuildItCommand for CoverageCommand {
};

// Call grcov, which has to be available on the path.
#[rustfmt::skip]
cmd!(
"grcov",
".",
"--binary-path",
format!("{}/debug/deps", target_coverage_dir),
"-s",
".",
"-t",
output_types,
"--branch",
"--excl-line",
"--binary-path", format!("{}/debug/deps", target_coverage_dir),
"-s", ".",
"-t", output_types,
// Exclude the following lines:
// ^\\s*(debug_)?assert(_eq|_ne)?! => debug_assert and assert variants
// ^\\s*#\\[.*$ => lines containing only an attribute
Expand All @@ -142,6 +131,7 @@ impl BuildItCommand for CoverageCommand {
// ^\\s*impl(<.*>)?\\s*[^ ]+\\s*\\{\\s*$ => lines containing only an impl declaration
// ^\\s*impl(<.*>)?\\s*[^ ]+\\s+for\\s+[^ ]*\\s*\\{\\s*$ => lines containing only an impl for declaration
// ^\\s*(pub|pub\\s*\\(\\s*crate\\s*\\)\\s*)?\\s*const\\s+.*\\s*[(){}]*\\s*$ => lines containing only a const definition
"--excl-line",
"^\\s*(debug_)?assert(_eq|_ne)?!\
|^\\s*#\\[.*$\
|^\\s*#!\\[.*$\
Expand All @@ -159,22 +149,14 @@ impl BuildItCommand for CoverageCommand {
|^\\s*impl(<.*>)?\\s*[^ ]+\\s+for\\s+[^ ]*\\s*\\{\\s*$\
|^\\s*(pub|pub\\s*\\(\\s*crate\\s*\\)\\s*)?\\s*const\\s+.*\\s*[(){}]*\\s*$",
"--ignore-not-existing",
"--ignore",
"buildit/*",
"--ignore",
"src/tests/*",
"--ignore",
"src/benches/*",
"--ignore",
"**/*_test.rs",
"--ignore",
"**/test_*.rs",
"--ignore",
"**/*_test_*.rs",
"--ignore",
"**/.cargo/registry/*",
"-o",
&output_path,
"--keep-only", "src/*",
"--ignore", "src/tests/*",
"--ignore", "src/benches/*",
"--ignore", "**/*_test.rs",
"--ignore", "**/test_*.rs",
"--ignore", "**/*_test_*.rs",
"--ignore", "**/.cargo/*",
"-o", &output_path,
)
.run()?;
output_path.push("html");
Expand Down
58 changes: 58 additions & 0 deletions src/cli/environment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#[cfg(test)]
use mockall::{automock, predicate::*};
use std::{
env::{self, VarError},
io::{self},
ops::Deref,
path::PathBuf,
};

#[cfg_attr(test, automock)]
pub(crate) trait EnvServiceTrait {
fn var(&self, key: &str) -> Result<String, VarError>;
fn current_dir(&self) -> io::Result<PathBuf>;
}

#[derive(Default)]
pub(crate) struct DefaultEnvService {}

impl EnvServiceTrait for DefaultEnvService {
fn var(&self, key: &str) -> Result<String, VarError> {
env::var(key)
}

fn current_dir(&self) -> io::Result<PathBuf> {
env::current_dir()
}
}

impl EnvServiceTrait for Box<dyn EnvServiceTrait> {
fn var(&self, key: &str) -> Result<String, VarError> {
self.deref().var(key)
}

fn current_dir(&self) -> io::Result<PathBuf> {
self.deref().current_dir()
}
}

#[cfg(test)]
mod test {
use super::{DefaultEnvService, EnvServiceTrait};

#[test]
fn default_env_service_current_dir_returns_env_current_dir() {
// Arrange
let service = DefaultEnvService::default();

// Act
let current_dir = service
.current_dir()
.expect("Should be able to get current dir via service");

// Assert
let env_current_dir =
std::env::current_dir().expect("Should be able to get current dir via env");
assert_eq!(env_current_dir, current_dir);
}
}
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod cli_command;
pub mod environment;
pub mod tui;
pub mod view_command;

Expand Down
10 changes: 7 additions & 3 deletions src/cli/skin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use ratatui::style::{Color, Modifier, Style};

#[cfg(test)]
#[path = "./skin_test.rs"]
mod skin_test;

#[derive(Clone, Copy)]
pub(crate) struct Skin {
pub(crate) title_fg_color: Color,
Expand All @@ -8,7 +12,7 @@ pub(crate) struct Skin {
pub(crate) table_header_bg_color: Color,
pub(crate) table_header_fg_color: Color,
pub(crate) value_fg_color: Option<Color>,
pub(crate) value_style_invert: bool,
pub(crate) value_style_reversed: bool,
pub(crate) delete_warning_text_fg_color: Color,
pub(crate) key_help_danger_bg_color: Color,
pub(crate) key_help_key_fg_color: Color,
Expand All @@ -28,7 +32,7 @@ impl Default for Skin {
table_header_bg_color: Color::Rgb(64, 64, 176),
table_header_fg_color: Color::White,
value_fg_color: Some(Color::Rgb(88, 144, 255)),
value_style_invert: false,
value_style_reversed: false,
delete_warning_text_fg_color: Color::Rgb(255, 165, 0),
key_help_danger_bg_color: Color::Rgb(192, 64, 64),
key_help_key_fg_color: Color::Rgb(192, 192, 192),
Expand All @@ -46,7 +50,7 @@ impl Skin {
if let Some(fg) = self.value_fg_color {
style = style.fg(fg);
}
if self.value_style_invert {
if self.value_style_reversed {
style = style.add_modifier(Modifier::REVERSED);
}
style
Expand Down
Loading

0 comments on commit 5c7fbae

Please sign in to comment.