1 //! Defines `Fixture` -- a convenient way to describe the initial state of
2 //! rust-analyzer database from a single string.
4 //! Fixtures are strings containing rust source code with optional metadata.
5 //! A fixture without metadata is parsed into a single source file.
6 //! Use this to test functionality local to one file.
12 //! println!("Hello World")
17 //! Metadata can be added to a fixture after a `//-` comment.
18 //! The basic form is specifying filenames,
19 //! which is also how to define multiple files in a single test fixture
21 //! Example using two files in the same crate:
35 //! Example using two crates with one file each, with one crate depending on the other:
38 //! //- /main.rs crate:a deps:b
42 //! //- /lib.rs crate:b
44 //! println!("Hello World")
49 //! Metadata allows specifying all settings and variables
50 //! that are available in a real rust project:
51 //! - crate names via `crate:cratename`
52 //! - dependencies via `deps:dep1,dep2`
53 //! - configuration settings via `cfg:dbg=false,opt_level=2`
54 //! - environment variables via `env:PATH=/bin,RUST_LOG=debug`
56 //! Example using all available metadata:
59 //! //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
60 //! fn insert_source_code_here() {}
64 use rustc_hash::FxHashMap;
65 use stdx::trim_indent;
67 #[derive(Debug, Eq, PartialEq)]
71 pub krate: Option<String>,
72 pub deps: Vec<String>,
73 pub cfg_atoms: Vec<String>,
74 pub cfg_key_values: Vec<(String, String)>,
75 pub edition: Option<String>,
76 pub env: FxHashMap<String, String>,
77 pub introduce_new_source_root: bool,
81 /// Parses text which looks like this:
89 pub fn parse(ra_fixture: &str) -> Vec<Fixture> {
90 let fixture = trim_indent(ra_fixture);
92 let mut res: Vec<Fixture> = Vec::new();
94 let default = if ra_fixture.contains("//-") { None } else { Some("//- /main.rs") };
96 for (ix, line) in default.into_iter().chain(fixture.split_inclusive('\n')).enumerate() {
97 if line.contains("//-") {
99 line.starts_with("//-"),
100 "Metadata line {} has invalid indentation. \
101 All metadata lines need to have the same indentation.\n\
102 The offending line: {:?}",
108 if line.starts_with("//-") {
109 let meta = Fixture::parse_meta_line(line);
111 } else if let Some(entry) = res.last_mut() {
112 entry.text.push_str(line);
119 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
120 fn parse_meta_line(meta: &str) -> Fixture {
121 assert!(meta.starts_with("//-"));
122 let meta = meta["//-".len()..].trim();
123 let components = meta.split_ascii_whitespace().collect::<Vec<_>>();
125 let path = components[0].to_string();
126 assert!(path.starts_with('/'));
128 let mut krate = None;
129 let mut deps = Vec::new();
130 let mut edition = None;
131 let mut cfg_atoms = Vec::new();
132 let mut cfg_key_values = Vec::new();
133 let mut env = FxHashMap::default();
134 let mut introduce_new_source_root = false;
135 for component in components[1..].iter() {
136 let (key, value) = component.split_once(':').unwrap();
138 "crate" => krate = Some(value.to_string()),
139 "deps" => deps = value.split(',').map(|it| it.to_string()).collect(),
140 "edition" => edition = Some(value.to_string()),
142 for entry in value.split(',') {
143 match entry.split_once('=') {
144 Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())),
145 None => cfg_atoms.push(entry.to_string()),
150 for key in value.split(',') {
151 if let Some((k, v)) = key.split_once('=') {
152 env.insert(k.into(), v.into());
156 "new_source_root" => introduce_new_source_root = true,
157 _ => panic!("bad component: {:?}", component),
170 introduce_new_source_root,
177 fn parse_fixture_checks_further_indented_metadata() {
191 fn parse_fixture_gets_full_meta() {
192 let parsed = Fixture::parse(
194 //- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
198 assert_eq!(1, parsed.len());
200 let meta = &parsed[0];
201 assert_eq!("mod m;\n", meta.text);
203 assert_eq!("foo", meta.krate.as_ref().unwrap());
204 assert_eq!("/lib.rs", meta.path);
205 assert_eq!(2, meta.env.len());