Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 25 additions & 34 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -18,31 +21,19 @@ 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"

fmt:
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:
Expand All @@ -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"

Expand All @@ -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
12 changes: 2 additions & 10 deletions .github/workflows/release-packaging.yaml.todo
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 }}
27 changes: 10 additions & 17 deletions .github/workflows/test.yml.todo
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@
# 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:
# os: [ubuntu-latest, macos-latest]
# 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
Expand All @@ -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
33 changes: 15 additions & 18 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod args;
mod execve;
mod help;
#[cfg(test)]
mod tests;

use std::{error::Error, fmt::Write, sync::Arc, time::Duration};

Expand Down Expand Up @@ -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)
}
74 changes: 74 additions & 0 deletions cli/src/tests/main.rs
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod main;
Loading