2020-06-23 11:58:45 -05:00
|
|
|
//! Defines `Fixture` -- a convenient way to describe the initial state of
|
|
|
|
//! rust-analyzer database from a single string.
|
2021-04-06 13:42:09 -05:00
|
|
|
//!
|
|
|
|
//! Fixtures are strings containing rust source code with optional metadata.
|
|
|
|
//! A fixture without metadata is parsed into a single source file.
|
|
|
|
//! Use this to test functionality local to one file.
|
|
|
|
//!
|
|
|
|
//! Simple Example:
|
|
|
|
//! ```
|
|
|
|
//! r#"
|
|
|
|
//! fn main() {
|
|
|
|
//! println!("Hello World")
|
|
|
|
//! }
|
|
|
|
//! "#
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Metadata can be added to a fixture after a `//-` comment.
|
|
|
|
//! The basic form is specifying filenames,
|
|
|
|
//! which is also how to define multiple files in a single test fixture
|
|
|
|
//!
|
|
|
|
//! Example using two files in the same crate:
|
|
|
|
//! ```
|
|
|
|
//! "
|
|
|
|
//! //- /main.rs
|
|
|
|
//! mod foo;
|
|
|
|
//! fn main() {
|
|
|
|
//! foo::bar();
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! //- /foo.rs
|
|
|
|
//! pub fn bar() {}
|
|
|
|
//! "
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Example using two crates with one file each, with one crate depending on the other:
|
|
|
|
//! ```
|
|
|
|
//! r#"
|
|
|
|
//! //- /main.rs crate:a deps:b
|
|
|
|
//! fn main() {
|
|
|
|
//! b::foo();
|
|
|
|
//! }
|
|
|
|
//! //- /lib.rs crate:b
|
|
|
|
//! pub fn b() {
|
|
|
|
//! println!("Hello World")
|
|
|
|
//! }
|
|
|
|
//! "#
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Metadata allows specifying all settings and variables
|
|
|
|
//! that are available in a real rust project:
|
|
|
|
//! - crate names via `crate:cratename`
|
|
|
|
//! - dependencies via `deps:dep1,dep2`
|
|
|
|
//! - configuration settings via `cfg:dbg=false,opt_level=2`
|
|
|
|
//! - environment variables via `env:PATH=/bin,RUST_LOG=debug`
|
|
|
|
//!
|
|
|
|
//! Example using all available metadata:
|
|
|
|
//! ```
|
|
|
|
//! "
|
|
|
|
//! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
|
|
|
//! fn insert_source_code_here() {}
|
|
|
|
//! "
|
|
|
|
//! ```
|
2020-06-23 11:58:45 -05:00
|
|
|
|
2020-06-23 10:59:56 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2021-05-04 06:10:49 -05:00
|
|
|
use stdx::trim_indent;
|
2020-06-23 10:59:56 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-06-23 11:46:56 -05:00
|
|
|
pub struct Fixture {
|
2020-06-23 10:59:56 -05:00
|
|
|
pub path: String,
|
2020-06-23 11:34:50 -05:00
|
|
|
pub text: String,
|
2020-06-24 03:22:02 -05:00
|
|
|
pub krate: Option<String>,
|
2020-06-23 10:59:56 -05:00
|
|
|
pub deps: Vec<String>,
|
2020-06-23 11:56:26 -05:00
|
|
|
pub cfg_atoms: Vec<String>,
|
|
|
|
pub cfg_key_values: Vec<(String, String)>,
|
2020-06-23 10:59:56 -05:00
|
|
|
pub edition: Option<String>,
|
|
|
|
pub env: FxHashMap<String, String>,
|
2021-03-16 09:28:02 -05:00
|
|
|
pub introduce_new_source_root: bool,
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 13:02:40 -05:00
|
|
|
pub struct MiniCore {
|
|
|
|
activated_flags: Vec<String>,
|
|
|
|
valid_flags: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:46:56 -05:00
|
|
|
impl Fixture {
|
|
|
|
/// Parses text which looks like this:
|
|
|
|
///
|
|
|
|
/// ```not_rust
|
|
|
|
/// //- some meta
|
|
|
|
/// line 1
|
|
|
|
/// line 2
|
2021-04-06 13:42:09 -05:00
|
|
|
/// //- other meta
|
2020-06-23 11:46:56 -05:00
|
|
|
/// ```
|
2021-06-15 13:02:40 -05:00
|
|
|
///
|
|
|
|
/// Fixture can also start with a minicore declaration:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// //- minicore: sized
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// That will include a subset of `libcore` into the fixture, see
|
|
|
|
/// `minicore.rs` for what's available.
|
|
|
|
pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<Fixture>) {
|
2020-06-23 15:27:24 -05:00
|
|
|
let fixture = trim_indent(ra_fixture);
|
2021-06-15 13:02:40 -05:00
|
|
|
let mut fixture = fixture.as_str();
|
|
|
|
let mut mini_core = None;
|
2020-06-23 11:46:56 -05:00
|
|
|
let mut res: Vec<Fixture> = Vec::new();
|
2020-06-23 15:27:24 -05:00
|
|
|
|
2021-06-15 13:02:40 -05:00
|
|
|
if fixture.starts_with("//- minicore:") {
|
|
|
|
let first_line = fixture.split_inclusive('\n').next().unwrap();
|
|
|
|
mini_core = Some(MiniCore::parse(first_line));
|
|
|
|
fixture = &fixture[first_line.len()..];
|
|
|
|
}
|
|
|
|
|
|
|
|
let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") };
|
2020-06-23 17:30:34 -05:00
|
|
|
|
2021-05-04 06:10:49 -05:00
|
|
|
for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
|
2020-06-23 15:27:24 -05:00
|
|
|
if line.contains("//-") {
|
|
|
|
assert!(
|
|
|
|
line.starts_with("//-"),
|
|
|
|
"Metadata line {} has invalid indentation. \
|
|
|
|
All metadata lines need to have the same indentation.\n\
|
|
|
|
The offending line: {:?}",
|
|
|
|
ix,
|
|
|
|
line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:46:56 -05:00
|
|
|
if line.starts_with("//-") {
|
2020-06-23 14:45:40 -05:00
|
|
|
let meta = Fixture::parse_meta_line(line);
|
2020-06-23 11:46:56 -05:00
|
|
|
res.push(meta)
|
2021-06-16 02:30:29 -05:00
|
|
|
} else {
|
|
|
|
if line.starts_with("// ")
|
|
|
|
&& line.contains(":")
|
|
|
|
&& !line.contains("::")
|
|
|
|
&& line.chars().all(|it| !it.is_uppercase())
|
|
|
|
{
|
|
|
|
panic!("looks like invalid metadata line: {:?}", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(entry) = res.last_mut() {
|
|
|
|
entry.text.push_str(line);
|
|
|
|
}
|
2020-06-23 11:46:56 -05:00
|
|
|
}
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
2020-06-23 15:27:24 -05:00
|
|
|
|
2021-06-15 13:02:40 -05:00
|
|
|
(mini_core, res)
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 11:46:56 -05:00
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
|
2020-06-24 03:22:02 -05:00
|
|
|
fn parse_meta_line(meta: &str) -> Fixture {
|
2020-06-23 13:38:36 -05:00
|
|
|
assert!(meta.starts_with("//-"));
|
|
|
|
let meta = meta["//-".len()..].trim();
|
2020-06-23 11:46:56 -05:00
|
|
|
let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let path = components[0].to_string();
|
2021-06-17 03:18:37 -05:00
|
|
|
assert!(path.starts_with('/'), "fixture path does not start with `/`: {:?}", path);
|
2020-06-23 11:46:56 -05:00
|
|
|
|
|
|
|
let mut krate = None;
|
|
|
|
let mut deps = Vec::new();
|
|
|
|
let mut edition = None;
|
2020-06-23 11:56:26 -05:00
|
|
|
let mut cfg_atoms = Vec::new();
|
|
|
|
let mut cfg_key_values = Vec::new();
|
2020-06-23 11:46:56 -05:00
|
|
|
let mut env = FxHashMap::default();
|
2021-03-16 09:28:02 -05:00
|
|
|
let mut introduce_new_source_root = false;
|
2020-06-23 11:46:56 -05:00
|
|
|
for component in components[1..].iter() {
|
2021-06-16 02:48:07 -05:00
|
|
|
let (key, value) = component
|
|
|
|
.split_once(':')
|
|
|
|
.unwrap_or_else(|| panic!("invalid meta line: {:?}", meta));
|
2020-06-23 11:46:56 -05:00
|
|
|
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" => {
|
2020-06-23 11:56:26 -05:00
|
|
|
for entry in value.split(',') {
|
2021-05-04 06:10:49 -05:00
|
|
|
match entry.split_once('=') {
|
2020-06-23 11:56:26 -05:00
|
|
|
Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
|
|
|
|
None => cfg_atoms.push(entry.to_string()),
|
2020-06-23 11:46:56 -05:00
|
|
|
}
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-23 11:46:56 -05:00
|
|
|
"env" => {
|
|
|
|
for key in value.split(',') {
|
2021-05-04 06:10:49 -05:00
|
|
|
if let Some((k, v)) = key.split_once('=') {
|
2020-06-23 11:46:56 -05:00
|
|
|
env.insert(k.into(), v.into());
|
|
|
|
}
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
2021-03-16 09:28:02 -05:00
|
|
|
"new_source_root" => introduce_new_source_root = true,
|
2020-06-23 11:46:56 -05:00
|
|
|
_ => panic!("bad component: {:?}", component),
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:28:02 -05:00
|
|
|
Fixture {
|
|
|
|
path,
|
|
|
|
text: String::new(),
|
|
|
|
krate,
|
|
|
|
deps,
|
|
|
|
cfg_atoms,
|
|
|
|
cfg_key_values,
|
|
|
|
edition,
|
|
|
|
env,
|
|
|
|
introduce_new_source_root,
|
|
|
|
}
|
2020-06-23 11:46:56 -05:00
|
|
|
}
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 13:02:40 -05:00
|
|
|
impl MiniCore {
|
|
|
|
fn has_flag(&self, flag: &str) -> bool {
|
|
|
|
self.activated_flags.iter().any(|it| it == flag)
|
|
|
|
}
|
|
|
|
|
2021-06-15 14:59:51 -05:00
|
|
|
#[track_caller]
|
2021-06-15 13:02:40 -05:00
|
|
|
fn assert_valid_flag(&self, flag: &str) {
|
|
|
|
if !self.valid_flags.iter().any(|it| it == flag) {
|
|
|
|
panic!("invalid flag: {:?}, valid flags: {:?}", flag, self.valid_flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(line: &str) -> MiniCore {
|
|
|
|
let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() };
|
|
|
|
|
|
|
|
let line = line.strip_prefix("//- minicore:").unwrap().trim();
|
|
|
|
for entry in line.split(", ") {
|
|
|
|
if res.has_flag(entry) {
|
|
|
|
panic!("duplicate minicore flag: {:?}", entry)
|
|
|
|
}
|
|
|
|
res.activated_flags.push(entry.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Strips parts of minicore.rs which are flagged by inactive flags.
|
|
|
|
///
|
|
|
|
/// This is probably over-engineered to support flags dependencies.
|
|
|
|
pub fn source_code(mut self) -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
let raw_mini_core = include_str!("./minicore.rs");
|
|
|
|
let mut lines = raw_mini_core.split_inclusive('\n');
|
|
|
|
|
|
|
|
let mut parsing_flags = false;
|
|
|
|
let mut implications = Vec::new();
|
|
|
|
|
|
|
|
// Parse `//!` preamble and extract flags and dependencies.
|
|
|
|
for line in lines.by_ref() {
|
|
|
|
let line = match line.strip_prefix("//!") {
|
|
|
|
Some(it) => it,
|
|
|
|
None => {
|
|
|
|
assert!(line.trim().is_empty());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if parsing_flags {
|
|
|
|
let (flag, deps) = line.split_once(':').unwrap();
|
|
|
|
let flag = flag.trim();
|
|
|
|
self.valid_flags.push(flag.to_string());
|
|
|
|
for dep in deps.split(", ") {
|
|
|
|
let dep = dep.trim();
|
|
|
|
if !dep.is_empty() {
|
|
|
|
self.assert_valid_flag(dep);
|
|
|
|
implications.push((flag, dep));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if line.contains("Available flags:") {
|
|
|
|
parsing_flags = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for flag in &self.activated_flags {
|
|
|
|
self.assert_valid_flag(flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixed point loop to compute transitive closure of flags.
|
|
|
|
loop {
|
|
|
|
let mut changed = false;
|
|
|
|
for &(u, v) in implications.iter() {
|
|
|
|
if self.has_flag(u) && !self.has_flag(v) {
|
|
|
|
self.activated_flags.push(v.to_string());
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !changed {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
let mut active_regions = Vec::new();
|
2021-06-15 14:38:21 -05:00
|
|
|
let mut seen_regions = Vec::new();
|
2021-06-15 13:02:40 -05:00
|
|
|
for line in lines {
|
|
|
|
let trimmed = line.trim();
|
|
|
|
if let Some(region) = trimmed.strip_prefix("// region:") {
|
2021-06-16 02:30:29 -05:00
|
|
|
active_regions.push(region);
|
2021-06-15 13:02:40 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some(region) = trimmed.strip_prefix("// endregion:") {
|
2021-06-16 02:30:29 -05:00
|
|
|
let prev = active_regions.pop().unwrap();
|
|
|
|
assert_eq!(prev, region);
|
2021-06-15 13:02:40 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
let mut line_region = false;
|
2021-06-15 13:02:40 -05:00
|
|
|
if let Some(idx) = trimmed.find("// :") {
|
2021-06-16 02:30:29 -05:00
|
|
|
line_region = true;
|
|
|
|
active_regions.push(&trimmed[idx + "// :".len()..]);
|
2021-06-15 13:02:40 -05:00
|
|
|
}
|
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
let mut keep = true;
|
|
|
|
for ®ion in &active_regions {
|
|
|
|
assert!(
|
|
|
|
!region.starts_with(' '),
|
|
|
|
"region marker starts with a space: {:?}",
|
|
|
|
region
|
|
|
|
);
|
|
|
|
self.assert_valid_flag(region);
|
|
|
|
seen_regions.push(region);
|
|
|
|
keep &= self.has_flag(region);
|
|
|
|
}
|
2021-06-15 13:02:40 -05:00
|
|
|
|
2021-06-16 02:30:29 -05:00
|
|
|
if keep {
|
2021-06-15 13:02:40 -05:00
|
|
|
buf.push_str(line)
|
|
|
|
}
|
2021-06-16 02:30:29 -05:00
|
|
|
if line_region {
|
|
|
|
active_regions.pop().unwrap();
|
|
|
|
}
|
2021-06-15 13:02:40 -05:00
|
|
|
}
|
2021-06-15 14:38:21 -05:00
|
|
|
|
|
|
|
for flag in &self.valid_flags {
|
|
|
|
if !seen_regions.iter().any(|it| it == flag) {
|
|
|
|
panic!("unused minicore flag: {:?}", flag);
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 02:30:29 -05:00
|
|
|
format!("{}", buf);
|
2021-06-15 13:02:40 -05:00
|
|
|
buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 10:59:56 -05:00
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn parse_fixture_checks_further_indented_metadata() {
|
2020-06-23 11:56:26 -05:00
|
|
|
Fixture::parse(
|
2020-06-23 10:59:56 -05:00
|
|
|
r"
|
|
|
|
//- /lib.rs
|
|
|
|
mod bar;
|
|
|
|
|
|
|
|
fn foo() {}
|
|
|
|
//- /bar.rs
|
|
|
|
pub fn baz() {}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fixture_gets_full_meta() {
|
2021-06-15 13:02:40 -05:00
|
|
|
let (mini_core, parsed) = Fixture::parse(
|
|
|
|
r#"
|
|
|
|
//- minicore: coerce_unsized
|
|
|
|
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
|
|
|
|
mod m;
|
|
|
|
"#,
|
2020-06-23 10:59:56 -05:00
|
|
|
);
|
2021-06-15 13:02:40 -05:00
|
|
|
assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]);
|
2020-06-23 10:59:56 -05:00
|
|
|
assert_eq!(1, parsed.len());
|
|
|
|
|
2020-06-23 11:34:50 -05:00
|
|
|
let meta = &parsed[0];
|
2020-06-23 15:27:24 -05:00
|
|
|
assert_eq!("mod m;\n", meta.text);
|
2020-06-23 10:59:56 -05:00
|
|
|
|
2020-06-24 03:22:02 -05:00
|
|
|
assert_eq!("foo", meta.krate.as_ref().unwrap());
|
2020-06-23 11:34:50 -05:00
|
|
|
assert_eq!("/lib.rs", meta.path);
|
|
|
|
assert_eq!(2, meta.env.len());
|
2020-06-23 10:59:56 -05:00
|
|
|
}
|