Skip to content

Commit

Permalink
add support to pass a json as arg for js-script (#1902)
Browse files Browse the repository at this point in the history
* add support to pass a json as arg for js-script

* Fix nix hash

* fmt

* lint

---------

Co-authored-by: pepoviola <[email protected]>
  • Loading branch information
pepoviola and pepoviola authored Oct 11, 2024
1 parent c8d4a0a commit b11637d
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/parser-wrapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dsl-parser-wrapper"
version = "0.1.10"
version = "0.1.11"
edition = "2021"
description = "Zombienet DSL parser: produces a test definition, in json format, that can be used with the ZombieNet's test-runnner."
license = "GPL-3.0-or-later"
Expand Down
2 changes: 2 additions & 0 deletions crates/parser-wrapper/todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
wasm-pack build --target nodejs --scope zombienet
cd pkg && npm publish --access=public
2 changes: 1 addition & 1 deletion crates/parser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser"
version = "0.0.1"
version = "0.0.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 3 additions & 0 deletions crates/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ fn parse_custom_script_rule(
Rule::double_quoted_string => {
args = Some(inner_record.as_str().trim_matches('"').to_owned());
}
Rule::single_quoted_string => {
args = Some(inner_record.as_str().trim_matches('\'').to_owned());
}
Rule::comparison => {
cmp = Some(parse_comparison(inner_record)?);
}
Expand Down
30 changes: 30 additions & 0 deletions crates/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,36 @@ fn custom_js_with_args_parse_ok() {
assert_eq!(result, t);
}

#[test]
fn custom_js_with_single_quotes_args_parse_ok() {
let line: &str =
r#"alice: js-script ./0008-custom.js with '{"a":1,"b":["demo"]}' within 200 seconds"#;
let data = r#"{
"description": null,
"network": "./a.toml",
"creds": "config",
"assertions": [
{
"original_line": "alice: js-script ./0008-custom.js with '{\"a\":1,\"b\":[\"demo\"]}' within 200 seconds",
"parsed": {
"fn": "CustomJs",
"args": {
"node_name": "alice",
"file_path": "./0008-custom.js",
"custom_args": "{\"a\":1,\"b\":[\"demo\"]}",
"timeout": 200,
"is_ts": false
}
}
}
]
}"#;
let t: TestDefinition = serde_json::from_str(data).unwrap();

let result = parse(&[NETWORK, CREDS, line].join("\n")).unwrap();
assert_eq!(result, t);
}

#[test]
fn custom_ts_parse_ok() {
let line: &str = r#"alice: ts-script ./0008-custom-ts.ts within 200 seconds"#;
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/src/zombienet.pest
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ log_match = { node_name ~ "log line" ~ ("contains"|"matches") ~ match_type? ~ do
count_log_match = { node_name ~ "count of log lines" ~ ("containing"|"matching") ~ match_type? ~ double_quoted_string ~ "is" ~ (comparison | int+) ~ within? }
trace = { node_name ~ "trace with traceID" ~ span_id ~ "contains" ~ square_brackets_strings ~ within? }
system_event = { node_name ~ "system event" ~ ("contains"|"matches") ~ match_type? ~ double_quoted_string ~ within? }
custom_js = { node_name ~ "js-script" ~ file_path ~ ("with" ~ double_quoted_string)? ~ ( "return" ~ comparison )? ~ within? }
custom_js = { node_name ~ "js-script" ~ file_path ~ ("with" ~ (double_quoted_string|single_quoted_string))? ~ ( "return" ~ comparison )? ~ within? }
custom_ts = { node_name ~ "ts-script" ~ file_path ~ ("with" ~ double_quoted_string)? ~ ( "return" ~ comparison )? ~ within? }
custom_sh = { node_name ~ "run" ~ file_path ~ ("with" ~ double_quoted_string)? ~ ( "return" ~ comparison )? ~ within? }

Expand Down
2 changes: 1 addition & 1 deletion flake-module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
...
}: let
# this change on each change of dependencies, unfortunately this hash not yet automatically updated from SRI of package.lock
npmDepsHash = "sha256-JwQKIrLKh3leP+NYI06Rpcn12pidnhAWUB+l/XXKXTs=";
npmDepsHash = "sha256-UnHUaTZYwJ1DA9dltMz5bS8spSFUBfqS+IvjzGyHz/k=";
####

# there is officia polkadot on nixpkgs, but it has no local rococo wasm to run
Expand Down
14 changes: 7 additions & 7 deletions javascript/package-lock.json

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

2 changes: 1 addition & 1 deletion javascript/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"url": "https://github.com/paritytech/zombienet/issues"
},
"dependencies": {
"@zombienet/dsl-parser-wrapper": "^0.1.10",
"@zombienet/dsl-parser-wrapper": "^0.1.11",
"@zombienet/orchestrator": "^0.0.95",
"@zombienet/utils": "^0.0.25",
"cli-progress": "^3.12.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ const CustomJs = ({
const call_args = custom_args
? custom_args === ""
? []
: custom_args.split(",")
: custom_args.charAt(0) === "{" // allow to pass a single quoted json
? JSON.parse(custom_args)
: custom_args.split(",")
: [];

let resolvedJsFilePath = path.resolve(configBasePath, file_path!);
Expand Down
20 changes: 20 additions & 0 deletions tests/smoke/0001-custom-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
async function run(nodeName, networkInfo, argObject) {
let err = false;
console.log(argObject);
if (argObject.account != "alice") {
console.log("JSON err: field name");
err = true;
}
if (argObject.nums[1] !== 1) {
console.log("JSON err: field nums");
err = true;
}
if (argObject.bool !== true) {
console.log("JSON err: field nums");
err = true;
}

if(err) throw new Error('JSON should be {"account": "alice", "nums": [0,1,2,3], "bool": true}');
}

module.exports = { run }
2 changes: 2 additions & 0 deletions tests/smoke/0001-smoke.zndsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ alice: system event matches "paraId(.)*[0-9]+" within 20 seconds
alice: js-script ./0001-custom.js return is greater than 1 within 200 seconds
alice: js-script ./0001-custom.js within 200 seconds
alice: js-script ./0001-custom.js with "alice" within 200 seconds
# json argument
alice: js-script ./0001-custom-json.js with '{"account": "alice", "nums": [0,1,2,3], "bool": true}' within 200 seconds
alice: run ./0001-custom.sh within 200 seconds

# Histogram assertions
Expand Down

0 comments on commit b11637d

Please sign in to comment.