Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: test Modules::deps and handle error cases better #2141

Merged
merged 2 commits into from Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions cli/main.rs
Expand Up @@ -108,12 +108,18 @@ pub fn print_file_info(worker: &Worker, url: &str) {
);
}

let deps = worker.modules.deps(&out.module_name);
println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name);
if let Some(ref depsdeps) = deps.deps {
for d in depsdeps {
println!("{}", d);
if let Some(deps) = worker.modules.deps(&out.module_name) {
println!("{}{}", ansi::bold("deps:\n".to_string()), deps.name);
if let Some(ref depsdeps) = deps.deps {
for d in depsdeps {
println!("{}", d);
}
}
} else {
println!(
"{} cannot retrieve full dependency graph",
ansi::bold("deps:".to_string()),
);
}
}

Expand Down
54 changes: 43 additions & 11 deletions core/modules.rs
Expand Up @@ -439,11 +439,15 @@ impl Modules {
self.by_name.is_alias(name)
}

pub fn deps(&self, url: &str) -> Deps {
pub fn deps(&self, url: &str) -> Option<Deps> {
Deps::new(self, url)
}
}

/// This is a tree structure representing the dependencies of a given module.
/// Use Modules::deps to construct it. The 'deps' member is None if this module
/// was already seen elsewher in the tree.
#[derive(Debug, PartialEq)]
pub struct Deps {
pub name: String,
pub deps: Option<Vec<Deps>>,
Expand All @@ -452,7 +456,7 @@ pub struct Deps {
}

impl Deps {
pub fn new(modules: &Modules, module_name: &str) -> Deps {
fn new(modules: &Modules, module_name: &str) -> Option<Deps> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this function returns an Option then I think the field Deps::deps shouldn't be an Option any more.

let mut seen = HashSet::new();
Self::helper(&mut seen, "".to_string(), true, modules, module_name)
}
Expand All @@ -463,19 +467,19 @@ impl Deps {
is_last: bool,
modules: &Modules,
name: &str, // TODO(ry) rename url
) -> Deps {
) -> Option<Deps> {
if seen.contains(name) {
Deps {
Some(Deps {
name: name.to_string(),
prefix,
deps: None,
is_last,
}
})
} else {
let children = modules.get_children2(name)?;
seen.insert(name.to_string());
let children = modules.get_children2(name).unwrap();
let child_count = children.iter().count();
let deps = children
let child_count = children.len();
let deps: Vec<Deps> = children
.iter()
.enumerate()
.map(|(index, dep_name)| {
Expand All @@ -485,13 +489,16 @@ impl Deps {
new_prefix.push(' ');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The construction of this string would be more readable using format!().
It should also preferrably move out of the closure.


Self::helper(seen, new_prefix, new_is_last, modules, dep_name)
}).collect();
Deps {
})
// If any of the children are missing, return None.
.collect::<Option<_>>()?;

Some(Deps {
name: name.to_string(),
prefix,
deps: Some(deps),
is_last,
}
})
}
}
}
Expand Down Expand Up @@ -888,4 +895,29 @@ mod tests {
assert_eq!(either_err, JSErrorOr::Other(MockError::ResolveErr));
assert!(recursive_load.loader.is_none());
}

#[test]
fn empty_deps() {
let modules = Modules::new();
assert!(modules.deps("foo").is_none());
}

#[test]
fn deps() {
// "foo" -> "bar"
let mut modules = Modules::new();
modules.register(1, "foo");
modules.register(2, "bar");
modules.add_child(1, "bar");
let maybe_deps = modules.deps("foo");
assert!(maybe_deps.is_some());
let mut foo_deps = maybe_deps.unwrap();
assert_eq!(foo_deps.name, "foo");
assert!(foo_deps.deps.is_some());
let foo_children = foo_deps.deps.take().unwrap();
assert_eq!(foo_children.len(), 1);
let bar_deps = &foo_children[0];
assert_eq!(bar_deps.name, "bar");
assert_eq!(bar_deps.deps, Some(vec![]));
}
}