mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Enable CfgOptions test
for workspace crates
This commit is contained in:
parent
b1ed887d81
commit
d2ea776b8f
9 changed files with 134 additions and 26 deletions
|
@ -3,6 +3,7 @@
|
|||
use std::{panic, sync::Arc};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
use ra_cfg::CfgOptions;
|
||||
use ra_db::{
|
||||
salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot,
|
||||
SourceRootId,
|
||||
|
@ -74,13 +75,13 @@ impl MockDatabase {
|
|||
pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) {
|
||||
let mut ids = FxHashMap::default();
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
for (crate_name, (crate_root, edition, _)) in graph.0.iter() {
|
||||
for (crate_name, (crate_root, edition, cfg_options, _)) in graph.0.iter() {
|
||||
let crate_root = self.file_id_of(&crate_root);
|
||||
let crate_id = crate_graph.add_crate_root(crate_root, *edition);
|
||||
let crate_id = crate_graph.add_crate_root(crate_root, *edition, cfg_options.clone());
|
||||
Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone());
|
||||
ids.insert(crate_name, crate_id);
|
||||
}
|
||||
for (crate_name, (_, _, deps)) in graph.0.iter() {
|
||||
for (crate_name, (_, _, _, deps)) in graph.0.iter() {
|
||||
let from = ids[crate_name];
|
||||
for dep in deps {
|
||||
let to = ids[dep];
|
||||
|
@ -184,7 +185,7 @@ impl MockDatabase {
|
|||
|
||||
if is_crate_root {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018);
|
||||
crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
|
||||
self.set_crate_graph(Arc::new(crate_graph));
|
||||
}
|
||||
file_id
|
||||
|
@ -268,19 +269,27 @@ impl MockDatabase {
|
|||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, Vec<String>))>);
|
||||
pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, CfgOptions, Vec<String>))>);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! crate_graph {
|
||||
($($crate_name:literal: ($crate_path:literal, $($edition:literal,)? [$($dep:literal),*]),)*) => {{
|
||||
($(
|
||||
$crate_name:literal: (
|
||||
$crate_path:literal,
|
||||
$($edition:literal,)?
|
||||
[$($dep:literal),*]
|
||||
$(,$cfg:expr)?
|
||||
),
|
||||
)*) => {{
|
||||
let mut res = $crate::mock::CrateGraphFixture::default();
|
||||
$(
|
||||
#[allow(unused_mut, unused_assignments)]
|
||||
let mut edition = ra_db::Edition::Edition2018;
|
||||
$(edition = ra_db::Edition::from_string($edition);)?
|
||||
let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? };
|
||||
res.0.push((
|
||||
$crate_name.to_string(),
|
||||
($crate_path.to_string(), edition, vec![$($dep.to_string()),*])
|
||||
($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])
|
||||
));
|
||||
)*
|
||||
res
|
||||
|
|
|
@ -7,6 +7,7 @@ mod mod_resolution;
|
|||
use std::sync::Arc;
|
||||
|
||||
use insta::assert_snapshot;
|
||||
use ra_cfg::CfgOptions;
|
||||
use ra_db::SourceDatabase;
|
||||
use test_utils::covers;
|
||||
|
||||
|
@ -507,3 +508,72 @@ fn values_dont_shadow_extern_crates() {
|
|||
⋮foo: v
|
||||
"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cfg_not_test() {
|
||||
let map = def_map_with_crate_graph(
|
||||
r#"
|
||||
//- /main.rs
|
||||
use {Foo, Bar, Baz};
|
||||
//- /lib.rs
|
||||
#[prelude_import]
|
||||
pub use self::prelude::*;
|
||||
mod prelude {
|
||||
#[cfg(test)]
|
||||
pub struct Foo;
|
||||
#[cfg(not(test))]
|
||||
pub struct Bar;
|
||||
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
|
||||
pub struct Baz;
|
||||
}
|
||||
"#,
|
||||
crate_graph! {
|
||||
"main": ("/main.rs", ["std"]),
|
||||
"std": ("/lib.rs", []),
|
||||
},
|
||||
);
|
||||
|
||||
assert_snapshot!(map, @r###"
|
||||
⋮crate
|
||||
⋮Bar: t v
|
||||
⋮Baz: _
|
||||
⋮Foo: _
|
||||
"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cfg_test() {
|
||||
let map = def_map_with_crate_graph(
|
||||
r#"
|
||||
//- /main.rs
|
||||
use {Foo, Bar, Baz};
|
||||
//- /lib.rs
|
||||
#[prelude_import]
|
||||
pub use self::prelude::*;
|
||||
mod prelude {
|
||||
#[cfg(test)]
|
||||
pub struct Foo;
|
||||
#[cfg(not(test))]
|
||||
pub struct Bar;
|
||||
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
|
||||
pub struct Baz;
|
||||
}
|
||||
"#,
|
||||
crate_graph! {
|
||||
"main": ("/main.rs", ["std"]),
|
||||
"std": ("/lib.rs", [], CfgOptions::default()
|
||||
.atom("test".into())
|
||||
.feature("foo".into())
|
||||
.feature("bar".into())
|
||||
.option("opt".into(), "42".into())
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
assert_snapshot!(map, @r###"
|
||||
⋮crate
|
||||
⋮Bar: _
|
||||
⋮Baz: t v
|
||||
⋮Foo: t v
|
||||
"###);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue