Skip to content

Commit

Permalink
examples, tests: Setup prettier (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlinton committed Dec 15, 2021
1 parent efd3721 commit 713d436
Show file tree
Hide file tree
Showing 40 changed files with 810 additions and 710 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
- run: cd ts && yarn
- run: cd ts && yarn test
- run: cd ts && yarn lint
- run: cd examples/tutorial && yarn
- run: cd examples/tutorial && yarn lint
- run: cd tests && yarn
- run: cd tests && yarn lint

setup-anchor-cli:
name: Setup Anchor cli
Expand Down
12 changes: 7 additions & 5 deletions examples/tutorial/basic-0/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
// It is not expected users directly test with this example. For a more
// ergonomic example, see `tests/basic-0.js` in this workspace.

const anchor = require('@project-serum/anchor');
const anchor = require("@project-serum/anchor");

// Configure the local cluster.
anchor.setProvider(anchor.Provider.local());

async function main() {
// #region main
// Read the generated IDL.
const idl = JSON.parse(require('fs').readFileSync('./target/idl/basic_0.json', 'utf8'));
const idl = JSON.parse(
require("fs").readFileSync("./target/idl/basic_0.json", "utf8")
);

// Address of the deployed program.
const programId = new anchor.web3.PublicKey('<YOUR-PROGRAM-ID>');
const programId = new anchor.web3.PublicKey("<YOUR-PROGRAM-ID>");

// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);
Expand All @@ -23,5 +25,5 @@ async function main() {
// #endregion main
}

console.log('Running client.');
main().then(() => console.log('Success'));
console.log("Running client.");
main().then(() => console.log("Success"));
42 changes: 22 additions & 20 deletions examples/tutorial/basic-2/tests/basic-2.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,48 @@
const assert = require('assert');
const anchor = require('@project-serum/anchor');
const assert = require("assert");
const anchor = require("@project-serum/anchor");
const { SystemProgram } = anchor.web3;

describe('basic-2', () => {
const provider = anchor.Provider.local()
describe("basic-2", () => {
const provider = anchor.Provider.local();

// Configure the client to use the local cluster.
anchor.setProvider(provider)
anchor.setProvider(provider);

// Counter for the tests.
const counter = anchor.web3.Keypair.generate()
const counter = anchor.web3.Keypair.generate();

// Program for the tests.
const program = anchor.workspace.Basic2
const program = anchor.workspace.Basic2;

it('Creates a counter', async () => {
it("Creates a counter", async () => {
await program.rpc.create(provider.wallet.publicKey, {
accounts: {
counter: counter.publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [counter],
})
});

let counterAccount = await program.account.counter.fetch(counter.publicKey)
let counterAccount = await program.account.counter.fetch(counter.publicKey);

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() === 0)
})
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() === 0);
});

it('Updates a counter', async () => {
it("Updates a counter", async () => {
await program.rpc.increment({
accounts: {
counter: counter.publicKey,
authority: provider.wallet.publicKey,
},
})
});

const counterAccount = await program.account.counter.fetch(counter.publicKey)
const counterAccount = await program.account.counter.fetch(
counter.publicKey
);

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() == 1)
})
})
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() == 1);
});
});
8 changes: 4 additions & 4 deletions examples/tutorial/basic-3/tests/basic-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ describe("basic-3", () => {

// Invoke the puppet master to perform a CPI to the puppet.
await puppetMaster.rpc.pullStrings(new anchor.BN(111), {
accounts: {
puppet: newPuppetAccount.publicKey,
puppetProgram: puppet.programId,
},
accounts: {
puppet: newPuppetAccount.publicKey,
puppetProgram: puppet.programId,
},
});

// Check the state updated.
Expand Down
7 changes: 6 additions & 1 deletion examples/tutorial/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "anchor-examples",
"private": true,
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
},
"workspaces": [
"basic-0",
"basic-1",
Expand All @@ -12,6 +16,7 @@
"@project-serum/anchor": "^0.19.0"
},
"devDependencies": {
"mocha": "^9.1.3"
"mocha": "^9.1.3",
"prettier": "^2.5.1"
}
}
17 changes: 11 additions & 6 deletions examples/tutorial/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@
"@ethersproject/logger" "^5.5.0"
hash.js "1.1.7"

"@project-serum/anchor@^0.18.0":
version "0.18.0"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.18.0.tgz#867144282e59482230f797f73ee9f5634f846061"
integrity sha512-WTm+UB93MoxyCbjnHIibv/uUEoO/5gL4GEtE/aMioLF8Z4i0vCMPnvAN0xpk9VBu3t7ld2DcCE/L+6Z7dwU++w==
"@project-serum/anchor@^0.19.0":
version "0.19.0"
resolved "https://registry.yarnpkg.com/@project-serum/anchor/-/anchor-0.19.0.tgz#79f1fbe7c3134860ccbfe458a0e09daf79644885"
integrity sha512-cs0LBmJOrL9eJ8MRNqitnzbpCT5QEzVdJmiIjfNV5YaGn1K9vISR7DtISj3Bdl3KBdLqii4CTw1mpHdi8iXUCg==
dependencies:
"@project-serum/borsh" "^0.2.2"
"@solana/web3.js" "^1.17.0"
base64-js "^1.5.1"
bn.js "^5.1.2"
bs58 "^4.0.1"
buffer-layout "^1.2.0"
buffer-layout "^1.2.2"
camelcase "^5.3.1"
crypto-hash "^1.3.0"
eventemitter3 "^4.0.7"
Expand Down Expand Up @@ -257,7 +257,7 @@ bs58@^4.0.0, bs58@^4.0.1:
dependencies:
base-x "^3.0.2"

buffer-layout@^1.2.0:
buffer-layout@^1.2.0, buffer-layout@^1.2.2:
version "1.2.2"
resolved "https://registry.yarnpkg.com/buffer-layout/-/buffer-layout-1.2.2.tgz#b9814e7c7235783085f9ca4966a0cfff112259d5"
integrity sha512-kWSuLN694+KTk8SrYvCqwP2WcgQjoRCiF5b4QDvkkz8EmgD+aWAIceGFKMIAdmF/pH+vpgNV3d3kAKorcdAmWA==
Expand Down Expand Up @@ -812,6 +812,11 @@ picomatch@^2.0.4, picomatch@^2.2.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==

prettier@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.5.1.tgz#fff75fa9d519c54cf0fce328c1017d94546bc56a"
integrity sha512-vBZcPRUR5MZJwoyi3ZoyQlc1rXeEck8KgeC9AwwOn+exuxLxq5toTRDTSaVrXHxelDMHy9zlicw8u66yxoSUFg==

randombytes@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/randombytes/-/randombytes-2.1.0.tgz#df6f84372f0270dc65cdf6291349ab7a473d4f2a"
Expand Down
2 changes: 2 additions & 0 deletions tests/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/target/types/*.ts
cfo/deps/
2 changes: 1 addition & 1 deletion tests/bpf-upgradeable-state/migrations/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports = async function (provider) {
anchor.setProvider(provider);

// Add your deploy script here.
}
};
96 changes: 58 additions & 38 deletions tests/bpf-upgradeable-state/tests/bpf-upgradable-state.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,70 @@
import * as anchor from '@project-serum/anchor';
import { Program } from '@project-serum/anchor';
import { findProgramAddressSync } from '@project-serum/anchor/dist/cjs/utils/pubkey';
import { PublicKey } from '@solana/web3.js';
import assert from 'assert';
import { BpfUpgradeableState } from '../target/types/bpf_upgradeable_state';
import * as anchor from "@project-serum/anchor";
import { Program } from "@project-serum/anchor";
import { findProgramAddressSync } from "@project-serum/anchor/dist/cjs/utils/pubkey";
import { PublicKey } from "@solana/web3.js";
import assert from "assert";
import { BpfUpgradeableState } from "../target/types/bpf_upgradeable_state";

describe('bpf_upgradeable_state', () => {
describe("bpf_upgradeable_state", () => {
const provider = anchor.Provider.env();
// Configure the client to use the local cluster.
anchor.setProvider(provider);

const program = anchor.workspace.BpfUpgradeableState as Program<BpfUpgradeableState>;
const program = anchor.workspace
.BpfUpgradeableState as Program<BpfUpgradeableState>;
const programDataAddress = findProgramAddressSync(
[program.programId.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
)[0];

it('Reads ProgramData and sets field', async () => {
it("Reads ProgramData and sets field", async () => {
const settings = anchor.web3.Keypair.generate();
const tx = await program.rpc.setAdminSettings(new anchor.BN(500), {
accounts: {
authority: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
programData: programDataAddress,
program: program.programId,
settings: settings.publicKey
settings: settings.publicKey,
},
signers: [settings]
signers: [settings],
});
assert.equal((await program.account.settings.fetch(settings.publicKey)).adminData, 500);
assert.equal(
(await program.account.settings.fetch(settings.publicKey)).adminData,
500
);
});

it('Reads ProgramData and sets field, uses program state', async () => {
it("Reads ProgramData and sets field, uses program state", async () => {
const settings = anchor.web3.Keypair.generate();
const tx = await program.rpc.setAdminSettingsUseProgramState(new anchor.BN(500), {
accounts: {
authority: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
programData: programDataAddress,
program: program.programId,
settings: settings.publicKey
},
signers: [settings]
});
assert.equal((await program.account.settings.fetch(settings.publicKey)).adminData, 500);
const tx = await program.rpc.setAdminSettingsUseProgramState(
new anchor.BN(500),
{
accounts: {
authority: program.provider.wallet.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
programData: programDataAddress,
program: program.programId,
settings: settings.publicKey,
},
signers: [settings],
}
);
assert.equal(
(await program.account.settings.fetch(settings.publicKey)).adminData,
500
);
});

it('Validates constraint on ProgramData', async () => {
it("Validates constraint on ProgramData", async () => {
const settings = anchor.web3.Keypair.generate();
try {
const authority = anchor.web3.Keypair.generate();
await provider.connection.confirmTransaction(
await provider.connection.requestAirdrop(authority.publicKey, 10000000000),
await provider.connection.requestAirdrop(
authority.publicKey,
10000000000
),
"confirmed"
);
await program.rpc.setAdminSettings(new anchor.BN(500), {
Expand All @@ -62,7 +75,7 @@ describe('bpf_upgradeable_state', () => {
settings: settings.publicKey,
program: program.programId,
},
signers: [settings, authority]
signers: [settings, authority],
});
assert.ok(false);
} catch (err) {
Expand All @@ -71,7 +84,7 @@ describe('bpf_upgradeable_state', () => {
}
});

it('Validates that account is ProgramData', async () => {
it("Validates that account is ProgramData", async () => {
const settings = anchor.web3.Keypair.generate();
try {
await program.rpc.setAdminSettings(new anchor.BN(500), {
Expand All @@ -82,7 +95,7 @@ describe('bpf_upgradeable_state', () => {
settings: settings.publicKey,
program: program.programId,
},
signers: [settings]
signers: [settings],
});
assert.ok(false);
} catch (err) {
Expand All @@ -91,7 +104,7 @@ describe('bpf_upgradeable_state', () => {
}
});

it('Validates that account is owned by the upgradeable bpf loader', async () => {
it("Validates that account is owned by the upgradeable bpf loader", async () => {
const settings = anchor.web3.Keypair.generate();
try {
await program.rpc.setAdminSettings(new anchor.BN(500), {
Expand All @@ -102,17 +115,22 @@ describe('bpf_upgradeable_state', () => {
settings: settings.publicKey,
program: program.programId,
},
signers: [settings]
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 3007);
assert.equal(err.msg, "The given account is not owned by the executing program");
assert.equal(
err.msg,
"The given account is not owned by the executing program"
);
}
});

it('Deserializes UpgradableLoaderState and validates that programData is the expected account', async () => {
const secondProgramAddress = new PublicKey("Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B");
it("Deserializes UpgradableLoaderState and validates that programData is the expected account", async () => {
const secondProgramAddress = new PublicKey(
"Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B"
);
const secondProgramProgramDataAddress = findProgramAddressSync(
[secondProgramAddress.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
Expand All @@ -128,16 +146,18 @@ describe('bpf_upgradeable_state', () => {
settings: settings.publicKey,
program: program.programId,
},
signers: [settings]
signers: [settings],
});
assert.ok(false);
} catch (err) {
assert.equal(err.code, 6000);
}
});

it('Deserializes Program and validates that programData is the expected account', async () => {
const secondProgramAddress = new PublicKey("Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B");
it("Deserializes Program and validates that programData is the expected account", async () => {
const secondProgramAddress = new PublicKey(
"Fkv67TwmbakfZw2PoW57wYPbqNexAH6vuxpyT8vmrc3B"
);
const secondProgramProgramDataAddress = findProgramAddressSync(
[secondProgramAddress.toBytes()],
new anchor.web3.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111")
Expand All @@ -153,7 +173,7 @@ describe('bpf_upgradeable_state', () => {
settings: settings.publicKey,
program: program.programId,
},
signers: [settings]
signers: [settings],
});
assert.ok(false);
} catch (err) {
Expand Down
Loading

0 comments on commit 713d436

Please sign in to comment.