Skip to content

Commit

Permalink
Generate ABI JSONs for scripts (FuelLabs#1838)
Browse files Browse the repository at this point in the history
* Use assert_matches to show errors during tests

* Generate ABI JSONs for scripts
  • Loading branch information
AlicanC authored Jun 4, 2022
1 parent 40d7975 commit 913afb0
Show file tree
Hide file tree
Showing 117 changed files with 1,579 additions and 35 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ impl ManifestFile {
}

/// Given the current directory and expected program type, determines whether the correct program type is present.
pub fn check_program_type(&self, expected_type: TreeType) -> Result<()> {
pub fn check_program_type(&self, expected_types: Vec<TreeType>) -> Result<()> {
let parsed_type = self.program_type()?;
if parsed_type != expected_type {
if !expected_types.iter().any(|t| t == &parsed_type) {
bail!(wrong_program_type(
&self.project.name,
expected_type,
expected_types,
parsed_type
));
} else {
Expand Down
7 changes: 5 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,9 @@ fn generate_json_abi(kind: &TypedProgramKind) -> JsonABI {
TypedProgramKind::Contract { abi_entries, .. } => {
abi_entries.iter().map(|x| x.generate_json_abi()).collect()
}
TypedProgramKind::Script { main_function, .. } => {
vec![main_function.generate_json_abi()]
}
_ => vec![],
}
}
Expand Down Expand Up @@ -1336,12 +1339,12 @@ pub fn parsing_failed(project_name: &str, errors: Vec<CompileError>) -> anyhow::
/// Format an error message if an incorrect program type is present.
pub fn wrong_program_type(
project_name: &str,
expected_type: TreeType,
expected_types: Vec<TreeType>,
parse_type: TreeType,
) -> anyhow::Error {
let message = format!(
"{} is not a '{:?}' it is a '{:?}'",
project_name, expected_type, parse_type
project_name, expected_types, parse_type
);
Error::msg(message)
}
Expand Down
2 changes: 1 addition & 1 deletion forc/src/ops/forc_abi_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn build(command: JsonAbiCommand) -> Result<Value> {
std::env::current_dir()?
};
let manifest = ManifestFile::from_dir(&curr_dir, SWAY_GIT_TAG)?;
manifest.check_program_type(TreeType::Contract)?;
manifest.check_program_type(vec![TreeType::Contract, TreeType::Script])?;

let build_command = BuildCommand {
path: command.path,
Expand Down
2 changes: 1 addition & 1 deletion forc/src/ops/forc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
std::env::current_dir()?
};
let manifest = ManifestFile::from_dir(&curr_dir, SWAY_GIT_TAG)?;
manifest.check_program_type(TreeType::Contract)?;
manifest.check_program_type(vec![TreeType::Contract])?;

let DeployCommand {
path,
Expand Down
2 changes: 1 addition & 1 deletion forc/src/ops/forc_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
};
let manifest = ManifestFile::from_dir(&path_dir, SWAY_GIT_TAG)?;
manifest.check_program_type(TreeType::Script)?;
manifest.check_program_type(vec![TreeType::Script])?;

let input_data = &command.data.unwrap_or_else(|| "".into());
let data = format_hex_data(input_data);
Expand Down
1 change: 1 addition & 0 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ publish = false

[dependencies]
anyhow = "1.0.41"
assert_matches = "1.5.0"
forc = { path = "../forc", features = ["test"], default-features = false }
forc-util = { path = "../forc-util" }
fuel-asm = "0.5"
Expand Down
53 changes: 27 additions & 26 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod harness;
use assert_matches::assert_matches;
use forc_util::init_tracing_subscriber;
use fuel_vm::prelude::*;
pub fn run(filter_regex: Option<regex::Regex>) {
Expand All @@ -10,10 +11,31 @@ pub fn run(filter_regex: Option<regex::Regex>) {
.unwrap_or(true)
};

// Non-contract programs that should successfully compile and terminate
// with some known state. Note that if you are adding a contract, it may pass by mistake.
// Please add contracts to `positive_project_names_with_abi`.
let positive_project_names_no_abi = vec![
// Predicate programs that should successfully compile and terminate
// with some known state. Note that if you are adding a non-predicate, it may pass by mistake.
// Please add non-predicates to `positive_project_names_with_abi`.
let positive_project_names_no_abi = vec![(
"should_pass/language/basic_predicate",
ProgramState::Return(1),
)];

let mut number_of_tests_run =
positive_project_names_no_abi
.iter()
.fold(0, |acc, (name, res)| {
if filter(name) {
assert_eq!(crate::e2e_vm_tests::harness::runs_in_vm(name), *res);
acc + 1
} else {
acc
}
});

// Programs that should successfully compile, include abi and terminate
// with some known state. Note that if a predicate is included
// it will be rejected during assertion. Please move it to
// `positive_project_names_no_abi` above.
let positive_project_names_with_abi = vec![
(
"should_pass/forc/dependency_package_field",
ProgramState::Return(0),
Expand Down Expand Up @@ -396,25 +418,6 @@ pub fn run(filter_regex: Option<regex::Regex>) {
"should_pass/language/reassignment_operators",
ProgramState::Return(1),
),
];

let mut number_of_tests_run =
positive_project_names_no_abi
.iter()
.fold(0, |acc, (name, res)| {
if filter(name) {
assert_eq!(crate::e2e_vm_tests::harness::runs_in_vm(name), *res);
acc + 1
} else {
acc
}
});

// Programs that should successfully compile, include abi and terminate
// with some known state. Note that if a non-contract is included
// it will be rejected during assertion. Please move it to
// `positive_project_names_no_abi` above.
let positive_project_names_with_abi = vec![
(
"should_pass/language/valid_impurity",
ProgramState::Revert(0), // false
Expand Down Expand Up @@ -474,9 +477,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
.fold(0, |acc, (name, res)| {
if filter(name) {
assert_eq!(crate::e2e_vm_tests::harness::runs_in_vm(name), *res);
// cannot use partial eq on type `anyhow::Error` so I've used `matches!` here instead.
// https://users.rust-lang.org/t/issues-in-asserting-result/61198/3 for reference.
assert!(crate::e2e_vm_tests::harness::test_json_abi(name).is_ok());
assert_matches!(crate::e2e_vm_tests::harness::test_json_abi(name), Ok(_));
acc + 1
} else {
acc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u32"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": [],
"name": "",
"type": "()"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool"
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[[package]]
name = 'basic_predicate'
dependencies = ['core']

[[package]]
name = 'core'
dependencies = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "basic_predicate"
entry = "main.sw"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
predicate;

fn main() -> bool {
true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "u64"
}
],
"type": "function"
}
]
Loading

0 comments on commit 913afb0

Please sign in to comment.