From eeb98237d1cf5134ca3e41b85e1e9e07cab83c75 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 11:16:32 +0300 Subject: parse fixture meta in test_utils crate --- crates/test_utils/Cargo.toml | 4 ++ crates/test_utils/src/lib.rs | 98 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 8ec986bcb..4d185b01c 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -11,3 +11,7 @@ doctest = false difference = "2.0.0" text-size = "1.0.0" serde_json = "1.0.48" +relative-path = "1.0.0" +rustc-hash = "1.1.0" + +ra_cfg = { path = "../ra_cfg" } \ No newline at end of file diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index b1e3c328f..12ae5f451 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -14,8 +14,12 @@ use std::{ path::{Path, PathBuf}, }; +pub use ra_cfg::CfgOptions; + use serde_json::Value; use text_size::{TextRange, TextSize}; +pub use relative_path::{RelativePath, RelativePathBuf}; +pub use rustc_hash::FxHashMap; pub use difference::Changeset as __Changeset; @@ -159,6 +163,33 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { pub struct FixtureEntry { pub meta: String, pub text: String, + + pub parsed_meta: FixtureMeta, +} + +#[derive(Debug, Eq, PartialEq)] +pub enum FixtureMeta { + Root { path: RelativePathBuf }, + File(FileMeta), +} + +#[derive(Debug, Eq, PartialEq)] +pub struct FileMeta { + pub path: RelativePathBuf, + pub krate: Option, + pub deps: Vec, + pub cfg: ra_cfg::CfgOptions, + pub edition: Option, + pub env: FxHashMap, +} + +impl FixtureMeta { + pub fn path(&self) -> &RelativePath { + match self { + FixtureMeta::Root { path } => &path, + FixtureMeta::File(f) => &f.path, + } + } } /// Parses text which looks like this: @@ -200,7 +231,8 @@ The offending line: {:?}"#, for line in lines.by_ref() { if line.starts_with("//-") { let meta = line["//-".len()..].trim().to_string(); - res.push(FixtureEntry { meta, text: String::new() }) + let parsed_meta = parse_meta(&meta); + res.push(FixtureEntry { meta, parsed_meta, text: String::new() }) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); entry.text.push('\n'); @@ -209,6 +241,58 @@ The offending line: {:?}"#, res } +//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo +fn parse_meta(meta: &str) -> FixtureMeta { + let components = meta.split_ascii_whitespace().collect::>(); + + if components[0] == "root" { + let path: RelativePathBuf = components[1].into(); + assert!(path.starts_with("/") && path.ends_with("/")); + return FixtureMeta::Root { path }; + } + + let path: RelativePathBuf = components[0].into(); + assert!(path.starts_with("/")); + + let mut krate = None; + let mut deps = Vec::new(); + let mut edition = None; + let mut cfg = CfgOptions::default(); + let mut env = FxHashMap::default(); + for component in components[1..].iter() { + let (key, value) = split1(component, ':').unwrap(); + match key { + "crate" => krate = Some(value.to_string()), + "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), + "edition" => edition = Some(value.to_string()), + "cfg" => { + for key in value.split(',') { + match split1(key, '=') { + None => cfg.insert_atom(key.into()), + Some((k, v)) => cfg.insert_key_value(k.into(), v.into()), + } + } + } + "env" => { + for key in value.split(',') { + if let Some((k, v)) = split1(key, '=') { + env.insert(k.into(), v.into()); + } + } + } + _ => panic!("bad component: {:?}", component), + } + } + + FixtureMeta::File(FileMeta { path, krate, deps, edition, cfg, env }) +} + +fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { + let idx = haystack.find(delim)?; + Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) +} + + /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// the other lines visually: @@ -288,6 +372,18 @@ struct Bar; ) } +#[test] +fn parse_fixture_gets_full_meta() { + let fixture = r" + //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo + "; + let parsed = parse_fixture(fixture); + assert_eq!(1, parsed.len()); + + let parsed = &parsed[0]; + assert_eq!("\n", parsed.text); +} + /// Same as `parse_fixture`, except it allow empty fixture pub fn parse_single_fixture(fixture: &str) -> Option { if !fixture.lines().any(|it| it.trim_start().starts_with("//-")) { -- cgit v1.2.3 From d901e0e709258f71183455865cb6f9e07b3dd5d3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 11:57:41 +0300 Subject: Reimplement ra_db::fixture::ParsedMeta in terms of test_utils::FixtureMeta --- crates/test_utils/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 12ae5f451..0c367ce71 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -16,10 +16,10 @@ use std::{ pub use ra_cfg::CfgOptions; -use serde_json::Value; -use text_size::{TextRange, TextSize}; pub use relative_path::{RelativePath, RelativePathBuf}; pub use rustc_hash::FxHashMap; +use serde_json::Value; +use text_size::{TextRange, TextSize}; pub use difference::Changeset as __Changeset; @@ -292,7 +292,6 @@ fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) } - /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// the other lines visually: -- cgit v1.2.3 From 256fb7556e9f4a329e673851427942c6403bacb6 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 12:25:26 +0300 Subject: Remove temporary FixtureEntry parsed_meta field. --- crates/test_utils/src/lib.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 0c367ce71..6a8d06ea7 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -161,10 +161,8 @@ pub fn add_cursor(text: &str, offset: TextSize) -> String { #[derive(Debug, Eq, PartialEq)] pub struct FixtureEntry { - pub meta: String, + pub meta: FixtureMeta, pub text: String, - - pub parsed_meta: FixtureMeta, } #[derive(Debug, Eq, PartialEq)] @@ -231,8 +229,8 @@ The offending line: {:?}"#, for line in lines.by_ref() { if line.starts_with("//-") { let meta = line["//-".len()..].trim().to_string(); - let parsed_meta = parse_meta(&meta); - res.push(FixtureEntry { meta, parsed_meta, text: String::new() }) + let meta = parse_meta(&meta); + res.push(FixtureEntry { meta, text: String::new() }) } else if let Some(entry) = res.last_mut() { entry.text.push_str(line); entry.text.push('\n'); -- cgit v1.2.3 From 2dde9b19943d3f9557520428c92a52d75fb1deb3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 13:17:21 +0300 Subject: Use FixtureMeta in MockAnalysis --- crates/test_utils/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 6a8d06ea7..584ca5c39 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -176,7 +176,7 @@ pub struct FileMeta { pub path: RelativePathBuf, pub krate: Option, pub deps: Vec, - pub cfg: ra_cfg::CfgOptions, + pub cfg: CfgOptions, pub edition: Option, pub env: FxHashMap, } @@ -188,6 +188,13 @@ impl FixtureMeta { FixtureMeta::File(f) => &f.path, } } + + pub fn cfg_options(&self) -> Option<&CfgOptions> { + match self { + FixtureMeta::File(f) => Some(&f.cfg), + _ => None, + } + } } /// Parses text which looks like this: -- cgit v1.2.3 From 2c00bd8c6a9476dbac427c84941fe6f54a8a95b1 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 15:23:43 +0300 Subject: Propogate fixture meta to AnalysisHost Except crate name. --- crates/test_utils/src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 584ca5c39..e7fa201e9 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -174,7 +174,7 @@ pub enum FixtureMeta { #[derive(Debug, Eq, PartialEq)] pub struct FileMeta { pub path: RelativePathBuf, - pub krate: Option, + pub crate_name: Option, pub deps: Vec, pub cfg: CfgOptions, pub edition: Option, @@ -189,12 +189,52 @@ impl FixtureMeta { } } + pub fn crate_name(&self) -> Option<&String> { + match self { + FixtureMeta::File(f) => f.crate_name.as_ref(), + _ => None, + } + } + pub fn cfg_options(&self) -> Option<&CfgOptions> { match self { FixtureMeta::File(f) => Some(&f.cfg), _ => None, } } + + pub fn edition(&self) -> Option<&String> { + match self { + FixtureMeta::File(f) => f.edition.as_ref(), + _ => None, + } + } + + pub fn env(&self) -> impl Iterator { + struct EnvIter<'a> { + iter: Option>, + } + + impl<'a> EnvIter<'a> { + fn new(meta: &'a FixtureMeta) -> Self { + Self { + iter: match meta { + FixtureMeta::File(f) => Some(f.env.iter()), + _ => None, + }, + } + } + } + + impl<'a> Iterator for EnvIter<'a> { + type Item = (&'a String, &'a String); + fn next(&mut self) -> Option { + self.iter.as_mut().and_then(|i| i.next()) + } + } + + EnvIter::new(self) + } } /// Parses text which looks like this: @@ -289,7 +329,7 @@ fn parse_meta(meta: &str) -> FixtureMeta { } } - FixtureMeta::File(FileMeta { path, krate, deps, edition, cfg, env }) + FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env }) } fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { -- cgit v1.2.3 From 7e9c7ac4ee5c9295d811670277bc1aeb9775998c Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 16:56:41 +0300 Subject: smoke test --- crates/test_utils/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index e7fa201e9..fc5ae8f07 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -426,6 +426,12 @@ fn parse_fixture_gets_full_meta() { let parsed = &parsed[0]; assert_eq!("\n", parsed.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()); } /// Same as `parse_fixture`, except it allow empty fixture -- cgit v1.2.3 From cd45c73b660f85acc9b564e536bc407f0836891d Mon Sep 17 00:00:00 2001 From: vsrs Date: Sat, 16 May 2020 17:25:12 +0300 Subject: JFF, ra_fixture arg name for a code highlighting --- crates/test_utils/src/lib.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index fc5ae8f07..45abfead9 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -245,8 +245,8 @@ impl FixtureMeta { /// line 2 /// // - other meta /// ``` -pub fn parse_fixture(fixture: &str) -> Vec { - let fixture = indent_first_line(fixture); +pub fn parse_fixture(ra_fixture: &str) -> Vec { + let fixture = indent_first_line(ra_fixture); let margin = fixture_margin(&fixture); let mut lines = fixture @@ -418,14 +418,16 @@ struct Bar; #[test] fn parse_fixture_gets_full_meta() { - let fixture = r" + let parsed = parse_fixture( + r" //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo - "; - let parsed = parse_fixture(fixture); + mod m; + ", + ); assert_eq!(1, parsed.len()); let parsed = &parsed[0]; - assert_eq!("\n", parsed.text); + assert_eq!("mod m;\n\n", parsed.text); let meta = &parsed.meta; assert_eq!("foo", meta.crate_name().unwrap()); @@ -435,12 +437,12 @@ fn parse_fixture_gets_full_meta() { } /// Same as `parse_fixture`, except it allow empty fixture -pub fn parse_single_fixture(fixture: &str) -> Option { - if !fixture.lines().any(|it| it.trim_start().starts_with("//-")) { +pub fn parse_single_fixture(ra_fixture: &str) -> Option { + if !ra_fixture.lines().any(|it| it.trim_start().starts_with("//-")) { return None; } - let fixtures = parse_fixture(fixture); + let fixtures = parse_fixture(ra_fixture); if fixtures.len() > 1 { panic!("too many fixtures"); } -- cgit v1.2.3 From f06b2bcd91329fb795839a4eabd8f43aa472aeb2 Mon Sep 17 00:00:00 2001 From: Aaron Loucks Date: Wed, 3 Jun 2020 07:26:15 -0400 Subject: Use split1 when formatting function signature params --- crates/test_utils/Cargo.toml | 3 ++- crates/test_utils/src/lib.rs | 6 +----- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'crates/test_utils') diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 4d185b01c..8840bf36a 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -14,4 +14,5 @@ serde_json = "1.0.48" relative-path = "1.0.0" rustc-hash = "1.1.0" -ra_cfg = { path = "../ra_cfg" } \ No newline at end of file +ra_cfg = { path = "../ra_cfg" } +stdx = { path = "../stdx" } \ No newline at end of file diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index 1bd97215c..2141bfc20 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -15,6 +15,7 @@ use std::{ }; pub use ra_cfg::CfgOptions; +use stdx::split1; pub use relative_path::{RelativePath, RelativePathBuf}; pub use rustc_hash::FxHashMap; @@ -332,11 +333,6 @@ fn parse_meta(meta: &str) -> FixtureMeta { FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env }) } -fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> { - let idx = haystack.find(delim)?; - Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) -} - /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// the other lines visually: -- cgit v1.2.3