Skip to content

Commit

Permalink
Add script
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Nov 13, 2024
1 parent 9f99da1 commit 0892683
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.github
/bin
node_modules
/scripts
/src
/tests

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"storybook:dev": "start-storybook -c ./storybook",
"storybook:build": "build-storybook -c ./storybook -o ./.docs",
"test:e2e": "npx cypress run",
"test:unit": "wp-scripts test-unit-js"
"test:unit": "wp-scripts test-unit-js",
"test:clean-module": "bash scripts/test-clean-module"
}
}
50 changes: 50 additions & 0 deletions scripts/test-clean-module
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

# This script runs Cypress clean tests (reset db before test) for a specific module.

# Source utils
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR"
source ./utils

# Project root
cd ../

# Check if a module is provided
if [ -z "$1" ]; then
print_error "No module provided."
echo "Usage: npm run test:clean-module -- <wp-module-name>"
exit 1
fi

MODULE=$1
MODULE_DIR="vendor/newfold-labs/$MODULE"

# Check if the module exists
if [ ! -d "$MODULE_DIR" ]; then
print_error "Can't find $MODULE in vendor/newfold-labs."
exit 1
fi

# Check if the module has tests
if ! find "$MODULE_DIR/tests" -type f -name "*.cy.js" 2>/dev/null | grep -q .; then
print_error "Can't find any test files in $MODULE_DIR."
exit 1
fi

# Warn the user that the database will be reset
confirm_prompt "${YELLOW}Running a clean test will reset the database. Proceed?${TEXT}"

# Check if the answer is "y" or "Y"
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "Proceeding with tests for ${CYAN}${MODULE}${TEXT}..."

# Run the commands
wp-env stop
wp-env clean all
wp-env start
npm run test:e2e -- --browser chrome --spec "vendor/(newfold-labs/$MODULE/tests/**/*.cy.js)"
else
echo "Cancelled."
exit 0
fi
32 changes: 32 additions & 0 deletions scripts/utils
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# Colors
GREEN='\033[0;32m'
TEXT='\033[0m'
BLUE='\033[0;34m'
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'

print_success() {
echo -e "${GREEN}Success:${TEXT} $1"
}

print_error() {
echo -e "${RED}Error:${TEXT} $1"
}

print_info() {
echo -e "${CYAN}$1"
}

print_warning() {
echo -e "${YELLOW}Warning:${TEXT} $1"
}

confirm_prompt() {
local message=$1
read -p "$(echo -e ${message}) (y/n): " -n 1 -r
echo # Move to a new line
[[ $REPLY =~ ^[Yy]$ ]] # Returns 0 if y/Y is entered, 1 otherwise
}

0 comments on commit 0892683

Please sign in to comment.