Skip to content

Commit

Permalink
[diem-transactional-tests] allow named addresses in transaction argum…
Browse files Browse the repository at this point in the history
…ent positions
  • Loading branch information
vgao1996 authored and bors-libra committed Dec 17, 2021
1 parent 4dc515b commit b61ea71
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![forbid(unsafe_code)]

use crate::tasks::{
taskify, InitCommand, PrintBytecodeCommand, PrintBytecodeInputChoice, PublishCommand,
taskify, Argument, InitCommand, PrintBytecodeCommand, PrintBytecodeInputChoice, PublishCommand,
RawAddress, RunCommand, SyntaxChoice, TaskCommand, TaskInput, ViewCommand,
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -69,6 +69,17 @@ impl<'a> CompiledState<'a> {
RawAddress::Anonymous(addr) => *addr,
}
}

pub fn resolve_args(&self, args: Vec<Argument>) -> Vec<TransactionArgument> {
args.into_iter()
.map(|arg| match arg {
Argument::NamedAddress(named_addr) => {
TransactionArgument::Address(self.resolve_named_address(named_addr.as_str()))
}
Argument::TransactionArgument(arg) => arg,
})
.collect()
}
}

pub trait MoveTestAdapter<'a> {
Expand Down Expand Up @@ -259,6 +270,7 @@ pub trait MoveTestAdapter<'a> {
}
SyntaxChoice::IR => (compile_ir_script(state.dep_modules(), data_path)?, None),
};
let args = self.compiled_state().resolve_args(args);
self.execute_script(script, type_args, signers, args, gas_budget, extra_args)?;
Ok(warning_opt)
}
Expand All @@ -277,6 +289,7 @@ pub trait MoveTestAdapter<'a> {
syntax.is_none(),
"syntax flag meaningless with function execution"
);
let args = self.compiled_state().resolve_args(args);
self.call_function(
&module,
name.as_ident_str(),
Expand Down
20 changes: 18 additions & 2 deletions language/testing-infra/transactional-test-runner/src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,20 @@ pub struct PublishCommand {
pub syntax: Option<SyntaxChoice>,
}

/// TODO: this is a hack to support named addresses in transaction argument positions.
/// Should reimplement in a better way in the future.
#[derive(Debug)]
pub enum Argument {
NamedAddress(Identifier),
TransactionArgument(TransactionArgument),
}

#[derive(Debug, StructOpt)]
pub struct RunCommand {
#[structopt(long = "signers", parse(try_from_str = RawAddress::parse))]
pub signers: Vec<RawAddress>,
#[structopt(long = "args", parse(try_from_str = parser::parse_transaction_argument))]
pub args: Vec<TransactionArgument>,
#[structopt(long = "args", parse(try_from_str = parse_argument))]
pub args: Vec<Argument>,
#[structopt(long = "type-args", parse(try_from_str = parser::parse_type_tag))]
pub type_args: Vec<TypeTag>,
#[structopt(long = "gas-budget")]
Expand Down Expand Up @@ -409,3 +417,11 @@ impl FromStr for PrintBytecodeInputChoice {
}
}
}

fn parse_argument(s: &str) -> Result<Argument> {
Ok(if let Some(stripped) = s.strip_prefix('@') {
Argument::NamedAddress(Identifier::new(stripped)?)
} else {
Argument::TransactionArgument(parser::parse_transaction_argument(s)?)
})
}

0 comments on commit b61ea71

Please sign in to comment.