tests: Refactor IDL tests (#2573)

This commit is contained in:
acheron 2023-07-17 22:47:55 +02:00 committed by GitHub
parent cf057acac0
commit c306463432
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 103 additions and 117 deletions

View File

@ -443,8 +443,8 @@ jobs:
path: tests/anchor-cli-account
- cmd: cd tests/bench && anchor test --skip-lint
path: tests/bench
- cmd: cd tests/idl-build && ./test.sh
path: tests/idl-build
- cmd: cd tests/idl && ./test.sh
path: tests/idl
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup/

View File

@ -1,13 +0,0 @@
[features]
seeds = true
[programs.localnet]
idl = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
idl_2 = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
[provider]
cluster = "localnet"
wallet = "~/.config/solana/id.json"
[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

View File

@ -1,11 +0,0 @@
#!/usr/bin/env bash
cd programs/idl
anchor idl parse --file src/lib.rs -o ../../tests/testdata/idl_parse_exp.json
anchor idl build -o ../../tests/testdata/idl_build_exp.json
cd ../generics
anchor idl build -o ../../tests/testdata/generics_build_exp.json
cd ../relations-derivation
anchor idl build -o ../../tests/testdata/relations_build_exp.json

15
tests/idl/Anchor.toml Normal file
View File

@ -0,0 +1,15 @@
[features]
seeds = true
[programs.localnet]
external = "Externa1111111111111111111111111111111111111"
generics = "Generics111111111111111111111111111111111111"
idl = "id11111111111111111111111111111111111111111"
relations_derivation = "Re1ationsDerivation111111111111111111111111"
[provider]
cluster = "localnet"
wallet = "~/.config/solana/id.json"
[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

13
tests/idl/generate.sh Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env bash
idls_dir=$PWD/idls
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
cd ../generics
anchor idl build -o $idls_dir/generics_build_exp.json
cd ../relations-derivation
anchor idl build -o $idls_dir/relations_build_exp.json

View File

@ -1,9 +1,6 @@
{
"version": "0.1.0",
"name": "example_program",
"docs": [
"This is an example program used for testing"
],
"name": "generics",
"instructions": [
{
"name": "generic",
@ -77,6 +74,18 @@
}
],
"types": [
{
"name": "Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
},
{
"name": "GenericEnum",
"generics": [
@ -404,18 +413,6 @@
}
]
}
},
{
"name": "Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
}
]
}

View File

@ -1,8 +1,8 @@
{
"version": "0.1.0",
"name": "example_program",
"name": "idl",
"docs": [
"This is an example program used for testing"
"IDL test program documentation."
],
"constants": [
{
@ -475,6 +475,18 @@
}
],
"types": [
{
"name": "external::Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
},
{
"name": "BarStruct",
"docs": [
@ -658,18 +670,6 @@
}
]
}
},
{
"name": "some_external_program::Baz",
"type": {
"kind": "struct",
"fields": [
{
"name": "someField",
"type": "u8"
}
]
}
}
],
"events": [
@ -684,7 +684,7 @@
{
"name": "externalBaz",
"type": {
"defined": "some_external_program::Baz"
"defined": "external::Baz"
},
"index": false
},

View File

@ -1,8 +1,8 @@
{
"version": "0.1.0",
"name": "example_program",
"name": "idl",
"docs": [
"This is an example program used for testing"
"IDL test program documentation."
],
"constants": [
{
@ -672,7 +672,7 @@
{
"name": "externalBaz",
"type": {
"defined": "some_external_program::Baz"
"defined": "external::Baz"
},
"index": false
},

View File

@ -1,5 +1,5 @@
{
"name": "idl-build",
"name": "idl",
"version": "0.28.0",
"license": "(MIT OR Apache-2.0)",
"homepage": "https://github.com/coral-xyz/anchor#readme",

View File

@ -1,12 +1,12 @@
[package]
name = "some-external-program"
name = "external"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "some_external_program"
name = "external"
[features]
no-entrypoint = []
@ -19,4 +19,3 @@ default = []
[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }

View File

@ -1,9 +1,9 @@
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Externa1111111111111111111111111111111111111");
#[program]
pub mod idl_2 {
pub mod external {
use super::*;
pub fn initialize(_ctx: Context<Initialize>, _baz: Baz) -> Result<()> {

View File

@ -13,13 +13,10 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
"some-external-program/idl-build",
]
idl-build = ["anchor-lang/idl-build", "external/idl-build"]
default = []
[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }
some-external-program = { path = "../some_external_program", features = ["no-entrypoint"] }
external = { path = "../external", features = ["no-entrypoint"] }

View File

@ -1,17 +1,15 @@
use anchor_lang::prelude::*;
use some_external_program;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Generics111111111111111111111111111111111111");
/// This is an example program used for testing
#[program]
pub mod example_program {
pub mod generics {
use super::*;
pub fn generic(
ctx: Context<GenericCtx>,
generic_field: GenericType::<u32, u64, 10>
) -> Result<()>{
generic_field: GenericType<u32, u64, 10>,
) -> Result<()> {
ctx.accounts.generic_acc.data = generic_field;
Ok(())
}
@ -28,7 +26,7 @@ pub struct GenericCtx<'info> {
#[account]
pub struct GenericAccount {
pub data: GenericType<u32, u64, 10>
pub data: GenericType<u32, u64, 10>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
@ -40,7 +38,7 @@ where
pub gen1: T,
pub gen2: U,
pub gen3: GenericNested<u32, U>,
pub gen4: GenericNested<T, some_external_program::Baz>,
pub gen4: GenericNested<T, external::Baz>,
pub gen5: GenericNested<T, U>,
pub gen6: GenericNested<u32, u64>,
pub gen7: GenericNested<T, GenericNested<T, U>>,
@ -71,10 +69,7 @@ where
U: AnchorSerialize + AnchorDeserialize,
{
Unnamed(T, U),
Named {
gen1: T,
gen2: U,
},
Named { gen1: T, gen2: U },
Struct(GenericNested<T, U>),
Arr([T; N]),
}

View File

@ -13,14 +13,11 @@ no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
"some-external-program/idl-build",
]
idl-build = ["anchor-lang/idl-build", "external/idl-build"]
default = []
[dependencies]
anchor-lang = { path = "../../../../lang" }
anchor-spl = { path = "../../../../spl" }
bytemuck = {version = "1.4.0", features = ["derive", "min_const_generics"]}
some-external-program = { path = "../some_external_program", features = ["no-entrypoint"] }
external = { path = "../external", features = ["no-entrypoint"] }

View File

@ -1,17 +1,16 @@
use anchor_lang::prelude::*;
use some_external_program;
use std::str::FromStr;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("id11111111111111111111111111111111111111111");
#[constant]
pub const FOO_CONST: u128 = 1_000_000;
#[constant]
pub const BAR_CONST: u8 = 6;
/// This is an example program used for testing
/// IDL test program documentation.
#[program]
pub mod example_program {
pub mod idl {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
@ -88,8 +87,11 @@ pub mod example_program {
vec_of_option: Vec<Option<u64>>,
box_field: Box<bool>,
) -> Result<SomeRetStruct> {
ctx.accounts.state.set_inner(State2 { vec_of_option, box_field });
Ok(SomeRetStruct { some_field: 3})
ctx.accounts.state.set_inner(State2 {
vec_of_option,
box_field,
});
Ok(SomeRetStruct { some_field: 3 })
}
pub fn cause_error(_ctx: Context<CauseError>) -> Result<()> {
@ -306,7 +308,7 @@ mod some_other_module {
#[event]
pub struct SomeEvent {
bool_field: bool,
external_baz: some_external_program::Baz,
external_baz: external::Baz,
other_module_baz: some_other_module::Baz,
}

View File

@ -13,9 +13,7 @@ name = "relations_derivation"
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
idl-build = [
"anchor-lang/idl-build",
]
idl-build = ["anchor-lang/idl-build"]
default = []
[dependencies]

View File

@ -1,9 +1,6 @@
//! The typescript example serves to show how one would setup an Anchor
//! workspace with TypeScript tests and migrations.
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
declare_id!("Re1ationsDerivation111111111111111111111111");
#[program]
pub mod relations_derivation {
@ -14,6 +11,7 @@ pub mod relations_derivation {
ctx.accounts.account.bump = ctx.bumps["account"];
Ok(())
}
pub fn test_relation(_ctx: Context<TestRelation>) -> Result<()> {
Ok(())
}
@ -32,7 +30,7 @@ pub struct InitBase<'info> {
bump,
)]
account: Account<'info, MyAccount>,
system_program: Program<'info, System>
system_program: Program<'info, System>,
}
#[derive(Accounts)]
@ -60,9 +58,8 @@ pub struct TestRelation<'info> {
nested: Nested<'info>,
}
#[account]
pub struct MyAccount {
pub my_account: Pubkey,
pub bump: u8
pub bump: u8,
}

View File

@ -2,24 +2,25 @@
set -x
set -e
TMPDIR=$(mktemp -d)
idls_dir=idls
tmp_dir=$(mktemp -d)
cd programs/idl
anchor idl parse --file src/lib.rs -o $TMPDIR/idl_parse_act.json
anchor idl build -o $TMPDIR/idl_build_act.json
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 $TMPDIR/generics_build_act.json
anchor idl build -o $tmp_dir/generics_build_act.json
cd ../relations-derivation
anchor idl build -o $TMPDIR/relations_build_act.json
anchor idl build -o $tmp_dir/relations_build_act.json
cd ../..
echo "----------------------------------------------------"
echo "idl parse before > after"
echo "----------------------------------------------------"
echo ""
diff -y --color tests/testdata/idl_parse_exp.json $TMPDIR/idl_parse_act.json
diff -y --color $idls_dir/idl_parse_exp.json $tmp_dir/idl_parse_act.json
PARSE_RETCODE=$?
echo ""
@ -28,7 +29,7 @@ echo "----------------------------------------------------"
echo "idl build before > after"
echo "----------------------------------------------------"
echo ""
diff -y --color tests/testdata/idl_build_exp.json $TMPDIR/idl_build_act.json
diff -y --color $idls_dir/idl_build_exp.json $tmp_dir/idl_build_act.json
GEN_RETCODE=$?
echo ""
@ -37,7 +38,7 @@ echo "----------------------------------------------------"
echo "idl generics build before > after"
echo "----------------------------------------------------"
echo ""
diff -y --color tests/testdata/generics_build_exp.json $TMPDIR/generics_build_act.json
diff -y --color $idls_dir/generics_build_exp.json $tmp_dir/generics_build_act.json
GEN_GENERICS_RETCODE=$?
echo ""
@ -46,7 +47,7 @@ echo "----------------------------------------------------"
echo "idl relations build before > after"
echo "----------------------------------------------------"
echo ""
diff -y --color tests/testdata/relations_build_exp.json $TMPDIR/relations_build_act.json
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

View File

@ -1,7 +1,6 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",

View File

@ -18,7 +18,7 @@
"escrow",
"events",
"floats",
"idl-build",
"idl",
"ido-pool",
"interface",
"lockup",