Skip to content

Commit

Permalink
Add child_packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Nov 26, 2023
1 parent d8b9ea0 commit ff0eef7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/import_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ impl ImportGraph {
Ok(packages)
}

pub fn child_packages(&self, package: &str) -> Result<HashSet<&str>> {
let package: &Package = match self.borrow_packages_by_pypath().get(package) {
Some(package) => package,
None => {
return Err(Error::PackageNotFound(package.to_string()))?;
}
};
let mut packages = HashSet::new();
for child in package.children.iter() {
packages.insert(child.pypath.as_str());
}
Ok(packages)
}

pub fn child_modules(&self, package: &str) -> Result<HashSet<&str>> {
let package: &Package = match self.borrow_packages_by_pypath().get(package) {
Some(package) => package,
Expand Down
16 changes: 16 additions & 0 deletions tests/test_import_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ fn test_packages_from_modules() {
);
}

#[test]
fn test_child_packages() {
let root_package_path = Path::new("./testpackages/somesillypackage");
let import_graph = ImportGraphBuilder::new(root_package_path).build().unwrap();
assert_eq!(
import_graph.child_packages("somesillypackage").unwrap(),
hashset! {
"somesillypackage.child1",
"somesillypackage.child2",
"somesillypackage.child3",
"somesillypackage.child4",
"somesillypackage.child5",
},
);
}

#[test]
fn test_child_modules() {
let root_package_path = Path::new("./testpackages/somesillypackage");
Expand Down

0 comments on commit ff0eef7

Please sign in to comment.