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

Commit ad0e1a0

Browse files
authored
Merge pull request #695 from estin/master
Fix #191 check tests directory via 'all-targets' cargo flag
2 parents 30ea1e2 + 7fd2a0b commit ad0e1a0

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Currently we accept the following options:
115115
* `analyze_package` (`String`, defaults to `""`) When `workspace_mode` is
116116
enabled, analysis will be only provided for the specified package (runs as
117117
if `-p <analyze_package>` was passed).
118+
* `all_targets` (`bool`, defaults to `false`) checks the project as if you were
119+
running `cargo check --all-targets`. I.e., check all targets and integration
120+
tests too.
118121

119122

120123
## Troubleshooting

src/build/cargo.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ fn run_cargo(
173173
false,
174174
&[],
175175
false,
176-
false,
176+
opts.all_targets,
177177
),
178178
features: &opts.features,
179179
all_features: opts.all_features,
@@ -413,7 +413,13 @@ impl Executor for RlsExecutor {
413413
// Because we only try to emulate `cargo test` using `cargo check`, so for now
414414
// assume crate_type arg (i.e. in `cargo test` it isn't specified for --test targets)
415415
// and build test harness only for final crate type
416-
let crate_type = crate_type.expect("no crate-type in rustc command line");
416+
let crate_type = if config.all_targets {
417+
// Crate type may be undefined when `all_targets` is true, for example for integration tests
418+
crate_type.unwrap_or("undefined".to_owned())
419+
} else {
420+
// Panic if crate type undefined for other cases
421+
crate_type.expect("no crate-type in rustc command line")
422+
};
417423
let build_lib = *config.build_lib.as_ref();
418424
let is_final_crate_type = crate_type == "bin" || (crate_type == "lib" && build_lib);
419425

@@ -512,6 +518,7 @@ struct CargoOptions {
512518
no_default_features: bool,
513519
features: Vec<String>,
514520
jobs: Option<u32>,
521+
all_targets: bool,
515522
}
516523

517524
impl Default for CargoOptions {
@@ -525,6 +532,7 @@ impl Default for CargoOptions {
525532
no_default_features: false,
526533
features: vec![],
527534
jobs: None,
535+
all_targets: false
528536
}
529537
}
530538
}
@@ -538,6 +546,7 @@ impl CargoOptions {
538546
all_features: config.all_features,
539547
no_default_features: config.no_default_features,
540548
jobs: config.jobs,
549+
all_targets: config.all_targets,
541550
..CargoOptions::default()
542551
}
543552
} else {
@@ -562,6 +571,7 @@ impl CargoOptions {
562571
all_features: config.all_features,
563572
no_default_features: config.no_default_features,
564573
jobs: config.jobs,
574+
all_targets: config.all_targets,
565575
..CargoOptions::default()
566576
}
567577
}

src/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub struct Config {
132132
pub all_features: bool,
133133
pub no_default_features: bool,
134134
pub jobs: Option<u32>,
135+
pub all_targets: bool,
135136
}
136137

137138
impl Default for Config {
@@ -156,6 +157,7 @@ impl Default for Config {
156157
all_features: false,
157158
no_default_features: false,
158159
jobs: None,
160+
all_targets: false,
159161
};
160162
result.normalise();
161163
result

src/test/mod.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1478,3 +1478,39 @@ fn test_deglob() {
14781478
],
14791479
);
14801480
}
1481+
1482+
#[test]
1483+
fn test_all_targets() {
1484+
let mut env = Environment::new("bin_lib");
1485+
1486+
let root_path = env.cache.abs_path(Path::new("."));
1487+
1488+
let messages = vec![
1489+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
1490+
];
1491+
1492+
env.with_config(|c| {
1493+
c.all_targets = true;
1494+
c.cfg_test = true;
1495+
});
1496+
let (mut server, results) = env.mock_server(messages);
1497+
// Initialize and build.
1498+
assert_eq!(
1499+
ls_server::LsService::handle_message(&mut server),
1500+
ls_server::ServerStateChange::Continue
1501+
);
1502+
expect_messages(
1503+
results.clone(),
1504+
&[
1505+
ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
1506+
ExpectedMessage::new(None).expect_contains("beginBuild"),
1507+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
1508+
1509+
ExpectedMessage::new(None)
1510+
.expect_contains(r#"bin_lib/tests/tests.rs"#)
1511+
.expect_contains(r#"unused variable: `unused_var`"#),
1512+
1513+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd"),
1514+
],
1515+
);
1516+
}

test_data/bin_lib/tests/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[test]
2+
fn test_some_foo() {
3+
let unused_var = true;
4+
assert!(true);
5+
}

0 commit comments

Comments
 (0)