Răsfoiți Sursa

tests: Refactor IDL tests (#2573)

acheron 2 ani în urmă
părinte
comite
c306463432

+ 2 - 2
.github/workflows/reusable-tests.yaml

@@ -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/

+ 0 - 13
tests/idl-build/Anchor.toml

@@ -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"

+ 0 - 11
tests/idl-build/gen_testdata.sh

@@ -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 - 0
tests/idl/Anchor.toml

@@ -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"

+ 0 - 0
tests/idl-build/Cargo.toml → tests/idl/Cargo.toml


+ 13 - 0
tests/idl/generate.sh

@@ -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

+ 13 - 16
tests/idl-build/tests/testdata/generics_build_exp.json → tests/idl/idls/generics_build_exp.json

@@ -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"
-          }
-        ]
-      }
     }
   ]
 }

+ 15 - 15
tests/idl-build/tests/testdata/idl_build_exp.json → tests/idl/idls/idl_build_exp.json

@@ -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
         },

+ 3 - 3
tests/idl-build/tests/testdata/idl_parse_exp.json → tests/idl/idls/idl_parse_exp.json

@@ -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
         },

+ 0 - 0
tests/idl-build/tests/testdata/relations_build_exp.json → tests/idl/idls/relations_build_exp.json


+ 1 - 1
tests/idl-build/package.json → tests/idl/package.json

@@ -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",

+ 2 - 3
tests/idl-build/programs/some_external_program/Cargo.toml → tests/idl/programs/external/Cargo.toml

@@ -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" }
-

+ 0 - 0
tests/idl-build/programs/generics/Xargo.toml → tests/idl/programs/external/Xargo.toml


+ 2 - 2
tests/idl-build/programs/some_external_program/src/lib.rs → tests/idl/programs/external/src/lib.rs

@@ -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<()> {

+ 2 - 5
tests/idl-build/programs/generics/Cargo.toml → tests/idl/programs/generics/Cargo.toml

@@ -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"] }

+ 0 - 0
tests/idl-build/programs/idl/Xargo.toml → tests/idl/programs/generics/Xargo.toml


+ 7 - 12
tests/idl-build/programs/generics/src/lib.rs → tests/idl/programs/generics/src/lib.rs

@@ -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]),
 }

+ 2 - 5
tests/idl-build/programs/idl/Cargo.toml → tests/idl/programs/idl/Cargo.toml

@@ -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"] }

+ 0 - 0
tests/idl-build/programs/some_external_program/Xargo.toml → tests/idl/programs/idl/Xargo.toml


+ 10 - 8
tests/idl-build/programs/idl/src/lib.rs → tests/idl/programs/idl/src/lib.rs

@@ -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,
 }
 
@@ -323,4 +325,4 @@ pub struct SomeZcAccount {
 #[derive(AnchorSerialize, AnchorDeserialize, Clone)]
 pub struct SomeRetStruct {
     pub some_field: u8,
-}
+}

+ 1 - 3
tests/idl-build/programs/relations-derivation/Cargo.toml → tests/idl/programs/relations-derivation/Cargo.toml

@@ -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]

+ 0 - 0
tests/idl-build/programs/relations-derivation/Xargo.toml → tests/idl/programs/relations-derivation/Xargo.toml


+ 4 - 7
tests/idl-build/programs/relations-derivation/src/lib.rs → tests/idl/programs/relations-derivation/src/lib.rs

@@ -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,
 }

+ 10 - 9
tests/idl-build/test.sh → tests/idl/test.sh

@@ -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

+ 0 - 1
tests/idl-build/tsconfig.json → tests/idl/tsconfig.json

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

+ 1 - 1
tests/package.json

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