Skip to content

Commit 08c56c8

Browse files
authored
Factor out the mdtest crate (#24616)
Summary -- This is a first step toward adding mdtests for Ruff. I actually wrote the code in the opposite order, first copy-pasting `ty_test` to a `ruff_test` crate, and then factoring out the shared code, but I figured it would be easier to review in this order. I also opened a stacked PR with the `ruff_test` changes (#24617) to show that the API works well for that too. The main change here is moving several of the modules from `ty_test` to a new `mdtest` crate: - `assertion` - `diagnostic` - `matcher` - `parser` Beyond moving these files to the new crate, I made `Matcher` functions take a `&dyn Db` to support passing a different concrete type from `ruff_test`, and I also made the parser generic over an `MdtestConfig` trait to allow Ruff to use a separate config struct. I also introduced new `TestConfig` and `TestDb` types to allow testing the `matcher` and `parser` within the `mdtest` crate without depending on either the real ty `Db` or `ty_test` config type. The lib.rs file from `ty_test` was essentially split in half, with the shared code moved to the `mdtest` crate and the ty-specific parts kept in `ty_test`. Test Plan -- All existing mdtests and the unit tests from `ty_test` should still pass, and the stacked branch with the `ruff_test` crate tests the split API
1 parent 725fbb7 commit 08c56c8

12 files changed

Lines changed: 849 additions & 663 deletions

File tree

Cargo.lock

Lines changed: 31 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ty_static = { path = "crates/ty_static" }
5757
ty_test = { path = "crates/ty_test" }
5858
ty_vendored = { path = "crates/ty_vendored" }
5959

60+
mdtest = { path = "crates/mdtest" }
61+
6062
aho-corasick = { version = "1.1.3" }
6163
anstream = { version = "1.0.0" }
6264
anstyle = { version = "1.0.10" }

crates/mdtest/Cargo.toml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[package]
2+
name = "mdtest"
3+
version = "0.0.0"
4+
publish = false
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
authors.workspace = true
11+
license.workspace = true
12+
13+
[lib]
14+
doctest = false
15+
16+
[dependencies]
17+
ruff_db = { workspace = true }
18+
ruff_diagnostics = { workspace = true }
19+
ruff_index = { workspace = true }
20+
ruff_python_ast = { workspace = true }
21+
ruff_python_trivia = { workspace = true }
22+
ruff_source_file = { workspace = true }
23+
ruff_text_size = { workspace = true }
24+
25+
anyhow = { workspace = true }
26+
camino = { workspace = true }
27+
colored = { workspace = true }
28+
indexmap = { workspace = true }
29+
insta = { workspace = true }
30+
memchr = { workspace = true }
31+
path-slash = { workspace = true }
32+
regex = { workspace = true }
33+
rustc-hash = { workspace = true }
34+
rustc-stable-hash = { workspace = true }
35+
salsa = { workspace = true }
36+
serde = { workspace = true }
37+
similar = { workspace = true }
38+
smallvec = { workspace = true }
39+
thiserror = { workspace = true }
40+
toml = { workspace = true }
41+
tracing = { workspace = true }
42+
43+
[lints]
44+
workspace = true
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -497,30 +497,16 @@ pub(crate) enum ErrorAssertionParseError<'a> {
497497
#[cfg(test)]
498498
mod tests {
499499
use super::*;
500-
use crate::Db;
500+
use crate::tests::TestDb;
501+
use ruff_db::files::system_path_to_file;
501502
use ruff_db::parsed::parsed_module;
502503
use ruff_db::source::line_index;
503504
use ruff_db::system::DbWithWritableSystem as _;
504-
use ruff_db::{Db as _, files::system_path_to_file};
505505
use ruff_python_trivia::textwrap::dedent;
506506
use ruff_source_file::OneIndexed;
507-
use ty_module_resolver::SearchPathSettings;
508-
use ty_python_core::platform::PythonPlatform;
509-
use ty_python_core::program::{FallibleStrategy, Program, ProgramSettings};
510-
use ty_python_semantic::PythonVersionWithSource;
511507

512508
fn get_assertions(source: &str) -> InlineFileAssertions<'_> {
513-
let mut db = Db::setup();
514-
515-
let settings = ProgramSettings {
516-
python_version: PythonVersionWithSource::default(),
517-
python_platform: PythonPlatform::default(),
518-
search_paths: SearchPathSettings::new(Vec::new())
519-
.to_search_paths(db.system(), db.vendored(), &FallibleStrategy)
520-
.unwrap(),
521-
};
522-
Program::init_or_update(&mut db, settings);
523-
509+
let mut db = TestDb::setup();
524510
db.write_file("/src/test.py", source).unwrap();
525511
let file = system_path_to_file(&db, "/src/test.py").unwrap();
526512
let parsed = parsed_module(&db, file).load(&db);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,18 @@ struct DiagnosticWithLine<'a> {
140140

141141
#[cfg(test)]
142142
mod tests {
143-
use crate::db::Db;
144143
use ruff_db::diagnostic::{Annotation, Diagnostic, DiagnosticId, LintName, Severity, Span};
145144
use ruff_db::files::system_path_to_file;
146145
use ruff_db::source::line_index;
147146
use ruff_db::system::DbWithWritableSystem as _;
148147
use ruff_source_file::OneIndexed;
149148
use ruff_text_size::{TextRange, TextSize};
150149

150+
use crate::tests::TestDb;
151+
151152
#[test]
152153
fn sort_and_group() {
153-
let mut db = Db::setup();
154+
let mut db = TestDb::setup();
154155
db.write_file("/src/test.py", "one\ntwo\n").unwrap();
155156
let file = system_path_to_file(&db, "/src/test.py").unwrap();
156157
let lines = line_index(&db, file);

0 commit comments

Comments
 (0)