Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
tests: fix test functions according to the recently added cache feature
Browse files Browse the repository at this point in the history
  • Loading branch information
haroune-mohammedi authored and tdelabro committed Feb 20, 2023
1 parent c197a4b commit 0074d66
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
6 changes: 2 additions & 4 deletions src/cli/commands/test/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ pub fn run_single_test(
test_path: &PathBuf,
max_steps: u64,
) -> Result<TestResult, TestCommandError> {
let (_, path_to_compiled, _) = compile_and_list_entrypoints(test_path.to_owned())?;
let file = File::open(path_to_compiled).unwrap();
let reader = BufReader::new(file);
let program_json = deserialize_program_json(reader)?;
let (_, program_json, _) = compile_and_list_entrypoints(test_path.to_owned())?;
let program_json = serde_json::from_value(program_json)?;

test_single_entrypoint(
program_json,
Expand Down
38 changes: 16 additions & 22 deletions src/compile/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,10 @@ pub struct CompileCacheItem {
pub program_json: Value,
}

pub fn read_cache(path: &PathBuf) -> Result<Cache, CacheError> {
let file_content = read_to_string(path)
.map_err(|e| CacheError::ReadFile(path.as_path().display().to_string(), e))?;
let data = serde_json::from_str::<Cache>(file_content.as_str())
.map_err(|e| CacheError::DeserializeError(file_content, e))?;
Ok(data)
impl PartialEq for CompileCacheItem {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}

impl CompileCacheItem {
Expand All @@ -103,9 +101,7 @@ impl CompileCacheItem {
}
}

pub fn get_cache_path(path_to_cairo_file: &PathBuf) -> Result<PathBuf, CacheError> {
let path_to_cache_dir = dirs::cache_dir().ok_or(CacheDirNotSupported)?;

pub fn get_compile_cache_path(path_to_cairo_file: &PathBuf) -> Result<PathBuf, CacheError> {
// Retrieve only the file name to create a clean compiled file name.
let filename = path_to_cairo_file.file_stem().and_then(|f| f.to_str()).ok_or_else(|| {
CacheError::StemlessFile(path_to_cairo_file.as_path().display().to_string())
Expand All @@ -114,8 +110,7 @@ pub fn get_cache_path(path_to_cairo_file: &PathBuf) -> Result<PathBuf, CacheErro
let path_hash = hash(path_to_cairo_file);

// Build path to save the compiled file
let mut cache_path = PathBuf::new();
cache_path.push(&path_to_cache_dir);
let mut cache_path = cache_dir()?;
cache_path.push(CAIRO_FOUNDRY_COMPILED_CONTRACT_DIR);

std::fs::create_dir_all(&cache_path)
Expand All @@ -139,17 +134,16 @@ fn is_valid_cairo_contract(contract_path: &PathBuf) -> Result<(), CacheError> {
Ok(())
}

// fn get_cache_path(contract_path: &PathBuf, root_dir: &PathBuf) -> Result<PathBuf, CacheError> {
// // check if contract_path have .cairo extension
// is_valid_cairo_contract(contract_path)?;
// let cache_dir = dirs::cache_dir().ok_or(CacheError::CacheDirNotSupportedError)?;
// // get relative dir path from root_dir
// let contract_relative_path = contract_path.strip_prefix(root_dir)?;

// let mut cache_path = cache_dir.join(CAIRO_FOUNDRY_CACHE_DIR).join(contract_relative_path);
// cache_path.set_extension("json");
// Ok(cache_path)
// }
fn get_cache_path(contract_path: &PathBuf, root_dir: &PathBuf) -> Result<PathBuf, CacheError> {
// check if contract_path have .cairo extension
is_valid_cairo_contract(contract_path)?;
// get relative dir path from root_dir
let contract_relative_path = contract_path.strip_prefix(root_dir)?;

let mut cache_path = cache_dir()?.join(CAIRO_FOUNDRY_CACHE_DIR).join(contract_relative_path);
cache_path.set_extension("json");
Ok(cache_path)
}

fn get_compiled_contract_path(
contract_path: &PathBuf,
Expand Down
27 changes: 13 additions & 14 deletions src/compile/cache/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@ use std::path::PathBuf;
use assert_matches::assert_matches;

use super::{
cache_dir, get_cache_path, get_compiled_contract_path, read_cache_file, Cache, CacheError,
cache_dir, get_cache_path, get_compiled_contract_path, CacheError, CompileCacheItem,
CAIRO_FOUNDRY_CACHE_DIR, CAIRO_FOUNDRY_COMPILED_CONTRACT_DIR,
};

const CAIRO_FOUNDRY_CACHE_DIR: &str = "cairo-foundry-cache";
const CAIRO_FOUNDRY_COMPILED_CONTRACT_DIR: &str = "compiled-cairo-files";

#[test]
fn read_cache_with_valid_input() {
let current_dir = std::env::current_dir().unwrap();
let cache_path = current_dir.join("test_cache_files").join("test_valid_program.json");
let cache = read_cache_file(&cache_path).unwrap();
let cache = CompileCacheItem::read(&cache_path).unwrap();

let expected = Cache {
contract_path: PathBuf::from("test_cairo_contracts/test_valid_program.cairo"),
compiled_contract_path: PathBuf::from("test_compiled_contracts/test_valid_program.json"),
hash: "0x0000000000000000000000000000000000000000000000000000000000000001".to_string(),
let expected = CompileCacheItem {
program_json: "".into(),
hash: 10,
};

assert_eq!(cache, expected);
Expand All @@ -28,20 +25,20 @@ fn read_cache_with_valid_input() {
fn read_non_existing_cache_file() {
let current_dir = std::env::current_dir().unwrap();
let cache_path = current_dir.join("test_cache_files").join("non_existing_cache.json");
let result = read_cache_file(&cache_path);
assert_matches!(result, Err(CacheError::FileNotFoundError(_)));
let result = CompileCacheItem::read(&cache_path);
assert_matches!(result, Err(CacheError::ReadFile(_, _)));
}

#[test]
fn read_existing_cache_with_incorrect_field() {
let current_dir = std::env::current_dir().unwrap();
let cache_path = current_dir.join("test_cache_files").join("test_invalid_structure.json");
let result = read_cache_file(&cache_path);
assert_matches!(result, Err(CacheError::DeserializeError(_)));
let result = CompileCacheItem::read(&cache_path);
assert_matches!(result, Err(CacheError::DeserializeError(_, _)));
}

#[test]
fn get_cache_path_for_valid_contract_path() {
fn get_cache_path_for_valid_contract_path() -> Result<(), CacheError> {
let current_dir = std::env::current_dir().unwrap();
let root_dir = current_dir.join("test_cairo_contracts");

Expand Down Expand Up @@ -89,6 +86,8 @@ fn get_cache_path_for_valid_contract_path() {
.join("test_nested_dir")
.join("test_valid_program_in_cairo_contracts_dir.json");
assert_eq!(cache_path, expected);

Ok(())
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::{fmt::Debug, io, path::PathBuf, process::Command};
use thiserror::Error;
use which::{which, Error as WhichError};

use self::cache::{get_cache_path, hash_file, CacheError, CompileCacheItem};
use self::cache::{get_compile_cache_path, hash_file, CacheError, CompileCacheItem};

mod cache;
pub mod cache;

const CAIRO_COMPILE_BINARY: &str = "cairo-compile";

Expand Down Expand Up @@ -56,7 +56,7 @@ pub enum Error {
/// # }
/// ```
pub fn compile(path_to_cairo_file: &PathBuf) -> Result<Value, Error> {
let cache_path = get_cache_path(path_to_cairo_file)?;
let cache_path = get_compile_cache_path(path_to_cairo_file)?;

let hash = hash_file(&path_to_cairo_file)?;

Expand Down
7 changes: 3 additions & 4 deletions test_cache_files/test_valid_program.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"contract_path": "test_cairo_contracts/test_valid_program.cairo",
"compiled_contract_path": "test_compiled_contracts/test_valid_program.json",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001"
}
"hash": 10,
"program_json": ""
}

0 comments on commit 0074d66

Please sign in to comment.