Skip to content

Commit

Permalink
Merge pull request #216 from oli-obk/librarification
Browse files Browse the repository at this point in the history
Add some convenience helpers for adding custom flags
  • Loading branch information
oli-obk authored Apr 8, 2024
2 parents 2e194d2 + 054116d commit 3f834e7
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 73 deletions.
51 changes: 14 additions & 37 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ impl Config {
}
}

let _ = comment_defaults.base().custom.insert(
"edition",
Spanned::dummy(vec![Box::new(Edition("2021".into()))]),
);
let _ = comment_defaults.base().custom.insert(
"rustfix",
Spanned::dummy(vec![Box::new(RustfixMode::MachineApplicable)]),
);
comment_defaults
.base()
.add_custom("edition", Edition("2021".into()));
comment_defaults
.base()
.add_custom("rustfix", RustfixMode::MachineApplicable);
let filters = vec![
(Match::PathBackslash, b"/".to_vec()),
#[cfg(windows)]
Expand Down Expand Up @@ -154,34 +152,20 @@ impl Config {
.custom_comments
.insert("no-rustfix", |parser, _args, span| {
// args are ignored (can be used as comment)
let prev = parser
.custom
.insert("no-rustfix", Spanned::new(vec![Box::new(())], span.clone()));
parser.check(span, prev.is_none(), "cannot specify `no-rustfix` twice");
parser.set_custom_once("no-rustfix", (), span);
});

config
.custom_comments
.insert("edition", |parser, args, span| {
let prev = parser.custom.insert(
"edition",
Spanned::new(vec![Box::new(Edition((*args).into()))], args.span()),
);
parser.check(span, prev.is_none(), "cannot specify `edition` twice");
.insert("edition", |parser, args, _span| {
parser.set_custom_once("edition", Edition((*args).into()), args.span());
});

config
.custom_comments
.insert("needs-asm-support", |parser, args, span| {
let prev = parser.custom.insert(
"needs-asm-support",
Spanned::new(vec![Box::new(NeedsAsmSupport)], args.span()),
);
parser.check(
span,
prev.is_none(),
"cannot specify `needs-asm-support` twice",
);
.insert("needs-asm-support", |parser, _args, span| {
// args are ignored (can be used as comment)
parser.set_custom_once("needs-asm-support", NeedsAsmSupport, span);
});

config.custom_comments.insert("run", |parser, args, span| {
Expand All @@ -191,10 +175,7 @@ impl Config {
"cannot specify test mode changes twice",
);
let set = |exit_code| {
parser.custom.insert(
"run",
Spanned::new(vec![Box::new(Run { exit_code })], args.span()),
);
parser.set_custom_once("run", Run { exit_code }, args.span());
parser.mode = Spanned::new(Mode::Pass, args.span()).into();

let prev = parser
Expand Down Expand Up @@ -223,11 +204,7 @@ impl Config {
};

parser
.custom
.entry("aux-build")
.or_insert_with(|| Spanned::new(vec![], span))
.content
.push(Box::new(AuxBuilder { aux_file: name.map(|n| n.into())}));
.add_custom_spanned("aux-build", AuxBuilder { aux_file: name.map(|n| n.into())}, span);
});
config
}
Expand Down
41 changes: 41 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,38 @@ pub struct Revisioned {
/// The keys are just labels for overwriting or retrieving the value later.
/// They are mostly used by `Config::custom_comments` handlers,
/// `ui_test` itself only ever looks at the values, not the keys.
///
/// You usually don't modify this directly but use the `add_custom` or `set_custom_once`
/// helpers.
pub custom: BTreeMap<&'static str, Spanned<Vec<Box<dyn Flag>>>>,
}

impl Revisioned {
/// Append another flag to an existing or new key
pub fn add_custom(&mut self, key: &'static str, custom: impl Flag + 'static) {
self.add_custom_spanned(key, custom, Span::default())
}

/// Append another flag to an existing or new key
pub fn add_custom_spanned(
&mut self,
key: &'static str,
custom: impl Flag + 'static,
span: Span,
) {
self.custom
.entry(key)
.or_insert_with(|| Spanned::new(vec![], span))
.content
.push(Box::new(custom));
}
/// Override or set a flag
pub fn set_custom(&mut self, key: &'static str, custom: impl Flag + 'static) {
self.custom
.insert(key, Spanned::dummy(vec![Box::new(custom)]));
}
}

/// Main entry point to parsing comments and handling parsing errors.
#[derive(Debug)]
pub struct CommentParser<T> {
Expand Down Expand Up @@ -633,6 +662,18 @@ impl CommentParser<&mut Revisioned> {
let regex = self.parse_regex(from)?.content;
Some((regex, to.as_bytes().to_owned()))
}

/// Add a flag or error if it already existed
pub fn set_custom_once(&mut self, key: &'static str, custom: impl Flag + 'static, span: Span) {
let prev = self
.custom
.insert(key, Spanned::new(vec![Box::new(custom)], span.clone()));
self.check(
span,
prev.is_none(),
format!("cannot specify `{key}` twice"),
);
}
}

impl CommentParser<Comments> {
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/basic-bin/tests/ui_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ui_test::{dependencies::DependencyBuilder, spanned::Spanned, *};
use ui_test::{dependencies::DependencyBuilder, *};

fn main() -> ui_test::color_eyre::Result<()> {
let path = "../../../target";
Expand All @@ -11,10 +11,10 @@ fn main() -> ui_test::color_eyre::Result<()> {
bless_command: Some("cargo test".to_string()),
..Config::rustc("tests/actual_tests")
};
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
Expand Down
9 changes: 4 additions & 5 deletions tests/integrations/basic-fail-mode/tests/run_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::{Path, PathBuf};
use ui_test::color_eyre::{eyre::ensure, Result};
use ui_test::dependencies::DependencyBuilder;
use ui_test::spanned::Spanned;
use ui_test::*;

#[test]
Expand Down Expand Up @@ -55,10 +54,10 @@ fn run_file_no_deps() -> Result<()> {

// Don't build a binary, we only provide .rmeta dependencies for now
config.program.args.push("--emit=metadata".into());
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());

let mut result = ui_test::test_command(
config,
Expand Down
8 changes: 4 additions & 4 deletions tests/integrations/basic-fail-mode/tests/ui_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ fn main() -> ui_test::color_eyre::Result<()> {
require_patterns: true,
})
.into();
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());
config.stderr_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/basic-fail/Cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ error: test failed, to rerun pass `--test ui_tests`

Caused by:
process didn't exit successfully: `$DIR/target/ui/tests/integrations/basic-fail/debug/deps/ui_tests-HASH` (exit status: 1)
thread 'main' panicked at tests/ui_tests_bless.rs:64:18:
thread 'main' panicked at tests/ui_tests_bless.rs:63:18:
invalid mode/result combo: yolo: Err(tests failed

Location:
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/basic-fail/tests/ui_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ui_test::{dependencies::DependencyBuilder, spanned::Spanned, *};
use ui_test::{dependencies::DependencyBuilder, *};

fn main() -> ui_test::color_eyre::Result<()> {
let path = "../../../target";
Expand All @@ -9,10 +9,10 @@ fn main() -> ui_test::color_eyre::Result<()> {
..Config::rustc("tests/actual_tests")
};

config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());

// hide binaries generated for successfully passing tests
let tmp_dir = tempfile::tempdir_in(path)?;
Expand Down
11 changes: 5 additions & 6 deletions tests/integrations/basic-fail/tests/ui_tests_bless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ fn main() -> ui_test::color_eyre::Result<()> {
config
.comment_defaults
.base()
.custom
.insert("rustfix", Spanned::dummy(vec![Box::new(rustfix)]));
.set_custom("rustfix", rustfix);

config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());

// hide binaries generated for successfully passing tests
let tmp_dir = tempfile::tempdir_in(path)?;
Expand Down
9 changes: 4 additions & 5 deletions tests/integrations/basic/tests/run_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::{Path, PathBuf};
use ui_test::color_eyre::{eyre::ensure, Result};
use ui_test::dependencies::DependencyBuilder;
use ui_test::spanned::Spanned;
use ui_test::*;

#[test]
Expand Down Expand Up @@ -37,10 +36,10 @@ fn run_file_with_deps() -> Result<()> {

// Don't build a binary, we only provide .rmeta dependencies for now
config.program.args.push("--emit=metadata".into());
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());

let mut result = ui_test::test_command(
config,
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/basic/tests/ui_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ui_test::{dependencies::DependencyBuilder, spanned::Spanned, *};
use ui_test::{dependencies::DependencyBuilder, *};

fn main() -> ui_test::color_eyre::Result<()> {
let path = "../../../target";
Expand All @@ -15,10 +15,10 @@ fn main() -> ui_test::color_eyre::Result<()> {
config.stdout_filter("in ([0-9]m )?[0-9\\.]+s", "");
config.stderr_filter(r"[^ ]*/\.?cargo/registry/.*/", "$$CARGO_REGISTRY");
config.path_stderr_filter(&std::path::Path::new(path), "$DIR");
config.comment_defaults.base().custom.insert(
"dependencies",
Spanned::dummy(vec![Box::new(DependencyBuilder::default())]),
);
config
.comment_defaults
.base()
.set_custom("dependencies", DependencyBuilder::default());

if let Ok(target) = std::env::var("UITEST_TEST_TARGET") {
config.target = Some(target);
Expand Down

0 comments on commit 3f834e7

Please sign in to comment.