This commit is contained in:
Aleksey Kladov 2020-06-23 18:34:50 +02:00
parent 30748161f0
commit 21f751a0e5
5 changed files with 24 additions and 31 deletions

View file

@ -61,7 +61,9 @@ use std::{str::FromStr, sync::Arc};
use ra_cfg::CfgOptions;
use rustc_hash::FxHashMap;
use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER};
use test_utils::{
extract_offset, parse_fixture, parse_single_fixture, FixtureEntry, CURSOR_MARKER,
};
use vfs::{file_set::FileSet, VfsPath};
use crate::{
@ -112,7 +114,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
let fixture = parse_single_fixture(ra_fixture);
let crate_graph = if let Some(entry) = fixture {
let meta = match ParsedMeta::from(&entry.meta) {
let meta = match ParsedMeta::from(&entry) {
ParsedMeta::File(it) => it,
};
@ -165,7 +167,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
let mut file_position = None;
for entry in fixture.iter() {
let meta = match ParsedMeta::from(&entry.meta) {
let meta = match ParsedMeta::from(entry) {
ParsedMeta::File(it) => it,
};
assert!(meta.path.starts_with(&source_root_prefix));
@ -243,8 +245,8 @@ struct FileMeta {
env: Env,
}
impl From<&test_utils::FileMeta> for ParsedMeta {
fn from(f: &test_utils::FileMeta) -> Self {
impl From<&FixtureEntry> for ParsedMeta {
fn from(f: &FixtureEntry) -> Self {
Self::File(FileMeta {
path: f.path.to_owned(),
krate: f.crate_name.to_owned(),

View file

@ -25,7 +25,7 @@ impl MockFileData {
fn path(&self) -> &str {
match self {
MockFileData::Plain { path, .. } => path.as_str(),
MockFileData::Fixture(f) => f.meta.path.as_str(),
MockFileData::Fixture(f) => f.path.as_str(),
}
}
@ -38,25 +38,23 @@ impl MockFileData {
fn cfg_options(&self) -> CfgOptions {
match self {
MockFileData::Fixture(f) => f.meta.cfg.clone(),
MockFileData::Fixture(f) => f.cfg.clone(),
_ => CfgOptions::default(),
}
}
fn edition(&self) -> Edition {
match self {
MockFileData::Fixture(f) => f
.meta
.edition
.as_ref()
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
MockFileData::Fixture(f) => {
f.edition.as_ref().map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap())
}
_ => Edition::Edition2018,
}
}
fn env(&self) -> Env {
match self {
MockFileData::Fixture(f) => Env::from(f.meta.env.iter()),
MockFileData::Fixture(f) => Env::from(f.env.iter()),
_ => Env::default(),
}
}

View file

@ -69,7 +69,7 @@ impl<'a> Project<'a> {
let mut paths = vec![];
for entry in parse_fixture(self.fixture) {
let path = tmp_dir.path().join(&entry.meta.path['/'.len_utf8()..]);
let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();
paths.push((path, entry.text));

View file

@ -4,13 +4,8 @@ use stdx::split1;
#[derive(Debug, Eq, PartialEq)]
pub struct FixtureEntry {
pub meta: FileMeta,
pub text: String,
}
#[derive(Debug, Eq, PartialEq)]
pub struct FileMeta {
pub path: String,
pub text: String,
pub crate_name: Option<String>,
pub deps: Vec<String>,
pub cfg: CfgOptions,
@ -71,7 +66,7 @@ The offending line: {:?}"#,
if line.starts_with("//-") {
let meta = line["//-".len()..].trim().to_string();
let meta = parse_meta(&meta);
res.push(FixtureEntry { meta, text: String::new() })
res.push(meta)
} else if let Some(entry) = res.last_mut() {
entry.text.push_str(line);
entry.text.push('\n');
@ -81,7 +76,7 @@ The offending line: {:?}"#,
}
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
fn parse_meta(meta: &str) -> FileMeta {
fn parse_meta(meta: &str) -> FixtureEntry {
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
let path = components[0].to_string();
@ -117,7 +112,7 @@ fn parse_meta(meta: &str) -> FileMeta {
}
}
FileMeta { path, crate_name: krate, deps, edition, cfg, env }
FixtureEntry { path, text: String::new(), crate_name: krate, deps, edition, cfg, env }
}
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
@ -209,12 +204,10 @@ fn parse_fixture_gets_full_meta() {
);
assert_eq!(1, parsed.len());
let parsed = &parsed[0];
assert_eq!("mod m;\n\n", parsed.text);
let meta = &parsed[0];
assert_eq!("mod m;\n\n", meta.text);
let meta = &parsed.meta;
assert_eq!("foo", meta.crate_name().unwrap());
assert_eq!("/lib.rs", meta.path());
assert!(meta.cfg_options().is_some());
assert_eq!(2, meta.env().count());
assert_eq!("foo", meta.crate_name.as_ref().unwrap());
assert_eq!("/lib.rs", meta.path);
assert_eq!(2, meta.env.len());
}

View file

@ -22,7 +22,7 @@ pub use difference::Changeset as __Changeset;
pub use ra_cfg::CfgOptions;
pub use rustc_hash::FxHashMap;
pub use crate::fixture::{parse_fixture, parse_single_fixture, FileMeta, FixtureEntry};
pub use crate::fixture::{parse_fixture, parse_single_fixture, FixtureEntry};
pub const CURSOR_MARKER: &str = "<|>";