Skip to content

Commit 4c136c4

Browse files
committed
refactor/traverse: introduce unit tests for module traversal
1 parent e049d26 commit 4c136c4

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

demo/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Just a lil somethin somethin
12
mod lib;
23

34
pub mod nested;

src/literalset.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ fn find_coverage<'a>(
256256
/// A set of consecutive literals.
257257
///
258258
/// Provides means to render them as a code block
259-
#[derive(Clone, Default, Debug, PartialEq, Eq)]
259+
#[derive(Clone, Default, Debug, Hash, PartialEq, Eq)]
260260
pub struct LiteralSet {
261261
/// consecutive set of literals mapped by line number
262262
literals: Vec<TrimmedLiteral>,

src/traverse/iter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct TraverseModulesIter {
2121
impl Default for TraverseModulesIter {
2222
fn default() -> Self {
2323
Self {
24-
mex_depth: usize::MAX,
24+
max_depth: usize::MAX,
2525
queue: VecDeque::with_capacity(128),
2626
}
2727
}
@@ -36,7 +36,7 @@ impl TraverseModulesIter {
3636
let path = path.canonicalize().unwrap();
3737
let meta = path.metadata().unwrap();
3838
if meta.is_file() {
39-
self.queue.push((path, level));
39+
self.queue.push_back((path, level));
4040
} else if meta.is_dir() {
4141
walkdir::WalkDir::new(path)
4242
.max_depth(1)
@@ -55,7 +55,7 @@ impl TraverseModulesIter {
5555
.is_some()
5656
})
5757
.try_for_each::<_, Result<()>>(|path| {
58-
self.queue.push((path, level));
58+
self.queue.push_back((path, level));
5959
Ok(())
6060
})?;
6161
}

src/traverse/mod.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -267,22 +267,21 @@ pub(crate) fn extract(
267267
Markdown(PathBuf),
268268
}
269269

270-
// convert all `Cargo.toml` manifest files to their respective product files
271-
// so after this conversion all of them are considered
270+
// stage 1 - obtain canonical paths
272271
let mut flow = VecDeque::<PathBuf>::with_capacity(32);
273272
flow.extend(paths.into_iter().filter_map(|path_in| {
274-
if path_in.is_absolute() {
273+
let path = if path_in.is_absolute() {
275274
path_in.to_owned()
276275
} else {
277276
cwd.join(&path_in)
278-
}
279-
.canonicalize()
280-
.ok()
277+
};
278+
info!("Processing {} -> {}", path_in.display(), path.display());
279+
path.canonicalize().ok()
281280
}));
282281

282+
// stage 2 - check for manifest, .rs , .md files and directories
283283
let mut files_to_check = Vec::with_capacity(64);
284-
for path in flow {
285-
info!("Processing {} -> {}", path_in.display(), path.display());
284+
while let Some(path) = flow.pop_front() {
286285
files_to_check.push(if let Ok(meta) = path.metadata() {
287286
if meta.is_file() {
288287
match path.file_name().map(|x| x.to_str()).flatten() {
@@ -295,16 +294,16 @@ pub(crate) fn extract(
295294
}
296295
}
297296
} else if meta.is_dir() {
298-
let cargo_toml = to_manifest_dir(path).unwrap().join("Cargo.toml");
297+
let cargo_toml = to_manifest_dir(&path).unwrap().join("Cargo.toml");
299298
if cargo_toml.is_file() {
300-
301299
Extraction::Manifest(cargo_toml)
302300
} else {
303301
// @todo should we just collect all .rs files here instead?
304302

305303
// we know it's a directory, and we limit the entries to 0 levels,
306-
// will cause to yield all .rs files in that dir
307-
flow.extend(traverse_with_depth_limit(path, 0)?.into_iter());
304+
// will cause to yield all "^.*\.rs$" files in that dir
305+
// which is what we want in this case
306+
flow.extend(TraverseModulesIter::with_depth_limit(&path, 0)?);
308307
continue;
309308
}
310309
} else {
@@ -315,26 +314,30 @@ pub(crate) fn extract(
315314
})
316315
}
317316

318-
let files_to_check = files_to_check.into_iter().try_fold::<Vec<_>, _, Result<_>>(Vec::with_capacity(64), |mut acc, tagged_path| {
319-
match tagged_path {
320-
Extraction::Manifest(ref cargo_toml_path) => {
321-
let manifest_list = handle_manifest(cargo_toml_path)?;
322-
acc.extend(manifest_list);
317+
// stage 3 - resolve the manifest products and workspaces, warn about missing
318+
let files_to_check = files_to_check
319+
.into_iter()
320+
.try_fold::<Vec<_>, _, Result<_>>(Vec::with_capacity(64), |mut acc, tagged_path| {
321+
match tagged_path {
322+
Extraction::Manifest(ref cargo_toml_path) => {
323+
let manifest_list = handle_manifest(cargo_toml_path)?;
324+
acc.extend(manifest_list);
325+
}
326+
Extraction::Missing(ref missing_path) => warn!(
327+
"File passed as argument or listed in Cargo.toml manifest does not exist: {}",
328+
missing_path.display()
329+
),
330+
Extraction::Source(path) => acc.push(CheckEntity::Source(path)),
331+
Extraction::Markdown(path) => acc.push(CheckEntity::Markdown(path)),
323332
}
324-
Extraction::Missing(ref missing_path) => warn!(
325-
"File passed as argument or listed in Cargo.toml manifest does not exist: {}",
326-
missing_path.display()
327-
),
328-
Extraction::Source(path) => acc.push(CheckEntity::Source(path)),
329-
Extraction::Markdown(path) => acc.push(CheckEntity::Markdown(path)),
330-
}
331-
Ok(acc)
332-
})?;
333+
Ok(acc)
334+
})?;
333335

336+
// stage 4 - expand from the passed source files, if recursive, recurse down the module train
334337
let docs: Vec<Documentation> = files_to_check
335338
.iter()
336339
.try_fold::<Vec<Documentation>, _, Result<Vec<Documentation>>>(
337-
Vec::with_capacity(items.len()),
340+
Vec::with_capacity(files_to_check.len()),
338341
|mut acc, item| {
339342
match item {
340343
CheckEntity::Source(path) => {

0 commit comments

Comments
 (0)