Skip to content

Commit

Permalink
Add some testing
Browse files Browse the repository at this point in the history
* restructure libs/binary so integ test can run
* add unit test for discover and clean
* add test data
* add integ level test example
* test "kondo -- --version" command
* create a test project and run kondo in it
  • Loading branch information
tompscanlan committed Aug 28, 2023
1 parent cc1ba07 commit b7b4452
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 142 deletions.
44 changes: 43 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion kondo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,26 @@ keywords = ["clean", "cleanup", "delete", "free"]
exclude = ["test_dir"]
edition = "2021"


[dependencies]
clap = { version = "4", features = ["derive"] }

# need recursive copy for testing
fs_extra = "1.3.0"

# need temporary test dirs
tempfile = "3.8.0"

[dependencies.kondo-lib]
path = "../kondo-lib"
version = "0.7"

[lib]
# rlib here is important for getting our integ test to run
# https://github.com/rust-lang/cargo/issues/6659
crate-type = ["rlib", "staticlib", "cdylib"]
bench = false

[[bin]]
name = "kondo"
test = true
bench = false
81 changes: 81 additions & 0 deletions kondo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use std::{
error::Error,
fmt,
num::ParseIntError};

#[derive(Debug)]
pub enum ParseAgeFilterError {
ParseIntError(ParseIntError),
InvalidUnit,
}

impl fmt::Display for ParseAgeFilterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseAgeFilterError::ParseIntError(e) => e.fmt(f),
ParseAgeFilterError::InvalidUnit => {
"invalid age unit, must be one of m, h, d, w, M, y".fmt(f)
}
}
}
}

impl From<ParseIntError> for ParseAgeFilterError {
fn from(e: ParseIntError) -> Self {
Self::ParseIntError(e)
}
}

impl Error for ParseAgeFilterError {}

pub fn parse_age_filter(age_filter: &str) -> Result<u64, ParseAgeFilterError> {
const MINUTE: u64 = 60;
const HOUR: u64 = MINUTE * 60;
const DAY: u64 = HOUR * 24;
const WEEK: u64 = DAY * 7;
const MONTH: u64 = WEEK * 4;
const YEAR: u64 = DAY * 365;

let (digit_end, unit) = age_filter
.char_indices()
.last()
.ok_or(ParseAgeFilterError::InvalidUnit)?;


let multiplier = match unit {
'm' => MINUTE,
'h' => HOUR,
'd' => DAY,
'w' => WEEK,
'M' => MONTH,
'y' => YEAR,
_ => return Err(ParseAgeFilterError::InvalidUnit),
};

let count = age_filter[..digit_end].parse::<u64>()?;
let seconds = count * multiplier;
Ok(seconds)
}

#[test]
fn test_age_filter_120s() {
let hours = parse_age_filter("2h").unwrap();
let minutes = parse_age_filter("120m").unwrap();

assert_eq!(minutes, hours);
}
#[test]
fn test_age_filter_10m() {
let res = parse_age_filter("10m");
let age_filter = res.unwrap();
assert_eq!(age_filter, (60*10));
}

#[ignore = "failing unexpectedly"]
#[test]
fn test_age_filter_year_months() {
let year = parse_age_filter("1y").unwrap();
let months = parse_age_filter("12M").unwrap();

assert_eq!(year, months);
}
Loading

0 comments on commit b7b4452

Please sign in to comment.