Files
espanso/test.sh
T
naomi 6cc00611e0
Node.js CI / CI (push) Successful in 11s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 55s
fix: update test regex
2025-12-22 17:46:23 -08:00

170 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
# Color definitions
RESET='\033[0m'
BOLD='\033[1m'
RED='\033[91m'
GREEN='\033[92m'
YELLOW='\033[93m'
BLUE='\033[94m'
MAGENTA='\033[95m'
CYAN='\033[96m'
# Directory containing YAML files
MATCH_DIR="match"
# Check if directory exists
if [ ! -d "$MATCH_DIR" ]; then
echo -e "${RED}${BOLD}Error:${RESET} ${RED}Directory '$MATCH_DIR' not found!${RESET}"
exit 1
fi
# Temporary files to store triggers and replaces with metadata
# Format: trigger|replace|file|trigger_line|replace_line
ALL_TRIGGERS_DATA=$(mktemp)
ALL_REPLACES_DATA=$(mktemp)
# Find all YAML files and extract triggers with their replace values and line numbers
echo -e "${CYAN}${BOLD}Scanning YAML files in $MATCH_DIR directory...${RESET}"
for file in "$MATCH_DIR"/*.y*ml; do
if [ -f "$file" ]; then
echo -e "${BLUE}Processing file:${RESET} ${BOLD}$(basename "$file")${RESET}"
# Process file line by line to track trigger-replace pairs
trigger_line=""
trigger_value=""
replace_value=""
replace_line=""
line_num=0
while IFS= read -r line; do
line_num=$((line_num + 1))
# Check if this is a replace line (comes first due to alphabetical ordering)
if echo "$line" | grep -qE '^\s*-\s*replace:'; then
replace_line=$line_num
replace_value=$(echo "$line" | sed 's/.*replace:\s*"\([^"]*\)".*/\1/')
trigger_value="" # Reset trigger value for new entry
trigger_line="" # Reset trigger line
# Check if this is a trigger line (should be right after replace)
elif echo "$line" | grep -qE '^\s*trigger:'; then
trigger_line=$line_num
trigger_value=$(echo "$line" | sed 's/.*trigger:\s*"\([^"]*\)".*/\1/')
# Save trigger data: trigger|replace|file|trigger_line|replace_line
if [ -n "$replace_value" ]; then
echo "$trigger_value|$replace_value|$(basename "$file")|$trigger_line|$replace_line" >> "$ALL_TRIGGERS_DATA"
fi
fi
done < "$file"
else
echo -e "${YELLOW}${BOLD}Warning:${RESET} ${YELLOW}No YAML files found in $MATCH_DIR directory!${RESET}"
continue
fi
done
# Also create a reverse mapping for replace values: replace|trigger|file|trigger_line|replace_line
awk -F'|' '{print $2"|"$1"|"$3"|"$4"|"$5}' "$ALL_TRIGGERS_DATA" > "$ALL_REPLACES_DATA"
# Check if we found any triggers
if [ ! -s "$ALL_TRIGGERS_DATA" ]; then
echo -e "${RED}${BOLD}Error:${RESET} ${RED}No triggers found in YAML files. Check file format or directory.${RESET}"
rm "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
exit 1
fi
# Extract just triggers to find duplicates
ALL_TRIGGERS=$(mktemp)
cut -d'|' -f1 "$ALL_TRIGGERS_DATA" > "$ALL_TRIGGERS"
# Sort triggers and find duplicates
echo -e "\n${CYAN}${BOLD}Checking for duplicate triggers...${RESET}"
DUPLICATE_TRIGGERS=$(sort "$ALL_TRIGGERS" | uniq -d)
# Display results
if [ -z "$DUPLICATE_TRIGGERS" ]; then
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All triggers are unique!${RESET}"
TOTAL=$(wc -l < "$ALL_TRIGGERS")
echo -e "${CYAN}Total number of triggers found:${RESET} ${BOLD}$TOTAL${RESET}"
else
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Duplicate triggers found:${RESET}"
echo -e "${YELLOW}$DUPLICATE_TRIGGERS${RESET}"
# Show detailed information for each duplicate
echo -e "\n${CYAN}${BOLD}Detailed information for duplicates:${RESET}"
for dupe in $DUPLICATE_TRIGGERS; do
echo -e "\n${MAGENTA}${BOLD}Trigger${RESET} ${BOLD}\"$dupe\"${RESET} ${MAGENTA}found:${RESET}"
grep "^$dupe|" "$ALL_TRIGGERS_DATA" | while IFS='|' read -r trigger replace file trigger_line replace_line; do
echo -e " ${YELLOW}${RESET} Line ${MAGENTA}${BOLD}$trigger_line${RESET} in ${BLUE}$file${RESET}: ${CYAN}replace=${RESET}\"${GREEN}$replace${RESET}\""
done
done
rm "$ALL_TRIGGERS" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
exit 1
fi
# Extract just replace values to find duplicates
ALL_REPLACES=$(mktemp)
cut -d'|' -f1 "$ALL_REPLACES_DATA" > "$ALL_REPLACES"
# Sort replace values and find duplicates
echo -e "\n${CYAN}${BOLD}Checking for duplicate replace values...${RESET}"
DUPLICATE_REPLACES=$(sort "$ALL_REPLACES" | uniq -d)
# Display results
if [ -z "$DUPLICATE_REPLACES" ]; then
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All replace values are unique!${RESET}"
TOTAL_REPLACES=$(wc -l < "$ALL_REPLACES")
echo -e "${CYAN}Total number of replace values found:${RESET} ${BOLD}$TOTAL_REPLACES${RESET}"
else
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Duplicate replace values found:${RESET}"
echo -e "${YELLOW}$DUPLICATE_REPLACES${RESET}"
# Show detailed information for each duplicate
echo -e "\n${CYAN}${BOLD}Detailed information for duplicates:${RESET}"
for dupe in $DUPLICATE_REPLACES; do
echo -e "\n${MAGENTA}${BOLD}Replace value${RESET} ${BOLD}\"$dupe\"${RESET} ${MAGENTA}found:${RESET}"
grep "^$dupe|" "$ALL_REPLACES_DATA" | while IFS='|' read -r replace trigger file trigger_line replace_line; do
echo -e " ${YELLOW}${RESET} Line ${MAGENTA}${BOLD}$replace_line${RESET} in ${BLUE}$file${RESET}: ${CYAN}trigger=${RESET}\"${GREEN}$trigger${RESET}\""
done
done
rm "$ALL_TRIGGERS" "$ALL_REPLACES" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
exit 1
fi
# Clean up
rm "$ALL_TRIGGERS" "$ALL_REPLACES" "$ALL_TRIGGERS_DATA" "$ALL_REPLACES_DATA"
### Now we test that all gifs.yml replace values match "c.tenor.com".
# Temporary file to store all replace values
REPLACE_VALUES=$(mktemp)
# Read gifs.yml file and extract replace values
echo -e "${CYAN}${BOLD}Scanning gifs.yml for replace values...${RESET}"
if [ -f "$MATCH_DIR/gifs.yml" ]; then
# We match lines like: ` - replace: "https://c.tenor.com/some-value/tenor.gif"`
grep -E '^\s*-\s*replace:' "$MATCH_DIR/gifs.yml" | sed 's/.*replace:\s*"\([^"]*\)".*/\1/' >> "$REPLACE_VALUES"
else
echo -e "${RED}${BOLD}Error:${RESET} ${RED}gifs.yml file not found in $MATCH_DIR directory!${RESET}"
rm "$REPLACE_VALUES"
exit 1
fi
# Check if we found any replace values
if [ ! -s "$REPLACE_VALUES" ]; then
echo -e "${RED}${BOLD}Error:${RESET} ${RED}No replace values found in gifs.yml. Check file format.${RESET}"
rm "$REPLACE_VALUES"
exit 1
fi
# Check if all replace values start with "https://c.tenor.com/"
echo -e "\n${CYAN}${BOLD}Checking replace values in gifs.yml...${RESET}"
INVALID_REPLACE=$(grep -v '^https://c\.tenor\.com/' "$REPLACE_VALUES")
if [ -z "$INVALID_REPLACE" ]; then
echo -e "${GREEN}${BOLD}✓ Success:${RESET} ${GREEN}All replace values in gifs.yml are valid!${RESET}"
else
echo -e "${RED}${BOLD}✗ Error:${RESET} ${RED}Invalid replace values found in gifs.yml:${RESET}"
echo -e "${YELLOW}$INVALID_REPLACE${RESET}"
exit 1
fi
# This must always be the final line, so that CI can detect success. If you want to exit with a failure, do so at the conditional point.
exit 0