Skip to content

Commit

Permalink
tests: Improve IDL comparison tests (coral-xyz#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jul 27, 2023
1 parent df3e959 commit 8f30f00
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 55 deletions.
2 changes: 1 addition & 1 deletion tests/idl/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 10 additions & 5 deletions tests/idl/generate.sh
Original file line number Diff line number Diff line change
@@ -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
anchor idl build -o $dir/relations_build.json
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 27 additions & 49 deletions tests/idl/test.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8f30f00

Please sign in to comment.