This commit is contained in:
Aleksey Kladov 2020-06-23 18:20:32 +02:00
parent 6996ec860b
commit 30748161f0
5 changed files with 27 additions and 85 deletions

View file

@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc};
use ra_cfg::CfgOptions;
use rustc_hash::FxHashMap;
use test_utils::{extract_offset, parse_fixture, parse_single_fixture, FixtureMeta, CURSOR_MARKER};
use test_utils::{extract_offset, parse_fixture, parse_single_fixture, CURSOR_MARKER};
use vfs::{file_set::FileSet, VfsPath};
use crate::{
@ -243,20 +243,18 @@ struct FileMeta {
env: Env,
}
impl From<&FixtureMeta> for ParsedMeta {
fn from(meta: &FixtureMeta) -> Self {
match meta {
FixtureMeta::File(f) => Self::File(FileMeta {
path: f.path.to_owned(),
krate: f.crate_name.to_owned(),
deps: f.deps.to_owned(),
cfg: f.cfg.to_owned(),
edition: f
.edition
.as_ref()
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
env: Env::from(f.env.iter()),
}),
}
impl From<&test_utils::FileMeta> for ParsedMeta {
fn from(f: &test_utils::FileMeta) -> Self {
Self::File(FileMeta {
path: f.path.to_owned(),
krate: f.crate_name.to_owned(),
deps: f.deps.to_owned(),
cfg: f.cfg.to_owned(),
edition: f
.edition
.as_ref()
.map_or(Edition::Edition2018, |v| Edition::from_str(&v).unwrap()),
env: Env::from(f.env.iter()),
})
}
}

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(),
MockFileData::Fixture(f) => f.meta.path.as_str(),
}
}
@ -38,25 +38,25 @@ impl MockFileData {
fn cfg_options(&self) -> CfgOptions {
match self {
MockFileData::Fixture(f) => {
f.meta.cfg_options().map_or_else(Default::default, |o| o.clone())
}
MockFileData::Fixture(f) => f.meta.cfg.clone(),
_ => CfgOptions::default(),
}
}
fn edition(&self) -> Edition {
match self {
MockFileData::Fixture(f) => {
f.meta.edition().map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap())
}
MockFileData::Fixture(f) => f
.meta
.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()),
MockFileData::Fixture(f) => Env::from(f.meta.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.meta.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,15 +4,10 @@ use stdx::split1;
#[derive(Debug, Eq, PartialEq)]
pub struct FixtureEntry {
pub meta: FixtureMeta,
pub meta: FileMeta,
pub text: String,
}
#[derive(Debug, Eq, PartialEq)]
pub enum FixtureMeta {
File(FileMeta),
}
#[derive(Debug, Eq, PartialEq)]
pub struct FileMeta {
pub path: String,
@ -23,57 +18,6 @@ pub struct FileMeta {
pub env: FxHashMap<String, String>,
}
impl FixtureMeta {
pub fn path(&self) -> &str {
match self {
FixtureMeta::File(f) => &f.path,
}
}
pub fn crate_name(&self) -> Option<&String> {
match self {
FixtureMeta::File(f) => f.crate_name.as_ref(),
}
}
pub fn cfg_options(&self) -> Option<&CfgOptions> {
match self {
FixtureMeta::File(f) => Some(&f.cfg),
}
}
pub fn edition(&self) -> Option<&String> {
match self {
FixtureMeta::File(f) => f.edition.as_ref(),
}
}
pub fn env(&self) -> impl Iterator<Item = (&String, &String)> {
struct EnvIter<'a> {
iter: Option<std::collections::hash_map::Iter<'a, String, String>>,
}
impl<'a> EnvIter<'a> {
fn new(meta: &'a FixtureMeta) -> Self {
Self {
iter: match meta {
FixtureMeta::File(f) => Some(f.env.iter()),
},
}
}
}
impl<'a> Iterator for EnvIter<'a> {
type Item = (&'a String, &'a String);
fn next(&mut self) -> Option<Self::Item> {
self.iter.as_mut().and_then(|i| i.next())
}
}
EnvIter::new(self)
}
}
/// Same as `parse_fixture`, except it allow empty fixture
pub fn parse_single_fixture(ra_fixture: &str) -> Option<FixtureEntry> {
if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) {
@ -137,7 +81,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) -> FixtureMeta {
fn parse_meta(meta: &str) -> FileMeta {
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
let path = components[0].to_string();
@ -173,7 +117,7 @@ fn parse_meta(meta: &str) -> FixtureMeta {
}
}
FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env })
FileMeta { path, crate_name: krate, deps, edition, cfg, env }
}
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.

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, FixtureEntry, FixtureMeta};
pub use crate::fixture::{parse_fixture, parse_single_fixture, FileMeta, FixtureEntry};
pub const CURSOR_MARKER: &str = "<|>";