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

Test: Add serial_test crate to resolve issue with prallel tests #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions 07_findr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ assert_cmd = "2.0.13"
predicates = "3.0.4"
pretty_assertions = "1.4.0"
rand = "0.8.5"
serial_test = "3.1"
38 changes: 32 additions & 6 deletions 07_findr/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use assert_cmd::Command;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use rand::{distributions::Alphanumeric, Rng};
use serial_test::{parallel, serial};
use std::{borrow::Cow, fs, path::Path};

const PRG: &str = "findr";
Expand All @@ -24,6 +25,7 @@ fn gen_bad_file() -> String {

// --------------------------------------------------
#[test]
#[parallel]
fn skips_bad_dir() -> Result<()> {
let bad = gen_bad_file();
let expected = format!("{}: .* [(]os error [23][)]", &bad);
Expand All @@ -37,6 +39,7 @@ fn skips_bad_dir() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn dies_bad_name() -> Result<()> {
Command::cargo_bin(PRG)?
.args(["--name", "*.csv"])
Expand All @@ -48,6 +51,7 @@ fn dies_bad_name() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn dies_bad_type() -> Result<()> {
let expected = "error: invalid value 'x' for '--type [<TYPE>...]'";
Command::cargo_bin(PRG)?
Expand Down Expand Up @@ -76,15 +80,13 @@ fn format_file_name(expected_file: &str) -> Cow<str> {
fn run(args: &[&str], expected_file: &str) -> Result<()> {
let file = format_file_name(expected_file);
let contents = fs::read_to_string(file.as_ref())?;
let mut expected: Vec<&str> =
contents.split('\n').filter(|s| !s.is_empty()).collect();
let mut expected: Vec<&str> = contents.split('\n').filter(|s| !s.is_empty()).collect();
expected.sort();

let cmd = Command::cargo_bin(PRG)?.args(args).assert().success();
let out = cmd.get_output();
let stdout = String::from_utf8(out.stdout.clone())?;
let mut lines: Vec<&str> =
stdout.split('\n').filter(|s| !s.is_empty()).collect();
let mut lines: Vec<&str> = stdout.split('\n').filter(|s| !s.is_empty()).collect();
lines.sort();

assert_eq!(lines, expected);
Expand All @@ -94,30 +96,35 @@ fn run(args: &[&str], expected_file: &str) -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn path1() -> Result<()> {
run(&["tests/inputs"], "tests/expected/path1.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn path_a() -> Result<()> {
run(&["tests/inputs/a"], "tests/expected/path_a.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn path_a_b() -> Result<()> {
run(&["tests/inputs/a/b"], "tests/expected/path_a_b.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn path_d() -> Result<()> {
run(&["tests/inputs/d"], "tests/expected/path_d.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn path_a_b_d() -> Result<()> {
run(
&["tests/inputs/a/b", "tests/inputs/d"],
Expand All @@ -127,12 +134,14 @@ fn path_a_b_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_f() -> Result<()> {
run(&["tests/inputs", "-t", "f"], "tests/expected/type_f.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_path_a() -> Result<()> {
run(
&["tests/inputs/a", "-t", "f"],
Expand All @@ -142,6 +151,7 @@ fn type_f_path_a() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_path_a_b() -> Result<()> {
run(
&["tests/inputs/a/b", "--type", "f"],
Expand All @@ -151,6 +161,7 @@ fn type_f_path_a_b() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_path_d() -> Result<()> {
run(
&["tests/inputs/d", "--type", "f"],
Expand All @@ -160,6 +171,7 @@ fn type_f_path_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_path_a_b_d() -> Result<()> {
run(
&["tests/inputs/a/b", "tests/inputs/d", "--type", "f"],
Expand All @@ -169,12 +181,14 @@ fn type_f_path_a_b_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_d() -> Result<()> {
run(&["tests/inputs", "-t", "d"], "tests/expected/type_d.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn type_d_path_a() -> Result<()> {
run(
&["tests/inputs/a", "-t", "d"],
Expand All @@ -184,6 +198,7 @@ fn type_d_path_a() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_d_path_a_b() -> Result<()> {
run(
&["tests/inputs/a/b", "--type", "d"],
Expand All @@ -193,6 +208,7 @@ fn type_d_path_a_b() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_d_path_d() -> Result<()> {
run(
&["tests/inputs/d", "--type", "d"],
Expand All @@ -202,6 +218,7 @@ fn type_d_path_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_d_path_a_b_d() -> Result<()> {
run(
&["tests/inputs/a/b", "tests/inputs/d", "--type", "d"],
Expand All @@ -211,12 +228,14 @@ fn type_d_path_a_b_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_l() -> Result<()> {
run(&["tests/inputs", "-t", "l"], "tests/expected/type_l.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_l() -> Result<()> {
run(
&["tests/inputs", "-t", "l", "f"],
Expand All @@ -226,6 +245,7 @@ fn type_f_l() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn name_csv() -> Result<()> {
run(
&["tests/inputs", "-n", ".*[.]csv"],
Expand All @@ -235,6 +255,7 @@ fn name_csv() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn name_csv_mp3() -> Result<()> {
run(
&["tests/inputs", "-n", ".*[.]csv", "-n", ".*[.]mp3"],
Expand All @@ -244,6 +265,7 @@ fn name_csv_mp3() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn name_txt_path_a_d() -> Result<()> {
run(
&["tests/inputs/a", "tests/inputs/d", "--name", ".*.txt"],
Expand All @@ -253,12 +275,14 @@ fn name_txt_path_a_d() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn name_a() -> Result<()> {
run(&["tests/inputs", "-n", "a"], "tests/expected/name_a.txt")
}

// --------------------------------------------------
#[test]
#[parallel]
fn type_f_name_a() -> Result<()> {
run(
&["tests/inputs", "-t", "f", "-n", "a"],
Expand All @@ -268,6 +292,7 @@ fn type_f_name_a() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn type_d_name_a() -> Result<()> {
run(
&["tests/inputs", "--type", "d", "--name", "a"],
Expand All @@ -277,12 +302,14 @@ fn type_d_name_a() -> Result<()> {

// --------------------------------------------------
#[test]
#[parallel]
fn path_g() -> Result<()> {
run(&["tests/inputs/g.csv"], "tests/expected/path_g.txt")
}

// --------------------------------------------------
#[test]
#[serial]
#[cfg(not(windows))]
fn unreadable_dir() -> Result<()> {
let dirname = "tests/inputs/cant-touch-this";
Expand All @@ -303,8 +330,7 @@ fn unreadable_dir() -> Result<()> {

let out = cmd.get_output();
let stdout = String::from_utf8(out.stdout.clone())?;
let lines: Vec<&str> =
stdout.split('\n').filter(|s| !s.is_empty()).collect();
let lines: Vec<&str> = stdout.split('\n').filter(|s| !s.is_empty()).collect();

assert_eq!(lines.len(), 17);

Expand Down