From 40d0bd059af25131d33cb6f5be4ff6cff3c250cf Mon Sep 17 00:00:00 2001 From: schaeff Date: Fri, 14 Feb 2020 12:08:14 +0100 Subject: [PATCH] fix resolver tests --- zokrates_fs_resolver/src/lib.rs | 45 ++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/zokrates_fs_resolver/src/lib.rs b/zokrates_fs_resolver/src/lib.rs index f7e3797a..bf7e894b 100644 --- a/zokrates_fs_resolver/src/lib.rs +++ b/zokrates_fs_resolver/src/lib.rs @@ -6,7 +6,9 @@ use std::path::{Component, PathBuf}; const ZOKRATES_HOME: &str = &"ZOKRATES_HOME"; +// path to the current file we're importing into type CurrentLocation = PathBuf; +// path we're importing from type ImportLocation<'a> = PathBuf; type SourceCode = String; @@ -47,20 +49,17 @@ mod tests { #[test] fn valid_path() { - use std::io::Write; - // create a source folder with a zok file let folder = tempfile::tempdir().unwrap(); let file_path = folder.path().join("bar.zok"); - let mut file = File::create(file_path).unwrap(); - writeln!(file, "some code").unwrap(); - let (_, next_location) = resolve(folder.path().to_path_buf(), "./bar".into()).unwrap(); - assert_eq!(next_location, folder.path()); + File::create(file_path.clone()).unwrap(); + let (_, next_location) = resolve(file_path.clone(), "./bar.zok".into()).unwrap(); + assert_eq!(next_location, file_path); } #[test] fn non_existing_file() { - let res = resolve("./src".into(), "./rubbish".into()); + let res = resolve("./source.zok".into(), "./rubbish".into()); assert!(res.is_err()); } @@ -72,13 +71,22 @@ mod tests { #[test] fn not_a_file() { - let res = resolve(".".into(), "./src/".into()); + // create a source folder with a zok file + let folder = tempfile::tempdir().unwrap(); + let dir_path = folder.path().join("dir"); + std::fs::create_dir(dir_path.clone()).unwrap(); + + let res = resolve(".".into(), "./dir/".into()); assert!(res.is_err()); } #[test] fn no_parent() { - let res = resolve(".".into(), ".".into()); + // create a source folder with a zok file + let folder = tempfile::tempdir().unwrap(); + let file_path = folder.path().join("foo.zok"); + File::create(file_path.clone()).unwrap(); + let res = resolve(file_path, ".".into()); assert!(res.is_err()); } @@ -95,13 +103,13 @@ mod tests { // create a user folder with a code file let source_folder = tempfile::tempdir().unwrap(); let file_path = source_folder.path().join("bar.zok"); - let mut file = File::create(file_path).unwrap(); + let mut file = File::create(file_path.clone()).unwrap(); writeln!(file, "").unwrap(); // assign HOME folder to ZOKRATES_HOME std::env::set_var(ZOKRATES_HOME, zokrates_home_folder.path()); - let result = resolve(source_folder.path().to_path_buf(), "./bar.zok".into()); + let result = resolve(file_path, "./bar.zok".into()); assert!(result.is_ok()); // the imported file should be the user's assert_eq!(result.unwrap().0, String::from("\n")); @@ -120,13 +128,13 @@ mod tests { // create a user folder with a code file let source_folder = tempfile::tempdir().unwrap(); let file_path = source_folder.path().join("bar.zok"); - let mut file = File::create(file_path).unwrap(); + let mut file = File::create(file_path.clone()).unwrap(); writeln!(file, "").unwrap(); // assign HOME folder to ZOKRATES_HOME std::env::set_var(ZOKRATES_HOME, zokrates_home_folder.path()); - let result = resolve(source_folder.path().to_path_buf(), "bar.zok".into()); + let result = resolve(file_path.clone(), "bar.zok".into()); assert!(result.is_ok()); // the imported file should be the user's assert_eq!(result.unwrap().0, String::from("\n")); @@ -143,7 +151,10 @@ mod tests { let mut file = File::create(file_path).unwrap(); writeln!(file, "").unwrap(); - let result = resolve(source_subfolder.path().to_path_buf(), "../bar.zok".into()); + let result = resolve( + source_subfolder.path().to_path_buf().join("foo.zok"), + "../bar.zok".into(), + ); assert!(result.is_ok()); // the imported file should be the user's assert_eq!(result.unwrap().0, String::from("\n")); @@ -162,14 +173,14 @@ mod tests { // assign HOME folder to ZOKRATES_HOME std::env::set_var(ZOKRATES_HOME, zokrates_home_folder.path()); - let result = resolve("/path/to/user/folder".into(), "./bar.zok".into()); + let result = resolve("/path/to/source.zok".into(), "./bar.zok".into()); assert!(result.is_err()); } #[test] fn fail_if_not_found_in_std() { std::env::set_var(ZOKRATES_HOME, ""); - let result = resolve("/path/to/source".into(), "bar.zok".into()); + let result = resolve("/path/to/source.zok".into(), "bar.zok".into()); assert!(result.is_err()); } @@ -177,6 +188,6 @@ mod tests { #[should_panic] fn panic_if_home_not_set() { std::env::remove_var(ZOKRATES_HOME); - let _ = resolve("/path/to/source".into(), "bar.zok".into()); + let _ = resolve("/path/to/source.zok".into(), "bar.zok".into()); } }