Skip to content

Add test script to contrib folder - #33

Merged
Sjors merged 2 commits into
2140-dev:masterfrom
rustaceanrob:test-startup-shell
Sep 18, 2026
Merged

Sjors merged 2 commits into
2140-dev:masterfrom
rustaceanrob:test-startup-shell

Conversation

@rustaceanrob

Copy link
Copy Markdown
Member

Completing each step to run the integration tests isn't great, and all that is needed is a path to the bitcoin core source code, so this adds a shell script to build and run everything in a single script.

@xyzconstant xyzconstant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 4992ba0

Tested in macOS and Ubuntu. LGTM, left one inline nit.

Also, I think a good addition would be a datadir env config, so it doesn't mess with the default datadir and always runs over a fresh setup.

I have this diff locally with that datadir option, and it works, also the fallback datadir path is unaffected:

diff --git a/contrib/run-tests.sh b/contrib/run-tests.sh
index 4e7c4a9..1076a5d 100755
--- a/contrib/run-tests.sh
+++ b/contrib/run-tests.sh
@@ -15,6 +15,7 @@ fi
 BITCOIN_SRC=$(cd "$1" && pwd)
 REPO_ROOT=$(cd "$(dirname "$0")/.." && pwd)
 BITCOIN_BIN="$BITCOIN_SRC/build/bin/bitcoin"
+TEMP_DATADIR=/tmp/capnp-types-tests
 
 echo "==> Building Bitcoin Core in $BITCOIN_SRC"
 (
@@ -26,17 +27,19 @@ echo "==> Building Bitcoin Core in $BITCOIN_SRC"
 stop_bitcoin() {
     if [ -n "${BITCOIN_STARTED:-}" ]; then
         echo "==> Stopping bitcoin"
-        "$BITCOIN_BIN" rpc -chain=regtest stop || true
+        "$BITCOIN_BIN" rpc -chain=regtest -datadir="$TEMP_DATADIR" stop || true
     fi
 }
 trap stop_bitcoin EXIT
 
 echo "==> Starting bitcoin node (regtest, IPC)"
-"$BITCOIN_BIN" node -chain=regtest -ipcbind=unix -server -debug=ipc -daemon
+rm -rf "$TEMP_DATADIR"
+mkdir -p "$TEMP_DATADIR"
+"$BITCOIN_BIN" node -chain=regtest -ipcbind=unix -server -debug=ipc -datadir="$TEMP_DATADIR" -daemon
 BITCOIN_STARTED=1
 
 echo "==> Running cargo test"
 (
     cd "$REPO_ROOT"
-    BITCOIN_BIN="$BITCOIN_BIN" cargo test
+    BITCOIN_BIN="$BITCOIN_BIN" BITCOIN_DATADIR="$TEMP_DATADIR" cargo test
 )
diff --git a/tests/util/bitcoin_core.rs b/tests/util/bitcoin_core.rs
index b830e16..1efebab 100644
--- a/tests/util/bitcoin_core.rs
+++ b/tests/util/bitcoin_core.rs
@@ -5,7 +5,8 @@ use std::{
 };
 
 use crate::util::bitcoin_core_wallet::{
-    bitcoin_rpc_json, bitcoin_test_wallet, ensure_wallet_loaded, mine_blocks_to_new_address,
+    bitcoin_datadir, bitcoin_rpc_json, bitcoin_test_wallet, ensure_wallet_loaded,
+    mine_blocks_to_new_address,
 };
 use bitcoin_capnp_types::{
     init_capnp::init,
@@ -23,15 +24,20 @@ use tokio_util::compat::{Compat, TokioAsyncReadCompatExt, TokioAsyncWriteCompatE
 static CHAIN_SETUP: Once = Once::new();
 
 pub fn unix_socket_path() -> PathBuf {
-    let home_dir_string = std::env::var("HOME").unwrap();
-    let home_dir = home_dir_string.parse::<PathBuf>().unwrap();
-    let bitcoin_dir = if cfg!(target_os = "macos") {
-        home_dir
-            .join("Library")
-            .join("Application Support")
-            .join("Bitcoin")
-    } else {
-        home_dir.join(".bitcoin")
+    let bitcoin_dir = match bitcoin_datadir() {
+        Some(datadir) => datadir.parse::<PathBuf>().unwrap(),
+        None => {
+            let home_dir_string = std::env::var("HOME").unwrap();
+            let home_dir = home_dir_string.parse::<PathBuf>().unwrap();
+            if cfg!(target_os = "macos") {
+                home_dir
+                    .join("Library")
+                    .join("Application Support")
+                    .join("Bitcoin")
+            } else {
+                home_dir.join(".bitcoin")
+            }
+        }
     };
     let regtest_dir = bitcoin_dir.join("regtest");
     regtest_dir.join("node.sock")
diff --git a/tests/util/bitcoin_core_wallet.rs b/tests/util/bitcoin_core_wallet.rs
index 9c37fdf..e8fb4db 100644
--- a/tests/util/bitcoin_core_wallet.rs
+++ b/tests/util/bitcoin_core_wallet.rs
@@ -9,6 +9,10 @@ fn bitcoin_bin() -> String {
     std::env::var("BITCOIN_BIN").unwrap_or_else(|_| "bitcoin".to_owned())
 }
 
+pub fn bitcoin_datadir() -> Option<String> {
+    std::env::var("BITCOIN_DATADIR").ok()
+}
+
 fn bitcoin_rpc(wallet: Option<&str>, args: &[&str]) -> Result<String, String> {
     let owned_args: Vec<String> = args.iter().map(|arg| (*arg).to_owned()).collect();
     bitcoin_rpc_owned(wallet, &owned_args)
@@ -29,6 +33,9 @@ pub fn bitcoin_test_wallet() -> String {
 fn bitcoin_rpc_owned(wallet: Option<&str>, args: &[String]) -> Result<String, String> {
     let mut command = Command::new(bitcoin_bin());
     command.arg("rpc").arg("-chain=regtest").arg("-rpcwait");
+    if let Some(datadir) = bitcoin_datadir() {
+        command.arg(format!("-datadir={datadir}"));
+    }
     if let Some(wallet) = wallet {
         command.arg(format!("-rpcwallet={wallet}"));
     }

Overall, I think this script is fine since it simplifies the development workflow.

Comment thread contrib/run-tests.sh Resolved
@Sjors

Sjors commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

@rustaceanrob do you want to take the suggestions from @xyzconstant or leave the PR as is?

@rustaceanrob

Copy link
Copy Markdown
Member Author

leave the PR as is

Yes. Perhaps that change can be done as follow up, where we also might want to use this in CI so it does not go stale.

@Sjors

Sjors commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

where we also might want to use this in CI so it does not go stale

That seems better to do now.

@rustaceanrob

Copy link
Copy Markdown
Member Author

Added it to the CI along with ccache flags in the script

@Sjors

Sjors commented Sep 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks, merged and pushed a tweak to master to use --parallel for macOS compatibility.

@Sjors
Sjors merged commit 918afc3 into 2140-dev:master Sep 18, 2026
1 check passed
@rustaceanrob
rustaceanrob deleted the test-startup-shell branch September 18, 2026 10:43
@xyzconstant

Copy link
Copy Markdown
Contributor

Just opened #40 applying the diff I suggested in my review: #33 (review).

@plebhash plebhash mentioned this pull request Oct 23, 2026
bit-aloo pushed a commit that referenced this pull request Oct 23, 2026
bit-aloo pushed a commit that referenced this pull request Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants