Skip to content

Commit bb05baa

Browse files
committed
solana: Add testing script and common utils
1 parent c88f2ed commit bb05baa

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

solana/run-tests

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Run each tests/*.test.ts file separately to avoid account state persisting between tests
4+
for file in `ls tests/*.test.ts`
5+
do
6+
# convert file-name to FILE_NAME
7+
declare -u env_flag
8+
filename=$(basename -- "$file")
9+
filename="${filename%.test.*}"
10+
env_flag=${filename//-/_}
11+
12+
env $env_flag=1 bash -c 'anchor test --skip-build'
13+
done

solana/tests/utils/index.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
ChainContext,
3+
Signer,
4+
signSendWait as ssw,
5+
} from "@wormhole-foundation/sdk";
6+
import path from "path";
7+
8+
const TESTFILE_MATCH_PATTERN = /.test.ts$/;
9+
10+
/**
11+
* Skips test file execution if the corresponding environment variable is not set.
12+
*
13+
* eg:- To run `file-name.test.ts`, `FILE_NAME` environment variable should be set
14+
*/
15+
export const handleTestSkip = (filename: string) => {
16+
const testName = path.basename(filename).replace(TESTFILE_MATCH_PATTERN, "");
17+
const envVar = testName.replaceAll("-", "_").toUpperCase();
18+
const shouldRun = process.env[envVar];
19+
if (!shouldRun) {
20+
test.only("Skipping all tests", () => {});
21+
}
22+
};
23+
24+
export const signSendWait = async (
25+
chain: ChainContext<any, any, any>,
26+
txs: AsyncGenerator<any>,
27+
signer: Signer,
28+
shouldLog = true,
29+
shouldThrow = false
30+
) => {
31+
try {
32+
await ssw(chain, txs, signer);
33+
} catch (e) {
34+
if (shouldLog) {
35+
console.error(e);
36+
}
37+
if (shouldThrow) {
38+
throw e;
39+
}
40+
}
41+
};

0 commit comments

Comments
 (0)