Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple test. #98

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 45 additions & 1 deletion Cargo.lock

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

7 changes: 7 additions & 0 deletions kondo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ license = "MIT"
[dependencies]
ignore = "0.4.18"
walkdir = "2"

[dev-dependencies]
# need recursive copy for testing
fs_extra = "1.3.0"

# need temporary test dirs
tempfile = "3.8.0"
45 changes: 45 additions & 0 deletions kondo-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod lib_test;

use std::{
borrow::Cow,
error::{self, Error},
Expand Down Expand Up @@ -482,6 +484,8 @@ pub fn path_canonicalise(

#[cfg(test)]
mod tests {
use std::fs::create_dir_all;

use super::print_elapsed;

#[test]
Expand Down Expand Up @@ -522,4 +526,45 @@ mod tests {
assert_eq!(print_elapsed(2419200 * 25), "25 months ago");
assert_eq!(print_elapsed(2419200 * 48), "4 years ago");
}

#[test]
fn test_path_canonicalise_works_on_extant_tail() {
let str_tail = "idoexist".to_string();

let base = tempfile::tempdir().unwrap().into_path();
let expected = base.join(str_tail.clone());
let tail = std::path::PathBuf::from(str_tail.clone());

// must make sure the tail exists in base dir, or we'll error that it doesn't exist
create_dir_all(base.join(std::path::PathBuf::from(str_tail))).unwrap();

let actual = super::path_canonicalise(&base, tail).unwrap();
assert_eq!(actual, expected);
}

#[test]
#[should_panic]
fn path_canonicalise_fails_on_nonextant_tail() {
let str_tail = "idontexist".to_string();

let base = tempfile::tempdir().unwrap().into_path();
let tail = std::path::PathBuf::from(str_tail.clone());

// here tail has not been created

super::path_canonicalise(&base, tail).unwrap();
}
#[test]
fn path_canonicalise_works_on_absolute_dirs_that_dont_exist() {
let str_tail = "/idontexist".to_string();

let base = tempfile::tempdir().unwrap().into_path();
let expected = base.join(str_tail.clone());
let tail = std::path::PathBuf::from(str_tail.clone());

assert!(!tail.exists());

let actual = super::path_canonicalise(&base, tail).unwrap();
assert_eq!(actual, expected);
}
}
126 changes: 126 additions & 0 deletions kondo-lib/src/lib_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#[cfg(test)]
mod test {

use crate::{Project, ProjectType, ScanOptions};
use std::{io::Write, path::PathBuf};

// 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,
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(),
"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()
}
}
19 changes: 18 additions & 1 deletion kondo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,28 @@ keywords = ["clean", "cleanup", "delete", "free"]
exclude = ["test_dir"]
edition = "2021"


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

[dev-dependencies]
# need recursive copy for testing
fs_extra = "1.3.0"

# need temporary test dirs
tempfile = "3.8.0"
tompscanlan marked this conversation as resolved.
Show resolved Hide resolved

[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
tompscanlan marked this conversation as resolved.
Show resolved Hide resolved
Loading