Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions contrib/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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=$(mktemp -d)

echo "==> Building Bitcoin Core in $BITCOIN_SRC"
(
Expand All @@ -31,17 +32,17 @@ 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
echo "==> Starting bitcoin node (regtest, IPC) with -datadir=$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
)
26 changes: 16 additions & 10 deletions tests/util/bitcoin_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions tests/util/bitcoin_core_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}"));
}
Expand Down
Loading