Skip to content

Commit

Permalink
move creating test project to lib
Browse files Browse the repository at this point in the history
  • Loading branch information
tompscanlan committed Aug 31, 2023
1 parent 312bdd7 commit c7912e2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
31 changes: 30 additions & 1 deletion kondo-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ mod lib_test;
use std::{
borrow::Cow,
error::{self, Error},
fs, path,
fs,
io::Write,
path,
time::SystemTime,
};

Expand Down Expand Up @@ -481,6 +483,33 @@ pub fn path_canonicalise(
Ok(base.join(tail).canonicalize()?)
}
}
// 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();

// make a new root in the tmp dir
let project_dir = tmp_dir.path().join(name);
std::fs::create_dir(&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();

tmp_dir
}

#[cfg(test)]
mod tests {
Expand Down
4 changes: 2 additions & 2 deletions kondo-lib/src/lib_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod test {
use std::path::PathBuf;
use tempfile;

use crate::{ScanOptions, Project, ProjectType};
use crate::{Project, ProjectType, ScanOptions};

#[test]
fn test_clean() {
Expand Down Expand Up @@ -49,7 +49,6 @@ mod test {
tempdir.close().unwrap();
}


// copy the test_data dir to a ephemeral temp dir for destructive testing
fn get_copy_of_test_data_as_temp_dir() -> tempfile::TempDir {
extern crate fs_extra;
Expand All @@ -68,4 +67,5 @@ mod test {

return tmp_dir;
}

}
2 changes: 1 addition & 1 deletion kondo/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ pub fn bin() -> PathBuf {
panic!("kondo binary not found at {:?}", path);
}
return path;
}
}
33 changes: 2 additions & 31 deletions kondo/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod common;
use std::{io::Write, process::Command};

use kondo;
use std::process::Command;

// parse age filter is marked public. Can we use it?
#[test]
Expand All @@ -27,7 +26,7 @@ fn test_version() {
#[ignore = "in progress"]
#[test]
fn test_can_run_cargo() {
let tmpdir = create_fake_python_project("testing".to_string());
let tmpdir = kondo_lib::create_fake_python_project("testing".to_string());
let bin = common::bin();

println!("tmpdr: {:?}", tmpdir.path());
Expand Down Expand Up @@ -59,31 +58,3 @@ fn test_can_run_cargo() {
// clean up
tmpdir.close().unwrap();
}

// Given a name, create a new simulated python project in a safe to delete directry
fn create_fake_python_project(name: String) -> tempfile::TempDir {
// Make a new project in a temporary directory
let tmp_dir = tempfile::tempdir().unwrap();

// make a new root in the tmp dir
let project_dir = tmp_dir.path().join(&name);
std::fs::create_dir(&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();

return tmp_dir;
}

0 comments on commit c7912e2

Please sign in to comment.