Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtask: generate katana test db #2271

Merged
merged 9 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[alias]
generate-test-db = "run --manifest-path ./xtask/generate-test-db/Cargo.toml --"
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ members = [
"crates/torii/server",
"crates/torii/types-test",
"examples/spawn-and-move",
"xtask/generate-test-db",
]

[workspace.package]
Expand Down
7 changes: 7 additions & 0 deletions crates/katana/runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
pub log_path: Option<PathBuf>,
/// The messaging config file
pub messaging: Option<String>,
/// The path to the database dir.
pub db_dir: Option<PathBuf>,
}

impl Default for KatanaRunnerConfig {
Expand All @@ -58,6 +60,7 @@
run_name: None,
log_path: None,
messaging: None,
db_dir: None,
glihm marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -99,6 +102,10 @@
builder = builder.messaging(messaging_file);
}

if let Some(path) = config.db_dir {
builder = builder.db_dir(path);

Check warning on line 106 in crates/katana/runner/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/runner/src/lib.rs#L106

Added line #L106 was not covered by tests
}
glihm marked this conversation as resolved.
Show resolved Hide resolved

let mut katana = builder.spawn();

let stdout =
Expand Down
14 changes: 14 additions & 0 deletions xtask/generate-test-db/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
edition.workspace = true
license.workspace = true
name = "xtask-generate-test-db"
repository.workspace = true
version.workspace = true

[dependencies]
dojo-test-utils.workspace = true
dojo-world = { workspace = true, features = [ "migration" ] }
katana-runner.workspace = true
scarb.workspace = true
sozo-ops = { workspace = true, features = [ "test-utils" ] }
tokio.workspace = true
54 changes: 54 additions & 0 deletions xtask/generate-test-db/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::path::PathBuf;
use std::process::Command;

use dojo_test_utils::compiler::CompilerTestSetup;
use dojo_world::migration::TxnConfig;
use katana_runner::{KatanaRunner, KatanaRunnerConfig};
use scarb::compiler::Profile;
use sozo_ops::migration::{self, MigrationOutput};
use sozo_ops::test_utils;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

async fn migrate(runner: &KatanaRunner) -> Result<MigrationOutput> {
// migrate the example project
let acc = runner.account(0);

// setup scarb workspace
let setup = CompilerTestSetup::from_examples("crates/dojo-core", "examples/");
let cfg = setup.build_test_config("spawn-and-move", Profile::DEV);
let ws = scarb::ops::read_workspace(cfg.manifest_path(), &cfg)?;

Check warning on line 20 in xtask/generate-test-db/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xtask/generate-test-db/src/main.rs#L13-L20

Added lines #L13 - L20 were not covered by tests

// migrate the example project
let (strat, _) = test_utils::setup::setup_migration(&cfg)?;
let output = migration::execute_strategy(&ws, &strat, &acc, TxnConfig::init_wait()).await?;

Check warning on line 24 in xtask/generate-test-db/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xtask/generate-test-db/src/main.rs#L23-L24

Added lines #L23 - L24 were not covered by tests

Ok(output)
}

Check warning on line 27 in xtask/generate-test-db/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xtask/generate-test-db/src/main.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
glihm marked this conversation as resolved.
Show resolved Hide resolved

#[tokio::main]
async fn main() -> Result<()> {
let db_path = PathBuf::from("test-db");
let compressed_path = "test-db.tar.gz";

// Instantiate Katana with db-dir at ./test-db
let cfg = KatanaRunnerConfig { db_dir: Some(db_path.clone()), ..Default::default() };
let runner = KatanaRunner::new_with_config(cfg)?;

let _ = migrate(&runner).await?;
drop(runner);

// ensure the test-db directory have been created
assert!(db_path.exists(), "test-db directory does not exist");

// Compress the ./test-db directory using tar
Command::new("tar")
.args(&["-czf", compressed_path, "-C", ".", "test-db"])
.status()
.expect("Failed to compress test-db directory");

// ensure the test-db directory have been created
assert!(PathBuf::from(db_path).exists(), "test-db directory does not exist");

Ok(())
}

Check warning on line 54 in xtask/generate-test-db/src/main.rs

View check run for this annotation

Codecov / codecov/patch

xtask/generate-test-db/src/main.rs#L30-L54

Added lines #L30 - L54 were not covered by tests
glihm marked this conversation as resolved.
Show resolved Hide resolved
Loading