1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

fix resolver tests

This commit is contained in:
schaeff 2020-02-14 12:08:14 +01:00
parent df09835962
commit 40d0bd059a

View file

@ -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, "<user code>").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("<user code>\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, "<user code>").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("<stdlib code>\n"));
@ -143,7 +151,10 @@ mod tests {
let mut file = File::create(file_path).unwrap();
writeln!(file, "<user code>").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("<user code>\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());
}
}