From 0572a3850819e33b1b5934138e9d5810430bb825 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Fri, 10 Mar 2023 10:28:46 +0700 Subject: [PATCH] cli+api+feat: Change the measured quantities --- ci/github-actions/benchmark/matrix.ts | 32 +++++++++++++------------- exports/completion.bash | 2 +- exports/completion.fish | 2 +- exports/completion.zsh | 6 ++--- src/args.rs | 8 +++---- src/args/quantity.rs | 24 +++++++++---------- src/size_getters.rs | 4 ++-- tests/_utils.rs | 10 +++++++- tests/cli_errors.rs | 1 + tests/flag_combinations.rs | 4 ++-- tests/fs_tree_builder.rs | 4 ++-- tests/json.rs | 5 ++-- tests/usual_cli.rs | 33 +++++++++++++++------------ 13 files changed, 75 insertions(+), 60 deletions(-) diff --git a/ci/github-actions/benchmark/matrix.ts b/ci/github-actions/benchmark/matrix.ts index 6fc21f36..285d391a 100644 --- a/ci/github-actions/benchmark/matrix.ts +++ b/ci/github-actions/benchmark/matrix.ts @@ -1,6 +1,6 @@ import { getReleasedPduName } from './pdu-programs' -export const QUANTITY = ['len', 'blksize', 'blocks'] as const +export const QUANTITY = ['apparent-size', 'block-size', 'block-count'] as const export const MAX_DEPTH = ['1', '10'] as const export const MIN_RATIO = ['0.01', '0'] as const export const PROGRESS = [false, true] as const @@ -39,7 +39,7 @@ export function parseSelfBenchmarkCategory(category: SelfBenchmarkCategory) { } export const RELEASED_PDU_VERSIONS = [ - '0.0.0', + '0.9.0', ] as const export const ACCEPTABLE_PERFORMANCE_REGRESSION = 1.1 // 10% @@ -90,8 +90,8 @@ export interface CompetingBenchmarkCategory { export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = [ { - id: 'len', - pduCliArgs: ['--quantity=len'], + id: 'apparent-size', + pduCliArgs: ['--quantity=apparent-size'], competitors: [ ['dust', '--apparent-size'], ['dua', '--apparent-size'], @@ -101,8 +101,8 @@ export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = ], }, { - id: 'blksize', - pduCliArgs: ['--quantity=blksize'], + id: 'block-size', + pduCliArgs: ['--quantity=block-size'], competitors: [ ['dust'], ['dua'], @@ -115,7 +115,7 @@ export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = id: 'top-down', pduCliArgs: ['--top-down'], competitors: [ - ['dust', '--apparent-size', '--reverse'], + ['dust', '--reverse'], ], }, { @@ -123,8 +123,8 @@ export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = pduCliArgs: ['--max-depth=1'], competitors: [ ['dutree', '--summary'], - ['dua', '--apparent-size'], - ['du', '--apparent-size', '--summarize'], + ['dua'], + ['du', '--summarize'], ], }, { @@ -140,20 +140,20 @@ export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = id: 'no-sort', pduCliArgs: ['--no-sort'], competitors: [ - ['du', '--apparent-size'], - ['dua', '--apparent-size'], + ['du'], + ['dua'], ['ncdu', '-o', '/dev/stdout', '-0'], - ['gdu', '--show-apparent-size', '--non-interactive', '--no-progress'], + ['gdu', '--non-interactive', '--no-progress'], ], }, { id: 'no-sort+summary', pduCliArgs: ['--no-sort', '--max-depth=1'], competitors: [ - ['dua', '--apparent-size'], + ['dua'], ['ncdu', '-o', '/dev/null', '-0'], - ['gdu', '--show-apparent-size', '--non-interactive', '--no-progress'], - ['du', '--apparent-size', '--summarize'], + ['gdu', '--non-interactive', '--no-progress'], + ['du', '--summarize'], ], }, { @@ -161,7 +161,7 @@ export const COMPETING_BENCHMARK_MATRIX: readonly CompetingBenchmarkCategory[] = pduCliArgs: ['--progress'], competitors: [ ['ncdu', '-o', '/dev/stdout', '-1'], - ['gdu', '--show-apparent-size', '--non-interactive'], + ['gdu', '--non-interactive'], ], }, ] diff --git a/exports/completion.bash b/exports/completion.bash index f2fa8ecc..c4104d78 100644 --- a/exports/completion.bash +++ b/exports/completion.bash @@ -30,7 +30,7 @@ _pdu() { return 0 ;; --quantity) - COMPREPLY=($(compgen -W "len blksize blocks" -- "${cur}")) + COMPREPLY=($(compgen -W "apparent-size block-size block-count" -- "${cur}")) return 0 ;; --max-depth) diff --git a/exports/completion.fish b/exports/completion.fish index ae6054fe..4d19641b 100644 --- a/exports/completion.fish +++ b/exports/completion.fish @@ -1,5 +1,5 @@ complete -c pdu -l bytes-format -d 'How to display the numbers of bytes' -r -f -a "{plain Display plain number of bytes without units,metric Use metric scale\, i.e. 1K = 1000B\, 1M = 1000K\, and so on,binary Use binary scale\, i.e. 1K = 1024B\, 1M = 1024K\, and so on}" -complete -c pdu -l quantity -d 'Aspect of the files/directories to be measured' -r -f -a "{len Measure apparent sizes,blksize Measure blksize,blocks Count number of blocks}" +complete -c pdu -l quantity -d 'Aspect of the files/directories to be measured' -r -f -a "{apparent-size Measure apparent sizes,block-size Measure block sizes (block-count * 512B),block-count Count numbers of blocks}" complete -c pdu -l max-depth -d 'Maximum depth to display the data (must be greater than 0)' -r complete -c pdu -l total-width -d 'Width of the visualization' -r complete -c pdu -l column-width -d 'Maximum widths of the tree column and width of the bar column' -r diff --git a/exports/completion.zsh b/exports/completion.zsh index 27fb6093..80a5e0cf 100644 --- a/exports/completion.zsh +++ b/exports/completion.zsh @@ -18,9 +18,9 @@ _pdu() { '--bytes-format=[How to display the numbers of bytes]:BYTES_FORMAT:((plain\:"Display plain number of bytes without units" metric\:"Use metric scale, i.e. 1K = 1000B, 1M = 1000K, and so on" binary\:"Use binary scale, i.e. 1K = 1024B, 1M = 1024K, and so on"))' \ -'--quantity=[Aspect of the files/directories to be measured]:QUANTITY:((len\:"Measure apparent sizes" -blksize\:"Measure blksize" -blocks\:"Count number of blocks"))' \ +'--quantity=[Aspect of the files/directories to be measured]:QUANTITY:((apparent-size\:"Measure apparent sizes" +block-size\:"Measure block sizes (block-count * 512B)" +block-count\:"Count numbers of blocks"))' \ '--max-depth=[Maximum depth to display the data (must be greater than 0)]:MAX_DEPTH: ' \ '(--column-width)--total-width=[Width of the visualization]:TOTAL_WIDTH: ' \ '*--column-width=[Maximum widths of the tree column and width of the bar column]:TREE_WIDTH: :TREE_WIDTH: ' \ diff --git a/src/args.rs b/src/args.rs index 973de663..544ab8fe 100644 --- a/src/args.rs +++ b/src/args.rs @@ -29,7 +29,7 @@ use text_block_macros::text_block; " $ pdu" " $ pdu path/to/file/or/directory" " $ pdu file.txt dir/" - " $ pdu --quantity=blksize" + " $ pdu --quantity=apparent-size" " $ pdu --bytes-format=plain" " $ pdu --bytes-format=binary" " $ pdu --min-ratio=0" @@ -49,8 +49,8 @@ use text_block_macros::text_block; " Compare disk usages of multiple files and/or directories" " $ pdu file.txt dir/" "" - " Show chart in block sizes instead of apparent sizes" - " $ pdu --quantity=blksize" + " Show chart in apparent sizes instead of block sizes" + " $ pdu --quantity=apparent-size" "" " Show data in plain numbers instead of metric units" " $ pdu --bytes-format=plain" @@ -98,7 +98,7 @@ pub struct Args { pub align_left: bool, /// Aspect of the files/directories to be measured. - #[clap(long, value_enum, default_value_t = Quantity::ApparentSize)] + #[clap(long, value_enum, default_value_t = Quantity::DEFAULT)] pub quantity: Quantity, /// Maximum depth to display the data (must be greater than 0). diff --git a/src/args/quantity.rs b/src/args/quantity.rs index 14534252..549f45e0 100644 --- a/src/args/quantity.rs +++ b/src/args/quantity.rs @@ -5,21 +5,21 @@ use clap::ValueEnum; #[derive(Debug, Clone, Copy, PartialEq, Eq)] #[cfg_attr(feature = "cli", derive(ValueEnum))] pub enum Quantity { - /// Measure apparent sizes, equivalent to the - /// [len](std::fs::Metadata::len) method. - #[cfg_attr(feature = "cli", clap(name = "len", help = "Measure apparent sizes"))] + /// Measure apparent sizes. ApparentSize, - /// Measure block sizes, equivalent to the - /// [blksize](std::os::unix::prelude::MetadataExt::blksize) method (POSIX only). + /// Measure block sizes (block-count * 512B). #[cfg(unix)] - #[cfg_attr(feature = "cli", clap(name = "blksize", help = "Measure blksize"))] BlockSize, - /// Count numbers of blocks, equivalent to the - /// [blocks](std::os::unix::prelude::MetadataExt::blocks) method (POSIX only). + /// Count numbers of blocks. #[cfg(unix)] - #[cfg_attr( - feature = "cli", - clap(name = "blocks", help = "Count number of blocks") - )] BlockCount, } + +impl Quantity { + /// Default value of the `--quantity` flag. + #[cfg(unix)] + pub(crate) const DEFAULT: Self = Quantity::BlockSize; + /// Default value of the `--quantity` flag. + #[cfg(not(unix))] + pub(crate) const DEFAULT: Self = Quantity::ApparentSize; +} diff --git a/src/size_getters.rs b/src/size_getters.rs index 7d0be188..75a40d5b 100644 --- a/src/size_getters.rs +++ b/src/size_getters.rs @@ -10,9 +10,9 @@ use std::os::unix::prelude::MetadataExt; pub type SizeGetter = fn(&Metadata) -> Size; /// Returns [`metadata.len()`](Metadata::len). pub const GET_APPARENT_SIZE: SizeGetter = |metadata| metadata.len().into(); -/// Returns [`metadata.blksize()`](Metadata::blksize) (POSIX only). +/// Returns [`metadata.blocks() * 512`](Metadata::blksize) (POSIX only). #[cfg(unix)] -pub const GET_BLOCK_SIZE: SizeGetter = |metadata| metadata.blksize().into(); +pub const GET_BLOCK_SIZE: SizeGetter = |metadata| (metadata.blocks() * 512).into(); /// Returns [`metadata.blocks()`](Metadata::blocks) (POSIX only). #[cfg(unix)] pub const GET_BLOCK_COUNT: SizeGetter = |metadata| metadata.blocks().into(); diff --git a/tests/_utils.rs b/tests/_utils.rs index 3d33fa54..42924c9a 100644 --- a/tests/_utils.rs +++ b/tests/_utils.rs @@ -6,7 +6,8 @@ use parallel_disk_usage::{ fs_tree_builder::FsTreeBuilder, os_string_display::OsStringDisplay, reporter::ErrorOnlyReporter, - size::Size, + size::{Bytes, Size}, + size_getters::{self, SizeGetter}, }; use pipe_trait::Pipe; use pretty_assertions::assert_eq; @@ -20,6 +21,13 @@ use std::{ process::{Command, Output}, }; +/// Default size getter method. +#[cfg(unix)] +pub const DEFAULT_GET_SIZE: SizeGetter = size_getters::GET_BLOCK_SIZE; +/// Default size getter method. +#[cfg(not(unix))] +pub const DEFAULT_GET_SIZE: SizeGetter = size_getters::GET_APPARENT_SIZE; + /// Representation of a temporary filesystem item. /// /// **NOTE:** Delete this once https://github.com/samgiles/rs-mktemp/issues/8 is resolved. diff --git a/tests/cli_errors.rs b/tests/cli_errors.rs index 8c7b51d6..72efb2b8 100644 --- a/tests/cli_errors.rs +++ b/tests/cli_errors.rs @@ -119,6 +119,7 @@ fn fs_errors() { .with_current_dir(workspace.as_path()) .with_arg("--min-ratio=0") .with_arg("--total-width=100") + .with_arg("--quantity=apparent-size") .pipe(stdio) .output() .expect("spawn command"); diff --git a/tests/flag_combinations.rs b/tests/flag_combinations.rs index b866cfea..22a36eb8 100644 --- a/tests/flag_combinations.rs +++ b/tests/flag_combinations.rs @@ -12,9 +12,9 @@ use std::process::Stdio; #[test] fn flag_combinations() { #[cfg(unix)] - let quantity = ["len", "blksize", "blocks"]; + let quantity = ["apparent-size", "block-size", "block-count"]; #[cfg(windows)] - let quantity = ["len"]; + let quantity = ["apparent-size"]; let list = CommandList::default() .option_matrix("--quantity", quantity) diff --git a/tests/fs_tree_builder.rs b/tests/fs_tree_builder.rs index 750b3085..8bb8793d 100644 --- a/tests/fs_tree_builder.rs +++ b/tests/fs_tree_builder.rs @@ -16,9 +16,9 @@ fn len_as_bytes() { #[cfg(unix)] #[test] -fn blksize_as_bytes() { +fn blocks_as_bytes() { let workspace = SampleWorkspace::default(); - test_sample_tree::(&workspace, |metadata| metadata.blksize()); + test_sample_tree::(&workspace, |metadata| metadata.blocks() * 512); } #[cfg(unix)] diff --git a/tests/json.rs b/tests/json.rs index 3dc106de..15ef0414 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -62,7 +62,7 @@ fn json_output() { let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--json-output") - .with_arg("--quantity=len") + .with_arg("--quantity=apparent-size") .with_arg("--min-ratio=0") .with_arg(workspace.as_path()) .with_stdin(Stdio::null()) @@ -149,7 +149,7 @@ fn json_output_json_input() { let json_output = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--json-output") - .with_arg("--quantity=len") + .with_arg("--quantity=apparent-size") .with_arg(workspace.as_path()) .with_stdin(Stdio::null()) .with_stdout(Stdio::piped()) @@ -180,6 +180,7 @@ fn json_output_json_input() { .with_arg("--bytes-format=metric") .with_arg("--total-width=100") .with_arg("--max-depth=10") + .with_arg("--quantity=apparent-size") .with_arg(workspace.as_path()) .with_stdin(Stdio::piped()) .with_stdout(Stdio::piped()) diff --git a/tests/usual_cli.rs b/tests/usual_cli.rs index 0b811442..1f3df673 100644 --- a/tests/usual_cli.rs +++ b/tests/usual_cli.rs @@ -41,7 +41,7 @@ fn total_width() { let builder = FsTreeBuilder { root: workspace.to_path_buf(), - get_data: GET_APPARENT_SIZE, + get_data: DEFAULT_GET_SIZE, reporter: ErrorOnlyReporter::new(ErrorReport::SILENT), }; let mut data_tree: DataTree = builder.into(); @@ -79,7 +79,7 @@ fn column_width() { let builder = FsTreeBuilder { root: workspace.to_path_buf(), - get_data: GET_APPARENT_SIZE, + get_data: DEFAULT_GET_SIZE, reporter: ErrorOnlyReporter::new(ErrorReport::SILENT), }; let mut data_tree: DataTree = builder.into(); @@ -106,6 +106,7 @@ fn min_ratio_0() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) + .with_arg("--quantity=apparent-size") .with_arg("--total-width=100") .with_arg("--min-ratio=0") .pipe(stdio) @@ -142,6 +143,7 @@ fn min_ratio() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) + .with_arg("--quantity=apparent-size") .with_arg("--total-width=100") .with_arg("--min-ratio=0.1") .pipe(stdio) @@ -179,6 +181,7 @@ fn max_depth_2() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) + .with_arg("--quantity=apparent-size") .with_arg("--total-width=100") .with_arg("--max-depth=2") .pipe(stdio) @@ -216,6 +219,7 @@ fn max_depth_1() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) + .with_arg("--quantity=apparent-size") .with_arg("--total-width=100") .with_arg("--max-depth=1") .pipe(stdio) @@ -263,7 +267,7 @@ fn top_down() { let builder = FsTreeBuilder { root: workspace.to_path_buf(), - get_data: GET_APPARENT_SIZE, + get_data: DEFAULT_GET_SIZE, reporter: ErrorOnlyReporter::new(ErrorReport::SILENT), }; let mut data_tree: DataTree = builder.into(); @@ -300,7 +304,7 @@ fn align_left() { let builder = FsTreeBuilder { root: workspace.to_path_buf(), - get_data: GET_APPARENT_SIZE, + get_data: DEFAULT_GET_SIZE, reporter: ErrorOnlyReporter::new(ErrorReport::SILENT), }; let mut data_tree: DataTree = builder.into(); @@ -323,12 +327,12 @@ fn align_left() { } #[test] -fn quantity_len() { +fn quantity_apparent_size() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=len") + .with_arg("--quantity=apparent-size") .pipe(stdio) .output() .expect("spawn command") @@ -361,12 +365,12 @@ fn quantity_len() { #[cfg(unix)] #[test] -fn quantity_blksize() { +fn quantity_block_size() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=blksize") + .with_arg("--quantity=block-size") .pipe(stdio) .output() .expect("spawn command") @@ -399,12 +403,12 @@ fn quantity_blksize() { #[cfg(unix)] #[test] -fn quantity_blocks() { +fn quantity_block_count() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=blocks") + .with_arg("--quantity=block-count") .pipe(stdio) .output() .expect("spawn command") @@ -442,7 +446,7 @@ fn bytes_format_plain() { let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=blksize") + .with_arg("--quantity=block-size") .with_arg("--bytes-format=plain") .pipe(stdio) .output() @@ -481,7 +485,7 @@ fn bytes_format_metric() { let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=blksize") + .with_arg("--quantity=block-size") .with_arg("--bytes-format=metric") .pipe(stdio) .output() @@ -520,7 +524,7 @@ fn bytes_format_binary() { let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) .with_arg("--total-width=100") - .with_arg("--quantity=blksize") + .with_arg("--quantity=block-size") .with_arg("--bytes-format=binary") .pipe(stdio) .output() @@ -567,7 +571,7 @@ fn path_to_workspace() { let builder = FsTreeBuilder { root: workspace.to_path_buf(), - get_data: GET_APPARENT_SIZE, + get_data: DEFAULT_GET_SIZE, reporter: ErrorOnlyReporter::new(ErrorReport::SILENT), }; let mut data_tree: DataTree = builder.into(); @@ -593,6 +597,7 @@ fn multiple_names() { let workspace = SampleWorkspace::default(); let actual = Command::new(PDU) .with_current_dir(workspace.as_path()) + .with_arg("--quantity=apparent-size") .with_arg("--total-width=100") .with_arg("nested") .with_arg("flat")