diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2e2688f..af1219d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -5,11 +5,14 @@ on: - main paths: - "**/*.rs" - - Cargo.toml + - "**/Cargo.toml" - Cargo.lock name: ci·rs +concurrency: + group: ci·rs·${{ github.ref}} + jobs: check: strategy: @@ -18,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" @@ -33,16 +30,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - run: rustup component add rustfmt - - uses: actions-rs/cargo@v1 + - uses: dtolnay/rust-toolchain@stable with: - command: fmt - args: --all -- --check + components: rustfmt + - run: cargo fmt --all -- --check clippy: strategy: @@ -51,15 +42,22 @@ 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" + + test: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@stable + - run: cargo test --all-features env: RUSTFLAGS: "-D warnings" @@ -71,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 diff --git a/cli/src/main.rs b/cli/src/main.rs index 143d03e..f3b32ab 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}; @@ -308,32 +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 > 1024.0 && i < units.len() - 1 { - size /= 1024.0; - i += 1; - divisor *= 1024; - } + + // 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[i], - precision = precision(size) + units[thousands], + precision = precision ); (formatted, divisor) } fn precision(n: f64) -> usize { - if n < 10.0 { - 2 - } else if n < 100.0 { - 1 - } else { - 0 - } + // 1 > 1.00, 10 > 10.0, 100 > 100 + 2 - (n.log10().clamp(0.0, 2.0) as usize) } 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;