[ty] Simplify semantic token tests (#21206)

This commit is contained in:
Micha Reiser 2025-11-03 16:35:42 +01:00 committed by GitHub
parent 1b2ed6a503
commit e8c35b9704
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 422 additions and 411 deletions

View file

@ -302,7 +302,6 @@ mod tests {
use insta::assert_snapshot;
use ruff_db::{
Db as _,
files::{File, system_path_to_file},
source::source_text,
};
@ -311,9 +310,6 @@ mod tests {
use ruff_db::system::{DbWithWritableSystem, SystemPathBuf};
use ty_project::ProjectMetadata;
use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
};
pub(super) fn inlay_hint_test(source: &str) -> InlayHintTest {
const START: &str = "<START>";
@ -324,6 +320,8 @@ mod tests {
SystemPathBuf::from("/"),
));
db.init_program().unwrap();
let source = dedent(source);
let start = source.find(START);
@ -345,19 +343,6 @@ mod tests {
let file = system_path_to_file(&db, "main.py").expect("newly written file to existing");
let search_paths = SearchPathSettings::new(vec![SystemPathBuf::from("/")])
.to_search_paths(db.system(), db.vendored())
.expect("Valid search path settings");
Program::from_settings(
&db,
ProgramSettings {
python_version: PythonVersionWithSource::default(),
python_platform: PythonPlatform::default(),
search_paths,
},
);
InlayHintTest { db, file, range }
}

View file

@ -338,9 +338,6 @@ mod tests {
use ruff_python_trivia::textwrap::dedent;
use ruff_text_size::TextSize;
use ty_project::ProjectMetadata;
use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
};
/// A way to create a simple single-file (named `main.py`) cursor test.
///
@ -417,18 +414,7 @@ mod tests {
SystemPathBuf::from("/"),
));
let search_paths = SearchPathSettings::new(vec![SystemPathBuf::from("/")])
.to_search_paths(db.system(), db.vendored())
.expect("Valid search path settings");
Program::from_settings(
&db,
ProgramSettings {
python_version: PythonVersionWithSource::default(),
python_platform: PythonPlatform::default(),
search_paths,
},
);
db.init_program().unwrap();
let mut cursor: Option<Cursor> = None;
for &Source {

File diff suppressed because it is too large Load diff

View file

@ -516,11 +516,13 @@ pub(crate) mod tests {
use std::sync::{Arc, Mutex};
use ruff_db::Db as SourceDb;
use ruff_db::files::Files;
use ruff_db::files::{FileRootKind, Files};
use ruff_db::system::{DbWithTestSystem, System, TestSystem};
use ruff_db::vendored::VendoredFileSystem;
use ty_python_semantic::Program;
use ty_python_semantic::lint::{LintRegistry, RuleSelection};
use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
};
use crate::db::Db;
use crate::{Project, ProjectMetadata};
@ -560,6 +562,27 @@ pub(crate) mod tests {
db.project = Some(project);
db
}
pub fn init_program(&mut self) -> anyhow::Result<()> {
let root = self.project().root(self);
let search_paths = SearchPathSettings::new(vec![root.to_path_buf()])
.to_search_paths(self.system(), self.vendored())
.expect("Valid search path settings");
Program::from_settings(
self,
ProgramSettings {
python_version: PythonVersionWithSource::default(),
python_platform: PythonPlatform::default(),
search_paths,
},
);
self.files().try_add_root(self, root, FileRootKind::Project);
Ok(())
}
}
impl TestDb {

View file

@ -751,34 +751,20 @@ mod tests {
use crate::ProjectMetadata;
use crate::check_file_impl;
use crate::db::tests::TestDb;
use ruff_db::Db as _;
use ruff_db::files::system_path_to_file;
use ruff_db::source::source_text;
use ruff_db::system::{DbWithTestSystem, DbWithWritableSystem as _, SystemPath, SystemPathBuf};
use ruff_db::testing::assert_function_query_was_not_run;
use ruff_python_ast::name::Name;
use ty_python_semantic::types::check_types;
use ty_python_semantic::{
Program, ProgramSettings, PythonPlatform, PythonVersionWithSource, SearchPathSettings,
};
#[test]
fn check_file_skips_type_checking_when_file_cant_be_read() -> ruff_db::system::Result<()> {
let project = ProjectMetadata::new(Name::new_static("test"), SystemPathBuf::from("/"));
let mut db = TestDb::new(project);
db.init_program().unwrap();
let path = SystemPath::new("test.py");
Program::from_settings(
&db,
ProgramSettings {
python_version: PythonVersionWithSource::default(),
python_platform: PythonPlatform::default(),
search_paths: SearchPathSettings::new(vec![SystemPathBuf::from(".")])
.to_search_paths(db.system(), db.vendored())
.expect("Valid search path settings"),
},
);
db.write_file(path, "x = 10")?;
let file = system_path_to_file(&db, path).unwrap();