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

Add test-dev npm script for easier per-test testing #343

Merged
merged 17 commits into from
Apr 28, 2023
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ starknet-hardhat-example
starknet-hardhat-example-link
cache
.vscode
my-venv
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"test-venv-tests": "TEST_SUBDIR=venv-tests ./scripts/test.sh",
"test-integrated-devnet-tests": "TEST_SUBDIR=integrated-devnet-tests ./scripts/test.sh",
"test-recompilation-tests": "TEST_SUBDIR=recompilation-tests ./scripts/test.sh",
"test-dev": "./scripts/test-dev.sh",
"build": "rm -rf dist && tsc && npm run copy-files",
"copy-files": "cp src/*.py dist/src/",
"lint": "eslint $(git ls-files '*.ts')",
Expand Down
7 changes: 7 additions & 0 deletions scripts/devnet-restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
HOST=127.0.0.1
PORT=5050

echo "Emptying state on Devnet at $HOST:$PORT"

curl -X POST "http://$HOST:$PORT/restart"
48 changes: 48 additions & 0 deletions scripts/devnet-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# This script starts devnet in background and checks until it is responsive.
# Outputs the PID of the bg process.

set -eu

HOST=127.0.0.1
PORT=5050

if [[ -n "${STARKNET_HARDHAT_DEV:-}" ]]; then
echo "Running dockerized Devnet..."
# Get default from config.json file
# We may be inside example dir or main plugin dir
# This script needs to be generic and work in both cases
if [[ -e "./config.json" ]]; then
STARKNET_DEVNET_DEFAULT=$(node -e "console.log(require('./config.json').STARKNET_DEVNET)")
else
STARKNET_DEVNET_DEFAULT=$(node -e "console.log(require('../config.json').STARKNET_DEVNET)")
fi

STARKNET_DEVNET="${STARKNET_DEVNET:=$STARKNET_DEVNET_DEFAULT}"
docker pull -q shardlabs/starknet-devnet:$STARKNET_DEVNET
container_id=$(docker run --rm --name starknet_hardhat_devnet -d -p 0.0.0.0:$PORT:$PORT shardlabs/starknet-devnet --seed 42)
echo "Running devnet in container starknet_hardhat_devnet $container_id"

else
starknet-devnet --host $HOST --port $PORT --seed 42 >/dev/null 2>&1 &
echo "Spawned devnet with PID $!"
fi

echo "Waiting for devnet... "
sleep 1
for ((i = 0 ; i < 35 ; i++)); do
echo -ne "\r $((i+1)) "

if is_alive=$(curl -s -w "\n" "http://$HOST:$PORT/is_alive"); then
echo "$is_alive"
break
else
sleep 1
fi
done

if [[ $i -ge 35 ]]; then
echo "Failed to run devnet :("
exit 1
fi
10 changes: 10 additions & 0 deletions scripts/devnet-stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

if [[ -n "${STARKNET_HARDHAT_DEV:-}" ]]; then
# Stop docker devnet container
docker rm -f starknet_hardhat_devnet
else
# Kill devnet process
# specifying signal for pkill fails on mac
pkill -f starknet-devnet
fi
14 changes: 0 additions & 14 deletions scripts/run-devnet.sh

This file was deleted.

1 change: 0 additions & 1 deletion scripts/setup-cairo1-compiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ if [ "$TEST_SUBDIR" == "configuration-tests" ]; then
-- \
--version
fi

107 changes: 107 additions & 0 deletions scripts/test-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/bin/bash

TEST_SUBDIR_PENDING="true"
TEST_NAME_PENDING="true"

# Flag: dev mode for testing (and not usual CI)
export STARKNET_HARDHAT_DEV=1
shramee marked this conversation as resolved.
Show resolved Hide resolved
export STARKNET_HARDHAT_DEV_NETWORK="integrated-devnet"

cd test

# Loops until a suitable TEST_SUBDIR is provided
while [[ -n $TEST_SUBDIR_PENDING ]]; do
shramee marked this conversation as resolved.
Show resolved Hide resolved
echo "(Tab to autocomplete)"
read -e -p "Test suite: " TEST_SUBDIR
TEST_SUBDIR="${TEST_SUBDIR%/}"

if [[ -d $TEST_SUBDIR ]]; then
if [[ $TEST_SUBDIR != "${TEST_SUBDIR%-tests}" ]]; then
TEST_SUBDIR_PENDING=""
fi
else
echo ""
echo "Please pick from,"
ls | grep '\-tests' | awk '{print " - "$1}'
echo ""
fi
done

TEST_SUBDIR="${TEST_SUBDIR%/}" # remove trailing slash

cd $TEST_SUBDIR

# Loops until a suitable test_name is provided
while [[ -n $TEST_NAME_PENDING ]]; do
shramee marked this conversation as resolved.
Show resolved Hide resolved
echo ""
echo "(Tab to autocomplete)"
read -e -p "Test: " test_name
test_name="${test_name%/}"

if [[ -d $test_name ]]; then
echo "Running test $test_name from $TEST_SUBDIR"
TEST_NAME_PENDING=""
else
echo "Please pick from,"
ls | awk '{print " - "$1}'
echo ""
fi
done

cd ../..

test_name="${test_name%/}" # remove trailing slash
RUN_SETUP="y"
CONFIG_FILE_NAME="hardhat.config.ts"

if [[ -d starknet-hardhat-example/ ]]; then
echo ""
read -e -p "Example repo found, y to force run setup: " RUN_SETUP
shramee marked this conversation as resolved.
Show resolved Hide resolved
fi

if [[ "y" == "$RUN_SETUP" ]]; then
echo ""
source ./scripts/setup-cairo1-compiler.sh
rm -rf starknet-hardhat-example
git clone -b "${EXAMPLE_REPO_BRANCH:=plugin}" --single-branch https://github.com/0xSpaceShard/starknet-hardhat-example.git
cd starknet-hardhat-example
npm ci
npm install ../ # install plugin from source (parent dir)
cd ..
else
echo "Skipped setup."
fi

pwd

echo ""
echo "If your test needs devnet (and not integrated-devnet),"
shramee marked this conversation as resolved.
Show resolved Hide resolved
read -e -p "y to run devnet: " RUN_DEVNET

if [[ "y" == "$RUN_DEVNET" ]]; then
export STARKNET_HARDHAT_DEV_NETWORK="devnet"
./scripts/devnet-run.sh
echo ""
echo "Devnet running, to stop devnet use,"
echo "+--------------------------------------+"
echo "| docker rm -f starknet_hardhat_devnet |"
echo "+--------------------------------------+"
fi

CONTINUE_TESTING="1"
while [[ "q" != "$CONTINUE_TESTING" ]]; do
echo ""
echo "Updating starknet-hardhat-plugin,"
cd starknet-hardhat-example
npm install ../ # install plugin from source (parent dir)
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
cd ..
echo ""
TEST_SUBDIR=$TEST_SUBDIR ./scripts/test.sh $test_name
echo ""
echo "----------------------------------------------"
echo ""
read -e -p "q to gracefully shutdown. Re-run test? " CONTINUE_TESTING
done

docker rm -f starknet_hardhat_devnet
echo "Devnet stopped."
27 changes: 27 additions & 0 deletions scripts/test-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
trap 'for killable in $(jobs -p); do kill -9 $killable; done' EXIT
shramee marked this conversation as resolved.
Show resolved Hide resolved

./scripts/ensure-python.sh

# setup example repo
rm -rf starknet-hardhat-example
EXAMPLE_REPO_BRANCH="plugin"
if [[ "$CIRCLE_BRANCH" == "master" ]] && [[ "$EXAMPLE_REPO_BRANCH" != "plugin" ]]; then
echo "Invalid example repo branch: $EXAMPLE_REPO_BRANCH"
exit 1
fi

git clone -b "$EXAMPLE_REPO_BRANCH" --single-branch git@github.com:0xSpaceShard/starknet-hardhat-example.git
cd starknet-hardhat-example
git log -n 1
npm ci
npm install ../ # install plugin from source (parent dir)

# if docker is available on the system pull docker image
CAIRO_CLI_DOCKER_REPOSITORY_WITH_TAG=$(node -e "console.log(require('../dist/src/constants.js').CAIRO_CLI_DOCKER_REPOSITORY_WITH_TAG)")

if docker --version >/dev/null 2>&1; then
docker pull "$CAIRO_CLI_DOCKER_REPOSITORY_WITH_TAG"
fi

# used by some cases
../scripts/setup-venv.sh
Loading