From fcc63e56c6b2a33d738fd403fe06d299e184a858 Mon Sep 17 00:00:00 2001 From: Jacob Heider Date: Thu, 9 Jan 2025 17:34:44 -0500 Subject: [PATCH 1/5] one more, with sanity checks --- .github/workflows/ci.yaml | 21 +++++++++++ cli/src/main.rs | 2 ++ cli/src/tests/main.rs | 74 +++++++++++++++++++++++++++++++++++++++ cli/src/tests/mod.rs | 1 + 4 files changed, 98 insertions(+) create mode 100644 cli/src/tests/main.rs create mode 100644 cli/src/tests/mod.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2e2688f..66c4638 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -10,6 +10,9 @@ on: name: ci·rs +concurrency: + group: ci·rs·${{ github.ref}} + jobs: check: strategy: @@ -63,6 +66,24 @@ jobs: env: RUSTFLAGS: "-D warnings" + test: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - uses: actions-rs/cargo@v1 + with: + command: test + args: --all-features + env: + RUSTFLAGS: "-D warnings" + smoke: strategy: matrix: diff --git a/cli/src/main.rs b/cli/src/main.rs index 143d03e..9de2696 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -1,6 +1,8 @@ mod args; mod execve; mod help; +#[cfg(test)] +mod tests; use std::{error::Error, fmt::Write, sync::Arc, time::Duration}; diff --git a/cli/src/tests/main.rs b/cli/src/tests/main.rs new file mode 100644 index 0000000..c7d248a --- /dev/null +++ b/cli/src/tests/main.rs @@ -0,0 +1,74 @@ +use crate::{precision, pretty_size}; + +#[test] +fn test_pretty_size() { + assert_eq!(pretty_size(0), ("0 B".to_string(), 1)); + assert_eq!(pretty_size(1), ("1 B".to_string(), 1)); + assert_eq!(pretty_size(1024), ("1.00 KiB".to_string(), 1024)); + assert_eq!( + pretty_size(1024 * 1024), + ("1.00 MiB".to_string(), 1024 * 1024) + ); + assert_eq!( + pretty_size(1024 * 1024 * 1024), + ("1.00 GiB".to_string(), 1024 * 1024 * 1024) + ); + assert_eq!( + pretty_size(1024 * 1024 * 1024 * 1024), + ("1.00 TiB".to_string(), 1024 * 1024 * 1024 * 1024) + ); + assert_eq!( + pretty_size(1024 * 1024 * 1024 * 1024 * 1024), + ("1.00 PiB".to_string(), 1024 * 1024 * 1024 * 1024 * 1024) + ); + assert_eq!( + pretty_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024), + ( + "1.00 EiB".to_string(), + 1024 * 1024 * 1024 * 1024 * 1024 * 1024 + ) + ); + // these are bigger than u64 + // assert_eq!( + // pretty_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024), + // ( + // "1 ZiB".to_string(), + // 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 + // ) + // ); + // assert_eq!( + // pretty_size(1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024), + // ( + // "1 YiB".to_string(), + // 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 + // ) + // ); + assert_eq!(pretty_size(5000), ("4.88 KiB".to_string(), 1024)); + assert_eq!(pretty_size(5120), ("5.00 KiB".to_string(), 1024)); + + assert_eq!( + pretty_size(1024 * 1024 + 1), + ("1.00 MiB".to_string(), 1024 * 1024) + ); + assert_eq!( + pretty_size(35_245 * 1024), + ("34.4 MiB".to_string(), 1024 * 1024) + ); + assert_eq!( + pretty_size(356_245 * 1024 + 1), + ("348 MiB".to_string(), 1024 * 1024) + ); +} + +#[test] +fn test_precision() { + assert_eq!(precision(1.0), 2); + assert_eq!(precision(1.1), 2); + assert_eq!(precision(9.99), 2); + assert_eq!(precision(10.0), 1); + assert_eq!(precision(10.1), 1); + assert_eq!(precision(99.9), 1); + assert_eq!(precision(100.0), 0); + assert_eq!(precision(100.1), 0); + assert_eq!(precision(999.9), 0); +} diff --git a/cli/src/tests/mod.rs b/cli/src/tests/mod.rs new file mode 100644 index 0000000..d18669a --- /dev/null +++ b/cli/src/tests/mod.rs @@ -0,0 +1 @@ +mod main; From ee628ce95ce9197a0ab2437abea2f85612c067a2 Mon Sep 17 00:00:00 2001 From: Jacob Heider Date: Thu, 9 Jan 2025 18:14:45 -0500 Subject: [PATCH 2/5] migrate ci to `dtolnaty/rust-toolchain` see: https://www.reddit.com/r/rust/comments/z1mlls/actionsrs_github_actions_need_more_maintainers_or/ --- .github/workflows/ci.yaml | 54 +++++-------------- .github/workflows/release-packaging.yaml.todo | 12 +---- .github/workflows/test.yml.todo | 27 ++++------ 3 files changed, 24 insertions(+), 69 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 66c4638..af1219d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,7 +5,7 @@ on: - main paths: - "**/*.rs" - - Cargo.toml + - "**/Cargo.toml" - Cargo.lock name: ci·rs @@ -21,14 +21,8 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: check + - uses: dtolnay/rust-toolchain@stable + - run: cargo check env: RUSTFLAGS: "-D warnings" @@ -36,16 +30,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@stable with: - profile: minimal - toolchain: stable - override: true - - run: rustup component add rustfmt - - uses: actions-rs/cargo@v1 - with: - command: fmt - args: --all -- --check + components: rustfmt + - run: cargo fmt --all -- --check clippy: strategy: @@ -54,15 +42,10 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@stable with: components: clippy - toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: clippy - args: --all-features + - run: cargo clippy --all-features env: RUSTFLAGS: "-D warnings" @@ -73,14 +56,8 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: test - args: --all-features + - uses: dtolnay/rust-toolchain@stable + - run: cargo test --all-features env: RUSTFLAGS: "-D warnings" @@ -92,12 +69,5 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: run - args: --all-features -- git --version + - uses: dtolnay/rust-toolchain@stable + - run: cargo run --all-features -- git --version diff --git a/.github/workflows/release-packaging.yaml.todo b/.github/workflows/release-packaging.yaml.todo index 52deeac..2e33d10 100644 --- a/.github/workflows/release-packaging.yaml.todo +++ b/.github/workflows/release-packaging.yaml.todo @@ -16,11 +16,7 @@ # runs-on: ubuntu-latest # steps: # - uses: actions/checkout@v4 -# - uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: nightly -# override: true +# - uses: dtolnay/rust-toolchain@stable # - name: Release Build # run: cargo build --release # - name: "Upload Artifact" @@ -34,11 +30,7 @@ # runs-on: ubuntu-latest # steps: # - uses: actions/checkout@v4 -# - uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: nightly -# override: true +# - uses: dtolnay/rust-toolchain@stable # - uses: katyo/publish-crates@v2 # with: # registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }} diff --git a/.github/workflows/test.yml.todo b/.github/workflows/test.yml.todo index 440f8a5..63941d1 100644 --- a/.github/workflows/test.yml.todo +++ b/.github/workflows/test.yml.todo @@ -20,7 +20,7 @@ # env: # PROJECT_NAME_UNDERSCORE: semverator # CARGO_INCREMENTAL: 0 -# RUSTFLAGS: -Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort -D warnings +# RUSTFLAGS: -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort -D warnings # RUSTDOCFLAGS: -Cpanic=abort # strategy: # matrix: @@ -28,15 +28,12 @@ # runs-on: ${{ matrix.os }} # steps: # - uses: actions/checkout@v4 -# - uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: nightly -# override: true +# - uses: dtolnay/rust-toolchain@stable + id: toolchain # - name: Cache dependencies # uses: actions/cache@v4 # env: -# cache-name: cache-dependencies +# cache-name: ${{ steps.toolchain.outputs.cachekey }} # with: # path: | # ~/.cargo/.crates.toml @@ -54,14 +51,10 @@ # runs-on: ${{ matrix.os }} # steps: # - uses: actions/checkout@v4 -# - uses: actions-rs/toolchain@v1 -# with: -# profile: minimal -# toolchain: nightly -# override: true +# - uses: dtolnay/rust-toolchain@stable +# - run: cargo install cargo-tarpaulin # - name: Generate test result and coverage report -# run: | -# cargo install cargo-tarpaulin -# cargo tarpaulin --engine ptrace -o lcov --output-dir coverage --coveralls $COVERALLS_TOKEN -# env: -# COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }} +# run: cargo tarpaulin --engine ptrace -o lcov --output-dir coverage +# - uses: coverallsapp/github-action@v2 +# with: +# path-to-lcov: coverage/lcov.info From a0ded2c59ef994758eb56c7d9d7e88a503e75060 Mon Sep 17 00:00:00 2001 From: Jacob Heider Date: Fri, 10 Jan 2025 12:14:01 -0500 Subject: [PATCH 3/5] fix tests --- cli/src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 9de2696..dd239db 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -314,18 +314,15 @@ fn pretty_size(n: u64) -> (String, u64) { let mut i = 0; let mut divisor = 1; - while size > 1024.0 && i < units.len() - 1 { + while size >= 1000.0 && i < units.len() - 1 { size /= 1024.0; i += 1; divisor *= 1024; } - let formatted = format!( - "{:.precision$} {}", - size, - units[i], - precision = precision(size) - ); + let precision = if i == 0 { 0 } else { precision(size) }; + + let formatted = format!("{:.precision$} {}", size, units[i], precision = precision); (formatted, divisor) } From fd1f06e4dd9bc73a8e22bc6db2ccb6085ab62ad4 Mon Sep 17 00:00:00 2001 From: Jacob Heider Date: Thu, 9 Jan 2025 17:11:37 -0500 Subject: [PATCH 4/5] use math to reduce clarity --- cli/src/main.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index dd239db..1ecf9d3 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -328,11 +328,5 @@ fn pretty_size(n: u64) -> (String, u64) { } fn precision(n: f64) -> usize { - if n < 10.0 { - 2 - } else if n < 100.0 { - 1 - } else { - 0 - } + 2 - (n.log10().clamp(0.0, 2.0) as usize) } From f13d5029b22221ed9595f572464995dca526e99c Mon Sep 17 00:00:00 2001 From: Jacob Heider Date: Thu, 9 Jan 2025 17:34:44 -0500 Subject: [PATCH 5/5] one more --- cli/src/main.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 1ecf9d3..f3b32ab 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -310,23 +310,27 @@ fn configure_bar(pb: &ProgressBar) { fn pretty_size(n: u64) -> (String, u64) { let units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; - let mut size = n as f64; - let mut i = 0; - let mut divisor = 1; - - while size >= 1000.0 && i < units.len() - 1 { - size /= 1024.0; - i += 1; - divisor *= 1024; - } - - let precision = if i == 0 { 0 } else { precision(size) }; - let formatted = format!("{:.precision$} {}", size, units[i], precision = precision); + // number of 1024s + let thousands = n.max(1).ilog(1024).clamp(0, units.len() as u32 - 1) as usize; + // size in the appropriate unit + let size = n as f64 / 1024.0f64.powi(thousands as i32); + // the divisor to get back to bytes + let divisor = 1024u64.pow(thousands as u32); + // number of decimal places to show (0 if we're bytes. no fractional bytes. come on.) + let precision = if thousands == 0 { 0 } else { precision(size) }; + + let formatted = format!( + "{:.precision$} {}", + size, + units[thousands], + precision = precision + ); (formatted, divisor) } fn precision(n: f64) -> usize { + // 1 > 1.00, 10 > 10.0, 100 > 100 2 - (n.log10().clamp(0.0, 2.0) as usize) }