Add toolchain meta for tests

This commit is contained in:
Ryo Yoshida 2023-04-11 00:15:39 +09:00
parent 5d41affc77
commit 584d2697cc
No known key found for this signature in database
GPG Key ID: E25698A930586171
6 changed files with 66 additions and 44 deletions

View File

@ -4,7 +4,8 @@
use cfg::CfgOptions;
use rustc_hash::FxHashMap;
use test_utils::{
extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
extract_range_or_offset, Fixture, FixtureWithProjectMeta, RangeOrOffset, CURSOR_MARKER,
ESCAPED_CURSOR_MARKER,
};
use tt::token_id::{Leaf, Subtree, TokenTree};
use vfs::{file_set::FileSet, VfsPath};
@ -12,7 +13,7 @@
use crate::{
input::{CrateName, CrateOrigin, LangCrateOrigin},
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacros,
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacros, ReleaseChannel,
SourceDatabaseExt, SourceRoot, SourceRootId,
};
@ -102,7 +103,14 @@ pub fn parse_with_proc_macros(
ra_fixture: &str,
mut proc_macro_defs: Vec<(String, ProcMacro)>,
) -> ChangeFixture {
let (mini_core, proc_macro_names, fixture) = Fixture::parse(ra_fixture);
let FixtureWithProjectMeta { fixture, mini_core, proc_macro_names, toolchain } =
FixtureWithProjectMeta::parse(ra_fixture);
let toolchain = toolchain
.map(|it| {
ReleaseChannel::from_str(&it)
.unwrap_or_else(|| panic!("unknown release channel found: {it}"))
})
.unwrap_or(ReleaseChannel::Stable);
let mut change = Change::new();
let mut files = Vec::new();
@ -166,7 +174,7 @@ pub fn parse_with_proc_macros(
.as_deref()
.map(Arc::from)
.ok_or_else(|| "target_data_layout unset".into()),
None,
Some(toolchain),
);
let prev = crates.insert(crate_name.clone(), crate_id);
assert!(prev.is_none());
@ -205,7 +213,7 @@ pub fn parse_with_proc_macros(
default_target_data_layout
.map(|x| x.into())
.ok_or_else(|| "target_data_layout unset".into()),
None,
Some(toolchain),
);
} else {
for (from, to, prelude) in crate_deps {
@ -247,7 +255,7 @@ pub fn parse_with_proc_macros(
false,
CrateOrigin::Lang(LangCrateOrigin::Core),
target_layout.clone(),
None,
Some(toolchain),
);
for krate in all_crates {
@ -286,7 +294,7 @@ pub fn parse_with_proc_macros(
true,
CrateOrigin::Local { repo: None, name: None },
target_layout,
None,
Some(toolchain),
);
proc_macros.insert(proc_macros_crate, Ok(proc_macro));

View File

@ -116,7 +116,7 @@ fn external_docs_doc_url_std_crate() {
//- /main.rs crate:std
use self$0;
"#,
expect![[r#"https://doc.rust-lang.org/nightly/std/index.html"#]],
expect!["https://doc.rust-lang.org/stable/std/index.html"],
);
}

View File

@ -4242,7 +4242,7 @@ fn foo() {
/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
mod move_keyword {}
"#,
expect![[r##"
expect![[r#"
*move*
```rust
@ -4251,11 +4251,11 @@ mod move_keyword {}
---
[closure](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
[closures](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
[threads](https://doc.rust-lang.org/nightly/book/ch16-01-threads.html#using-move-closures-with-threads)
[closure](https://doc.rust-lang.org/stable/book/ch13-01-closures.html)
[closures](https://doc.rust-lang.org/stable/book/ch13-01-closures.html)
[threads](https://doc.rust-lang.org/stable/book/ch16-01-threads.html#using-move-closures-with-threads)
<https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
"##]],
"#]],
);
}

View File

@ -13,7 +13,7 @@
use rust_analyzer::{config::Config, lsp_ext, main_loop};
use serde::Serialize;
use serde_json::{json, to_string_pretty, Value};
use test_utils::Fixture;
use test_utils::FixtureWithProjectMeta;
use vfs::AbsPathBuf;
use crate::testdir::TestDir;
@ -84,10 +84,12 @@ pub(crate) fn server(self) -> Server {
profile::init_from(crate::PROFILE);
});
let (mini_core, proc_macros, fixtures) = Fixture::parse(self.fixture);
assert!(proc_macros.is_empty());
let FixtureWithProjectMeta { fixture, mini_core, proc_macro_names, toolchain } =
FixtureWithProjectMeta::parse(self.fixture);
assert!(proc_macro_names.is_empty());
assert!(mini_core.is_none());
for entry in fixtures {
assert!(toolchain.is_none());
for entry in fixture {
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();

View File

@ -86,7 +86,14 @@ pub struct MiniCore {
valid_flags: Vec<String>,
}
impl Fixture {
pub struct FixtureWithProjectMeta {
pub fixture: Vec<Fixture>,
pub mini_core: Option<MiniCore>,
pub proc_macro_names: Vec<String>,
pub toolchain: Option<String>,
}
impl FixtureWithProjectMeta {
/// Parses text which looks like this:
///
/// ```not_rust
@ -96,37 +103,40 @@ impl Fixture {
/// //- other meta
/// ```
///
/// Fixture can also start with a proc_macros and minicore declaration(in that order):
/// Fixture can also start with a proc_macros and minicore declaration (in that order):
///
/// ```
/// //- toolchain: nightly
/// //- proc_macros: identity
/// //- minicore: sized
/// ```
///
/// That will include predefined proc macros and a subset of `libcore` into the fixture, see
/// `minicore.rs` for what's available.
pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<String>, Vec<Fixture>) {
pub fn parse(ra_fixture: &str) -> Self {
let fixture = trim_indent(ra_fixture);
let mut fixture = fixture.as_str();
let mut toolchain = None;
let mut mini_core = None;
let mut res: Vec<Fixture> = Vec::new();
let mut test_proc_macros = vec![];
let mut proc_macro_names = vec![];
if fixture.starts_with("//- proc_macros:") {
let first_line = fixture.split_inclusive('\n').next().unwrap();
test_proc_macros = first_line
.strip_prefix("//- proc_macros:")
.unwrap()
.split(',')
.map(|it| it.trim().to_string())
.collect();
fixture = &fixture[first_line.len()..];
if let Some(meta) = fixture.strip_prefix("//- toolchain:") {
let (meta, remain) = meta.split_once('\n').unwrap();
toolchain = Some(meta.trim().to_string());
fixture = remain;
}
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()..];
if let Some(meta) = fixture.strip_prefix("//- proc_macros:") {
let (meta, remain) = meta.split_once('\n').unwrap();
proc_macro_names = meta.split(',').map(|it| it.trim().to_string()).collect();
fixture = remain;
}
if let Some(meta) = fixture.strip_prefix("//- minicore:") {
let (meta, remain) = meta.split_once('\n').unwrap();
mini_core = Some(MiniCore::parse(meta));
fixture = remain;
}
let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") };
@ -142,7 +152,7 @@ pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<String>, Vec<Fixture>)
}
if line.starts_with("//-") {
let meta = Fixture::parse_meta_line(line);
let meta = Self::parse_meta_line(line);
res.push(meta);
} else {
if line.starts_with("// ")
@ -160,7 +170,7 @@ pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<String>, Vec<Fixture>)
}
}
(mini_core, test_proc_macros, res)
Self { fixture: res, mini_core, proc_macro_names, toolchain }
}
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
@ -257,8 +267,7 @@ fn assert_valid_flag(&self, flag: &str) {
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(", ") {
for entry in line.trim().split(", ") {
if res.has_flag(entry) {
panic!("duplicate minicore flag: {entry:?}");
}
@ -372,7 +381,7 @@ pub fn source_code(mut self) -> String {
#[test]
#[should_panic]
fn parse_fixture_checks_further_indented_metadata() {
Fixture::parse(
FixtureWithProjectMeta::parse(
r"
//- /lib.rs
mod bar;
@ -386,15 +395,18 @@ pub fn baz() {}
#[test]
fn parse_fixture_gets_full_meta() {
let (mini_core, proc_macros, parsed) = Fixture::parse(
r#"
let FixtureWithProjectMeta { fixture: parsed, mini_core, proc_macro_names, toolchain } =
FixtureWithProjectMeta::parse(
r#"
//- toolchain: nightly
//- proc_macros: identity
//- minicore: coerce_unsized
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
mod m;
"#,
);
assert_eq!(proc_macros, vec!["identity".to_string()]);
);
assert_eq!(toolchain, Some("nightly".to_string()));
assert_eq!(proc_macro_names, vec!["identity".to_string()]);
assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]);
assert_eq!(1, parsed.len());

View File

@ -27,7 +27,7 @@
pub use crate::{
assert_linear::AssertLinear,
fixture::{Fixture, MiniCore},
fixture::{Fixture, FixtureWithProjectMeta, MiniCore},
};
pub const CURSOR_MARKER: &str = "$0";