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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>>, | ||
|
@@ -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> { | ||
let mut seen = HashSet::new(); | ||
Self::helper(&mut seen, "".to_string(), true, modules, module_name) | ||
} | ||
|
@@ -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)| { | ||
|
@@ -485,13 +489,16 @@ impl Deps { | |
new_prefix.push(' '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The construction of this string would be more readable using |
||
|
||
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, | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
@@ -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![])); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.