Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Sep 21, 2023
1 parent 1e55872 commit ed61413
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 425 deletions.
437 changes: 49 additions & 388 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cargo-near/src/abi_command/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn strip_docs(abi_root: &mut near_abi::AbiRoot) {
}
}

pub(crate) fn run(args: super::AbiCommand) -> near_cli_rs::CliResult {
pub fn run(args: super::AbiCommand) -> near_cli_rs::CliResult {
let color = args.color.unwrap_or(ColorPreference::Auto);
color.apply();

Expand Down
8 changes: 4 additions & 4 deletions cargo-near/src/abi_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ pub struct AbiCommand {
/// Copy final artifacts to this directory
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
/// Path to the `Cargo.toml` of the contract to build
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
/// Coloring: auto, always, never
#[interactive_clap(long)]
#[interactive_clap(value_enum)]
Expand Down Expand Up @@ -87,13 +87,13 @@ impl AbiCommand {

fn input_out_dir(
_context: &near_cli_rs::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBuf>> {
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBufInner>> {
Ok(None)
}

fn input_manifest_path(
_context: &near_cli_rs::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBuf>> {
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBufInner>> {
Ok(None)
}
}
10 changes: 5 additions & 5 deletions cargo-near/src/build_command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod build;
pub mod build;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
Expand All @@ -20,11 +20,11 @@ pub struct BuildCommand {
/// Copy final artifacts to this directory
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
pub out_dir: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
/// Path to the `Cargo.toml` of the contract to build
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBuf>,
pub manifest_path: Option<crate::types::utf8_path_buf::Utf8PathBufInner>,
/// Coloring: auto, always, never
#[interactive_clap(long)]
#[interactive_clap(value_enum)]
Expand Down Expand Up @@ -98,13 +98,13 @@ impl BuildCommand {

fn input_out_dir(
_context: &near_cli_rs::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBuf>> {
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBufInner>> {
Ok(None)
}

fn input_manifest_path(
_context: &near_cli_rs::GlobalContext,
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBuf>> {
) -> color_eyre::eyre::Result<Option<crate::types::utf8_path_buf::Utf8PathBufInner>> {
Ok(None)
}
}
13 changes: 10 additions & 3 deletions cargo-near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
pub use near_cli_rs::CliResult;
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod abi_command;
mod build_command;
pub mod abi_command;
pub mod build_command;
mod common;
mod types;
pub mod types;
mod util;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
pub struct Cmd {
#[interactive_clap(subcommand)]
opts: Opts,
}

#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
Expand Down
7 changes: 1 addition & 6 deletions cargo-near/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use interactive_clap::ToCliArgs;
pub use near_cli_rs::CliResult;
use std::env;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = near_cli_rs::GlobalContext)]
struct Cmd {
#[interactive_clap(subcommand)]
opts: cargo_near::Opts,
}
use cargo_near::Cmd;

fn main() -> CliResult {
env_logger::init();
Expand Down
8 changes: 4 additions & 4 deletions cargo-near/src/types/utf8_path_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
derive_more::FromStr,
)]
#[as_ref(forward)]
pub struct Utf8PathBuf(pub camino::Utf8PathBuf);
pub struct Utf8PathBufInner(pub camino::Utf8PathBuf);

impl std::fmt::Display for Utf8PathBuf {
impl std::fmt::Display for Utf8PathBufInner {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}

impl interactive_clap::ToCli for Utf8PathBuf {
type CliVariant = Utf8PathBuf;
impl interactive_clap::ToCli for Utf8PathBufInner {
type CliVariant = Utf8PathBufInner;
}
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ syn = "1.0"
tempfile = "3.3"
tokio = "1.0"
quote = "1.0"
workspaces = "0.4.1"
workspaces = "0.7.0"
zstd = "0.11"
36 changes: 29 additions & 7 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,34 @@ macro_rules! invoke_cargo_near {

std::env::set_var("CARGO_TARGET_DIR", workspace_dir.join("target"));

let cargo_near::Opts::Near(mut args) = clap::Parser::try_parse_from($cli_opts.split(" "))?;
match &mut args.cmd {
cargo_near::NearCommand::Abi(cmd) => cmd.manifest_path = Some(cargo_path),
cargo_near::NearCommand::Build(cmd) => cmd.manifest_path = Some(cargo_path),
let cargo_near::CliOpts::Near(cli_args) = cargo_near::Opts::try_parse_from($cli_opts)?;

match cli_args.cmd {
Some(cargo_near::CliNearCommand::Abi(cmd)) => {
let args = cargo_near::abi_command::AbiCommand {
doc: cmd.doc,
compact_abi: cmd.compact_abi,
out_dir: cmd.out_dir,
manifest_path: Some(cargo_near::types::utf8_path_buf::Utf8PathBufInner(cargo_path)),
color: cmd.color,
};
cargo_near::abi_command::abi::run(args)?;
},
Some(cargo_near::CliNearCommand::Build(cmd)) => {
let args = cargo_near::build_command::BuildCommand {
release: cmd.release,
embed_abi: cmd.embed_abi,
doc: cmd.doc,
no_abi: cmd.no_abi,
out_dir: cmd.out_dir,
manifest_path: Some(cargo_near::types::utf8_path_buf::Utf8PathBufInner(cargo_path)),
color: cmd.color,
};
println!("### {args:?}");
cargo_near::build_command::build::run(args)?;
},
None => ()
}
cargo_near::exec(args.cmd)?;

workspace_dir.join("target").join("near")
}};
Expand All @@ -60,7 +82,7 @@ macro_rules! generate_abi_with {
$(let opts = format!("cargo near abi {}", $cli_opts);)?;
let result_dir = $crate::invoke_cargo_near! {
$(Cargo: $cargo_path;)? $(Vars: $cargo_vars;)?
Opts: opts;
Opts: &opts;
Code:
$($code)*
};
Expand Down Expand Up @@ -132,7 +154,7 @@ macro_rules! build_with {
$(let opts = format!("cargo near build {}", $cli_opts);)?;
let result_dir = $crate::invoke_cargo_near! {
$(Cargo: $cargo_path;)? $(Vars: $cargo_vars;)?
Opts: opts;
Opts: &opts;
Code:
$($code)*
};
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/tests/build/embed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::util;
use cargo_near_integration_tests::{build_fn, build_fn_with};
use function_name::named;
use workspaces::prelude::DevAccountDeployer;

#[tokio::test]
#[named]
Expand Down Expand Up @@ -42,7 +41,7 @@ async fn test_build_no_embed_abi() -> cargo_near::CliResult {

let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(&build_result.wasm).await?;
let outcome = contract.call(&worker, "__contract_abi").view().await;
let outcome = contract.call("__contract_abi").view().await;
outcome.unwrap_err();

Ok(())
Expand Down
7 changes: 3 additions & 4 deletions integration-tests/tests/util.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use near_abi::AbiRoot;
use near_abi::{AbiBorshParameter, AbiJsonParameter, AbiParameters};
use serde_json::json;
use workspaces::prelude::DevAccountDeployer;

/// Utility method to test that the `add` function is available and works as intended
pub async fn test_add(wasm: &[u8]) -> cargo_near::CliResult {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(wasm).await?;
let outcome = contract
.call(&worker, "add")
.call("add")
.args_json(json!({
"a": 2u32,
"b": 3u32,
}))?
}))
.view()
.await?;
assert_eq!(outcome.json::<u32>()?, 5);
Expand All @@ -22,7 +21,7 @@ pub async fn test_add(wasm: &[u8]) -> cargo_near::CliResult {
pub async fn fetch_contract_abi(wasm: &[u8]) -> color_eyre::eyre::Result<AbiRoot> {
let worker = workspaces::sandbox().await?;
let contract = worker.dev_deploy(wasm).await?;
let outcome = contract.call(&worker, "__contract_abi").view().await?;
let outcome = contract.call("__contract_abi").view().await?;
let outcome_json = zstd::decode_all(outcome.result.as_slice())?;
Ok(serde_json::from_slice::<AbiRoot>(&outcome_json)?)
}
Expand Down

0 comments on commit ed61413

Please sign in to comment.