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

chore: run 'capsule test' without docker #96

Merged
merged 5 commits into from
Apr 20, 2023
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
6 changes: 4 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ jobs:
run: cargo build
- name: Install dependencies
run: cargo install --version 0.7.3 moleculec
- name: Run tests
run: cargo test && cargo run -p tests
- name: Cargo test
run: cargo test
- name: Run integration tests
run: bash ./dev-tools/integration-tests.sh
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ members = [
"tests",
"crates/testtool",
]
exclude = ["tmp"]
9 changes: 9 additions & 0 deletions dev-tools/integration-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

echo "Prepare test environment..."
rm -rf tmp/rust-demo/

echo "Running tests..."
cargo run -p tests

echo "Ok"
13 changes: 10 additions & 3 deletions src/bin/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ fn run_cli() -> Result<()> {
.args(&[Arg::with_name("name").short("n").long("name").required(true).takes_value(true).help("contract name"),
Arg::with_name("cmd").required(true).multiple(true).help("command to run")])
.display_order(4))
.subcommand(SubCommand::with_name("test").about("Run tests").arg(
Arg::with_name("release").long("release").help("Test release mode contracts.")
.subcommand(SubCommand::with_name("test").about("Run `cargo test` in the tests directory").arg(
Arg::with_name("release").long("release").help("Test release mode contracts.").display_order(1)
).arg(
Arg::with_name("testname")
.takes_value(true)
.value_name("TESTNAME")
.help("If specified, only run tests containing this string in their names.")
.display_order(2)
).display_order(5))
.subcommand(
SubCommand::with_name("deploy")
Expand Down Expand Up @@ -360,7 +366,8 @@ fn run_cli() -> Result<()> {
} else {
BuildEnv::Debug
};
Tester::run(&context, build_env, &signal, env_file)?;
let test_name = args.value_of("testname");
Tester::run(&context, build_env, test_name)?;
}
("deploy", Some(args)) => {
let ckb_cli_bin = args.value_of("ckb-cli").expect("ckb-cli");
Expand Down
2 changes: 1 addition & 1 deletion src/project_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const MIGRATIONS_DIR: &str = "migrations";
pub const CONFIG_FILE: &str = "capsule.toml";
pub const CARGO_CONFIG_FILE: &str = "Cargo.toml";

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BuildEnv {
Debug,
Release,
Expand Down
61 changes: 24 additions & 37 deletions src/tester.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,37 @@
use std::process::Command;

use crate::project_context::{BuildEnv, Context};
use crate::recipe::rust::DOCKER_IMAGE;
use crate::signal::Signal;
use crate::util::docker::DockerCommand;
use anyhow::Result;

const TEST_ENV_VAR: &str = "CAPSULE_TEST_ENV";
jjyr marked this conversation as resolved.
Show resolved Hide resolved
const TESTS_DIR: &str = "tests";
pub struct Tester;

impl Tester {
pub fn run(
project_context: &Context,
env: BuildEnv,
signal: &Signal,
docker_env_file: String,
) -> Result<()> {
let env_arg = match env {
BuildEnv::Debug => "debug",
BuildEnv::Release => "release",
};
let workspace_dir = project_context
.workspace_dir()?
.to_str()
.expect("project path")
.to_string();
pub fn run(project_context: &Context, env: BuildEnv, test_name: Option<&str>) -> Result<()> {
let workspace_dir = project_context.workspace_dir()?;
let test_dir = workspace_dir.join(TESTS_DIR);
// When workspace_dir is "contracts" we must mount build directory to /code/build so that test Loader can load the binary.
let build_dir = project_context
.contracts_build_dir()
.to_str()
.expect("build dir")
.to_string();
let cmd = DockerCommand::with_context(
project_context,
DOCKER_IMAGE.to_string(),
workspace_dir,
docker_env_file,
)
.map_volume(build_dir, "/code/build".to_string())
.fix_dir_permission("target".to_string())
.fix_dir_permission("Cargo.lock".to_string());
cmd.run(
format!(
"{}={} cargo test -p tests -- --nocapture",
TEST_ENV_VAR, env_arg
),
signal,
)?;
let mut cmd = Command::new("cargo");
cmd.arg("test").current_dir(&test_dir);
if env == BuildEnv::Release {
cmd.arg("--release");
}
if let Some(test_name) = test_name {
cmd.arg(test_name);
}
cmd.arg("--").arg("--nocapture");
let output = cmd.output()?;
jjyr marked this conversation as resolved.
Show resolved Hide resolved
if output.status.success() {
println!("{}", String::from_utf8(output.stdout)?);
} else {
return Err(anyhow::anyhow!(
"cargo test failed: \n{}",
String::from_utf8(output.stderr)?
));
}
Ok(())
}
}
5 changes: 3 additions & 2 deletions templates/Cargo-manifest.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[workspace]
members = ["tests"]
members = []
exclude = ["tests"]

[profile.release]
overflow-checks = true
opt-level = 's'
lto = false
codegen-units = 1
panic = 'abort'
panic = 'abort'
11 changes: 5 additions & 6 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ fn test_build<P: AsRef<Path>>(
contract_path.push(&dir);
contract_path.push(name);
println!("Creating {:?} ...", contract_path);
let exit_code = Command::new(bin_path)
let output = Command::new(bin_path)
.arg("new")
.arg(name)
.arg("--template")
.arg(template_type)
.spawn()?
.wait()?;
if !exit_code.success() {
panic!("command crash, exit_code {:?}", exit_code.code());
.output()?;
if !output.status.success() {
panic!("command crash, stderr {:?}", String::from_utf8(output.stderr)?);
}
println!("Building ...");
env::set_current_dir(&contract_path)?;
Expand All @@ -69,7 +68,7 @@ fn test_build<P: AsRef<Path>>(
println!("Run contract test ...");
let exit_code = Command::new("bash")
.arg("-c")
.arg(format!("cargo test -p tests"))
.arg(format!("capsule test"))
.spawn()?
.wait()?;
if !exit_code.success() {
Expand Down