-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
0 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::fs::{self, DirEntry}; | ||
use std::io::ErrorKind; | ||
use std::path::PathBuf; | ||
|
||
use crate::{report::ReportEntry, Collector, CrateInfo, Result}; | ||
|
||
use super::CollectionError; | ||
|
||
/// List information about entries in a directory. | ||
/// | ||
/// Limitations: | ||
/// * Is not recursive | ||
/// * Does not handle symbolic links | ||
/// * Only sizes of files are printed and not e.g. time of last modification | ||
/// | ||
/// # Example | ||
/// | ||
/// ```md | ||
/// #### File and dir | ||
/// | ||
/// - file.txt, 14 bytes | ||
/// - some_dir | ||
/// | ||
/// ``` | ||
pub struct DirectoryEntries { | ||
title: String, | ||
path: PathBuf, | ||
} | ||
|
||
impl DirectoryEntries { | ||
pub fn new(title: impl Into<String>, path: impl Into<PathBuf>) -> Self { | ||
Self { | ||
title: title.into(), | ||
path: path.into(), | ||
} | ||
} | ||
} | ||
|
||
impl Collector for DirectoryEntries { | ||
fn description(&self) -> &str { | ||
&self.title | ||
} | ||
|
||
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> { | ||
let path_str = &self.path.to_string_lossy(); | ||
|
||
let mut entries = fs::read_dir(&self.path) | ||
.map_err(|e| read_dir_error_to_report_entry(e, path_str))? | ||
.map(|e| match e { | ||
Ok(dir_entry) => dir_entry_to_report_entry(dir_entry), | ||
Err(e) => format!("Error: {}", e), | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
// For stable ordering | ||
entries.sort(); | ||
|
||
if entries.is_empty() { | ||
Ok(ReportEntry::Text(format!("'{}' is empty", path_str))) | ||
} else { | ||
Ok(ReportEntry::List( | ||
entries.into_iter().map(ReportEntry::Text).collect(), | ||
)) | ||
} | ||
} | ||
} | ||
|
||
fn read_dir_error_to_report_entry(error: std::io::Error, path_str: &str) -> CollectionError { | ||
CollectionError::CouldNotRetrieve(if error.kind() == ErrorKind::NotFound { | ||
format!("'{}' not found", path_str) | ||
} else { | ||
format!("'{}' not read: {}", path_str, error) | ||
}) | ||
} | ||
|
||
fn dir_entry_to_report_entry(dir_entry: DirEntry) -> String { | ||
let mut text = String::new(); | ||
|
||
text.push_str(&dir_entry.file_name().to_string_lossy()); | ||
|
||
if let Ok(metadata) = dir_entry.metadata() { | ||
if metadata.is_file() { | ||
text.push_str(&format!(", {} bytes", metadata.len())); | ||
} | ||
} | ||
|
||
text | ||
} |
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#![cfg(feature = "format_markdown")] | ||
|
||
use std::path::PathBuf; | ||
|
||
use pretty_assertions::assert_eq; | ||
use tempfile::tempdir; | ||
|
||
use bugreport::{bugreport, collector::DirectoryEntries, format::Markdown}; | ||
|
||
#[test] | ||
fn dir_not_found() { | ||
let actual = bugreport!() | ||
.info(DirectoryEntries::new("No dir", "this-dir-does-not-exist")) | ||
.format::<Markdown>(); | ||
|
||
let expected = "#### No dir | ||
'this-dir-does-not-exist' not found | ||
"; | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn dir_is_empty() -> Result<(), std::io::Error> { | ||
let empty_dir = tempdir()?; | ||
let empty_dir_path = empty_dir.path(); | ||
|
||
let actual = bugreport!() | ||
.info(DirectoryEntries::new("Empty dir", empty_dir_path)) | ||
.format::<Markdown>(); | ||
|
||
let expected = format!( | ||
"#### Empty dir | ||
'{}' is empty | ||
", | ||
empty_dir_path.to_string_lossy() | ||
); | ||
|
||
assert_eq!(expected, actual); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn dir_exists() -> Result<(), std::io::Error> { | ||
let dir = tempdir()?; | ||
let dir_path = dir.path(); | ||
|
||
// Put a file in the dir | ||
let mut some_file = PathBuf::from(dir_path); | ||
some_file.push("file.txt"); | ||
std::fs::write(some_file, "This is a file")?; | ||
|
||
// Put a dir in the dir | ||
let mut some_dir = PathBuf::from(dir_path); | ||
some_dir.push("some_dir"); | ||
std::fs::create_dir(some_dir)?; | ||
|
||
let actual = bugreport!() | ||
.info(DirectoryEntries::new("File and dir", dir_path)) | ||
.format::<Markdown>(); | ||
|
||
let expected = "#### File and dir | ||
- file.txt, 14 bytes | ||
- some_dir | ||
"; | ||
|
||
assert_eq!(expected, actual); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn new() { | ||
DirectoryEntries::new("a", "/a"); | ||
DirectoryEntries::new(String::from("b"), PathBuf::from("/b")); | ||
DirectoryEntries::new(&String::from("c"), &PathBuf::from("/c")); | ||
new_with_title_from_local_variable(); | ||
} | ||
|
||
fn new_with_title_from_local_variable() -> DirectoryEntries { | ||
let local_variable = String::from("pretend this is dynamically constructed"); | ||
DirectoryEntries::new(&local_variable, "/path") | ||
} |