From 044dc96e58a4e9a71aa63a23f7f310a20072acf4 Mon Sep 17 00:00:00 2001 From: damirka Date: Mon, 5 Apr 2021 16:08:54 +0300 Subject: [PATCH] adds tests for outputfile --- Cargo.lock | 1 + compiler/Cargo.toml | 3 +++ compiler/src/errors/output_file.rs | 3 --- compiler/src/output/output_file.rs | 36 +++++++++++++++++++----------- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 520a984b45..caef39fb6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1269,6 +1269,7 @@ dependencies = [ "snarkvm-gadgets", "snarkvm-r1cs", "snarkvm-utilities", + "tempfile", "thiserror", "tracing", ] diff --git a/compiler/Cargo.toml b/compiler/Cargo.toml index 1c7a47cea4..01c90ad6aa 100644 --- a/compiler/Cargo.toml +++ b/compiler/Cargo.toml @@ -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" ] diff --git a/compiler/src/errors/output_file.rs b/compiler/src/errors/output_file.rs index c8b18185d6..014327c3ad 100644 --- a/compiler/src/errors/output_file.rs +++ b/compiler/src/errors/output_file.rs @@ -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), diff --git a/compiler/src/output/output_file.rs b/compiler/src/output/output_file.rs index f9dd6eeda1..48d1b8bac4 100644 --- a/compiler/src/output/output_file.rs +++ b/compiler/src/output/output_file.rs @@ -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 { - 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> { + 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(()) + } +}