Skip to content

Commit

Permalink
Contribute a failing test on cyclic dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Veetaha committed May 1, 2020
1 parent 1a7904c commit fb9b234
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/bans.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use cargo_deny::{
bans::{
self,
cfg::{Config, ValidConfig},
},
diag::KrateSpans,
CheckCtx,
};
use std::{fs, path::PathBuf, time::Duration};

fn with_test_data(project_name: &str, accept_ctx: impl FnOnce(CheckCtx<'_, ValidConfig>)) {
let project_dir = PathBuf::from("./tests/test_data").join(project_name);

let mut metadata_cmd = krates::cm::MetadataCommand::new();
metadata_cmd.current_dir(&project_dir);

let krates = &krates::Builder::new()
.build(metadata_cmd, krates::NoneFilter)
.unwrap();

let spans = KrateSpans::new(krates);
let mut files = codespan::Files::new();
let spans_id = files.add(project_dir.join("Cargo.lock"), spans.1);

let config: Config = fs::read_to_string(project_dir.join("deny.toml"))
.map(|it| toml::from_str(&it).unwrap())
.unwrap_or_default();

accept_ctx(cargo_deny::CheckCtx {
krates,
krate_spans: &spans.0,
spans_id,
cfg: config.validate(spans_id).unwrap(),
});
}

// Covers issue https://github.com/EmbarkStudios/cargo-deny/issues/184
#[test]
fn cyclic_dependencies_do_not_cause_infinite_loop() {
let (tx, rx) = crossbeam::unbounded();

let handle = std::thread::spawn(|| {
with_test_data("cyclic_dependencies", |check_ctx| {
let graph_output = Box::new(|_| Ok(()));
bans::check(check_ctx, Some(graph_output), tx);
});
});

let timeout_duration = Duration::from_millis(10000);
let timeout = crossbeam::after(timeout_duration);
loop {
crossbeam::select! {
recv(rx) -> msg => {
if msg.is_err() {
// Yay, the sender was dopped (i.e. check was finished)
break;
}
}
recv(timeout) -> _ => {
panic!("Bans check timed out after {:?}", timeout_duration);
}
}
}

handle.join().unwrap();
}
57 changes: 57 additions & 0 deletions tests/test_data/cyclic_dependencies/Cargo.lock

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

2 changes: 2 additions & 0 deletions tests/test_data/cyclic_dependencies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = ["root", "leaf"]
Empty file.
9 changes: 9 additions & 0 deletions tests/test_data/cyclic_dependencies/leaf/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "leaf"
version = "0.1.0"
authors = ["veetaha <[email protected]>"]
edition = "2018"

[dev-dependencies]
root = { path = "../root" }
ansi_term = "0.12.1"
1 change: 1 addition & 0 deletions tests/test_data/cyclic_dependencies/leaf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn leaf() {}
9 changes: 9 additions & 0 deletions tests/test_data/cyclic_dependencies/root/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "root"
version = "0.1.0"
authors = ["veetaha <[email protected]>"]
edition = "2018"

[dependencies]
ansi_term = "0.11.0"
leaf = { path = "../leaf" }
1 change: 1 addition & 0 deletions tests/test_data/cyclic_dependencies/root/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn root() {}
1 change: 1 addition & 0 deletions tests/test_data/cyclic_dependencies/root/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}

0 comments on commit fb9b234

Please sign in to comment.