diff --git a/Cargo.lock b/Cargo.lock index 2549aa6436..98c46e0f00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2940,20 +2940,6 @@ dependencies = [ "starknet 0.2.0 (git+https://github.com/xJonathanLEI/starknet-rs?branch=dev/jsonrpc_0_3_0)", ] -[[package]] -name = "dojo-test-runner" -version = "0.1.0" -dependencies = [ - "anyhow", - "cairo-lang-compiler", - "cairo-lang-test-runner", - "camino", - "clap", - "dojo-lang", - "scarb", - "semver 1.0.17", -] - [[package]] name = "dojo-test-utils" version = "0.1.0" @@ -7496,6 +7482,7 @@ dependencies = [ "cairo-lang-sierra", "cairo-lang-sierra-to-casm", "cairo-lang-starknet", + "cairo-lang-test-runner", "camino", "clap", "dojo-lang", diff --git a/Cargo.toml b/Cargo.toml index 5ea12154f5..53e9448be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ members = [ "crates/dojo-lang", "crates/dojo-language-server", "crates/dojo-world", - "crates/dojo-test-runner", "crates/dojo-test-utils", "crates/dojo-signers", "crates/katana", diff --git a/crates/dojo-test-runner/Cargo.toml b/crates/dojo-test-runner/Cargo.toml deleted file mode 100644 index 946d6a0814..0000000000 --- a/crates/dojo-test-runner/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "dojo-test-runner" -version.workspace = true -edition.workspace = true -repository.workspace = true -license-file.workspace = true -description = "Dojo tests runner. Used to run tests written in Cairo." - -[dependencies] -anyhow.workspace = true -camino.workspace = true -cairo-lang-compiler.workspace = true -cairo-lang-test-runner.workspace = true -clap.workspace = true -dojo-lang = { path = "../dojo-lang" } -scarb.workspace = true -semver.workspace = true - -[[bin]] -name = "dojo-test" -path = "src/cli.rs" diff --git a/crates/dojo-test-runner/README.md b/crates/dojo-test-runner/README.md deleted file mode 100644 index 520e5f28e9..0000000000 --- a/crates/dojo-test-runner/README.md +++ /dev/null @@ -1,31 +0,0 @@ -# Testing dojo files - -``` -cargo run --bin dojo-test -- -p /path/to/file.cairo -``` - -We can use this command to run Cairo level tests. - -# Example - -``` -#[test] -fn test_assert_true() { - // Asserts that true - assert(true, 'assert(true)'); -} - -#[test] -#[should_panic] -fn test_assert_false() { - assert(false, 'assert(false)'); -} -``` - -# Longer Example - -Longer example can be found at [Core Library Test](../../corelib/test.cairo). - -``` -cargo run --bin dojo-test -- -p corelib/ -``` diff --git a/crates/dojo-test-runner/src/compiler.rs b/crates/dojo-test-runner/src/compiler.rs deleted file mode 100644 index 0544296fc6..0000000000 --- a/crates/dojo-test-runner/src/compiler.rs +++ /dev/null @@ -1,42 +0,0 @@ -use anyhow::{bail, Result}; -use cairo_lang_compiler::db::RootDatabase; -use cairo_lang_compiler::diagnostics::DiagnosticsReporter; -use cairo_lang_test_runner::TestRunner; -use dojo_lang::compiler::collect_main_crate_ids; -use scarb::compiler::{CompilationUnit, Compiler}; -use scarb::core::Workspace; - -pub struct DojoTestCompiler; - -impl Compiler for DojoTestCompiler { - fn target_kind(&self) -> &str { - "dojo" - } - - fn compile( - &self, - unit: CompilationUnit, - db: &mut RootDatabase, - _: &Workspace<'_>, - ) -> Result<()> { - let main_crate_ids = collect_main_crate_ids(&unit, db); - - if DiagnosticsReporter::stderr().check(db) { - bail!("failed to compile"); - } - - let runner = TestRunner { - db: db.snapshot(), - main_crate_ids, - // TODO: Pass these in - filter: "".to_string(), - include_ignored: false, - ignored: false, - starknet: true, - }; - - runner.run()?; - - Ok(()) - } -} diff --git a/crates/sozo/Cargo.toml b/crates/sozo/Cargo.toml index c79a082c35..4d0755aed3 100644 --- a/crates/sozo/Cargo.toml +++ b/crates/sozo/Cargo.toml @@ -22,6 +22,7 @@ cairo-lang-project.workspace = true cairo-lang-sierra.workspace = true cairo-lang-sierra-to-casm.workspace = true cairo-lang-starknet.workspace = true +cairo-lang-test-runner.workspace = true scarb.workspace = true semver.workspace = true serde.workspace = true diff --git a/crates/sozo/src/commands/mod.rs b/crates/sozo/src/commands/mod.rs index bf5526581c..6c21d1360e 100644 --- a/crates/sozo/src/commands/mod.rs +++ b/crates/sozo/src/commands/mod.rs @@ -1,28 +1,26 @@ -use clap::{Args, Parser, Subcommand}; +use clap::{Parser, Subcommand}; use self::build::BuildArgs; use self::init::InitArgs; use self::migrate::MigrateArgs; +use self::test::TestArgs; pub(crate) mod build; pub(crate) mod init; pub(crate) mod migrate; +pub(crate) mod test; #[derive(Subcommand)] pub enum Commands { - #[command( - about = "Build the project's ECS, outputting smart contracts artifacts for deployment" - )] + #[command(about = "Build the world, generating the necessary artifacts for deployment")] Build(BuildArgs), #[command(about = "Initialize a new project")] Init(InitArgs), #[command(about = "Run a migration, declaring and deploying contracts as necessary to \ update the world")] Migrate(MigrateArgs), - #[command(about = "Generate rust contract bindings")] - Bind(BindArgs), - #[command(about = "Retrieve an entity's state by entity ID")] - Inspect(InspectArgs), + #[command(about = "Test the project's smart contracts")] + Test(TestArgs), } #[derive(Parser)] @@ -32,14 +30,3 @@ pub struct App { #[command(subcommand)] pub command: Commands, } - -#[derive(Args)] -pub struct BindArgs {} - -#[derive(Args)] -pub struct InspectArgs { - #[clap(short, long, help = "Entity ID to retrieve state for")] - id: String, - #[clap(short, long, help = "World address to retrieve entity state from")] - world_address: String, -} diff --git a/crates/dojo-test-runner/src/cli.rs b/crates/sozo/src/commands/test.rs similarity index 55% rename from crates/dojo-test-runner/src/cli.rs rename to crates/sozo/src/commands/test.rs index 42dde307cd..575ce321ee 100644 --- a/crates/dojo-test-runner/src/cli.rs +++ b/crates/sozo/src/commands/test.rs @@ -1,23 +1,22 @@ -//! Compiles and runs a Dojo project. +//! Compiles and runs tests for a Dojo project. use std::env::{self, current_dir}; +use anyhow::{bail, Result}; +use cairo_lang_compiler::db::RootDatabase; +use cairo_lang_compiler::diagnostics::DiagnosticsReporter; +use cairo_lang_test_runner::TestRunner; use camino::Utf8PathBuf; -use clap::Parser; -use compiler::DojoTestCompiler; +use clap::Args; +use dojo_lang::compiler::collect_main_crate_ids; use dojo_lang::plugin::CairoPluginRepository; -use scarb::compiler::CompilerRepository; -use scarb::core::Config; +use scarb::compiler::{CompilationUnit, Compiler, CompilerRepository}; +use scarb::core::{Config, Workspace}; use scarb::ops; use scarb::ui::Verbosity; -mod compiler; - -/// Command line args parser. -/// Exits with 0/1 if the input is formatted correctly/incorrectly. -#[derive(Parser, Debug)] -#[clap(version, verbatim_doc_comment)] -struct Args { +#[derive(Args)] +pub struct TestArgs { /// The path to compile and run its tests. path: Utf8PathBuf, /// The filter for the tests, running only tests containing the filter string. @@ -31,8 +30,7 @@ struct Args { ignored: bool, } -fn main() -> anyhow::Result<()> { - let args = Args::parse(); +pub fn run(args: TestArgs) -> anyhow::Result<()> { let source_dir = if args.path.is_absolute() { args.path } else { @@ -62,3 +60,38 @@ fn main() -> anyhow::Result<()> { ops::compile(&ws) } + +pub struct DojoTestCompiler; + +impl Compiler for DojoTestCompiler { + fn target_kind(&self) -> &str { + "dojo" + } + + fn compile( + &self, + unit: CompilationUnit, + db: &mut RootDatabase, + _: &Workspace<'_>, + ) -> Result<()> { + let main_crate_ids = collect_main_crate_ids(&unit, db); + + if DiagnosticsReporter::stderr().check(db) { + bail!("failed to compile"); + } + + let runner = TestRunner { + db: db.snapshot(), + main_crate_ids, + // TODO: Pass these in + filter: "".to_string(), + include_ignored: false, + ignored: false, + starknet: true, + }; + + runner.run()?; + + Ok(()) + } +} diff --git a/crates/sozo/src/main.rs b/crates/sozo/src/main.rs index c83ebbd7c0..f9f13a989f 100644 --- a/crates/sozo/src/main.rs +++ b/crates/sozo/src/main.rs @@ -6,7 +6,7 @@ use log::error; mod commands; -use self::commands::{build, init, migrate, App, Commands}; +use self::commands::{build, init, migrate, test, App, Commands}; fn main() { env_logger::Builder::from_env(Env::default().default_filter_or("sozo=info")).init(); @@ -23,8 +23,7 @@ fn main() { Ok(()) } Commands::Migrate(args) => migrate::run(args), - Commands::Bind(..) => Ok(print!("Bind")), - Commands::Inspect(..) => Ok(print!("Inspect")), + Commands::Test(args) => test::run(args), }; if let Err(err) = res { diff --git a/scripts/cairo_test.sh b/scripts/cairo_test.sh index 51563a14fb..418e1a60f9 100755 --- a/scripts/cairo_test.sh +++ b/scripts/cairo_test.sh @@ -1,9 +1,9 @@ #!/bin/bash set -euxo pipefail -cargo +nightly-2023-05-28 run --bin dojo-test -- crates/dojo-core -cargo +nightly-2023-05-28 run --bin dojo-test -- crates/dojo-erc -#cargo +nightly-2023-05-28 run --bin dojo-test -- crates/dojo-physics -cargo +nightly-2023-05-28 run --bin dojo-test -- crates/dojo-core/tests -# cargo +nightly-2023-05-28 run --bin dojo-test -- crates/dojo-defi -cargo +nightly-2023-05-28 run --bin dojo-test -- examples/ecs +cargo +nightly-2023-05-28 run --bin sozo -- test crates/dojo-core +cargo +nightly-2023-05-28 run --bin sozo -- test crates/dojo-erc +#cargo +nightly-2023-05-28 run --bin sozo -- test crates/dojo-physics +cargo +nightly-2023-05-28 run --bin sozo -- test crates/dojo-core/tests +# cargo +nightly-2023-05-28 run --bin sozo -- test crates/dojo-defi +cargo +nightly-2023-05-28 run --bin sozo -- test examples/ecs