From 8f30f00ec363b7e82aa0b3c7041e912919b33cf5 Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Thu, 27 Jul 2023 23:29:26 +0200 Subject: [PATCH] tests: Improve IDL comparison tests (#2581) --- tests/idl/Anchor.toml | 2 +- tests/idl/generate.sh | 15 ++-- .../idls/{idl_build_exp.json => build.json} | 0 ...ics_build_exp.json => generics_build.json} | 0 .../idls/{idl_parse_exp.json => parse.json} | 0 ...ns_build_exp.json => relations_build.json} | 0 tests/idl/test.sh | 76 +++++++------------ 7 files changed, 38 insertions(+), 55 deletions(-) rename tests/idl/idls/{idl_build_exp.json => build.json} (100%) rename tests/idl/idls/{generics_build_exp.json => generics_build.json} (100%) rename tests/idl/idls/{idl_parse_exp.json => parse.json} (100%) rename tests/idl/idls/{relations_build_exp.json => relations_build.json} (100%) diff --git a/tests/idl/Anchor.toml b/tests/idl/Anchor.toml index c1485f8a5e..e56d9a8325 100644 --- a/tests/idl/Anchor.toml +++ b/tests/idl/Anchor.toml @@ -7,7 +7,7 @@ generics = "Generics111111111111111111111111111111111111" idl = "id11111111111111111111111111111111111111111" relations_derivation = "Re1ationsDerivation111111111111111111111111" non_existent = { address = "NonExistent11111111111111111111111111111111", idl = "non-existent.json" } -numbers_123 = { address = "Numbers111111111111111111111111111111111111", idl = "idls/relations_build_exp.json" } +numbers_123 = { address = "Numbers111111111111111111111111111111111111", idl = "idls/relations_build.json" } [provider] cluster = "localnet" diff --git a/tests/idl/generate.sh b/tests/idl/generate.sh index d0a930a197..99c792d2e9 100755 --- a/tests/idl/generate.sh +++ b/tests/idl/generate.sh @@ -1,13 +1,18 @@ #!/usr/bin/env bash -idls_dir=$PWD/idls +# `$1` is the directory to generate the IDLs in, defaults to `./idls` +if [ $# = 1 ]; then + dir=$1 +else + dir=$PWD/idls +fi cd programs/idl -anchor idl parse --file src/lib.rs -o $idls_dir/idl_parse_exp.json -anchor idl build -o $idls_dir/idl_build_exp.json +anchor idl parse --file src/lib.rs -o $dir/parse.json +anchor idl build -o $dir/build.json cd ../generics -anchor idl build -o $idls_dir/generics_build_exp.json +anchor idl build -o $dir/generics_build.json cd ../relations-derivation -anchor idl build -o $idls_dir/relations_build_exp.json \ No newline at end of file +anchor idl build -o $dir/relations_build.json \ No newline at end of file diff --git a/tests/idl/idls/idl_build_exp.json b/tests/idl/idls/build.json similarity index 100% rename from tests/idl/idls/idl_build_exp.json rename to tests/idl/idls/build.json diff --git a/tests/idl/idls/generics_build_exp.json b/tests/idl/idls/generics_build.json similarity index 100% rename from tests/idl/idls/generics_build_exp.json rename to tests/idl/idls/generics_build.json diff --git a/tests/idl/idls/idl_parse_exp.json b/tests/idl/idls/parse.json similarity index 100% rename from tests/idl/idls/idl_parse_exp.json rename to tests/idl/idls/parse.json diff --git a/tests/idl/idls/relations_build_exp.json b/tests/idl/idls/relations_build.json similarity index 100% rename from tests/idl/idls/relations_build_exp.json rename to tests/idl/idls/relations_build.json diff --git a/tests/idl/test.sh b/tests/idl/test.sh index 0e2214e3b3..7b0db254f0 100755 --- a/tests/idl/test.sh +++ b/tests/idl/test.sh @@ -1,57 +1,35 @@ #!/usr/bin/env bash -set -x set -e # Run anchor test anchor test --skip-lint -idls_dir=idls tmp_dir=$(mktemp -d) -cd programs/idl -anchor idl parse --file src/lib.rs -o $tmp_dir/idl_parse_act.json -anchor idl build -o $tmp_dir/idl_build_act.json - -cd ../generics -anchor idl build -o $tmp_dir/generics_build_act.json - -cd ../relations-derivation -anchor idl build -o $tmp_dir/relations_build_act.json - -cd ../.. -echo "----------------------------------------------------" -echo "idl parse before > after" -echo "----------------------------------------------------" -echo "" -diff -y --color $idls_dir/idl_parse_exp.json $tmp_dir/idl_parse_act.json -PARSE_RETCODE=$? - -echo "" -echo "" -echo "----------------------------------------------------" -echo "idl build before > after" -echo "----------------------------------------------------" -echo "" -diff -y --color $idls_dir/idl_build_exp.json $tmp_dir/idl_build_act.json -GEN_RETCODE=$? - -echo "" -echo "" -echo "----------------------------------------------------" -echo "idl generics build before > after" -echo "----------------------------------------------------" -echo "" -diff -y --color $idls_dir/generics_build_exp.json $tmp_dir/generics_build_act.json -GEN_GENERICS_RETCODE=$? - -echo "" -echo "" -echo "----------------------------------------------------" -echo "idl relations build before > after" -echo "----------------------------------------------------" -echo "" -diff -y --color $idls_dir/relations_build_exp.json $tmp_dir/relations_build_act.json -GEN_RELATIONS_RETCODE=$? - -# returns 0 when ok, or a positive integer when there are differences -exit $((PARSE_RETCODE+GEN_RETCODE+GEN_GENERICS_RETCODE+GEN_RELATIONS_RETCODE)) +# Generate IDLs +./generate.sh $tmp_dir + +# Exit status +ret=0 + +# Compare IDLs. `$ret` will be non-zero in the case of a mismatch. +compare() { + echo "----------------------------------------------------" + echo "IDL $1 before > after changes" + echo "----------------------------------------------------" + diff -y --color=always --suppress-common-lines idls/$1.json $tmp_dir/$1.json + ret=$(($ret+$?)) + + if [ "$ret" = "0" ]; then + echo "No changes" + fi + + echo "" +} + +compare "parse" +compare "build" +compare "generics_build" +compare "relations_build" + +exit $ret