Skip to content

Commit 584d269

Browse files
committed
Add toolchain meta for tests
1 parent 5d41aff commit 584d269

File tree

6 files changed

+67
-45
lines changed

6 files changed

+67
-45
lines changed

crates/base-db/src/fixture.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ use std::{mem, str::FromStr, sync::Arc};
44
use cfg::CfgOptions;
55
use rustc_hash::FxHashMap;
66
use test_utils::{
7-
extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER, ESCAPED_CURSOR_MARKER,
7+
extract_range_or_offset, Fixture, FixtureWithProjectMeta, RangeOrOffset, CURSOR_MARKER,
8+
ESCAPED_CURSOR_MARKER,
89
};
910
use tt::token_id::{Leaf, Subtree, TokenTree};
1011
use vfs::{file_set::FileSet, VfsPath};
1112

1213
use crate::{
1314
input::{CrateName, CrateOrigin, LangCrateOrigin},
1415
Change, CrateDisplayName, CrateGraph, CrateId, Dependency, Edition, Env, FileId, FilePosition,
15-
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacros,
16+
FileRange, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacros, ReleaseChannel,
1617
SourceDatabaseExt, SourceRoot, SourceRootId,
1718
};
1819

@@ -102,7 +103,14 @@ impl ChangeFixture {
102103
ra_fixture: &str,
103104
mut proc_macro_defs: Vec<(String, ProcMacro)>,
104105
) -> ChangeFixture {
105-
let (mini_core, proc_macro_names, fixture) = Fixture::parse(ra_fixture);
106+
let FixtureWithProjectMeta { fixture, mini_core, proc_macro_names, toolchain } =
107+
FixtureWithProjectMeta::parse(ra_fixture);
108+
let toolchain = toolchain
109+
.map(|it| {
110+
ReleaseChannel::from_str(&it)
111+
.unwrap_or_else(|| panic!("unknown release channel found: {it}"))
112+
})
113+
.unwrap_or(ReleaseChannel::Stable);
106114
let mut change = Change::new();
107115

108116
let mut files = Vec::new();
@@ -166,7 +174,7 @@ impl ChangeFixture {
166174
.as_deref()
167175
.map(Arc::from)
168176
.ok_or_else(|| "target_data_layout unset".into()),
169-
None,
177+
Some(toolchain),
170178
);
171179
let prev = crates.insert(crate_name.clone(), crate_id);
172180
assert!(prev.is_none());
@@ -205,7 +213,7 @@ impl ChangeFixture {
205213
default_target_data_layout
206214
.map(|x| x.into())
207215
.ok_or_else(|| "target_data_layout unset".into()),
208-
None,
216+
Some(toolchain),
209217
);
210218
} else {
211219
for (from, to, prelude) in crate_deps {
@@ -247,7 +255,7 @@ impl ChangeFixture {
247255
false,
248256
CrateOrigin::Lang(LangCrateOrigin::Core),
249257
target_layout.clone(),
250-
None,
258+
Some(toolchain),
251259
);
252260

253261
for krate in all_crates {
@@ -286,7 +294,7 @@ impl ChangeFixture {
286294
true,
287295
CrateOrigin::Local { repo: None, name: None },
288296
target_layout,
289-
None,
297+
Some(toolchain),
290298
);
291299
proc_macros.insert(proc_macros_crate, Ok(proc_macro));
292300

crates/ide/src/doc_links/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ fn external_docs_doc_url_std_crate() {
116116
//- /main.rs crate:std
117117
use self$0;
118118
"#,
119-
expect![[r#"https://doc.rust-lang.org/nightly/std/index.html"#]],
119+
expect!["https://doc.rust-lang.org/stable/std/index.html"],
120120
);
121121
}
122122

crates/ide/src/hover/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4242,7 +4242,7 @@ fn foo() {
42424242
/// [threads]: ../book/ch16-01-threads.html#using-move-closures-with-threads
42434243
mod move_keyword {}
42444244
"#,
4245-
expect![[r##"
4245+
expect![[r#"
42464246
*move*
42474247
42484248
```rust
@@ -4251,11 +4251,11 @@ mod move_keyword {}
42514251
42524252
---
42534253
4254-
[closure](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
4255-
[closures](https://doc.rust-lang.org/nightly/book/ch13-01-closures.html)
4256-
[threads](https://doc.rust-lang.org/nightly/book/ch16-01-threads.html#using-move-closures-with-threads)
4254+
[closure](https://doc.rust-lang.org/stable/book/ch13-01-closures.html)
4255+
[closures](https://doc.rust-lang.org/stable/book/ch13-01-closures.html)
4256+
[threads](https://doc.rust-lang.org/stable/book/ch16-01-threads.html#using-move-closures-with-threads)
42574257
<https://doc.rust-lang.org/nightly/book/ch13-01-closures.html>
4258-
"##]],
4258+
"#]],
42594259
);
42604260
}
42614261

crates/rust-analyzer/tests/slow-tests/support.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use project_model::ProjectManifest;
1313
use rust_analyzer::{config::Config, lsp_ext, main_loop};
1414
use serde::Serialize;
1515
use serde_json::{json, to_string_pretty, Value};
16-
use test_utils::Fixture;
16+
use test_utils::FixtureWithProjectMeta;
1717
use vfs::AbsPathBuf;
1818

1919
use crate::testdir::TestDir;
@@ -84,10 +84,12 @@ impl<'a> Project<'a> {
8484
profile::init_from(crate::PROFILE);
8585
});
8686

87-
let (mini_core, proc_macros, fixtures) = Fixture::parse(self.fixture);
88-
assert!(proc_macros.is_empty());
87+
let FixtureWithProjectMeta { fixture, mini_core, proc_macro_names, toolchain } =
88+
FixtureWithProjectMeta::parse(self.fixture);
89+
assert!(proc_macro_names.is_empty());
8990
assert!(mini_core.is_none());
90-
for entry in fixtures {
91+
assert!(toolchain.is_none());
92+
for entry in fixture {
9193
let path = tmp_dir.path().join(&entry.path['/'.len_utf8()..]);
9294
fs::create_dir_all(path.parent().unwrap()).unwrap();
9395
fs::write(path.as_path(), entry.text.as_bytes()).unwrap();

crates/test-utils/src/fixture.rs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ pub struct MiniCore {
8686
valid_flags: Vec<String>,
8787
}
8888

89-
impl Fixture {
89+
pub struct FixtureWithProjectMeta {
90+
pub fixture: Vec<Fixture>,
91+
pub mini_core: Option<MiniCore>,
92+
pub proc_macro_names: Vec<String>,
93+
pub toolchain: Option<String>,
94+
}
95+
96+
impl FixtureWithProjectMeta {
9097
/// Parses text which looks like this:
9198
///
9299
/// ```not_rust
@@ -96,37 +103,40 @@ impl Fixture {
96103
/// //- other meta
97104
/// ```
98105
///
99-
/// Fixture can also start with a proc_macros and minicore declaration(in that order):
106+
/// Fixture can also start with a proc_macros and minicore declaration (in that order):
100107
///
101108
/// ```
109+
/// //- toolchain: nightly
102110
/// //- proc_macros: identity
103111
/// //- minicore: sized
104112
/// ```
105113
///
106114
/// That will include predefined proc macros and a subset of `libcore` into the fixture, see
107115
/// `minicore.rs` for what's available.
108-
pub fn parse(ra_fixture: &str) -> (Option<MiniCore>, Vec<String>, Vec<Fixture>) {
116+
pub fn parse(ra_fixture: &str) -> Self {
109117
let fixture = trim_indent(ra_fixture);
110118
let mut fixture = fixture.as_str();
119+
let mut toolchain = None;
111120
let mut mini_core = None;
112121
let mut res: Vec<Fixture> = Vec::new();
113-
let mut test_proc_macros = vec![];
114-
115-
if fixture.starts_with("//- proc_macros:") {
116-
let first_line = fixture.split_inclusive('\n').next().unwrap();
117-
test_proc_macros = first_line
118-
.strip_prefix("//- proc_macros:")
119-
.unwrap()
120-
.split(',')
121-
.map(|it| it.trim().to_string())
122-
.collect();
123-
fixture = &fixture[first_line.len()..];
122+
let mut proc_macro_names = vec![];
123+
124+
if let Some(meta) = fixture.strip_prefix("//- toolchain:") {
125+
let (meta, remain) = meta.split_once('\n').unwrap();
126+
toolchain = Some(meta.trim().to_string());
127+
fixture = remain;
124128
}
125129

126-
if fixture.starts_with("//- minicore:") {
127-
let first_line = fixture.split_inclusive('\n').next().unwrap();
128-
mini_core = Some(MiniCore::parse(first_line));
129-
fixture = &fixture[first_line.len()..];
130+
if let Some(meta) = fixture.strip_prefix("//- proc_macros:") {
131+
let (meta, remain) = meta.split_once('\n').unwrap();
132+
proc_macro_names = meta.split(',').map(|it| it.trim().to_string()).collect();
133+
fixture = remain;
134+
}
135+
136+
if let Some(meta) = fixture.strip_prefix("//- minicore:") {
137+
let (meta, remain) = meta.split_once('\n').unwrap();
138+
mini_core = Some(MiniCore::parse(meta));
139+
fixture = remain;
130140
}
131141

132142
let default = if fixture.contains("//-") { None } else { Some("//- /main.rs") };
@@ -142,7 +152,7 @@ impl Fixture {
142152
}
143153

144154
if line.starts_with("//-") {
145-
let meta = Fixture::parse_meta_line(line);
155+
let meta = Self::parse_meta_line(line);
146156
res.push(meta);
147157
} else {
148158
if line.starts_with("// ")
@@ -160,7 +170,7 @@ impl Fixture {
160170
}
161171
}
162172

163-
(mini_core, test_proc_macros, res)
173+
Self { fixture: res, mini_core, proc_macro_names, toolchain }
164174
}
165175

166176
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b env:OUTDIR=path/to,OTHER=foo
@@ -257,8 +267,7 @@ impl MiniCore {
257267
fn parse(line: &str) -> MiniCore {
258268
let mut res = MiniCore { activated_flags: Vec::new(), valid_flags: Vec::new() };
259269

260-
let line = line.strip_prefix("//- minicore:").unwrap().trim();
261-
for entry in line.split(", ") {
270+
for entry in line.trim().split(", ") {
262271
if res.has_flag(entry) {
263272
panic!("duplicate minicore flag: {entry:?}");
264273
}
@@ -372,7 +381,7 @@ impl MiniCore {
372381
#[test]
373382
#[should_panic]
374383
fn parse_fixture_checks_further_indented_metadata() {
375-
Fixture::parse(
384+
FixtureWithProjectMeta::parse(
376385
r"
377386
//- /lib.rs
378387
mod bar;
@@ -386,15 +395,18 @@ fn parse_fixture_checks_further_indented_metadata() {
386395

387396
#[test]
388397
fn parse_fixture_gets_full_meta() {
389-
let (mini_core, proc_macros, parsed) = Fixture::parse(
390-
r#"
398+
let FixtureWithProjectMeta { fixture: parsed, mini_core, proc_macro_names, toolchain } =
399+
FixtureWithProjectMeta::parse(
400+
r#"
401+
//- toolchain: nightly
391402
//- proc_macros: identity
392403
//- minicore: coerce_unsized
393404
//- /lib.rs crate:foo deps:bar,baz cfg:foo=a,bar=b,atom env:OUTDIR=path/to,OTHER=foo
394405
mod m;
395406
"#,
396-
);
397-
assert_eq!(proc_macros, vec!["identity".to_string()]);
407+
);
408+
assert_eq!(toolchain, Some("nightly".to_string()));
409+
assert_eq!(proc_macro_names, vec!["identity".to_string()]);
398410
assert_eq!(mini_core.unwrap().activated_flags, vec!["coerce_unsized".to_string()]);
399411
assert_eq!(1, parsed.len());
400412

crates/test-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use rustc_hash::FxHashMap;
2727

2828
pub use crate::{
2929
assert_linear::AssertLinear,
30-
fixture::{Fixture, MiniCore},
30+
fixture::{Fixture, FixtureWithProjectMeta, MiniCore},
3131
};
3232

3333
pub const CURSOR_MARKER: &str = "$0";

0 commit comments

Comments
 (0)