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

Add Json Format, Intellij Options and make multi-threaded optional #34

Closed
wants to merge 9 commits into from
Closed
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- JSON Format Output [#34]
- No-Op Commandline Options to satisfy intellij IDEs [#34]

### Changed
- Gate multi-threaded execution behind a enabled-by-default feature [#34]
- Allow Trials with non-static lifetime [#34]

[#34]: https://github.com/LukasKalbertodt/libtest-mimic/pull/34

## [0.6.1] - 2022-11-05
### Fixed
Expand Down
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ readme = "README.md"

exclude = [".github"]

[features]
default = ["multithreaded"]
multithreaded = ["dep:crossbeam", "dep:num_cpus"]

[dependencies]
clap = { version = "4.0.8", features = ["derive"] }
threadpool = "1.8.1"
termcolor = "1.0.5"
escape8259 = "0.5.2"
crossbeam = { version = "0.8.3", optional = true }
num_cpus = { version = "1.16.0", optional = true }

[dev-dependencies]
fastrand = "1.8.0"
Expand Down
4 changes: 1 addition & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate libtest_mimic;

use libtest_mimic::{Arguments, Failed, Trial};
use std::{thread, time};
use libtest_mimic::{Arguments, Trial, Failed};


fn main() {
let args = Arguments::from_args();
Expand All @@ -18,7 +17,6 @@ fn main() {
libtest_mimic::run(&args, tests).exit();
}


// Tests

fn check_toph() -> Result<(), Failed> {
Expand Down
16 changes: 4 additions & 12 deletions examples/tidy.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
extern crate libtest_mimic;

use libtest_mimic::{Arguments, Trial, Failed};

use std::{
env,
error::Error,
ffi::OsStr,
fs,
path::Path,
};
use libtest_mimic::{Arguments, Failed, Trial};

use std::{env, error::Error, ffi::OsStr, fs, path::Path};

fn main() -> Result<(), Box<dyn Error>> {
let args = Arguments::from_args();
Expand All @@ -19,7 +12,7 @@ fn main() -> Result<(), Box<dyn Error>> {

/// Creates one test for each `.rs` file in the current directory or
/// sub-directories of the current directory.
fn collect_tests() -> Result<Vec<Trial>, Box<dyn Error>> {
fn collect_tests() -> Result<Vec<Trial<'static>>, Box<dyn Error>> {
fn visit_dir(path: &Path, tests: &mut Vec<Trial>) -> Result<(), Box<dyn Error>> {
for entry in fs::read_dir(path)? {
let entry = entry?;
Expand All @@ -34,8 +27,7 @@ fn collect_tests() -> Result<Vec<Trial>, Box<dyn Error>> {
.display()
.to_string();

let test = Trial::test(name, move || check_file(&path))
.with_kind("tidy");
let test = Trial::test(name, move || check_file(&path)).with_kind("tidy");
tests.push(test);
}
} else if file_type.is_dir() {
Expand Down
48 changes: 34 additions & 14 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use clap::{Parser, ValueEnum};
#[derive(Parser, Debug, Clone, Default)]
#[command(
help_template = "USAGE: [OPTIONS] [FILTER]\n\n{all-args}\n\n\n{after-help}",
disable_version_flag = true,
after_help = "By default, all tests are run in parallel. This can be altered with the \n\
--test-threads flag when running tests (set it to 1).",
disable_version_flag = true
)]
pub struct Arguments {
// ============== FLAGS ===================================================
Expand All @@ -30,7 +28,7 @@ pub struct Arguments {
#[arg(
long = "test",
conflicts_with = "bench",
help = "Run tests and not benchmarks",
help = "Run tests and not benchmarks"
)]
pub test: bool,

Expand All @@ -43,13 +41,24 @@ pub struct Arguments {
pub list: bool,

/// No-op, ignored (libtest-mimic always runs in no-capture mode)
#[arg(long = "nocapture", help = "No-op (libtest-mimic always runs in no-capture mode)")]
#[arg(
long = "nocapture",
help = "No-op (libtest-mimic always runs in no-capture mode)"
)]
pub nocapture: bool,

/// No-op, ignored. libtest-mimic does not currently capture stdout.
#[arg(long = "show-output")]
pub show_output: bool,

/// No-op, ignored. Flag only exists for CLI compatibility with libtest.
#[arg(short = 'Z')]
pub unstable_flags: Option<UnstableFlags>,

/// If set, filters are matched exactly rather than by substring.
#[arg(
long = "exact",
help = "Exactly match filters rather than by substring",
help = "Exactly match filters rather than by substring"
)]
pub exact: bool,

Expand All @@ -62,16 +71,17 @@ pub struct Arguments {
short = 'q',
long = "quiet",
conflicts_with = "format",
help = "Display one character per test instead of one line. Alias to --format=terse",
help = "Display one character per test instead of one line. Alias to --format=terse"
)]
pub quiet: bool,

// ============== OPTIONS =================================================
#[cfg(feature = "multithreaded")]
/// Number of threads used for parallel testing.
#[arg(
long = "test-threads",
help = "Number of threads used for running tests in parallel. If set to 1, \n\
all tests are run in the main thread.",
help = "Number of threads used for running tests in parallel. By default, all tests are run in parallel. If set to 1, \n\
all tests are run in the main thread."
)]
pub test_threads: Option<usize>,

Expand All @@ -80,7 +90,7 @@ pub struct Arguments {
#[arg(
long = "logfile",
value_name = "PATH",
help = "Write logs to the specified file instead of stdout",
help = "Write logs to the specified file instead of stdout"
)]
pub logfile: Option<String>,

Expand All @@ -89,7 +99,7 @@ pub struct Arguments {
#[arg(
long = "skip",
value_name = "FILTER",
help = "Skip tests whose names contain FILTER (this flag can be used multiple times)",
help = "Skip tests whose names contain FILTER (this flag can be used multiple times)"
)]
pub skip: Vec<String>,

Expand All @@ -101,7 +111,7 @@ pub struct Arguments {
help = "Configure coloring of output: \n\
- auto = colorize if stdout is a tty and tests are run on serially (default)\n\
- always = always colorize output\n\
- never = never colorize output\n",
- never = never colorize output\n"
)]
pub color: Option<ColorSetting>,

Expand All @@ -112,7 +122,8 @@ pub struct Arguments {
value_name = "pretty|terse|json",
help = "Configure formatting of output: \n\
- pretty = Print verbose output\n\
- terse = Display one character per test\n",
- terse = Display one character per test\n\
- json = Print json events\n"
)]
pub format: Option<FormatSetting>,

Expand All @@ -121,7 +132,7 @@ pub struct Arguments {
#[arg(
value_name = "FILTER",
help = "The FILTER string is tested against the name of all tests, and only those tests \
whose names contain the filter are run.",
whose names contain the filter are run."
)]
pub filter: Option<String>,
}
Expand Down Expand Up @@ -168,6 +179,12 @@ impl Default for ColorSetting {
}
}

/// Possible values for the `-Z` option
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum UnstableFlags {
UnstableOptions,
}

/// Possible values for the `--format` option.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum FormatSetting {
Expand All @@ -176,6 +193,9 @@ pub enum FormatSetting {

/// One character per test. Usefull for test suites with many tests.
Terse,

/// Json output
Json,
}

impl Default for FormatSetting {
Expand Down
Loading