-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* top level test_data dir has sub dirs for scenarios, two have been created (single cargo, two python) * restructure kondo/main into kondo/main and lib so integ test can run * unit test: kondo/discover and kondo-lib/clean * integration test: cli run of "kondo -- --version" command * infrastructure for creating a test project to run kondo in destructivly
- Loading branch information
1 parent
69c153b
commit 9af9cf3
Showing
26 changed files
with
758 additions
and
144 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod lib_test; | ||
|
||
use std::{ | ||
borrow::Cow, | ||
error::{self, Error}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#[cfg(test)] | ||
mod test { | ||
|
||
use crate::{Project, ProjectType, ScanOptions}; | ||
use std::{io::Write, path::PathBuf}; | ||
use tempfile; | ||
|
||
// Given test data, clean should remove some files | ||
#[test] | ||
fn test_clean() { | ||
let scan_options: ScanOptions = ScanOptions { | ||
follow_symlinks: false, | ||
same_file_system: true, | ||
}; | ||
|
||
let tempdir = create_fake_python_project("test_data".to_string()); | ||
let path = tempdir.path().join("test_data"); | ||
|
||
println!("path: {:?}", path); | ||
println!("tempdir: {:?}", tempdir.path()); | ||
|
||
let project_a = Project { | ||
path: path, | ||
project_type: ProjectType::Python, | ||
}; | ||
|
||
assert!( | ||
project_a.size(&scan_options) > 0, | ||
"size of project ought to be greater than 0" | ||
); | ||
assert!(project_a.path.exists(), "project ought to exist"); | ||
|
||
// Run clean and check before and after that file exists and is deleted | ||
assert!( | ||
project_a.path.join("__pycache__/cache.data").exists(), | ||
"cache file ought to exist" | ||
); | ||
Project::clean(&project_a); | ||
assert!( | ||
!project_a.path.join("__pycache__/cache.data").exists(), | ||
"cache file should have been deleted" | ||
); | ||
|
||
assert!(project_a.path.exists(), "project ought to still exist"); | ||
|
||
// clean up | ||
tempdir.close().unwrap(); | ||
} | ||
|
||
// #[ignore = "this is probably "] | ||
#[test] | ||
fn test_clean_nested_python_projects() { | ||
// make alpha project | ||
let alpha_tmp_dir = create_fake_python_project("alpha".to_string()); | ||
|
||
// inside of alpha, make nested project | ||
let project_nested_dir = create_fake_python_project_in_dir( | ||
alpha_tmp_dir.path().clone().to_path_buf(), | ||
Check warning on line 58 in kondo-lib/src/lib_test.rs GitHub Actions / Test using stable
|
||
"nested".to_string(), | ||
); | ||
|
||
// Given alpha project | ||
let project_alpha = Project { | ||
path: alpha_tmp_dir.into_path(), | ||
project_type: ProjectType::Python, | ||
}; | ||
// and nested project | ||
let project_nested = Project { | ||
path: project_nested_dir.clone(), | ||
project_type: ProjectType::Python, | ||
}; | ||
|
||
// Clean! | ||
Project::clean(&project_alpha); | ||
Project::clean(&project_nested); | ||
// Both project dirs exist | ||
assert!( | ||
project_alpha.path.exists(), | ||
"project alpha ought to still exist" | ||
); | ||
assert!( | ||
project_nested_dir.exists(), | ||
"nested project ought to still exist" | ||
); | ||
|
||
// Both cache files are gone | ||
assert!( | ||
!project_alpha.path.join("__pycache__/cache.data").exists(), | ||
"cache file of alpha should have been deleted" | ||
); | ||
assert!( | ||
!project_nested_dir.join("__pycache__/cache.data").exists(), | ||
"cache file of nested project should have been deleted" | ||
); | ||
} | ||
// TODO: this code is duplicated at konod/src/main.rs | ||
// Given a name, create a new simulated python project in a safe to delete directry | ||
pub fn create_fake_python_project(name: String) -> tempfile::TempDir { | ||
// Make a new project in a temporary directory | ||
let tmp_dir = tempfile::tempdir().unwrap(); | ||
create_fake_python_project_in_dir(tmp_dir.path().to_path_buf(), name); | ||
tmp_dir | ||
} | ||
|
||
pub fn create_fake_python_project_in_dir(dir: PathBuf, name: String) -> PathBuf { | ||
// make a new root in the dir | ||
let project_dir = dir.join(name); | ||
std::fs::create_dir_all(&project_dir).unwrap(); | ||
|
||
// Must have a directory to hold the project. | ||
let cache_dir = project_dir.join("__pycache__"); | ||
std::fs::create_dir(&cache_dir).unwrap(); | ||
|
||
// Must have data in the cache to delete | ||
let mut data_file = std::fs::File::create(cache_dir.join("cache.data")).unwrap(); | ||
data_file.write_all(b"#oodles of cache')\n").unwrap(); | ||
let mut data_file_b = std::fs::File::create(cache_dir.join("other.cache")).unwrap(); | ||
data_file_b.write_all(b"#oodles of cache')\n").unwrap(); | ||
|
||
// and a file of type .py to signal we're a python project | ||
let mut python_file = std::fs::File::create(project_dir.join("main.py")).unwrap(); | ||
python_file | ||
.write_all(b"#!/bin/python\n\nprint('Hello, world!')\n") | ||
.unwrap(); | ||
project_dir.to_path_buf() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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. BUG?"] | ||
#[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); | ||
} |
Oops, something went wrong.