From 089268388cea9dcd1ded07e9fe69f630ea29a0f9 Mon Sep 17 00:00:00 2001 From: wpalani Date: Tue, 12 Nov 2024 17:56:01 -0700 Subject: [PATCH] Add script --- .distignore | 1 + package.json | 3 ++- scripts/test-clean-module | 50 +++++++++++++++++++++++++++++++++++++++ scripts/utils | 32 +++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 scripts/test-clean-module create mode 100644 scripts/utils diff --git a/.distignore b/.distignore index 49dc07360..b00f4421d 100644 --- a/.distignore +++ b/.distignore @@ -3,6 +3,7 @@ .github /bin node_modules +/scripts /src /tests diff --git a/package.json b/package.json index 58c794b76..125bf54bf 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/scripts/test-clean-module b/scripts/test-clean-module new file mode 100644 index 000000000..95cc7953c --- /dev/null +++ b/scripts/test-clean-module @@ -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 -- " + 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 diff --git a/scripts/utils b/scripts/utils new file mode 100644 index 000000000..51ca5c75f --- /dev/null +++ b/scripts/utils @@ -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 +}