Skip to content

Commit

Permalink
chore: use the new Katana runner proc macro (#2465)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 23, 2024
1 parent bb149b7 commit 60c1575
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 230 deletions.
8 changes: 3 additions & 5 deletions bin/sozo/tests/register_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ mod utils;
use camino::Utf8PathBuf;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::{copy_spawn_and_move_db, prepare_migration_with_world_and_seed};
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::RunnerCtx;
use scarb::compiler::Profile;
use scarb::ops;
use starknet::accounts::Account;
use starknet::core::types::{BlockId, BlockTag};
use utils::snapbox::get_snapbox;

#[tokio::test(flavor = "multi_thread")]
async fn reregister_models() {
#[katana_runner::test(db_dir = copy_spawn_and_move_db().as_str())]
async fn reregister_models(sequencer: &RunnerCtx) {
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

Expand All @@ -22,9 +23,6 @@ async fn reregister_models() {
let target_path =
ws.target_dir().path_existent().unwrap().join(ws.config().profile().to_string());

let seq_config = KatanaRunnerConfig::default().with_db_dir(copy_spawn_and_move_db().as_str());
let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");

let (strat, _) = prepare_migration_with_world_and_seed(
manifest_path,
target_path,
Expand Down
8 changes: 3 additions & 5 deletions bin/sozo/tests/test_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs;

use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::copy_spawn_and_move_db;
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::{KatanaRunner, RunnerCtx};
use scarb::compiler::Profile;
use starknet::accounts::Account;
use starknet::core::types::{BlockId, BlockTag};
Expand Down Expand Up @@ -51,14 +51,12 @@ async fn migrate_dry_run() {
}

#[tokio::test(flavor = "multi_thread")]
async fn test_migrate_then_upgrade() {
#[katana_runner::test(db_dir = copy_spawn_and_move_db().as_str())]
async fn test_migrate_then_upgrade(sequencer: &RunnerCtx) {
let setup = CompilerTestSetup::from_examples("../../crates/dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);
let tmp_dir = config.manifest_path().parent().unwrap();

let seq_config = KatanaRunnerConfig::default().with_db_dir(copy_spawn_and_move_db().as_str());
let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");

let mut account = sequencer.account(0);
account.set_block_id(BlockId::Tag(BlockTag::Pending));

Expand Down
8 changes: 3 additions & 5 deletions crates/dojo-world/src/contracts/model_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::{copy_spawn_and_move_db, prepare_migration_with_world_and_seed};
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::RunnerCtx;
use scarb::compiler::Profile;
use starknet::accounts::ConnectedAccount;
use starknet::macros::felt;
Expand All @@ -12,10 +12,8 @@ use crate::contracts::model::ModelReader;
use crate::contracts::world::WorldContractReader;

#[tokio::test(flavor = "multi_thread")]
async fn test_model() {
let seq_config = KatanaRunnerConfig::default().with_db_dir(copy_spawn_and_move_db().as_str());
let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");

#[katana_runner::test(db_dir = copy_spawn_and_move_db().as_str())]
async fn test_model(sequencer: &RunnerCtx) {
let account = sequencer.account(0);
let provider = account.provider();

Expand Down
8 changes: 3 additions & 5 deletions crates/dojo-world/src/contracts/world_test.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::{copy_spawn_and_move_db, prepare_migration_with_world_and_seed};
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::RunnerCtx;
use scarb::compiler::Profile;
use starknet::accounts::ConnectedAccount;
use starknet::core::types::{BlockId, BlockTag};

use super::WorldContractReader;

#[tokio::test(flavor = "multi_thread")]
async fn test_world_contract_reader() {
#[katana_runner::test(db_dir = copy_spawn_and_move_db().as_str())]
async fn test_world_contract_reader(sequencer: &RunnerCtx) {
let setup = CompilerTestSetup::from_examples("../dojo-core", "../../examples/");
let config = setup.build_test_config("spawn-and-move", Profile::DEV);

let manifest_dir = config.manifest_path().parent().unwrap();
let target_dir = manifest_dir.join("target").join("dev");

let seq_config = KatanaRunnerConfig::default().with_db_dir(copy_spawn_and_move_db().as_str());
let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");

let mut account = sequencer.account(0);
account.set_block_id(BlockId::Tag(BlockTag::Pending));

Expand Down
9 changes: 3 additions & 6 deletions crates/dojo-world/src/manifest/manifest_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use camino::Utf8PathBuf;
use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_test_utils::migration::{copy_spawn_and_move_db, prepare_migration_with_world_and_seed};
use dojo_test_utils::rpc::MockJsonRpcTransport;
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::RunnerCtx;
use scarb::compiler::Profile;
use serde_json::json;
use starknet::accounts::ConnectedAccount;
Expand Down Expand Up @@ -285,11 +285,8 @@ fn events_without_block_number_arent_parsed() {
similar_asserts::assert_eq!(actual_contracts, expected_contracts);
}

#[test]
fn fetch_remote_manifest() {
let seq_config = KatanaRunnerConfig::default().with_db_dir(copy_spawn_and_move_db().as_str());
let sequencer = KatanaRunner::new_with_config(seq_config).expect("Failed to start runner.");

#[katana_runner::test(db_dir = copy_spawn_and_move_db().as_str())]
fn fetch_remote_manifest(sequencer: &RunnerCtx) {
let account = sequencer.account(0);
let provider = account.provider();

Expand Down
58 changes: 20 additions & 38 deletions crates/katana/runner/macro/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ use crate::utils::{parse_bool, parse_int, parse_path, parse_string};

pub const DEFAULT_ERROR_CONFIG: Configuration = Configuration::new(false);

/// Partial configuration for extracting the individual configuration values from the attribute
/// arguments.
pub struct Configuration {
pub crate_name: Option<syn::Path>,
pub dev: bool,
pub is_test: bool,
pub accounts: Option<u16>,
pub fee: Option<bool>,
pub validation: Option<bool>,
pub db_dir: Option<String>,
pub block_time: Option<u64>,
pub log_path: Option<syn::Path>,
pub accounts: Option<syn::Expr>,
pub fee: Option<syn::Expr>,
pub validation: Option<syn::Expr>,
pub db_dir: Option<syn::Expr>,
pub block_time: Option<syn::Expr>,
pub log_path: Option<syn::Expr>,
}

impl Configuration {
Expand Down Expand Up @@ -48,77 +46,63 @@ impl Configuration {

let name_path = parse_path(name, span, "crate")?;
self.crate_name = Some(name_path);

Ok(())
}

fn set_db_dir(&mut self, db_dir: syn::Lit, span: proc_macro2::Span) -> Result<(), syn::Error> {
fn set_db_dir(&mut self, db_dir: syn::Expr, span: proc_macro2::Span) -> Result<(), syn::Error> {
if self.db_dir.is_some() {
return Err(syn::Error::new(span, "`db_dir` set multiple times."));
}

let db_dir = parse_string(db_dir, span, "db_dir")?;
self.db_dir = Some(db_dir);

Ok(())
}

fn set_fee(&mut self, fee: syn::Lit, span: proc_macro2::Span) -> Result<(), syn::Error> {
fn set_fee(&mut self, fee: syn::Expr, span: proc_macro2::Span) -> Result<(), syn::Error> {
if self.fee.is_some() {
return Err(syn::Error::new(span, "`fee` set multiple times."));
}

let fee = parse_bool(fee, span, "fee")?;
self.fee = Some(fee);

Ok(())
}

fn set_validation(
&mut self,
validation: syn::Lit,
validation: syn::Expr,
span: proc_macro2::Span,
) -> Result<(), syn::Error> {
if self.validation.is_some() {
return Err(syn::Error::new(span, "`validation` set multiple times."));
}

let validation = parse_bool(validation, span, "validation")?;
self.validation = Some(validation);

Ok(())
}

fn set_block_time(
&mut self,
block_time: syn::Lit,
block_time: syn::Expr,
span: proc_macro2::Span,
) -> Result<(), syn::Error> {
if self.block_time.is_some() {
return Err(syn::Error::new(span, "`block_time` set multiple times."));
}

let block_time = parse_int(block_time, span, "block_time")? as u64;
self.block_time = Some(block_time);

Ok(())
}

fn set_accounts(
&mut self,
accounts: syn::Lit,
accounts: syn::Expr,
span: proc_macro2::Span,
) -> Result<(), syn::Error> {
if self.accounts.is_some() {
return Err(syn::Error::new(span, "`accounts` set multiple times."));
}

let int = parse_int(accounts, span, "accounts")?;
let accounts = u16::try_from(int)
.map_err(|_| syn::Error::new(span, "`accounts` must be a valid u16."))?;

self.accounts = Some(accounts);

Ok(())
}
}
Expand Down Expand Up @@ -168,30 +152,28 @@ pub fn build_config(
.to_string()
.to_lowercase();

// the value of the attribute
let lit = match &namevalue.value {
syn::Expr::Lit(syn::ExprLit { lit, .. }) => lit,
expr => return Err(syn::Error::new_spanned(expr, "Must be a literal")),
};

// the ident of the attribute
let ident = ident.as_str();
let arg = RunnerArg::from_str(ident)
.map_err(|err| syn::Error::new_spanned(&namevalue, err))?;

let expr = &namevalue.value;

match arg {
RunnerArg::BlockTime => {
config.set_block_time(lit.clone(), Spanned::span(lit))?
config.set_block_time(expr.clone(), Spanned::span(&namevalue))?
}
RunnerArg::Validation => {
config.set_validation(lit.clone(), Spanned::span(lit))?
config.set_validation(expr.clone(), Spanned::span(&namevalue))?
}
RunnerArg::Accounts => {
config.set_accounts(lit.clone(), Spanned::span(lit))?;
config.set_accounts(expr.clone(), Spanned::span(&namevalue))?;
}
RunnerArg::DbDir => {
config.set_db_dir(expr.clone(), Spanned::span(&namevalue))?
}

RunnerArg::Fee => config.set_fee(lit.clone(), Spanned::span(lit))?,
RunnerArg::DbDir => config.set_db_dir(lit.clone(), Spanned::span(lit))?,
RunnerArg::Fee => config.set_fee(expr.clone(), Spanned::span(&namevalue))?,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/katana/runner/macro/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn parse_knobs(input: ItemFn, is_test: bool, config: Configuration) -> Token
}

if let Some(value) = config.db_dir {
cfg = quote_spanned! (last_stmt_start_span=> #cfg db_dir: Some(std::path::PathBuf::from(#value)), );
cfg = quote_spanned! (last_stmt_start_span=> #cfg db_dir: Some(core::str::FromStr::from_str(#value).expect("valid path")), );
}

if let Some(value) = config.accounts {
Expand Down
2 changes: 1 addition & 1 deletion crates/katana/runner/tests/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn with_return(_: &RunnerCtx) -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

#[tokio::test]
#[tokio::test(flavor = "current_thread")]
#[katana_runner::test]
async fn with_async(ctx: &RunnerCtx) -> Result<(), Box<dyn std::error::Error>> {
let provider = ctx.provider();
Expand Down
41 changes: 13 additions & 28 deletions crates/sozo/ops/src/tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dojo_test_utils::migration::copy_spawn_and_move_db;
use dojo_utils::TxnConfig;
use dojo_world::contracts::naming::compute_selector_from_tag;
use dojo_world::contracts::world::WorldContract;
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use katana_runner::RunnerCtx;
use scarb_ui::{OutputFormat, Ui, Verbosity};
use starknet::accounts::{Account, ConnectedAccount};
use starknet::core::types::Felt;
Expand Down Expand Up @@ -49,13 +49,9 @@ fn get_resource_owners(owner: Felt) -> [ResourceOwner; 2] {
}

#[tokio::test(flavor = "multi_thread")]
async fn auth_grant_writer_ok() {
let config = KatanaRunnerConfig { n_accounts: 10, ..Default::default() }
.with_db_dir(copy_spawn_and_move_db().as_str());

let sequencer = KatanaRunner::new_with_config(config).expect("Failed to start runner.");

let world = setup::setup_with_world(&sequencer).await.unwrap();
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn auth_grant_writer_ok(sequencer: &RunnerCtx) {
let world = setup::setup_with_world(sequencer).await.unwrap();

// Overlays already have the writer set up. But running again to ensure we don't
// actually revert something with this call.
Expand All @@ -77,13 +73,9 @@ async fn auth_grant_writer_ok() {
}

#[tokio::test(flavor = "multi_thread")]
async fn auth_revoke_writer_ok() {
let config = KatanaRunnerConfig { n_accounts: 10, ..Default::default() }
.with_db_dir(copy_spawn_and_move_db().as_str());

let sequencer = KatanaRunner::new_with_config(config).expect("Failed to start runner.");

let world = setup::setup_with_world(&sequencer).await.unwrap();
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn auth_revoke_writer_ok(sequencer: &RunnerCtx) {
let world = setup::setup_with_world(sequencer).await.unwrap();

auth::grant_writer(
&Ui::new(Verbosity::Normal, OutputFormat::Text),
Expand Down Expand Up @@ -116,17 +108,14 @@ async fn auth_revoke_writer_ok() {
}

#[tokio::test(flavor = "multi_thread")]
async fn auth_grant_owner_ok() {
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn auth_grant_owner_ok(sequencer: &RunnerCtx) {
let move_model_selector = compute_selector_from_tag(MOVE_MODEL_TAG);
let position_model_selector = compute_selector_from_tag(POSITION_MODEL_TAG);

let config = KatanaRunnerConfig { n_accounts: 10, ..Default::default() }
.with_db_dir(copy_spawn_and_move_db().as_str());

let sequencer = KatanaRunner::new_with_config(config).expect("Failed to start runner.");
println!("sequencer logs: {:?}", sequencer.log_file_path());

let world = setup::setup_with_world(&sequencer).await.unwrap();
let world = setup::setup_with_world(sequencer).await.unwrap();

let default_account = sequencer.account(0).address();
let other_account = sequencer.account(1).address();
Expand Down Expand Up @@ -155,16 +144,12 @@ async fn auth_grant_owner_ok() {
}

#[tokio::test(flavor = "multi_thread")]
async fn auth_revoke_owner_ok() {
#[katana_runner::test(accounts = 10, db_dir = copy_spawn_and_move_db().as_str())]
async fn auth_revoke_owner_ok(sequencer: &RunnerCtx) {
let move_model_selector = compute_selector_from_tag(MOVE_MODEL_TAG);
let position_model_selector = compute_selector_from_tag(POSITION_MODEL_TAG);

let config = KatanaRunnerConfig { n_accounts: 10, ..Default::default() }
.with_db_dir(copy_spawn_and_move_db().as_str());

let sequencer = KatanaRunner::new_with_config(config).expect("Failed to start runner.");

let world = setup::setup_with_world(&sequencer).await.unwrap();
let world = setup::setup_with_world(sequencer).await.unwrap();

let default_account = sequencer.account(0).address();

Expand Down
Loading

0 comments on commit 60c1575

Please sign in to comment.