Skip to content

Commit

Permalink
[red-knot] Add walk_directories to System (#12297)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 16, 2024
1 parent 9a817a2 commit 85ae02d
Show file tree
Hide file tree
Showing 10 changed files with 1,060 additions and 73 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/red_knot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ license.workspace = true
red_knot_module_resolver = { workspace = true }
red_knot_python_semantic = { workspace = true }

ruff_db = { workspace = true }
ruff_db = { workspace = true, features = ["os"] }
ruff_python_ast = { workspace = true }

anyhow = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions crates/red_knot_module_resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ walkdir = { workspace = true }
zip = { workspace = true }

[dev-dependencies]
ruff_db = { workspace = true, features = ["os"] }

anyhow = { workspace = true }
insta = { workspace = true }
tempfile = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_module_resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ mod tests {

let temp_dir = tempfile::tempdir()?;
let root = SystemPath::from_std_path(temp_dir.path()).unwrap();
db.use_os_system(OsSystem::new(root));
db.use_system(OsSystem::new(root));

let src = root.join("src");
let site_packages = root.join("site-packages");
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ camino = { workspace = true }
countme = { workspace = true }
dashmap = { workspace = true }
filetime = { workspace = true }
ignore = { workspace = true, optional = true }
salsa = { workspace = true }
tracing = { workspace = true }
rustc-hash = { workspace = true }
Expand All @@ -28,3 +29,6 @@ zip = { workspace = true }
[dev-dependencies]
insta = { workspace = true }
tempfile = { workspace = true }

[features]
os = ["ignore"]
30 changes: 18 additions & 12 deletions crates/ruff_db/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
use std::fmt::Debug;

pub use memory_fs::MemoryFileSystem;
#[cfg(feature = "os")]
pub use os::OsSystem;
pub use test::{DbWithTestSystem, TestSystem};
use walk_directory::WalkDirectoryBuilder;

use crate::file_revision::FileRevision;

pub use self::path::{SystemPath, SystemPathBuf};

mod memory_fs;
#[cfg(feature = "os")]
mod os;
mod path;
mod test;
pub mod walk_directory;

pub type Result<T> = std::io::Result<T>;

Expand All @@ -27,7 +33,7 @@ pub type Result<T> = std::io::Result<T>;
/// * File watching isn't supported.
///
/// Abstracting the system also enables tests to use a more efficient in-memory file system.
pub trait System {
pub trait System: Debug {
/// Reads the metadata of the file or directory at `path`.
fn path_metadata(&self, path: &SystemPath) -> Result<Metadata>;

Expand Down Expand Up @@ -82,6 +88,12 @@ pub trait System {
path: &SystemPath,
) -> Result<Box<dyn Iterator<Item = Result<DirectoryEntry>> + 'a>>;

/// Recursively walks the content of `path`.
///
/// It is allowed to pass a `path` that points to a file. In this case, the walker
/// yields a single entry for that file.
fn walk_directory(&self, path: &SystemPath) -> WalkDirectoryBuilder;

fn as_any(&self) -> &dyn std::any::Any;
}

Expand Down Expand Up @@ -127,28 +139,22 @@ impl FileType {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct DirectoryEntry {
path: SystemPathBuf,
file_type: Result<FileType>,
file_type: FileType,
}

impl DirectoryEntry {
pub fn new(path: SystemPathBuf, file_type: Result<FileType>) -> Self {
pub fn new(path: SystemPathBuf, file_type: FileType) -> Self {
Self { path, file_type }
}

pub fn path(&self) -> &SystemPath {
&self.path
}

pub fn file_type(&self) -> &Result<FileType> {
&self.file_type
}
}

impl PartialEq for DirectoryEntry {
fn eq(&self, other: &Self) -> bool {
self.path == other.path
pub fn file_type(&self) -> FileType {
self.file_type
}
}
Loading

0 comments on commit 85ae02d

Please sign in to comment.