Skip to content

Commit b7d8c90

Browse files
Merge pull request #4 from andrewdavidmackenzie/add_find_type_test
Increase code coverage by tests
2 parents 09eac3d + 883e0b8 commit b7d8c90

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

src/lib.rs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl fmt::Display for Simpath {
194194
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195195
write!(f, "Search Path: '{}', Directories: {{", self.name).unwrap();
196196
for dir in &self.dirs {
197-
write!(f, "'{}'", dir.display()).unwrap();
197+
write!(f, "'{}', ", dir.display()).unwrap();
198198

199199
}
200200
write!(f, "}}").unwrap();
@@ -208,6 +208,7 @@ mod test {
208208
use std::env;
209209
use std::fs;
210210
use std::io::Write;
211+
use FileType;
211212

212213
#[test]
213214
fn can_create() {
@@ -249,7 +250,58 @@ mod test {
249250
}
250251

251252
#[test]
252-
fn single_dir_from_env_variable() {
253+
fn find_dir_from_env_variable() {
254+
// Create a temp dir for test
255+
let temp_dir= tempdir::TempDir::new("simpath").unwrap().into_path();
256+
let mut parent_dir = temp_dir.clone();
257+
parent_dir.pop();
258+
259+
// Create a ENV path that includes that dir
260+
let var_name = "MyPathEnv";
261+
env::set_var(var_name, &parent_dir);
262+
263+
// create a simpath from the env var
264+
let path = Simpath::new(var_name);
265+
266+
// Check that simpath can find the temp_dir
267+
let temp_dir_name = format!("{}.{}",
268+
temp_dir.file_stem().unwrap().to_str().unwrap(),
269+
temp_dir.extension().unwrap().to_str().unwrap());
270+
assert!(path.find_type(&temp_dir_name, FileType::Directory).is_ok(),
271+
"Could not find the directory '.' in Path set from env var");
272+
273+
// clean-up
274+
fs::remove_dir_all(temp_dir).unwrap();
275+
}
276+
277+
#[test]
278+
fn find_file_from_env_variable() {
279+
// Create a temp dir for test
280+
let temp_dir= tempdir::TempDir::new("simpath").unwrap().into_path();
281+
282+
// Create a ENV path that includes that dir
283+
let var_name = "MyPathEnv";
284+
env::set_var(var_name, &temp_dir);
285+
286+
// create a simpath from the env var
287+
let path = Simpath::new(var_name);
288+
289+
// Create a file in the directory
290+
let temp_filename = "simpath.test";
291+
let temp_file_path = format!("{}/{}", temp_dir.display(), temp_filename);
292+
let mut file = fs::File::create(&temp_file_path).unwrap();
293+
file.write_all(b"test file contents").unwrap();
294+
295+
// Check that simpath can find it
296+
assert!(path.find_type(temp_filename, FileType::File).is_ok(),
297+
"Could not find the directory '.' in Path set from env var");
298+
299+
// clean-up
300+
fs::remove_dir_all(temp_dir).unwrap();
301+
}
302+
303+
#[test]
304+
fn find_any_from_env_variable() {
253305
// Create a temp dir for test
254306
let temp_dir= tempdir::TempDir::new("simpath").unwrap().into_path();
255307

@@ -267,10 +319,11 @@ mod test {
267319
file.write_all(b"test file contents").unwrap();
268320

269321
// Check that simpath can find it
270-
assert!(path.find(temp_filename).is_ok(), "Could not find the directory '.' in Path set from env var");
322+
assert!(path.find(temp_filename).is_ok(),
323+
"Could not find the directory '.' in Path set from env var");
271324

272325
// clean-up
273-
fs::remove_file(temp_file_path).unwrap();
326+
fs::remove_dir_all(temp_dir).unwrap();
274327
}
275328

276329
#[test]
@@ -291,4 +344,14 @@ mod test {
291344
assert!(path.contains("."));
292345
assert!(path.contains("/"));
293346
}
347+
348+
#[test]
349+
fn display_a_simpath() {
350+
let var_name = "MyPathEnv";
351+
env::set_var(var_name, ".:/");
352+
let mut path = Simpath::new("MyName");
353+
path.add_from_env_var(var_name);
354+
355+
println!("Simpath can be printed: {}", path);
356+
}
294357
}

0 commit comments

Comments
 (0)