-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contracts): add a test-utils crate
- Loading branch information
1 parent
fe89215
commit 8207d37
Showing
4 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "test-utils" | ||
categories = [] | ||
description = "Test utilities" | ||
edition.workspace = true | ||
keywords.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
wavm-shims = { path = "../../lib/wavm-shims" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Unit-testing utilities for Stylus contracts. | ||
use std::sync::{Mutex, MutexGuard}; | ||
|
||
pub use wavm_shims::*; | ||
|
||
/// A global static mutex. | ||
/// | ||
/// We use this for scenarios where concurrent mutation of storage is wanted. | ||
/// For example, when a test harness is running, this ensures each test | ||
/// accesses storage in an non-overlapping manner. | ||
/// | ||
/// See [`with_storage`]. | ||
pub static STORAGE_MUTEX: Mutex<()> = Mutex::new(()); | ||
|
||
/// Acquires access to storage. | ||
pub fn acquire_storage() -> MutexGuard<'static, ()> { | ||
STORAGE_MUTEX.lock().unwrap() | ||
} | ||
|
||
/// Decorates a closure by running it with exclusive access to storage. | ||
pub fn with_storage<C: Default>(closure: impl FnOnce(&mut C)) { | ||
let _lock = acquire_storage(); | ||
let mut contract = C::default(); | ||
closure(&mut contract); | ||
reset_storage(); | ||
} |