Skip to content

Commit e96601b

Browse files
committed
Move parser test generation to xtask
1 parent 004a24e commit e96601b

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

src/tools/rust-analyzer/crates/parser/src/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
mod prefix_entries;
2-
mod sourcegen_inline_tests;
32
mod top_entries;
43

54
use std::{

src/tools/rust-analyzer/xtask/src/codegen.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ pub(crate) mod assists_doc_tests;
1414
pub(crate) mod diagnostics_docs;
1515
mod grammar;
1616
mod lints;
17+
mod parser_inline_tests;
1718

1819
impl flags::Codegen {
1920
pub(crate) fn run(self, _sh: &Shell) -> anyhow::Result<()> {
2021
match self.codegen_type.unwrap_or_default() {
2122
flags::CodegenType::All => {
2223
diagnostics_docs::generate(self.check);
2324
assists_doc_tests::generate(self.check);
25+
parser_inline_tests::generate(self.check);
2426
// diagnostics_docs::generate(self.check) doesn't generate any tests
2527
// lints::generate(self.check) Updating clones the rust repo, so don't run it unless
2628
// explicitly asked for
@@ -29,6 +31,7 @@ impl flags::Codegen {
2931
flags::CodegenType::AssistsDocTests => assists_doc_tests::generate(self.check),
3032
flags::CodegenType::DiagnosticsDocs => diagnostics_docs::generate(self.check),
3133
flags::CodegenType::LintDefinitions => lints::generate(self.check),
34+
flags::CodegenType::ParserTests => parser_inline_tests::generate(self.check),
3235
}
3336
Ok(())
3437
}
@@ -187,7 +190,7 @@ fn add_preamble(cg: CodegenType, mut text: String) -> String {
187190
/// Checks that the `file` has the specified `contents`. If that is not the
188191
/// case, updates the file and then fails the test.
189192
#[allow(clippy::print_stderr)]
190-
fn ensure_file_contents(file: &Path, contents: &str, check: bool) {
193+
fn ensure_file_contents(cg: CodegenType, file: &Path, contents: &str, check: bool) {
191194
if let Ok(old_contents) = fs::read_to_string(file) {
192195
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
193196
// File is already up to date.
@@ -201,9 +204,11 @@ fn ensure_file_contents(file: &Path, contents: &str, check: bool) {
201204
"{} was not up-to-date{}",
202205
file.display(),
203206
if std::env::var("CI").is_ok() {
204-
"\n NOTE: run `cargo codegen` locally and commit the updated files\n"
207+
format!(
208+
"\n NOTE: run `cargo codegen {cg}` locally and commit the updated files\n"
209+
)
205210
} else {
206-
""
211+
"".to_owned()
207212
}
208213
);
209214
} else {

src/tools/rust-analyzer/xtask/src/codegen/assists_doc_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ r#####"
4747
}
4848
let buf = add_preamble(crate::flags::CodegenType::AssistsDocTests, reformat(buf));
4949
ensure_file_contents(
50+
crate::flags::CodegenType::AssistsDocTests,
5051
&project_root().join("crates/ide-assists/src/tests/generated.rs"),
5152
&buf,
5253
check,

src/tools/rust-analyzer/xtask/src/codegen/grammar.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ use self::ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc
2727
pub(crate) fn generate(check: bool) {
2828
let syntax_kinds = generate_syntax_kinds(KINDS_SRC);
2929
let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs");
30-
ensure_file_contents(syntax_kinds_file.as_path(), &syntax_kinds, check);
30+
ensure_file_contents(
31+
crate::flags::CodegenType::Grammar,
32+
syntax_kinds_file.as_path(),
33+
&syntax_kinds,
34+
check,
35+
);
3136

3237
let grammar = fs::read_to_string(project_root().join("crates/syntax/rust.ungram"))
3338
.unwrap()
@@ -37,11 +42,21 @@ pub(crate) fn generate(check: bool) {
3742

3843
let ast_tokens = generate_tokens(&ast);
3944
let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs");
40-
ensure_file_contents(ast_tokens_file.as_path(), &ast_tokens, check);
45+
ensure_file_contents(
46+
crate::flags::CodegenType::Grammar,
47+
ast_tokens_file.as_path(),
48+
&ast_tokens,
49+
check,
50+
);
4151

4252
let ast_nodes = generate_nodes(KINDS_SRC, &ast);
4353
let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs");
44-
ensure_file_contents(ast_nodes_file.as_path(), &ast_nodes, check);
54+
ensure_file_contents(
55+
crate::flags::CodegenType::Grammar,
56+
ast_nodes_file.as_path(),
57+
&ast_nodes,
58+
check,
59+
);
4560
}
4661

4762
fn generate_tokens(grammar: &AstSrc) -> String {

src/tools/rust-analyzer/xtask/src/codegen/lints.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,12 @@ pub struct LintGroup {
7676
let contents = add_preamble(crate::flags::CodegenType::LintDefinitions, reformat(contents));
7777

7878
let destination = project_root().join(DESTINATION);
79-
ensure_file_contents(destination.as_path(), &contents, check);
79+
ensure_file_contents(
80+
crate::flags::CodegenType::LintDefinitions,
81+
destination.as_path(),
82+
&contents,
83+
check,
84+
);
8085
}
8186

8287
/// Parses the output of `rustdoc -Whelp` and prints `Lint` and `LintGroup` constants into `buf`.

src/tools/rust-analyzer/crates/parser/src/tests/sourcegen_inline_tests.rs renamed to src/tools/rust-analyzer/xtask/src/codegen/parser_inline_tests.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ use std::{
88
path::{Path, PathBuf},
99
};
1010

11-
#[test]
12-
fn sourcegen_parser_tests() {
13-
let grammar_dir = sourcegen::project_root().join(Path::new("crates/parser/src/grammar"));
11+
use crate::{
12+
codegen::{ensure_file_contents, list_rust_files, CommentBlock},
13+
project_root,
14+
};
15+
16+
pub(crate) fn generate(check: bool) {
17+
let grammar_dir = project_root().join(Path::new("crates/parser/src/grammar"));
1418
let tests = tests_from_dir(&grammar_dir);
1519

16-
install_tests(&tests.ok, "crates/parser/test_data/parser/inline/ok");
17-
install_tests(&tests.err, "crates/parser/test_data/parser/inline/err");
20+
install_tests(&tests.ok, "crates/parser/test_data/parser/inline/ok", check);
21+
install_tests(&tests.err, "crates/parser/test_data/parser/inline/err", check);
1822

19-
fn install_tests(tests: &HashMap<String, Test>, into: &str) {
20-
let tests_dir = sourcegen::project_root().join(into);
23+
fn install_tests(tests: &HashMap<String, Test>, into: &str, check: bool) {
24+
let tests_dir = project_root().join(into);
2125
if !tests_dir.is_dir() {
2226
fs::create_dir_all(&tests_dir).unwrap();
2327
}
@@ -37,7 +41,7 @@ fn sourcegen_parser_tests() {
3741
tests_dir.join(file_name)
3842
}
3943
};
40-
sourcegen::ensure_file_contents(&path, &test.text);
44+
ensure_file_contents(crate::flags::CodegenType::ParserTests, &path, &test.text, check);
4145
}
4246
}
4347
}
@@ -57,7 +61,7 @@ struct Tests {
5761

5862
fn collect_tests(s: &str) -> Vec<Test> {
5963
let mut res = Vec::new();
60-
for comment_block in sourcegen::CommentBlock::extract_untagged(s) {
64+
for comment_block in CommentBlock::extract_untagged(s) {
6165
let first_line = &comment_block.contents[0];
6266
let (name, ok) = if let Some(name) = first_line.strip_prefix("test ") {
6367
(name.to_owned(), true)
@@ -80,7 +84,7 @@ fn collect_tests(s: &str) -> Vec<Test> {
8084

8185
fn tests_from_dir(dir: &Path) -> Tests {
8286
let mut res = Tests::default();
83-
for entry in sourcegen::list_rust_files(dir) {
87+
for entry in list_rust_files(dir) {
8488
process_file(&mut res, entry.as_path());
8589
}
8690
let grammar_rs = dir.parent().unwrap().join("grammar.rs");

src/tools/rust-analyzer/xtask/src/flags.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ pub enum CodegenType {
185185
AssistsDocTests,
186186
DiagnosticsDocs,
187187
LintDefinitions,
188+
ParserTests,
188189
}
189190

190191
impl fmt::Display for CodegenType {
@@ -195,6 +196,7 @@ impl fmt::Display for CodegenType {
195196
Self::AssistsDocTests => write!(f, "assists-doc-tests"),
196197
Self::DiagnosticsDocs => write!(f, "diagnostics-docs"),
197198
Self::LintDefinitions => write!(f, "lint-definitions"),
199+
Self::ParserTests => write!(f, "parser-tests"),
198200
}
199201
}
200202
}
@@ -208,6 +210,7 @@ impl FromStr for CodegenType {
208210
"assists-doc-tests" => Ok(Self::AssistsDocTests),
209211
"diagnostics-docs" => Ok(Self::DiagnosticsDocs),
210212
"lint-definitions" => Ok(Self::LintDefinitions),
213+
"parser-tests" => Ok(Self::ParserTests),
211214
_ => Err("Invalid option".to_owned()),
212215
}
213216
}

0 commit comments

Comments
 (0)