Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Fix #191 check tests directory via 'all-targets' cargo flag
Browse files Browse the repository at this point in the history
  • Loading branch information
estin committed Feb 12, 2018
1 parent 5fe9745 commit 7fd2a0b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ Currently we accept the following options:
* `analyze_package` (`String`, defaults to `""`) When `workspace_mode` is
enabled, analysis will be only provided for the specified package (runs as
if `-p <analyze_package>` was passed).
* `all_targets` (`bool`, defaults to `false`) checks the project as if you were
running `cargo check --all-targets`. I.e., check all targets and integration
tests too.


## Troubleshooting
Expand Down
14 changes: 12 additions & 2 deletions src/build/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn run_cargo(
false,
&[],
false,
false,
opts.all_targets,
),
features: &opts.features,
all_features: opts.all_features,
Expand Down Expand Up @@ -413,7 +413,13 @@ impl Executor for RlsExecutor {
// Because we only try to emulate `cargo test` using `cargo check`, so for now
// assume crate_type arg (i.e. in `cargo test` it isn't specified for --test targets)
// and build test harness only for final crate type
let crate_type = crate_type.expect("no crate-type in rustc command line");
let crate_type = if config.all_targets {
// Crate type may be undefined when `all_targets` is true, for example for integration tests
crate_type.unwrap_or("undefined".to_owned())
} else {
// Panic if crate type undefined for other cases
crate_type.expect("no crate-type in rustc command line")
};
let build_lib = *config.build_lib.as_ref();
let is_final_crate_type = crate_type == "bin" || (crate_type == "lib" && build_lib);

Expand Down Expand Up @@ -512,6 +518,7 @@ struct CargoOptions {
no_default_features: bool,
features: Vec<String>,
jobs: Option<u32>,
all_targets: bool,
}

impl Default for CargoOptions {
Expand All @@ -525,6 +532,7 @@ impl Default for CargoOptions {
no_default_features: false,
features: vec![],
jobs: None,
all_targets: false
}
}
}
Expand All @@ -538,6 +546,7 @@ impl CargoOptions {
all_features: config.all_features,
no_default_features: config.no_default_features,
jobs: config.jobs,
all_targets: config.all_targets,
..CargoOptions::default()
}
} else {
Expand All @@ -562,6 +571,7 @@ impl CargoOptions {
all_features: config.all_features,
no_default_features: config.no_default_features,
jobs: config.jobs,
all_targets: config.all_targets,
..CargoOptions::default()
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub struct Config {
pub all_features: bool,
pub no_default_features: bool,
pub jobs: Option<u32>,
pub all_targets: bool,
}

impl Default for Config {
Expand All @@ -156,6 +157,7 @@ impl Default for Config {
all_features: false,
no_default_features: false,
jobs: None,
all_targets: false,
};
result.normalise();
result
Expand Down
36 changes: 36 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,3 +1478,39 @@ fn test_deglob() {
],
);
}

#[test]
fn test_all_targets() {
let mut env = Environment::new("bin_lib");

let root_path = env.cache.abs_path(Path::new("."));

let messages = vec![
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
];

env.with_config(|c| {
c.all_targets = true;
c.cfg_test = true;
});
let (mut server, results) = env.mock_server(messages);
// Initialize and build.
assert_eq!(
ls_server::LsService::handle_message(&mut server),
ls_server::ServerStateChange::Continue
);
expect_messages(
results.clone(),
&[
ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
ExpectedMessage::new(None).expect_contains("beginBuild"),
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),

ExpectedMessage::new(None)
.expect_contains(r#"bin_lib/tests/tests.rs"#)
.expect_contains(r#"unused variable: `unused_var`"#),

ExpectedMessage::new(None).expect_contains("diagnosticsEnd"),
],
);
}
5 changes: 5 additions & 0 deletions test_data/bin_lib/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn test_some_foo() {
let unused_var = true;
assert!(true);
}

0 comments on commit 7fd2a0b

Please sign in to comment.