Move parser test generation to xtask

This commit is contained in:
Lukas Wirth 2024-07-07 08:51:19 +02:00
parent b9c1c42959
commit 5802643900
7 changed files with 50 additions and 18 deletions

View file

@ -1,5 +1,4 @@
mod prefix_entries;
mod sourcegen_inline_tests;
mod top_entries;
use std::{

View file

@ -14,6 +14,7 @@ pub(crate) mod assists_doc_tests;
pub(crate) mod diagnostics_docs;
mod grammar;
mod lints;
mod parser_inline_tests;
impl flags::Codegen {
pub(crate) fn run(self, _sh: &Shell) -> anyhow::Result<()> {
@ -21,6 +22,7 @@ impl flags::Codegen {
flags::CodegenType::All => {
diagnostics_docs::generate(self.check);
assists_doc_tests::generate(self.check);
parser_inline_tests::generate(self.check);
// diagnostics_docs::generate(self.check) doesn't generate any tests
// lints::generate(self.check) Updating clones the rust repo, so don't run it unless
// explicitly asked for
@ -29,6 +31,7 @@ impl flags::Codegen {
flags::CodegenType::AssistsDocTests => assists_doc_tests::generate(self.check),
flags::CodegenType::DiagnosticsDocs => diagnostics_docs::generate(self.check),
flags::CodegenType::LintDefinitions => lints::generate(self.check),
flags::CodegenType::ParserTests => parser_inline_tests::generate(self.check),
}
Ok(())
}
@ -187,7 +190,7 @@ fn add_preamble(cg: CodegenType, mut text: String) -> String {
/// Checks that the `file` has the specified `contents`. If that is not the
/// case, updates the file and then fails the test.
#[allow(clippy::print_stderr)]
fn ensure_file_contents(file: &Path, contents: &str, check: bool) {
fn ensure_file_contents(cg: CodegenType, file: &Path, contents: &str, check: bool) {
if let Ok(old_contents) = fs::read_to_string(file) {
if normalize_newlines(&old_contents) == normalize_newlines(contents) {
// File is already up to date.
@ -201,9 +204,11 @@ fn ensure_file_contents(file: &Path, contents: &str, check: bool) {
"{} was not up-to-date{}",
file.display(),
if std::env::var("CI").is_ok() {
"\n NOTE: run `cargo codegen` locally and commit the updated files\n"
format!(
"\n NOTE: run `cargo codegen {cg}` locally and commit the updated files\n"
)
} else {
""
"".to_owned()
}
);
} else {

View file

@ -47,6 +47,7 @@ r#####"
}
let buf = add_preamble(crate::flags::CodegenType::AssistsDocTests, reformat(buf));
ensure_file_contents(
crate::flags::CodegenType::AssistsDocTests,
&project_root().join("crates/ide-assists/src/tests/generated.rs"),
&buf,
check,

View file

@ -27,7 +27,12 @@ use self::ast_src::{AstEnumSrc, AstNodeSrc, AstSrc, Cardinality, Field, KindsSrc
pub(crate) fn generate(check: bool) {
let syntax_kinds = generate_syntax_kinds(KINDS_SRC);
let syntax_kinds_file = project_root().join("crates/parser/src/syntax_kind/generated.rs");
ensure_file_contents(syntax_kinds_file.as_path(), &syntax_kinds, check);
ensure_file_contents(
crate::flags::CodegenType::Grammar,
syntax_kinds_file.as_path(),
&syntax_kinds,
check,
);
let grammar = fs::read_to_string(project_root().join("crates/syntax/rust.ungram"))
.unwrap()
@ -37,11 +42,21 @@ pub(crate) fn generate(check: bool) {
let ast_tokens = generate_tokens(&ast);
let ast_tokens_file = project_root().join("crates/syntax/src/ast/generated/tokens.rs");
ensure_file_contents(ast_tokens_file.as_path(), &ast_tokens, check);
ensure_file_contents(
crate::flags::CodegenType::Grammar,
ast_tokens_file.as_path(),
&ast_tokens,
check,
);
let ast_nodes = generate_nodes(KINDS_SRC, &ast);
let ast_nodes_file = project_root().join("crates/syntax/src/ast/generated/nodes.rs");
ensure_file_contents(ast_nodes_file.as_path(), &ast_nodes, check);
ensure_file_contents(
crate::flags::CodegenType::Grammar,
ast_nodes_file.as_path(),
&ast_nodes,
check,
);
}
fn generate_tokens(grammar: &AstSrc) -> String {

View file

@ -76,7 +76,12 @@ pub struct LintGroup {
let contents = add_preamble(crate::flags::CodegenType::LintDefinitions, reformat(contents));
let destination = project_root().join(DESTINATION);
ensure_file_contents(destination.as_path(), &contents, check);
ensure_file_contents(
crate::flags::CodegenType::LintDefinitions,
destination.as_path(),
&contents,
check,
);
}
/// Parses the output of `rustdoc -Whelp` and prints `Lint` and `LintGroup` constants into `buf`.

View file

@ -8,16 +8,20 @@ use std::{
path::{Path, PathBuf},
};
#[test]
fn sourcegen_parser_tests() {
let grammar_dir = sourcegen::project_root().join(Path::new("crates/parser/src/grammar"));
use crate::{
codegen::{ensure_file_contents, list_rust_files, CommentBlock},
project_root,
};
pub(crate) fn generate(check: bool) {
let grammar_dir = project_root().join(Path::new("crates/parser/src/grammar"));
let tests = tests_from_dir(&grammar_dir);
install_tests(&tests.ok, "crates/parser/test_data/parser/inline/ok");
install_tests(&tests.err, "crates/parser/test_data/parser/inline/err");
install_tests(&tests.ok, "crates/parser/test_data/parser/inline/ok", check);
install_tests(&tests.err, "crates/parser/test_data/parser/inline/err", check);
fn install_tests(tests: &HashMap<String, Test>, into: &str) {
let tests_dir = sourcegen::project_root().join(into);
fn install_tests(tests: &HashMap<String, Test>, into: &str, check: bool) {
let tests_dir = project_root().join(into);
if !tests_dir.is_dir() {
fs::create_dir_all(&tests_dir).unwrap();
}
@ -37,7 +41,7 @@ fn sourcegen_parser_tests() {
tests_dir.join(file_name)
}
};
sourcegen::ensure_file_contents(&path, &test.text);
ensure_file_contents(crate::flags::CodegenType::ParserTests, &path, &test.text, check);
}
}
}
@ -57,7 +61,7 @@ struct Tests {
fn collect_tests(s: &str) -> Vec<Test> {
let mut res = Vec::new();
for comment_block in sourcegen::CommentBlock::extract_untagged(s) {
for comment_block in CommentBlock::extract_untagged(s) {
let first_line = &comment_block.contents[0];
let (name, ok) = if let Some(name) = first_line.strip_prefix("test ") {
(name.to_owned(), true)
@ -80,7 +84,7 @@ fn collect_tests(s: &str) -> Vec<Test> {
fn tests_from_dir(dir: &Path) -> Tests {
let mut res = Tests::default();
for entry in sourcegen::list_rust_files(dir) {
for entry in list_rust_files(dir) {
process_file(&mut res, entry.as_path());
}
let grammar_rs = dir.parent().unwrap().join("grammar.rs");

View file

@ -185,6 +185,7 @@ pub enum CodegenType {
AssistsDocTests,
DiagnosticsDocs,
LintDefinitions,
ParserTests,
}
impl fmt::Display for CodegenType {
@ -195,6 +196,7 @@ impl fmt::Display for CodegenType {
Self::AssistsDocTests => write!(f, "assists-doc-tests"),
Self::DiagnosticsDocs => write!(f, "diagnostics-docs"),
Self::LintDefinitions => write!(f, "lint-definitions"),
Self::ParserTests => write!(f, "parser-tests"),
}
}
}
@ -208,6 +210,7 @@ impl FromStr for CodegenType {
"assists-doc-tests" => Ok(Self::AssistsDocTests),
"diagnostics-docs" => Ok(Self::DiagnosticsDocs),
"lint-definitions" => Ok(Self::LintDefinitions),
"parser-tests" => Ok(Self::ParserTests),
_ => Err("Invalid option".to_owned()),
}
}