[ty] Migrate the namespace package module resolver tests to mdtests (#18133)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Micha Reiser
2025-05-16 19:56:33 +02:00
committed by GitHub
parent e21972a79b
commit 3d55a16c91
2 changed files with 107 additions and 95 deletions

View File

@@ -1288,25 +1288,6 @@ mod tests {
);
}
#[test]
fn single_file_takes_priority_over_namespace_package() {
//const SRC: &[FileSpec] = &[("foo.py", "x = 1")];
const SRC: &[FileSpec] = &[("foo.py", "x = 1"), ("foo/bar.py", "x = 2")];
let TestCase { db, src, .. } = TestCaseBuilder::new().with_src_files(SRC).build();
let foo_module_name = ModuleName::new_static("foo").unwrap();
let foo_bar_module_name = ModuleName::new_static("foo.bar").unwrap();
// `foo.py` takes priority over the `foo` namespace package
let foo_module = resolve_module(&db, &foo_module_name).unwrap();
assert_eq!(foo_module.file().path(&db), &src.join("foo.py"));
// `foo.bar` isn't recognised as a module
let foo_bar_module = resolve_module(&db, &foo_bar_module_name);
assert_eq!(foo_bar_module, None);
}
#[test]
fn typing_stub_over_module() {
const SRC: &[FileSpec] = &[("foo.py", "print('Hello, world!')"), ("foo.pyi", "x: int")];
@@ -1349,82 +1330,6 @@ mod tests {
);
}
#[test]
fn namespace_package() {
// From [PEP420](https://peps.python.org/pep-0420/#nested-namespace-packages).
// But uses `src` for `project1` and `site-packages` for `project2`.
// ```
// src
// parent
// child
// one.py
// site_packages
// parent
// child
// two.py
// ```
let TestCase {
db,
src,
site_packages,
..
} = TestCaseBuilder::new()
.with_src_files(&[("parent/child/one.py", "print('Hello, world!')")])
.with_site_packages_files(&[("parent/child/two.py", "print('Hello, world!')")])
.build();
let one_module_name = ModuleName::new_static("parent.child.one").unwrap();
let one_module_path = FilePath::System(src.join("parent/child/one.py"));
assert_eq!(
resolve_module(&db, &one_module_name),
path_to_module(&db, &one_module_path)
);
let two_module_name = ModuleName::new_static("parent.child.two").unwrap();
let two_module_path = FilePath::System(site_packages.join("parent/child/two.py"));
assert_eq!(
resolve_module(&db, &two_module_name),
path_to_module(&db, &two_module_path)
);
}
#[test]
fn regular_package_in_namespace_package() {
// Adopted test case from the [PEP420 examples](https://peps.python.org/pep-0420/#nested-namespace-packages).
// The `src/parent/child` package is a regular package. Therefore, `site_packages/parent/child/two.py` should not be resolved.
// ```
// src
// parent
// child
// one.py
// site_packages
// parent
// child
// two.py
// ```
const SRC: &[FileSpec] = &[
("parent/child/__init__.py", "print('Hello, world!')"),
("parent/child/one.py", "print('Hello, world!')"),
];
const SITE_PACKAGES: &[FileSpec] = &[("parent/child/two.py", "print('Hello, world!')")];
let TestCase { db, src, .. } = TestCaseBuilder::new()
.with_src_files(SRC)
.with_site_packages_files(SITE_PACKAGES)
.build();
let one_module_path = FilePath::System(src.join("parent/child/one.py"));
let one_module_name =
resolve_module(&db, &ModuleName::new_static("parent.child.one").unwrap());
assert_eq!(one_module_name, path_to_module(&db, &one_module_path));
assert_eq!(
None,
resolve_module(&db, &ModuleName::new_static("parent.child.two").unwrap())
);
}
#[test]
fn module_search_path_priority() {
let TestCase {