Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana: Add Typescript test cases with more coverage to Solana CI/ Anchor Test #482

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Next Next commit
solana: Add testing script and common utils
nvsriram committed Jul 23, 2024
commit bb05baa0422e50a3a8c416cc7ad3f3f22c82876c
13 changes: 13 additions & 0 deletions solana/run-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# Run each tests/*.test.ts file separately to avoid account state persisting between tests
for file in `ls tests/*.test.ts`
do
# convert file-name to FILE_NAME
declare -u env_flag
filename=$(basename -- "$file")
filename="${filename%.test.*}"
env_flag=${filename//-/_}

env $env_flag=1 bash -c 'anchor test --skip-build'
done
41 changes: 41 additions & 0 deletions solana/tests/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
ChainContext,
Signer,
signSendWait as ssw,
} from "@wormhole-foundation/sdk";
import path from "path";

const TESTFILE_MATCH_PATTERN = /.test.ts$/;

/**
* Skips test file execution if the corresponding environment variable is not set.
*
* eg:- To run `file-name.test.ts`, `FILE_NAME` environment variable should be set
*/
export const handleTestSkip = (filename: string) => {
const testName = path.basename(filename).replace(TESTFILE_MATCH_PATTERN, "");
const envVar = testName.replaceAll("-", "_").toUpperCase();
const shouldRun = process.env[envVar];
if (!shouldRun) {
test.only("Skipping all tests", () => {});
}
};

export const signSendWait = async (
chain: ChainContext<any, any, any>,
txs: AsyncGenerator<any>,
signer: Signer,
shouldLog = true,
shouldThrow = false
) => {
try {
await ssw(chain, txs, signer);
} catch (e) {
if (shouldLog) {
console.error(e);
}
if (shouldThrow) {
throw e;
}
}
};