Fix stale File status in tests (#15030)

## Summary

Fixes https://github.com/astral-sh/ruff/issues/15027

The `MemoryFileSystem::write_file` API automatically creates
non-existing ancestor directoryes
but we failed to update the status of the now created ancestor
directories in the `Files` data structure.


## Test Plan

Tested that the case in https://github.com/astral-sh/ruff/issues/15027
now passes regardless of whether the *Simple* case is commented out or
not
This commit is contained in:
Micha Reiser 2024-12-17 12:45:36 +01:00 committed by GitHub
parent 7c2e7cf25e
commit dcb99cc817
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 10 deletions

View file

@ -73,6 +73,15 @@ enum SystemOrVendoredPathRef<'a> {
Vendored(&'a VendoredPath), Vendored(&'a VendoredPath),
} }
impl std::fmt::Display for SystemOrVendoredPathRef<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SystemOrVendoredPathRef::System(system) => system.fmt(f),
SystemOrVendoredPathRef::Vendored(vendored) => vendored.fmt(f),
}
}
}
/// Resolves the module for the file with the given id. /// Resolves the module for the file with the given id.
/// ///
/// Returns `None` if the file is not a module locatable via any of the known search paths. /// Returns `None` if the file is not a module locatable via any of the known search paths.

View file

@ -124,7 +124,6 @@ pub(crate) fn infer_definition_types<'db>(
let file = definition.file(db); let file = definition.file(db);
let _span = tracing::trace_span!( let _span = tracing::trace_span!(
"infer_definition_types", "infer_definition_types",
definition = ?definition.as_id(),
range = ?definition.kind(db).range(), range = ?definition.kind(db).range(),
file = %file.path(db) file = %file.path(db)
) )

View file

@ -175,19 +175,26 @@ pub trait DbWithTestSystem: Db + Sized {
/// ///
/// # Panics /// # Panics
/// If the system isn't using the memory file system. /// If the system isn't using the memory file system.
fn write_file( fn write_file(&mut self, path: impl AsRef<SystemPath>, content: impl ToString) -> Result<()> {
&mut self,
path: impl AsRef<SystemPath>,
content: impl ToString,
) -> crate::system::Result<()> {
let path = path.as_ref(); let path = path.as_ref();
let result = self
.test_system() let memory_fs = self.test_system().memory_file_system();
.memory_file_system()
.write_file(path, content); let sync_ancestors = path
.parent()
.is_some_and(|parent| !memory_fs.exists(parent));
let result = memory_fs.write_file(path, content);
if result.is_ok() { if result.is_ok() {
File::sync_path(self, path); File::sync_path(self, path);
// Sync the ancestor paths if the path's parent
// directory didn't exist before.
if sync_ancestors {
for ancestor in path.ancestors() {
File::sync_path(self, ancestor);
}
}
} }
result result