Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Apr 25, 2023
1 parent 85ce593 commit 739fe37
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 256 deletions.
1 change: 0 additions & 1 deletion src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ pub struct Context {
#[arg(long, requires = "hidden")]
pub no_git: bool,


#[arg(long)]
/// Print completions for a given shell to stdout
pub completions: Option<clap_complete::Shell>,
Expand Down
39 changes: 21 additions & 18 deletions src/render/disk_usage/file_size.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::units::{BinPrefix, PrefixKind, SiPrefix, UnitPrefix};
use ansi_term::Style;
use crate::{
render::styles::{self, get_du_theme, get_placeholder_style},
Context,
utils,
utils, Context,
};
use ansi_term::Style;
use clap::ValueEnum;
use filesize::PathExt;
use std::{borrow::Cow, fs::Metadata, ops::AddAssign, path::Path};
Expand Down Expand Up @@ -79,11 +78,13 @@ impl FileSize {
}

pub fn unpadded_display(&self) -> Option<&str> {
self.unpadded_display.as_ref().map(String::as_str)
self.unpadded_display.as_deref()
}

/// Precompute the raw (unpadded) display and sets the number of columns the size (without
/// the prefix) will occupy. Also sets the [Color] to use in advance to style the size output.
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
pub fn precompute_unpadded_display(&mut self) {
let fbytes = self.bytes as f64;

Expand All @@ -105,11 +106,11 @@ impl FileSize {
self.size_columns = utils::num_integral((size * 100.0).floor() as u64) + 1;
}

if let Some(theme) = get_du_theme().ok() {
if let Ok(theme) = get_du_theme() {
let style = theme.get(format!("{unit}").as_str());
self.style = style;
}
},
}
PrefixKind::Bin => {
let unit = BinPrefix::from(fbytes);
let base_value = unit.base_value();
Expand All @@ -127,11 +128,11 @@ impl FileSize {
self.size_columns = utils::num_integral((size * 100.0).floor() as u64) + 1;
}

if let Some(theme) = get_du_theme().ok() {
if let Ok(theme) = get_du_theme() {
let style = theme.get(format!("{unit}").as_str());
self.style = style;
}
},
}
}
}

Expand All @@ -147,12 +148,12 @@ impl FileSize {
};

if self.uses_base_unit.is_some() {
format!("{:>width$} {unit:>unit_padding$}", self.bytes, width = max_size_width)
format!("{:>max_size_width$} {unit:>unit_padding$}", self.bytes)
} else {
format!("{size:>width$} {unit:>unit_padding$}", width = max_size_width)
format!("{size:>max_size_width$} {unit:>unit_padding$}")
}
} else {
format!("{:<width$} B", self.bytes, width = max_size_width)
format!("{:<max_size_width$} B", self.bytes)
};

if let Some(style) = self.style {
Expand All @@ -165,20 +166,22 @@ impl FileSize {
/// Returns a placeholder or empty string.
pub fn placeholder(ctx: &Context) -> String {
if ctx.suppress_size || ctx.max_du_width == 0 {
return String::new()
return String::new();
}

let placeholder = get_placeholder_style().map_or_else(
|_| Cow::from(styles::PLACEHOLDER),
|style| Cow::from(style.paint(styles::PLACEHOLDER).to_string()),
);

let placeholder_padding = placeholder.len() + ctx.max_du_width + match ctx.unit {
PrefixKind::Si if ctx.human => 2,
PrefixKind::Bin if ctx.human => 3,
PrefixKind::Si => 0,
PrefixKind::Bin => 1,
};
let placeholder_padding = placeholder.len()
+ ctx.max_du_width
+ match ctx.unit {
PrefixKind::Si if ctx.human => 2,
PrefixKind::Bin if ctx.human => 3,
PrefixKind::Si => 0,
PrefixKind::Bin => 1,
};

format!("{placeholder:>placeholder_padding$}")
}
Expand Down
14 changes: 11 additions & 3 deletions src/render/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,15 @@ where
};

if is_dir {
Self::assemble_tree(tree, index, branches, node_comparator, inode_set, size_columns, ctx);
Self::assemble_tree(
tree,
index,
branches,
node_comparator,
inode_set,
size_columns,
ctx,
);
}

let node = tree[index].get();
Expand All @@ -244,7 +252,7 @@ where
let file_size_cols = file_size.size_columns;

if file_size_cols > *size_columns {
*size_columns = file_size_cols
*size_columns = file_size_cols;
}
}
}
Expand All @@ -257,7 +265,7 @@ where
let dir_size_cols = dir_size.size_columns;

if dir_size_cols > *size_columns {
*size_columns = dir_size_cols
*size_columns = dir_size_cols;
}

node.set_file_size(dir_size);
Expand Down
4 changes: 1 addition & 3 deletions src/render/tree/node/display/presenters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::render::{
context::Context, disk_usage::file_size::FileSize, tree::Node,
};
use crate::render::{context::Context, disk_usage::file_size::FileSize, tree::Node};
use std::borrow::Cow;

#[cfg(unix)]
Expand Down
10 changes: 5 additions & 5 deletions tests/dirs_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ fn dirs_only() {
assert_eq!(
utils::run_cmd(&["--dirs-only", "--sort", "name", "tests/data"]),
indoc!(
"1.21 KiB data
308 B ├─ dream_cycle
446 B ├─ lipsum
143 B └─ the_yellow_king
"143 B ┌─ the_yellow_king
446 B ├─ lipsum
308 B ├─ dream_cycle
1241 B data
3 directories"
3 directories"
)
)
}
66 changes: 30 additions & 36 deletions tests/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,63 @@ mod utils;
#[test]
fn flat() {
assert_eq!(
utils::run_cmd(&["--flat", "--sort", "name", "tests/data"]),
utils::run_cmd(&["--flat", "tests/data"]),
indoc!(
"1241 B data
308 B dream_cycle
308 B dream_cycle/polaris.txt
446 B lipsum
446 B lipsum/lipsum.txt
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
143 B the_yellow_king/cassildas_song.md
308 B dream_cycle
308 B dream_cycle/polaris.txt
446 B lipsum
446 B lipsum/lipsum.txt
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
143 B the_yellow_king/cassildas_song.md
3 directories, 6 files"
3 directories, 6 files"
)
)
}

#[test]
fn flat_human() {
assert_eq!(
utils::run_cmd(&["--flat", "--human", "--sort", "name", "tests/data"]),
utils::run_cmd(&["--flat", "--human", "tests/data"]),
indoc!(
"1.21 KiB data
308 B dream_cycle
308 B dream_cycle/polaris.txt
446 B lipsum
446 B lipsum/lipsum.txt
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
143 B the_yellow_king/cassildas_song.md
308 B dream_cycle
308 B dream_cycle/polaris.txt
446 B lipsum
446 B lipsum/lipsum.txt
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
143 B the_yellow_king/cassildas_song.md
3 directories, 6 files"
3 directories, 6 files"
)
)
}

#[test]
fn flat_with_level() {
assert_eq!(
utils::run_cmd(&["--flat", "--level", "1", "--sort", "name", "tests/data"]),
utils::run_cmd(&["--flat", "--level", "1", "tests/data"]),
indoc!(
"1241 B data
308 B dream_cycle
446 B lipsum
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
308 B dream_cycle
446 B lipsum
83 B necronomicon.txt
161 B nemesis.txt
100 B nylarlathotep.txt
143 B the_yellow_king
3 directories, 6 files"
3 directories, 6 files"
)
)
}

#[test]
#[should_panic]
fn flat_requires_human() {
utils::run_cmd(&["--human"]);
}

#[test]
#[should_panic]
fn flat_requires_file_name() {
Expand Down
Loading

0 comments on commit 739fe37

Please sign in to comment.