adds tests for outputfile

This commit is contained in:
damirka 2021-04-05 16:08:54 +03:00
parent ed8ff4468c
commit 044dc96e58
4 changed files with 27 additions and 16 deletions

1
Cargo.lock generated
View File

@ -1269,6 +1269,7 @@ dependencies = [
"snarkvm-gadgets",
"snarkvm-r1cs",
"snarkvm-utilities",
"tempfile",
"thiserror",
"tracing",
]

View File

@ -114,6 +114,9 @@ default-features = false
version = "0.2.1"
default-features = false
[dev-dependencies.tempfile]
version = "3.0.4"
[features]
default = [ ]
ci_skip = [ "leo-ast/ci_skip" ]

View File

@ -21,9 +21,6 @@ pub enum OutputFileError {
#[error("{}: {}", _0, _1)]
Crate(&'static str, String),
#[error("creating: {}", _0)]
Creating(io::Error),
#[error("Cannot read from the provided file path - {:?}", _0)]
FileReadError(PathBuf),

View File

@ -42,19 +42,6 @@ impl OutputFile {
}
}
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
/// Reads the output register variables from the given file path if it exists.
pub fn read_from(&self, path: &Path) -> Result<String, OutputFileError> {
let path = self.setup_file_path(path);
let output = fs::read_to_string(&path).map_err(|_| OutputFileError::FileReadError(path.into_owned()))?;
Ok(output)
}
/// Writes output to a file.
pub fn write(&self, path: &Path, bytes: &[u8]) -> Result<(), OutputFileError> {
// create output file
@ -88,3 +75,26 @@ impl OutputFile {
path
}
}
#[cfg(test)]
mod test_output_file {
use crate::{OutputFile, OUTPUTS_DIRECTORY_NAME};
use std::{error::Error, fs};
#[test]
fn test_all() -> Result<(), Box<dyn Error>> {
let dir = tempfile::tempdir()?;
let file = OutputFile::new("test");
let path = dir.path();
assert!(file.write(path, Default::default()).is_err());
assert!(file.remove(path)? == false);
fs::create_dir(dir.path().join(OUTPUTS_DIRECTORY_NAME))?;
assert!(file.write(path, Default::default()).is_ok());
assert!(file.remove(path)? == true);
Ok(())
}
}