diff --git a/src/import_graph/builder.rs b/src/import_graph/builder.rs new file mode 100644 index 0000000..80e2e21 --- /dev/null +++ b/src/import_graph/builder.rs @@ -0,0 +1,22 @@ +use anyhow::Result; +use std::path::{Path, PathBuf}; + +use super::graph; + +pub struct ImportGraphBuilder { + pub(super) root_package_path: PathBuf, // ignored_imports + // ignore_type_checking_imports + // cache_config +} + +impl ImportGraphBuilder { + pub fn new(root_package_path: &Path) -> Self { + ImportGraphBuilder { + root_package_path: root_package_path.to_path_buf(), + } + } + + pub fn build(self) -> Result { + graph::ImportGraph::build(self) + } +} diff --git a/src/import_graph/graph.rs b/src/import_graph/graph.rs new file mode 100644 index 0000000..7c30236 --- /dev/null +++ b/src/import_graph/graph.rs @@ -0,0 +1,282 @@ +use anyhow::Result; +use ouroboros::self_referencing; +use pathfinding::prelude::{bfs, bfs_reach}; +use std::collections::HashSet; + +use super::builder; +use super::errors::Error; +use super::import_discovery; +use super::indexing; +use super::package_discovery::{self, Module, Package}; + +#[self_referencing] +#[derive(Debug)] +pub struct ImportGraph { + root_package: package_discovery::Package, + + #[borrows(root_package)] + #[covariant] + packages_by_pypath: indexing::PackagesByPypath<'this>, + + #[borrows(root_package)] + #[covariant] + modules_by_pypath: indexing::ModulesByPypath<'this>, + + #[borrows(root_package)] + #[covariant] + packages_by_module: indexing::PackagesByModule<'this>, + + #[borrows(root_package, modules_by_pypath)] + #[covariant] + imports: import_discovery::Imports<'this>, + + #[borrows(imports)] + #[covariant] + reverse_imports: import_discovery::Imports<'this>, +} + +impl ImportGraph { + pub(super) fn build(builder: builder::ImportGraphBuilder) -> Result { + let import_graph = ImportGraphTryBuilder { + root_package: package_discovery::discover_package(builder.root_package_path.clone())?, + packages_by_pypath_builder: |root_package| { + indexing::get_packages_by_pypath(root_package) + }, + modules_by_pypath_builder: indexing::get_modules_by_pypath, + packages_by_module_builder: |root_package| { + indexing::get_packages_by_module(root_package) + }, + imports_builder: |root_package, modules_by_pypath| { + import_discovery::discover_imports(root_package, modules_by_pypath) + }, + reverse_imports_builder: indexing::reverse_imports, + } + .try_build()?; + Ok(import_graph) + } + + pub fn packages(&self) -> HashSet<&str> { + self.borrow_packages_by_pypath() + .values() + .map(|package| package.pypath.as_str()) + .collect() + } + + pub fn modules(&self) -> HashSet<&str> { + self.borrow_modules_by_pypath() + .values() + .map(|module| module.pypath.as_str()) + .collect() + } + + pub fn package_from_module(&self, module: &str) -> Result<&str> { + let module = match self.borrow_modules_by_pypath().get(module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(module.to_string()))?, + }; + Ok(self + .borrow_packages_by_module() + .get(module) + .map(|p| p.pypath.as_str()) + .unwrap()) + } + + pub fn packages_from_modules(&self, modules: HashSet<&str>) -> Result> { + let mut packages = HashSet::new(); + for module in modules.iter() { + let module = match self.borrow_modules_by_pypath().get(module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(module.to_string()))?, + }; + packages.insert( + self.borrow_packages_by_module() + .get(module) + .map(|p| p.pypath.as_str()) + .unwrap(), + ); + } + Ok(packages) + } + + pub fn child_modules(&self, package: &str) -> Result> { + let package: &Package = match self.borrow_packages_by_pypath().get(package) { + Some(package) => package, + None => { + return Err(Error::PackageNotFound(package.to_string()))?; + } + }; + let mut modules = HashSet::new(); + for module in package.modules.iter() { + modules.insert(module.pypath.as_str()); + } + Ok(modules) + } + + pub fn descendant_modules(&self, package: &str) -> Result> { + Ok(self + ._descendant_modules(package)? + .iter() + .map(|m| m.pypath.as_str()) + .collect()) + } + + fn _descendant_modules(&self, package: &str) -> Result> { + let package: &Package = match self.borrow_packages_by_pypath().get(package) { + Some(package) => package, + None => { + return Err(Error::PackageNotFound(package.to_string()))?; + } + }; + let mut modules = HashSet::new(); + let mut q = vec![package]; + while let Some(package) = q.pop() { + for module in package.modules.iter() { + modules.insert(module); + } + for child in package.children.iter() { + q.push(child); + } + } + Ok(modules) + } + + pub fn modules_directly_imported_by(&self, module_or_package: &str) -> Result> { + let from_modules = match self.borrow_packages_by_pypath().get(module_or_package) { + Some(_) => self._descendant_modules(module_or_package)?, + None => match self.borrow_modules_by_pypath().get(module_or_package) { + Some(module) => HashSet::from([module as &Module]), + None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, + }, + }; + let mut modules = HashSet::new(); + for from_module in from_modules { + modules.extend( + self.borrow_imports() + .get(from_module) + .unwrap() + .iter() + .map(|module| module.pypath.as_str()) + .collect::>(), + ) + } + Ok(modules) + } + + pub fn modules_that_directly_import(&self, module_or_package: &str) -> Result> { + let to_modules = match self.borrow_packages_by_pypath().get(module_or_package) { + Some(_) => self._descendant_modules(module_or_package)?, + None => match self.borrow_modules_by_pypath().get(module_or_package) { + Some(module) => HashSet::from([module as &Module]), + None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, + }, + }; + let mut modules = HashSet::new(); + for to_module in to_modules { + modules.extend( + self.borrow_reverse_imports() + .get(to_module) + .unwrap() + .iter() + .map(|module| module.pypath.as_str()) + .collect::>(), + ) + } + Ok(modules) + } + + pub fn downstream_modules(&self, module_or_package: &str) -> Result> { + let from_modules = match self.borrow_packages_by_pypath().get(module_or_package) { + Some(_) => self._descendant_modules(module_or_package)?, + None => match self.borrow_modules_by_pypath().get(module_or_package) { + Some(module) => HashSet::from([module as &Module]), + None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, + }, + }; + let mut downstream_modules = HashSet::new(); + for from_module in from_modules { + let reachable_modules = bfs_reach(from_module, |module| { + self.borrow_imports() + .get(module) + .unwrap() + .iter() + .map(|m| m as &Module) + .collect::>() + }) + .map(|m| m.pypath.as_str()) + .skip(1) // Remove starting module from the results. + .collect::>(); + downstream_modules.extend(reachable_modules); + } + Ok(downstream_modules) + } + + pub fn upstream_modules(&self, module_or_package: &str) -> Result> { + let to_modules = match self.borrow_packages_by_pypath().get(module_or_package) { + Some(_) => self._descendant_modules(module_or_package)?, + None => match self.borrow_modules_by_pypath().get(module_or_package) { + Some(module) => HashSet::from([module as &Module]), + None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, + }, + }; + let mut upstream_modules = HashSet::new(); + for to_module in to_modules { + let reachable_modules = bfs_reach(to_module, |module| { + self.borrow_reverse_imports() + .get(module) + .unwrap() + .iter() + .map(|m| m as &Module) + .collect::>() + }) + .map(|m| m.pypath.as_str()) + .skip(1) // Remove starting module from the results. + .collect::>(); + upstream_modules.extend(reachable_modules); + } + Ok(upstream_modules) + } + + pub fn path_exists(&self, from_module: &str, to_module: &str) -> Result { + let from_module = match self.borrow_modules_by_pypath().get(from_module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(from_module.to_string()))?, + }; + let to_module = match self.borrow_modules_by_pypath().get(to_module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(to_module.to_string()))?, + }; + Ok(self._shortest_path(from_module, to_module).is_some()) + } + + pub fn shortest_path(&self, from_module: &str, to_module: &str) -> Result>> { + let from_module = match self.borrow_modules_by_pypath().get(from_module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(from_module.to_string()))?, + }; + let to_module = match self.borrow_modules_by_pypath().get(to_module) { + Some(module) => module as &Module, + None => Err(Error::ModuleNotFound(to_module.to_string()))?, + }; + return Ok(self._shortest_path(from_module, to_module)); + } + + fn _shortest_path<'a>( + &'a self, + from_module: &'a Module, + to_module: &'a Module, + ) -> Option> { + let shortest_path = bfs( + &from_module, + |module| { + self.borrow_imports() + .get(module) + .unwrap() + .iter() + .map(|m| m as &Module) + .collect::>() + }, + |module| *module == to_module, + ); + shortest_path.map(|shortest_path| shortest_path.iter().map(|m| m.pypath.as_str()).collect()) + } +} diff --git a/src/import_graph/mod.rs b/src/import_graph/mod.rs index 4fabb53..2d333f6 100644 --- a/src/import_graph/mod.rs +++ b/src/import_graph/mod.rs @@ -1,285 +1,7 @@ mod ast_visit; -mod errors; -mod import_discovery; -mod indexing; -mod package_discovery; - -use anyhow::Result; -use ouroboros::self_referencing; -use pathfinding::prelude::{bfs, bfs_reach}; -use std::{collections::HashSet, path::Path}; - -pub use errors::Error; -use package_discovery::{Module, Package}; - -#[self_referencing] -#[derive(Debug)] -pub struct ImportGraph { - root_package: Package, - - #[borrows(root_package)] - #[covariant] - packages_by_pypath: indexing::PackagesByPypath<'this>, - - #[borrows(root_package)] - #[covariant] - modules_by_pypath: indexing::ModulesByPypath<'this>, - - #[borrows(root_package)] - #[covariant] - packages_by_module: indexing::PackagesByModule<'this>, - - #[borrows(root_package, modules_by_pypath)] - #[covariant] - imports: import_discovery::Imports<'this>, - - #[borrows(imports)] - #[covariant] - reverse_imports: import_discovery::Imports<'this>, -} - -impl ImportGraph { - pub fn build(root_package_path: &Path) -> Result { - let import_graph = ImportGraphTryBuilder { - root_package: package_discovery::discover_package(root_package_path)?, - packages_by_pypath_builder: |root_package| { - indexing::get_packages_by_pypath(root_package) - }, - modules_by_pypath_builder: indexing::get_modules_by_pypath, - packages_by_module_builder: |root_package| { - indexing::get_packages_by_module(root_package) - }, - imports_builder: |root_package, modules_by_pypath| { - import_discovery::discover_imports(root_package, modules_by_pypath) - }, - reverse_imports_builder: indexing::reverse_imports, - } - .try_build()?; - Ok(import_graph) - } - - pub fn packages(&self) -> HashSet<&str> { - self.borrow_packages_by_pypath() - .values() - .map(|package| package.pypath.as_str()) - .collect() - } - - pub fn modules(&self) -> HashSet<&str> { - self.borrow_modules_by_pypath() - .values() - .map(|module| module.pypath.as_str()) - .collect() - } - - pub fn package_from_module(&self, module: &str) -> Result<&str> { - let module = match self.borrow_modules_by_pypath().get(module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(module.to_string()))?, - }; - Ok(self - .borrow_packages_by_module() - .get(module) - .map(|p| p.pypath.as_str()) - .unwrap()) - } - - pub fn packages_from_modules(&self, modules: HashSet<&str>) -> Result> { - let mut packages = HashSet::new(); - for module in modules.iter() { - let module = match self.borrow_modules_by_pypath().get(module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(module.to_string()))?, - }; - packages.insert( - self.borrow_packages_by_module() - .get(module) - .map(|p| p.pypath.as_str()) - .unwrap(), - ); - } - Ok(packages) - } - - pub fn child_modules(&self, package: &str) -> Result> { - let package: &Package = match self.borrow_packages_by_pypath().get(package) { - Some(package) => package, - None => { - return Err(Error::PackageNotFound(package.to_string()))?; - } - }; - let mut modules = HashSet::new(); - for module in package.modules.iter() { - modules.insert(module.pypath.as_str()); - } - Ok(modules) - } - - pub fn descendant_modules(&self, package: &str) -> Result> { - Ok(self - ._descendant_modules(package)? - .iter() - .map(|m| m.pypath.as_str()) - .collect()) - } - - fn _descendant_modules(&self, package: &str) -> Result> { - let package: &Package = match self.borrow_packages_by_pypath().get(package) { - Some(package) => package, - None => { - return Err(Error::PackageNotFound(package.to_string()))?; - } - }; - let mut modules = HashSet::new(); - let mut q = vec![package]; - while let Some(package) = q.pop() { - for module in package.modules.iter() { - modules.insert(module); - } - for child in package.children.iter() { - q.push(child); - } - } - Ok(modules) - } - - pub fn modules_directly_imported_by(&self, module_or_package: &str) -> Result> { - let from_modules = match self.borrow_packages_by_pypath().get(module_or_package) { - Some(_) => self._descendant_modules(module_or_package)?, - None => match self.borrow_modules_by_pypath().get(module_or_package) { - Some(module) => HashSet::from([module as &Module]), - None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, - }, - }; - let mut modules = HashSet::new(); - for from_module in from_modules { - modules.extend( - self.borrow_imports() - .get(from_module) - .unwrap() - .iter() - .map(|module| module.pypath.as_str()) - .collect::>(), - ) - } - Ok(modules) - } - - pub fn modules_that_directly_import(&self, module_or_package: &str) -> Result> { - let to_modules = match self.borrow_packages_by_pypath().get(module_or_package) { - Some(_) => self._descendant_modules(module_or_package)?, - None => match self.borrow_modules_by_pypath().get(module_or_package) { - Some(module) => HashSet::from([module as &Module]), - None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, - }, - }; - let mut modules = HashSet::new(); - for to_module in to_modules { - modules.extend( - self.borrow_reverse_imports() - .get(to_module) - .unwrap() - .iter() - .map(|module| module.pypath.as_str()) - .collect::>(), - ) - } - Ok(modules) - } - - pub fn downstream_modules(&self, module_or_package: &str) -> Result> { - let from_modules = match self.borrow_packages_by_pypath().get(module_or_package) { - Some(_) => self._descendant_modules(module_or_package)?, - None => match self.borrow_modules_by_pypath().get(module_or_package) { - Some(module) => HashSet::from([module as &Module]), - None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, - }, - }; - let mut downstream_modules = HashSet::new(); - for from_module in from_modules { - let reachable_modules = bfs_reach(from_module, |module| { - self.borrow_imports() - .get(module) - .unwrap() - .iter() - .map(|m| m as &Module) - .collect::>() - }) - .map(|m| m.pypath.as_str()) - .skip(1) // Remove starting module from the results. - .collect::>(); - downstream_modules.extend(reachable_modules); - } - Ok(downstream_modules) - } - - pub fn upstream_modules(&self, module_or_package: &str) -> Result> { - let to_modules = match self.borrow_packages_by_pypath().get(module_or_package) { - Some(_) => self._descendant_modules(module_or_package)?, - None => match self.borrow_modules_by_pypath().get(module_or_package) { - Some(module) => HashSet::from([module as &Module]), - None => Err(Error::ModuleNotFound(module_or_package.to_string()))?, - }, - }; - let mut upstream_modules = HashSet::new(); - for to_module in to_modules { - let reachable_modules = bfs_reach(to_module, |module| { - self.borrow_reverse_imports() - .get(module) - .unwrap() - .iter() - .map(|m| m as &Module) - .collect::>() - }) - .map(|m| m.pypath.as_str()) - .skip(1) // Remove starting module from the results. - .collect::>(); - upstream_modules.extend(reachable_modules); - } - Ok(upstream_modules) - } - - pub fn path_exists(&self, from_module: &str, to_module: &str) -> Result { - let from_module = match self.borrow_modules_by_pypath().get(from_module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(from_module.to_string()))?, - }; - let to_module = match self.borrow_modules_by_pypath().get(to_module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(to_module.to_string()))?, - }; - Ok(self._shortest_path(from_module, to_module).is_some()) - } - - pub fn shortest_path(&self, from_module: &str, to_module: &str) -> Result>> { - let from_module = match self.borrow_modules_by_pypath().get(from_module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(from_module.to_string()))?, - }; - let to_module = match self.borrow_modules_by_pypath().get(to_module) { - Some(module) => module as &Module, - None => Err(Error::ModuleNotFound(to_module.to_string()))?, - }; - return Ok(self._shortest_path(from_module, to_module)); - } - - fn _shortest_path<'a>( - &'a self, - from_module: &'a Module, - to_module: &'a Module, - ) -> Option> { - let shortest_path = bfs( - &from_module, - |module| { - self.borrow_imports() - .get(module) - .unwrap() - .iter() - .map(|m| m as &Module) - .collect::>() - }, - |module| *module == to_module, - ); - shortest_path.map(|shortest_path| shortest_path.iter().map(|m| m.pypath.as_str()).collect()) - } -} +pub mod builder; +pub mod errors; +pub mod graph; +pub mod import_discovery; +pub mod indexing; +pub mod package_discovery; diff --git a/src/import_graph/package_discovery.rs b/src/import_graph/package_discovery.rs index b34787f..700bfe4 100644 --- a/src/import_graph/package_discovery.rs +++ b/src/import_graph/package_discovery.rs @@ -33,7 +33,7 @@ impl Package { #[derive(Debug, PartialEq, Eq, Hash)] pub struct Module { pub pypath: String, - pub(crate) path: PathBuf, + pub(super) path: PathBuf, } impl Module { @@ -42,8 +42,8 @@ impl Module { } } -pub fn discover_package(root_package_path: &Path) -> Result { - _discover_package(root_package_path, root_package_path) +pub fn discover_package(root_package_path: PathBuf) -> Result { + _discover_package(root_package_path.as_path(), root_package_path.as_path()) } fn _discover_package(root_package_path: &Path, package_path: &Path) -> Result { diff --git a/src/lib.rs b/src/lib.rs index c2c2dbd..a7e6499 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ mod import_graph; -pub use import_graph::{Error, ImportGraph}; +pub use import_graph::{builder::ImportGraphBuilder, errors::Error, graph::ImportGraph}; diff --git a/tests/test_import_graph.rs b/tests/test_import_graph.rs index fafe2bc..1ef3f00 100644 --- a/tests/test_import_graph.rs +++ b/tests/test_import_graph.rs @@ -1,12 +1,12 @@ use maplit::hashset; use std::path::Path; -use pyimports::ImportGraph; +use pyimports::ImportGraphBuilder; #[test] fn test_packages() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.packages(), hashset! { @@ -23,7 +23,7 @@ fn test_packages() { #[test] fn test_modules() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.modules(), hashset! { @@ -57,7 +57,7 @@ fn test_modules() { #[test] fn test_package_from_module() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .package_from_module("somesillypackage.__init__") @@ -87,7 +87,7 @@ fn test_package_from_module() { #[test] fn test_packages_from_modules() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .packages_from_modules(hashset! { @@ -107,7 +107,7 @@ fn test_packages_from_modules() { #[test] fn test_child_modules() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.child_modules("somesillypackage").unwrap(), hashset! { @@ -171,7 +171,7 @@ fn test_child_modules() { #[test] fn test_descendant_modules() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.descendant_modules("somesillypackage").unwrap(), hashset! { @@ -251,7 +251,7 @@ fn test_descendant_modules() { #[test] fn test_modules_directly_imported_by_module() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .modules_directly_imported_by("somesillypackage.__init__") @@ -345,7 +345,7 @@ fn test_modules_directly_imported_by_module() { #[test] fn test_modules_directly_imported_by_package() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .modules_directly_imported_by("somesillypackage") @@ -396,7 +396,7 @@ fn test_modules_directly_imported_by_package() { #[test] fn test_modules_that_directly_import_module() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .modules_that_directly_import("somesillypackage.__init__") @@ -453,7 +453,7 @@ fn test_modules_that_directly_import_module() { #[test] fn test_modules_that_directly_import_package() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .modules_that_directly_import("somesillypackage") @@ -496,7 +496,7 @@ fn test_modules_that_directly_import_package() { #[test] fn test_downstream_modules_of_module() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .downstream_modules("somesillypackage.a") @@ -528,7 +528,7 @@ fn test_downstream_modules_of_module() { #[test] fn test_downstream_modules_of_package() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.downstream_modules("somesillypackage").unwrap(), hashset! { @@ -584,7 +584,7 @@ fn test_downstream_modules_of_package() { #[test] fn test_upstream_modules_of_module() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.upstream_modules("somesillypackage.a").unwrap(), hashset! { @@ -623,7 +623,7 @@ fn test_upstream_modules_of_module() { #[test] fn test_upstream_modules_of_package() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph.upstream_modules("somesillypackage").unwrap(), hashset! { @@ -664,7 +664,7 @@ fn test_upstream_modules_of_package() { #[test] fn test_shortest_path() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .shortest_path("somesillypackage.a", "somesillypackage.e") @@ -684,7 +684,7 @@ fn test_shortest_path() { #[test] fn test_path_exists() { let root_package_path = Path::new("./testpackages/somesillypackage"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert!(import_graph .path_exists("somesillypackage.a", "somesillypackage.e") .unwrap(),); diff --git a/tests/test_requests.rs b/tests/test_requests.rs index 47be884..aa3daba 100644 --- a/tests/test_requests.rs +++ b/tests/test_requests.rs @@ -1,12 +1,12 @@ use maplit::hashset; use std::path::Path; -use pyimports::ImportGraph; +use pyimports::ImportGraphBuilder; #[test] fn test_modules_directly_imported_by() { let root_package_path = Path::new("./testpackages/requests"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .modules_directly_imported_by("requests.__init__") @@ -27,7 +27,7 @@ fn test_modules_directly_imported_by() { #[test] fn test_downstream_modules() { let root_package_path = Path::new("./testpackages/requests"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert_eq!( import_graph .downstream_modules("requests.__init__") @@ -68,7 +68,7 @@ fn test_downstream_modules() { #[test] fn test_shortest_path() { let root_package_path = Path::new("./testpackages/requests"); - let import_graph = ImportGraph::build(root_package_path).unwrap(); + let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap(); assert!( // There are 3 equally short paths. hashset! {