فهرست منبع

tests: Add CLI IDL command tests (#1808)

Tom Linton 3 سال پیش
والد
کامیت
b885c7b68c

+ 4 - 2
.github/workflows/no-cashing-tests.yaml

@@ -174,7 +174,7 @@ jobs:
   test-misc-non-rent-exempt:
   test-misc-non-rent-exempt:
     # the anchor cli is built with a different solana version
     # the anchor cli is built with a different solana version
     # but that's fine since it's just the cli
     # but that's fine since it's just the cli
-    needs: setup-anchor-cli 
+    needs: setup-anchor-cli
     name: Test tests/misc/nonRentExempt
     name: Test tests/misc/nonRentExempt
     runs-on: ubuntu-18.04
     runs-on: ubuntu-18.04
     timeout-minutes: 30
     timeout-minutes: 30
@@ -198,7 +198,7 @@ jobs:
           name: anchor-binary-no-caching
           name: anchor-binary-no-caching
           path: ~/.cargo/bin/
           path: ~/.cargo/bin/
       - run: chmod +x ~/.cargo/bin/anchor
       - run: chmod +x ~/.cargo/bin/anchor
-      
+
       - run: cd tests/misc && yarn --frozen-lockfile
       - run: cd tests/misc && yarn --frozen-lockfile
       - run: cd tests/misc
       - run: cd tests/misc
       - run: cd tests/misc && chmod +x ci.sh && ./ci.sh
       - run: cd tests/misc && chmod +x ci.sh && ./ci.sh
@@ -289,6 +289,8 @@ jobs:
             path: tests/multiple-suites
             path: tests/multiple-suites
           - cmd: cd tests/pda-derivation && anchor test --skip-lint && npx tsc --noEmit
           - cmd: cd tests/pda-derivation && anchor test --skip-lint && npx tsc --noEmit
             path: tests/pda-derivation
             path: tests/pda-derivation
+          - cmd: cd tests/anchor-cli-idl && ./test.sh
+            path: tests/anchor-cli-idl
     steps:
     steps:
       - uses: actions/checkout@v2
       - uses: actions/checkout@v2
       - uses: ./.github/actions/setup/
       - uses: ./.github/actions/setup/

+ 5 - 3
.github/workflows/tests.yaml

@@ -254,7 +254,7 @@ jobs:
   test-misc-non-rent-exempt:
   test-misc-non-rent-exempt:
     # the anchor cli is built with a different solana version
     # the anchor cli is built with a different solana version
     # but that's fine since it's just the cli
     # but that's fine since it's just the cli
-    needs: setup-anchor-cli 
+    needs: setup-anchor-cli
     name: Test tests/misc/nonRentExempt
     name: Test tests/misc/nonRentExempt
     runs-on: ubuntu-18.04
     runs-on: ubuntu-18.04
     timeout-minutes: 30
     timeout-minutes: 30
@@ -293,7 +293,7 @@ jobs:
         with:
         with:
           path: tests/misc/target
           path: tests/misc/target
           key: cargo-${{ runner.os }}-tests/misc-${{ env.ANCHOR_VERSION }}-1.8.14-${{ hashFiles('**/Cargo.lock') }}
           key: cargo-${{ runner.os }}-tests/misc-${{ env.ANCHOR_VERSION }}-1.8.14-${{ hashFiles('**/Cargo.lock') }}
-      
+
       - run: cd tests/misc && yarn --frozen-lockfile
       - run: cd tests/misc && yarn --frozen-lockfile
       - run: cd tests/misc
       - run: cd tests/misc
       - run: cd tests/misc && chmod +x ci.sh && ./ci.sh
       - run: cd tests/misc && chmod +x ci.sh && ./ci.sh
@@ -396,6 +396,8 @@ jobs:
             path: tests/multiple-suites
             path: tests/multiple-suites
           - cmd: cd tests/pda-derivation && anchor test --skip-lint && npx tsc --noEmit
           - cmd: cd tests/pda-derivation && anchor test --skip-lint && npx tsc --noEmit
             path: tests/pda-derivation
             path: tests/pda-derivation
+          - cmd: cd tests/anchor-cli-idl && ./test.sh
+            path: tests/anchor-cli-idl
     steps:
     steps:
       - uses: actions/checkout@v2
       - uses: actions/checkout@v2
       - uses: ./.github/actions/setup/
       - uses: ./.github/actions/setup/
@@ -419,7 +421,7 @@ jobs:
           name: anchor-binary
           name: anchor-binary
           path: ~/.cargo/bin/
           path: ~/.cargo/bin/
       - run: chmod +x ~/.cargo/bin/anchor
       - run: chmod +x ~/.cargo/bin/anchor
-      
+
       - uses: actions/cache@v2
       - uses: actions/cache@v2
         name: Cache ${{ matrix.node.path }} target
         name: Cache ${{ matrix.node.path }} target
         id: cache-test-target
         id: cache-test-target

+ 1 - 1
cli/src/lib.rs

@@ -2493,7 +2493,7 @@ fn create_idl_account(
         );
         );
         client.send_and_confirm_transaction_with_spinner_and_config(
         client.send_and_confirm_transaction_with_spinner_and_config(
             &tx,
             &tx,
-            CommitmentConfig::confirmed(),
+            CommitmentConfig::finalized(),
             RpcSendTransactionConfig {
             RpcSendTransactionConfig {
                 skip_preflight: true,
                 skip_preflight: true,
                 ..RpcSendTransactionConfig::default()
                 ..RpcSendTransactionConfig::default()

+ 16 - 0
tests/anchor-cli-idl/Anchor.toml

@@ -0,0 +1,16 @@
+[features]
+seeds = false
+
+[programs.localnet]
+idl_commands_one = "2uA3amp95zsEHUpo8qnLMhcFAUsiKVEcKHXS1JetFjU5"
+idl_commands_two = "DE4UbHnAcT6Kfh1fVTPRPwpiA3vipmQ4xR3gcLwX3wwS"
+
+[registry]
+url = "https://anchor.projectserum.com"
+
+[provider]
+cluster = "localnet"
+wallet = "./keypairs/deployer-keypair.json"
+
+[scripts]
+test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

+ 7 - 0
tests/anchor-cli-idl/Cargo.toml

@@ -0,0 +1,7 @@
+[profile.release]
+overflow-checks = true
+
+[workspace]
+members = [
+    "programs/*"
+]

+ 1 - 0
tests/anchor-cli-idl/keypairs/deployer-keypair.json

@@ -0,0 +1 @@
+[164,152,29,200,163,125,1,132,86,139,100,111,64,167,29,222,179,216,47,111,217,86,2,178,218,42,72,47,0,153,173,236,13,61,210,33,74,223,99,110,68,220,145,123,246,228,31,59,49,171,192,81,98,88,48,234,139,75,103,103,37,74,182,34]

+ 1 - 0
tests/anchor-cli-idl/keypairs/idl_commands_one-keypair.json

@@ -0,0 +1 @@
+[94,216,182,223,16,41,49,136,185,3,66,213,109,246,240,174,89,197,124,116,127,169,30,24,121,73,68,181,56,13,95,164,28,56,29,156,175,177,255,55,3,211,19,248,245,194,166,177,104,58,249,124,97,88,18,11,252,61,240,174,10,218,5,202]

+ 1 - 0
tests/anchor-cli-idl/keypairs/idl_commands_two-keypair.json

@@ -0,0 +1 @@
+[97,131,36,142,66,37,137,233,196,84,65,128,4,46,96,251,52,1,254,169,72,193,88,186,252,19,204,232,29,55,2,86,181,164,185,63,247,168,27,24,156,83,149,184,17,138,206,99,52,0,13,42,107,119,126,35,251,52,134,231,178,93,7,29]

+ 12 - 0
tests/anchor-cli-idl/migrations/deploy.ts

@@ -0,0 +1,12 @@
+// Migrations are an early feature. Currently, they're nothing more than this
+// single deploy script that's invoked from the CLI, injecting a provider
+// configured from the workspace's Anchor.toml.
+
+const anchor = require("@project-serum/anchor");
+
+module.exports = async function (provider) {
+  // Configure client to use the provider.
+  anchor.setProvider(provider);
+
+  // Add your deploy script here.
+};

+ 22 - 0
tests/anchor-cli-idl/package.json

@@ -0,0 +1,22 @@
+{
+  "name": "anchor-cli-idl",
+  "version": "0.24.2",
+  "license": "(MIT OR Apache-2.0)",
+  "homepage": "https://github.com/project-serum/anchor#readme",
+  "bugs": {
+    "url": "https://github.com/project-serum/anchor/issues"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/project-serum/anchor.git"
+  },
+  "engines": {
+    "node": ">=11"
+  },
+  "scripts": {
+    "test": "./test.sh"
+  },
+  "dependencies": {
+    "mocha": "^9.1.3"
+  }
+}

+ 19 - 0
tests/anchor-cli-idl/programs/idl-commands-one/Cargo.toml

@@ -0,0 +1,19 @@
+[package]
+name = "idl-commands-one"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "idl_commands_one"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+
+[dependencies]
+anchor-lang = { path = "../../../../lang" }

+ 2 - 0
tests/anchor-cli-idl/programs/idl-commands-one/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 15 - 0
tests/anchor-cli-idl/programs/idl-commands-one/src/lib.rs

@@ -0,0 +1,15 @@
+use anchor_lang::prelude::*;
+
+declare_id!("2uA3amp95zsEHUpo8qnLMhcFAUsiKVEcKHXS1JetFjU5");
+
+#[program]
+pub mod idl_commands_one {
+    use super::*;
+
+    pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct Initialize {}

+ 19 - 0
tests/anchor-cli-idl/programs/idl-commands-two/Cargo.toml

@@ -0,0 +1,19 @@
+[package]
+name = "idl-commands-two"
+version = "0.1.0"
+description = "Created with Anchor"
+edition = "2021"
+
+[lib]
+crate-type = ["cdylib", "lib"]
+name = "idl_commands_two"
+
+[features]
+no-entrypoint = []
+no-idl = []
+no-log-ix-name = []
+cpi = ["no-entrypoint"]
+default = []
+
+[dependencies]
+anchor-lang = { path = "../../../../lang" }

+ 2 - 0
tests/anchor-cli-idl/programs/idl-commands-two/Xargo.toml

@@ -0,0 +1,2 @@
+[target.bpfel-unknown-unknown.dependencies.std]
+features = []

+ 15 - 0
tests/anchor-cli-idl/programs/idl-commands-two/src/lib.rs

@@ -0,0 +1,15 @@
+use anchor_lang::prelude::*;
+
+declare_id!("DE4UbHnAcT6Kfh1fVTPRPwpiA3vipmQ4xR3gcLwX3wwS");
+
+#[program]
+pub mod idl_commands_two {
+    use super::*;
+
+    pub fn uninitialize(_ctx: Context<Initialize>) -> Result<()> {
+        Ok(())
+    }
+}
+
+#[derive(Accounts)]
+pub struct Initialize {}

+ 26 - 0
tests/anchor-cli-idl/test.sh

@@ -0,0 +1,26 @@
+#!/bin/bash
+
+# Write a keypair for program deploy
+mkdir -p target/deploy
+cp keypairs/idl_commands_one-keypair.json target/deploy
+
+echo "Building programs"
+
+anchor build
+
+echo "Starting local validator for test"
+
+solana-test-validator --reset \
+  -q \
+  --mint tgyXxAhCkpgtKCEi4W6xWJSzqwVGs3uk2RodbZP2J49 \
+  --bpf-program 2uA3amp95zsEHUpo8qnLMhcFAUsiKVEcKHXS1JetFjU5 target/deploy/idl_commands_one.so \
+  --bpf-program DE4UbHnAcT6Kfh1fVTPRPwpiA3vipmQ4xR3gcLwX3wwS target/deploy/idl_commands_one.so \
+  &
+
+sleep 10
+
+echo "Running tests"
+
+anchor test --skip-deploy --skip-local-validator
+
+trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT

+ 65 - 0
tests/anchor-cli-idl/tests/idl.ts

@@ -0,0 +1,65 @@
+import * as anchor from "@project-serum/anchor";
+import { Program } from "@project-serum/anchor";
+import { IdlCommandsOne } from "../target/types/idl_commands_one";
+import { IdlCommandsTwo } from "../target/types/idl_commands_two";
+import { assert } from "chai";
+import { execSync } from "child_process";
+
+describe("Test CLI IDL commands", () => {
+  // Configure the client to use the local cluster.
+  const provider = anchor.AnchorProvider.env();
+
+  anchor.setProvider(provider);
+
+  const programOne = anchor.workspace.IdlCommandsOne as Program<IdlCommandsOne>;
+  const programTwo = anchor.workspace.IdlCommandsTwo as Program<IdlCommandsTwo>;
+
+  it("Can initialize IDL account", async () => {
+    execSync(
+      `anchor idl init --filepath target/idl/idl_commands_one.json ${programOne.programId}`,
+      { stdio: "inherit" }
+    );
+  });
+
+  it("Can fetch an IDL using the TypeScript client", async () => {
+    const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
+    assert.deepEqual(idl, programOne.idl);
+  });
+
+  it("Can fetch an IDL via the CLI", async () => {
+    const idl = execSync(`anchor idl fetch ${programOne.programId}`).toString();
+    assert.deepEqual(JSON.parse(idl), programOne.idl);
+  });
+
+  it("Can write a new IDL using the upgrade command", async () => {
+    // Upgrade the IDL of program one to the IDL of program two to test upgrade
+    execSync(
+      `anchor idl upgrade --filepath target/idl/idl_commands_two.json ${programOne.programId}`,
+      { stdio: "inherit" }
+    );
+    const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
+    assert.deepEqual(idl, programTwo.idl);
+  });
+
+  it("Can write a new IDL using write-buffer and set-buffer", async () => {
+    // "Upgrade" back to program one via write-buffer set-buffer
+    let buffer = execSync(
+      `anchor idl write-buffer --filepath target/idl/idl_commands_one.json ${programOne.programId}`
+    ).toString();
+    buffer = buffer.replace("Idl buffer created: ", "").trim();
+    execSync(
+      `anchor idl set-buffer --buffer ${buffer} ${programOne.programId}`,
+      { stdio: "inherit" }
+    );
+    const idl = await anchor.Program.fetchIdl(programOne.programId, provider);
+    assert.deepEqual(idl, programOne.idl);
+  });
+
+  it("Can fetch an IDL authority via the CLI", async () => {
+    const authority = execSync(`anchor idl authority ${programOne.programId}`)
+      .toString()
+      .trim();
+
+    assert.equal(authority, provider.wallet.publicKey.toString());
+  });
+});

+ 10 - 0
tests/anchor-cli-idl/tsconfig.json

@@ -0,0 +1,10 @@
+{
+  "compilerOptions": {
+    "types": ["mocha", "chai"],
+    "typeRoots": ["./node_modules/@types"],
+    "lib": ["es2015"],
+    "module": "commonjs",
+    "target": "es6",
+    "esModuleInterop": true
+  }
+}

+ 1 - 0
tests/package.json

@@ -6,6 +6,7 @@
     "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
     "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
   },
   },
   "workspaces": [
   "workspaces": [
+    "anchor-cli-idl",
     "cashiers-check",
     "cashiers-check",
     "cfo",
     "cfo",
     "chat",
     "chat",