Skip to content

Commit

Permalink
add yarn env <plugin-name> (#4852)
Browse files Browse the repository at this point in the history
This new command creates convenient subshells with appropriate
environment variables set to run tests for _one_ plugin at a time.
  • Loading branch information
bengl authored Nov 1, 2024
1 parent 1188ea2 commit 28eb958
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"typings": "index.d.ts",
"scripts": {
"env": "bash ./plugin-env",
"preinstall": "node scripts/preinstall.js",
"bench": "node benchmark",
"bench:profiler": "node benchmark/profiler",
Expand Down
91 changes: 91 additions & 0 deletions plugin-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash
args=("$@")
plugin_name=${args[0]}

YELLOW='\033[33m'
RESET='\033[0m' # No Color

if [ -z "$plugin_name" ]; then
echo "Usage: ./plugin-env <plugin_name>"
echo " <plugin_name> is the name of the dd-trace plugin to enter the dev environment for."
echo ""
echo " It can be one of the following:"
node - << EOF
const fs=require('fs');
const yaml = require('yaml');
const pluginsData = fs.readFileSync('.github/workflows/plugins.yml', 'utf8');
const env=Object.keys(yaml.parse(pluginsData).jobs);
console.log(...env);
EOF
exit 1
fi

if ! hash node 2>/dev/null; then
echo "Node.js is not installed. Please install Node.js before running this script."
echo "You can use nvm to install Node.js. See https://nvm.sh for more information."
echo "For best results, use the latest version of Node.js."
exit 1
fi

if ! hash yarn 2>/dev/null; then
echo "[email protected] is not installed. Please install [email protected] before running this script."
echo "You can install yarn by running 'npm install -g yarn'."
exit 1
fi

read -r PLUGINS SERVICES <<<$(node - << EOF
const fs=require('fs');
const yaml = require('yaml');
const pluginsData = fs.readFileSync('.github/workflows/plugins.yml', 'utf8');
const { PLUGINS, SERVICES } = yaml.parse(pluginsData).jobs['$plugin_name'].env;
console.log(PLUGINS || '', SERVICES || '')
EOF
)

export PLUGINS
export SERVICES

if [ -z "$SERVICES" ]; then
echo "The plugin '$plugin_name' does not have any services defined. Nothing to do here."
else
if ! hash docker 2>/dev/null; then
echo "Docker is not installed. Please install Docker before running this script."
echo "You can install Docker by following the instructions at https://docs.docker.com/get-docker/."
exit 1
fi
if (! docker stats --no-stream >/dev/null); then
echo "The docker daemon is not running. Please start Docker before running this script."
exit 1
fi
if [ -z `docker ps -q --no-trunc | grep $(docker-compose ps -q $SERVICES)` ]; then
teardown=1
docker compose up -d $SERVICES
fi
fi

yarn services

echo -e $YELLOW
echo -e "You are now in a sub-shell (i.e. a dev environment) for the dd-trace plugin '$plugin_name'."
echo -e "The following environment variables set:${RESET}"
echo -e "\tPLUGINS=$PLUGINS"
echo -e "\tSERVICES=$SERVICES"
echo -e "${YELLOW}The ${RESET}versions${YELLOW} directory has been populated, and any ${RESET}\$SERVICES${YELLOW} have been brought up if not already running."
echo -e "You can now run the plugin's tests with:"
echo -e "\t${RESET}yarn test:plugins"
echo -e "${YELLOW}To exit this shell, type 'exit' or do Ctrl+D."
echo -e $RESET

$SHELL

if [ -n "$teardown" ]; then
docker compose stop $SERVICES
fi

echo -e $YELLOW
echo "Exited the sub-shell for the dd-trace plugin '$plugin_name'."
if [ -n "$teardown" ]; then
echo "Also stopped any services that were started."
fi
echo "You're now back in the main shell."
echo -e $RESET

0 comments on commit 28eb958

Please sign in to comment.