summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 12:36:24 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 12:36:24 +0530
commitc1cbcf355755fbd1ca6124cdba3b8e361a7bebf2 (patch)
tree4f2120ad0e7546423bec08711df84984fc0bfd6c
parentc50332cead2688e40de192e1b47e50a662763a78 (diff)
Better error reporting
-rw-r--r--src/lib.rs25
-rw-r--r--src/main.rs4
-rw-r--r--tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing6
-rw-r--r--tests/snapshots/success-no-arguments-multiple-input-paths2
-rwxr-xr-xtests/stateless-journey.sh7
5 files changed, 33 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c4d2cf5..0f7627a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -39,12 +39,12 @@ mod common {
#[derive(Default)]
pub struct WalkResult {
- pub num_errors: usize,
+ pub num_errors: u64,
}
impl fmt::Display for WalkResult {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- write!(f, "Encountered {} IO errors", self.num_errors)
+ write!(f, "Encountered {} IO error(s)", self.num_errors)
}
}
}
@@ -52,6 +52,7 @@ mod common {
mod aggregate {
use crate::{WalkOptions, WalkResult};
use failure::Error;
+ use std::borrow::Cow;
use std::{io, path::Path};
pub fn aggregate(
@@ -65,6 +66,7 @@ mod aggregate {
for path in paths.into_iter() {
num_roots += 1;
let mut num_bytes = 0u64;
+ let mut num_errors = 0u64;
for entry in options.iter_from_path(path.as_ref()) {
match entry {
Ok(entry) => {
@@ -72,7 +74,7 @@ mod aggregate {
Some(Ok(ref m)) if !m.is_dir() => m.len(),
Some(Ok(_)) => 0,
Some(Err(_)) => {
- res.num_errors += 1;
+ num_errors += 1;
0
}
None => unreachable!(
@@ -80,15 +82,16 @@ mod aggregate {
),
};
}
- Err(_) => res.num_errors += 1,
+ Err(_) => num_errors += 1,
}
}
- write_path(&mut out, &options, path, num_bytes)?;
+ write_path(&mut out, &options, path, num_bytes, num_errors)?;
total += num_bytes;
+ res.num_errors += num_errors;
}
if num_roots > 1 {
- write_path(&mut out, &options, Path::new("<TOTAL>"), total)?;
+ write_path(&mut out, &options, Path::new("total"), total, res.num_errors)?;
}
Ok(res)
}
@@ -98,12 +101,18 @@ mod aggregate {
options: &WalkOptions,
path: impl AsRef<Path>,
num_bytes: u64,
+ num_errors: u64,
) -> Result<(), io::Error> {
writeln!(
out,
- "{}\t{}",
+ "{}\t{}{}",
options.format_bytes(num_bytes),
- path.as_ref().display()
+ path.as_ref().display(),
+ if num_errors == 0 {
+ Cow::Borrowed("")
+ } else {
+ Cow::Owned(format!("\t<{} IO Error(s)>", num_errors))
+ }
)
}
}
diff --git a/src/main.rs b/src/main.rs
index 452d739..32154c4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,7 +7,7 @@ use structopt::StructOpt;
use dua::ByteFormat;
use failure::Error;
use failure_tools::ok_or_exit;
-use std::{io, path::PathBuf};
+use std::{io, path::PathBuf, process};
mod options {
use dua::ByteFormat as LibraryByteFormat;
@@ -93,7 +93,7 @@ fn run() -> Result<(), Error> {
}?;
if res.num_errors > 0 {
- writeln!(io::stderr(), "{}", res).ok();
+ process::exit(1);
}
Ok(())
}
diff --git a/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing b/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing
new file mode 100644
index 0000000..c685916
--- /dev/null
+++ b/tests/snapshots/failure-no-arguments-multiple-input-paths-some-not-existing
@@ -0,0 +1,6 @@
+1.26 MB .
+1.26 MB .
+0.00 B foo <1 IO Error(s)>
+0.00 B bar <1 IO Error(s)>
+0.00 B baz <1 IO Error(s)>
+2.52 MB total <3 IO Error(s)> \ No newline at end of file
diff --git a/tests/snapshots/success-no-arguments-multiple-input-paths b/tests/snapshots/success-no-arguments-multiple-input-paths
index 6b8938e..724b074 100644
--- a/tests/snapshots/success-no-arguments-multiple-input-paths
+++ b/tests/snapshots/success-no-arguments-multiple-input-paths
@@ -3,4 +3,4 @@
1.26 MB dir
1.26 MB ./dir/
256.00 KB ./dir/sub
-5.29 MB <TOTAL> \ No newline at end of file
+5.29 MB total \ No newline at end of file
diff --git a/tests/stateless-journey.sh b/tests/stateless-journey.sh
index a6e0fa5..95886d2 100755
--- a/tests/stateless-journey.sh
+++ b/tests/stateless-journey.sh
@@ -11,6 +11,7 @@ snapshot="$root/snapshots"
fixtures="$root/fixtures"
SUCCESSFULLY=0
+WITH_FAILURE=1
(with "a sample directory"
(sandbox
@@ -36,6 +37,12 @@ SUCCESSFULLY=0
expect_run ${SUCCESSFULLY} "$exe" . . dir ./dir/ ./dir/sub
}
)
+ (when "specifying no subcommand and some of the directories don't exist"
+ it "produces a human-readable (metric) aggregate of the current directory, with total" && {
+ WITH_SNAPSHOT="$snapshot/failure-no-arguments-multiple-input-paths-some-not-existing" \
+ expect_run ${WITH_FAILURE} "$exe" . . foo bar baz
+ }
+ )
)
)