Skip to content

Commit d2ea776

Browse files
committed
Enable CfgOptions test for workspace crates
1 parent b1ed887 commit d2ea776

File tree

9 files changed

+134
-26
lines changed

9 files changed

+134
-26
lines changed

crates/ra_db/src/input.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ struct CrateData {
114114
}
115115

116116
impl CrateData {
117-
fn new(file_id: FileId, edition: Edition) -> CrateData {
118-
// FIXME: cfg options
119-
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options: CfgOptions::default() }
117+
fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData {
118+
CrateData { file_id, edition, dependencies: Vec::new(), cfg_options }
120119
}
121120

122121
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -137,9 +136,14 @@ impl Dependency {
137136
}
138137

139138
impl CrateGraph {
140-
pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId {
139+
pub fn add_crate_root(
140+
&mut self,
141+
file_id: FileId,
142+
edition: Edition,
143+
cfg_options: CfgOptions,
144+
) -> CrateId {
141145
let crate_id = CrateId(self.arena.len() as u32);
142-
let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition));
146+
let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options));
143147
assert!(prev.is_none());
144148
crate_id
145149
}
@@ -228,14 +232,14 @@ impl CrateGraph {
228232

229233
#[cfg(test)]
230234
mod tests {
231-
use super::{CrateGraph, Edition::Edition2018, FileId, SmolStr};
235+
use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr};
232236

233237
#[test]
234238
fn it_should_panic_because_of_cycle_dependencies() {
235239
let mut graph = CrateGraph::default();
236-
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
237-
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
238-
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
240+
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
241+
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
242+
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
239243
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
240244
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
241245
assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@@ -244,9 +248,9 @@ mod tests {
244248
#[test]
245249
fn it_works() {
246250
let mut graph = CrateGraph::default();
247-
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018);
248-
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018);
249-
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018);
251+
let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
252+
let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
253+
let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
250254
assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
251255
assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
252256
}

crates/ra_hir/src/mock.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::{panic, sync::Arc};
44

55
use parking_lot::Mutex;
6+
use ra_cfg::CfgOptions;
67
use ra_db::{
78
salsa, CrateGraph, CrateId, Edition, FileId, FilePosition, SourceDatabase, SourceRoot,
89
SourceRootId,
@@ -74,13 +75,13 @@ impl MockDatabase {
7475
pub fn set_crate_graph_from_fixture(&mut self, graph: CrateGraphFixture) {
7576
let mut ids = FxHashMap::default();
7677
let mut crate_graph = CrateGraph::default();
77-
for (crate_name, (crate_root, edition, _)) in graph.0.iter() {
78+
for (crate_name, (crate_root, edition, cfg_options, _)) in graph.0.iter() {
7879
let crate_root = self.file_id_of(&crate_root);
79-
let crate_id = crate_graph.add_crate_root(crate_root, *edition);
80+
let crate_id = crate_graph.add_crate_root(crate_root, *edition, cfg_options.clone());
8081
Arc::make_mut(&mut self.crate_names).insert(crate_id, crate_name.clone());
8182
ids.insert(crate_name, crate_id);
8283
}
83-
for (crate_name, (_, _, deps)) in graph.0.iter() {
84+
for (crate_name, (_, _, _, deps)) in graph.0.iter() {
8485
let from = ids[crate_name];
8586
for dep in deps {
8687
let to = ids[dep];
@@ -184,7 +185,7 @@ impl MockDatabase {
184185

185186
if is_crate_root {
186187
let mut crate_graph = CrateGraph::default();
187-
crate_graph.add_crate_root(file_id, Edition::Edition2018);
188+
crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default());
188189
self.set_crate_graph(Arc::new(crate_graph));
189190
}
190191
file_id
@@ -268,19 +269,27 @@ impl MockDatabase {
268269
}
269270

270271
#[derive(Default)]
271-
pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, Vec<String>))>);
272+
pub struct CrateGraphFixture(pub Vec<(String, (String, Edition, CfgOptions, Vec<String>))>);
272273

273274
#[macro_export]
274275
macro_rules! crate_graph {
275-
($($crate_name:literal: ($crate_path:literal, $($edition:literal,)? [$($dep:literal),*]),)*) => {{
276+
($(
277+
$crate_name:literal: (
278+
$crate_path:literal,
279+
$($edition:literal,)?
280+
[$($dep:literal),*]
281+
$(,$cfg:expr)?
282+
),
283+
)*) => {{
276284
let mut res = $crate::mock::CrateGraphFixture::default();
277285
$(
278286
#[allow(unused_mut, unused_assignments)]
279287
let mut edition = ra_db::Edition::Edition2018;
280288
$(edition = ra_db::Edition::from_string($edition);)?
289+
let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? };
281290
res.0.push((
282291
$crate_name.to_string(),
283-
($crate_path.to_string(), edition, vec![$($dep.to_string()),*])
292+
($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*])
284293
));
285294
)*
286295
res

crates/ra_hir/src/nameres/tests.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod mod_resolution;
77
use std::sync::Arc;
88

99
use insta::assert_snapshot;
10+
use ra_cfg::CfgOptions;
1011
use ra_db::SourceDatabase;
1112
use test_utils::covers;
1213

@@ -507,3 +508,72 @@ fn values_dont_shadow_extern_crates() {
507508
⋮foo: v
508509
"###);
509510
}
511+
512+
#[test]
513+
fn cfg_not_test() {
514+
let map = def_map_with_crate_graph(
515+
r#"
516+
//- /main.rs
517+
use {Foo, Bar, Baz};
518+
//- /lib.rs
519+
#[prelude_import]
520+
pub use self::prelude::*;
521+
mod prelude {
522+
#[cfg(test)]
523+
pub struct Foo;
524+
#[cfg(not(test))]
525+
pub struct Bar;
526+
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
527+
pub struct Baz;
528+
}
529+
"#,
530+
crate_graph! {
531+
"main": ("/main.rs", ["std"]),
532+
"std": ("/lib.rs", []),
533+
},
534+
);
535+
536+
assert_snapshot!(map, @r###"
537+
⋮crate
538+
⋮Bar: t v
539+
⋮Baz: _
540+
⋮Foo: _
541+
"###);
542+
}
543+
544+
#[test]
545+
fn cfg_test() {
546+
let map = def_map_with_crate_graph(
547+
r#"
548+
//- /main.rs
549+
use {Foo, Bar, Baz};
550+
//- /lib.rs
551+
#[prelude_import]
552+
pub use self::prelude::*;
553+
mod prelude {
554+
#[cfg(test)]
555+
pub struct Foo;
556+
#[cfg(not(test))]
557+
pub struct Bar;
558+
#[cfg(all(not(any()), feature = "foo", feature = "bar", opt = "42"))]
559+
pub struct Baz;
560+
}
561+
"#,
562+
crate_graph! {
563+
"main": ("/main.rs", ["std"]),
564+
"std": ("/lib.rs", [], CfgOptions::default()
565+
.atom("test".into())
566+
.feature("foo".into())
567+
.feature("bar".into())
568+
.option("opt".into(), "42".into())
569+
),
570+
},
571+
);
572+
573+
assert_snapshot!(map, @r###"
574+
⋮crate
575+
⋮Bar: _
576+
⋮Baz: t v
577+
⋮Foo: t v
578+
"###);
579+
}

crates/ra_ide_api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ rand = { version = "0.7.0", features = ["small_rng"] }
2323
ra_syntax = { path = "../ra_syntax" }
2424
ra_text_edit = { path = "../ra_text_edit" }
2525
ra_db = { path = "../ra_db" }
26+
ra_cfg = { path = "../ra_cfg" }
2627
ra_fmt = { path = "../ra_fmt" }
2728
ra_prof = { path = "../ra_prof" }
2829
hir = { path = "../ra_hir", package = "ra_hir" }

crates/ra_ide_api/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mod test_utils;
4949

5050
use std::sync::Arc;
5151

52+
use ra_cfg::CfgOptions;
5253
use ra_db::{
5354
salsa::{self, ParallelDatabase},
5455
CheckCanceled, SourceDatabase,
@@ -322,7 +323,10 @@ impl Analysis {
322323
change.add_root(source_root, true);
323324
let mut crate_graph = CrateGraph::default();
324325
let file_id = FileId(0);
325-
crate_graph.add_crate_root(file_id, Edition::Edition2018);
326+
// FIXME: cfg options
327+
// Default to enable test for single file.
328+
let cfg_options = CfgOptions::default().atom("test".into());
329+
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
326330
change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text));
327331
change.set_crate_graph(crate_graph);
328332
host.apply_change(change);

crates/ra_ide_api/src/mock_analysis.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::sync::Arc;
44

5+
use ra_cfg::CfgOptions;
56
use relative_path::RelativePathBuf;
67
use test_utils::{extract_offset, extract_range, parse_fixture, CURSOR_MARKER};
78

@@ -93,10 +94,12 @@ impl MockAnalysis {
9394
assert!(path.starts_with('/'));
9495
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
9596
let file_id = FileId(i as u32 + 1);
97+
// FIXME: cfg options
98+
let cfg_options = CfgOptions::default();
9699
if path == "/lib.rs" || path == "/main.rs" {
97-
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018));
100+
root_crate = Some(crate_graph.add_crate_root(file_id, Edition2018, cfg_options));
98101
} else if path.ends_with("/lib.rs") {
99-
let other_crate = crate_graph.add_crate_root(file_id, Edition2018);
102+
let other_crate = crate_graph.add_crate_root(file_id, Edition2018, cfg_options);
100103
let crate_name = path.parent().unwrap().file_name().unwrap();
101104
if let Some(root_crate) = root_crate {
102105
crate_graph.add_dep(root_crate, crate_name.into(), other_crate).unwrap();

crates/ra_ide_api/src/parent_module.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod tests {
4141
AnalysisChange, CrateGraph,
4242
Edition::Edition2018,
4343
};
44+
use ra_cfg::CfgOptions;
4445

4546
#[test]
4647
fn test_resolve_parent_module() {
@@ -88,7 +89,7 @@ mod tests {
8889
assert!(host.analysis().crate_for(mod_file).unwrap().is_empty());
8990

9091
let mut crate_graph = CrateGraph::default();
91-
let crate_id = crate_graph.add_crate_root(root_file, Edition2018);
92+
let crate_id = crate_graph.add_crate_root(root_file, Edition2018, CfgOptions::default());
9293
let mut change = AnalysisChange::new();
9394
change.set_crate_graph(crate_graph);
9495
host.apply_change(change);

crates/ra_project_model/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ cargo_metadata = "0.8.2"
1212

1313
ra_arena = { path = "../ra_arena" }
1414
ra_db = { path = "../ra_db" }
15+
ra_cfg = { path = "../ra_cfg" }
1516

1617
serde = { version = "1.0.89", features = ["derive"] }
1718
serde_json = "1.0.39"

crates/ra_project_model/src/lib.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111
path::{Path, PathBuf},
1212
};
1313

14+
use ra_cfg::CfgOptions;
1415
use ra_db::{CrateGraph, CrateId, Edition, FileId};
1516
use rustc_hash::FxHashMap;
1617
use serde_json::from_reader;
@@ -131,7 +132,13 @@ impl ProjectWorkspace {
131132
json_project::Edition::Edition2015 => Edition::Edition2015,
132133
json_project::Edition::Edition2018 => Edition::Edition2018,
133134
};
134-
crates.insert(crate_id, crate_graph.add_crate_root(file_id, edition));
135+
// FIXME: cfg options
136+
// Default to enable test for workspace crates.
137+
let cfg_options = CfgOptions::default().atom("test".into());
138+
crates.insert(
139+
crate_id,
140+
crate_graph.add_crate_root(file_id, edition, cfg_options),
141+
);
135142
}
136143
}
137144

@@ -157,7 +164,11 @@ impl ProjectWorkspace {
157164
let mut sysroot_crates = FxHashMap::default();
158165
for krate in sysroot.crates() {
159166
if let Some(file_id) = load(krate.root(&sysroot)) {
160-
let crate_id = crate_graph.add_crate_root(file_id, Edition::Edition2018);
167+
// FIXME: cfg options
168+
// Crates from sysroot have `cfg(test)` disabled
169+
let cfg_options = CfgOptions::default();
170+
let crate_id =
171+
crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options);
161172
sysroot_crates.insert(krate, crate_id);
162173
names.insert(crate_id, krate.name(&sysroot).to_string());
163174
}
@@ -186,7 +197,11 @@ impl ProjectWorkspace {
186197
let root = tgt.root(&cargo);
187198
if let Some(file_id) = load(root) {
188199
let edition = pkg.edition(&cargo);
189-
let crate_id = crate_graph.add_crate_root(file_id, edition);
200+
// FIXME: cfg options
201+
// Default to enable test for workspace crates.
202+
let cfg_options = CfgOptions::default().atom("test".into());
203+
let crate_id =
204+
crate_graph.add_crate_root(file_id, edition, cfg_options);
190205
names.insert(crate_id, pkg.name(&cargo).to_string());
191206
if tgt.kind(&cargo) == TargetKind::Lib {
192207
lib_tgt = Some(crate_id);

0 commit comments

Comments
 (0)