Skip to content

Commit 6b9d66b

Browse files
Merge #3536
3536: Add get and set for `Env` r=matklad a=edwin0cheng This PR add three things : 1. Add `get` and `set` in `Env`. 2. Implement fixture meta for `with_single_file`. 3. Add `env` meta in fixture. Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
2 parents 34bc0f4 + 95ba7da commit 6b9d66b

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use ra_cfg::CfgOptions;
77
use rustc_hash::FxHashMap;
8-
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
8+
use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER};
99

1010
use crate::{
1111
input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf,
@@ -45,23 +45,37 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
4545

4646
impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
4747

48-
fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId {
48+
fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId {
4949
let file_id = FileId(0);
5050
let rel_path: RelativePathBuf = "/main.rs".into();
5151

5252
let mut source_root = SourceRoot::new_local();
5353
source_root.insert_file(rel_path.clone(), file_id);
5454

55-
let mut crate_graph = CrateGraph::default();
56-
crate_graph.add_crate_root(
57-
file_id,
58-
Edition::Edition2018,
59-
None,
60-
CfgOptions::default(),
61-
Env::default(),
62-
);
63-
64-
db.set_file_text(file_id, Arc::new(text.to_string()));
55+
let fixture = parse_single_fixture(ra_fixture);
56+
57+
let crate_graph = if let Some(entry) = fixture {
58+
let meta = match parse_meta(&entry.meta) {
59+
ParsedMeta::File(it) => it,
60+
_ => panic!("with_single_file only support file meta"),
61+
};
62+
63+
let mut crate_graph = CrateGraph::default();
64+
crate_graph.add_crate_root(file_id, meta.edition, meta.krate, meta.cfg, meta.env);
65+
crate_graph
66+
} else {
67+
let mut crate_graph = CrateGraph::default();
68+
crate_graph.add_crate_root(
69+
file_id,
70+
Edition::Edition2018,
71+
None,
72+
CfgOptions::default(),
73+
Env::default(),
74+
);
75+
crate_graph
76+
};
77+
78+
db.set_file_text(file_id, Arc::new(ra_fixture.to_string()));
6579
db.set_file_relative_path(file_id, rel_path);
6680
db.set_file_source_root(file_id, WORKSPACE);
6781
db.set_source_root(WORKSPACE, Arc::new(source_root));
@@ -104,7 +118,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
104118
meta.edition,
105119
Some(krate.clone()),
106120
meta.cfg,
107-
Env::default(),
121+
meta.env,
108122
);
109123
let prev = crates.insert(krate.clone(), crate_id);
110124
assert!(prev.is_none());
@@ -167,9 +181,10 @@ struct FileMeta {
167181
deps: Vec<String>,
168182
cfg: CfgOptions,
169183
edition: Edition,
184+
env: Env,
170185
}
171186

172-
//- /lib.rs crate:foo deps:bar,baz
187+
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo)
173188
fn parse_meta(meta: &str) -> ParsedMeta {
174189
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
175190

@@ -186,6 +201,7 @@ fn parse_meta(meta: &str) -> ParsedMeta {
186201
let mut deps = Vec::new();
187202
let mut edition = Edition::Edition2018;
188203
let mut cfg = CfgOptions::default();
204+
let mut env = Env::default();
189205
for component in components[1..].iter() {
190206
let (key, value) = split1(component, ':').unwrap();
191207
match key {
@@ -200,11 +216,18 @@ fn parse_meta(meta: &str) -> ParsedMeta {
200216
}
201217
}
202218
}
219+
"env" => {
220+
for key in value.split(',') {
221+
if let Some((k, v)) = split1(key, '=') {
222+
env.set(k.into(), v.into());
223+
}
224+
}
225+
}
203226
_ => panic!("bad component: {:?}", component),
204227
}
205228
}
206229

207-
ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg })
230+
ParsedMeta::File(FileMeta { path, krate, deps, edition, cfg, env })
208231
}
209232

210233
fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {

crates/ra_db/src/input.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ impl fmt::Display for Edition {
261261
}
262262
}
263263

264+
impl Env {
265+
pub fn set(&mut self, env: &str, value: String) {
266+
self.entries.insert(env.to_owned(), value);
267+
}
268+
269+
pub fn get(&self, env: &str) -> Option<String> {
270+
self.entries.get(env).cloned()
271+
}
272+
}
273+
264274
#[derive(Debug)]
265275
pub struct ParseEditionError {
266276
invalid_input: String,

crates/test_utils/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ pub fn parse_fixture(fixture: &str) -> Vec<FixtureEntry> {
202202
res
203203
}
204204

205+
/// Same as `parse_fixture`, except it allow empty fixture
206+
pub fn parse_single_fixture(fixture: &str) -> Option<FixtureEntry> {
207+
if !fixture.lines().any(|it| it.trim_start().starts_with("//-")) {
208+
return None;
209+
}
210+
211+
let fixtures = parse_fixture(fixture);
212+
if fixtures.len() > 1 {
213+
panic!("too many fixtures");
214+
}
215+
fixtures.into_iter().nth(0)
216+
}
217+
205218
// Comparison functionality borrowed from cargo:
206219

207220
/// Compare a line with an expected pattern.

0 commit comments

Comments
 (0)