Skip to content

Commit

Permalink
fixed html file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Dec 31, 2024
1 parent fcac4ea commit f7ff783
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 44 deletions.
43 changes: 28 additions & 15 deletions mettalog
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export RPWD=$PWD
IS_SOURCED=$( [[ "${BASH_SOURCE[0]}" != "${0}" ]] && echo 1 || echo 0)
# Function to exit the script properly
if [ "$IS_SOURCED" -eq "0" ]; then SCRIPT=$(readlink -f "$0"); else SCRIPT=$(readlink -f "${BASH_SOURCE[0]}"); fi
export MeTTa=$(realpath "$SCRIPT")
export MeTTa=$(readlink -m "$SCRIPT")
export METTALOG_DIR=$(dirname "$SCRIPT")
# echo "METTALOG_DIR=$METTALOG_DIR"
#cd "$METTALOG_DIR" || { echo "Failed to navigate to $METTALOG_DIR"; [[ "$IS_SOURCED" == "1" ]] && return 1 || exit 1; }
Expand Down Expand Up @@ -625,7 +625,7 @@ function handle_args {
if [[ "$NEXT_ARG_IS_RC_FILE" == true ]]; then
NEXT_ARG_IS_RC_FILE=false
rc_file_path="$PrevDir/$arg" # Resolve file path relative to PrevDir
rc_file_path="$(realpath "$rc_file_path")" # Resolve to absolute path
rc_file_path="$(readlink -m "$rc_file_path")" # Resolve to absolute path
load_rc_file "$rc_file_path"
use_rc_file=""
continue
Expand All @@ -645,7 +645,7 @@ function handle_args {
arg_realpath="$arg"
# track file paths but keep going
if [[ -f "$arg" || -d "$arg" ]]; then
arg_realpath="$(realpath "$arg")"
arg_realpath="$(readlink -m "$arg")"
if [[ -f "$arg_realpath" || -d "$arg_realpath" ]]; then
add_to_list "$arg_realpath" LIST_OF_FILE_ARGS
fi
Expand Down Expand Up @@ -826,7 +826,7 @@ function handle_args {
*)
add_to_list "$arg" METTALOG_OPTIONS
if [[ -f "$arg" || -d "$arg" ]]; then
arg_realpath="$(realpath "$arg")"
arg_realpath="$(readlink -m "$arg")"
if [[ -f "$arg_realpath" || -d "$arg_realpath" ]]; then
add_to_list "$arg_realpath" LIST_OF_FILE_ARGS
fi
Expand All @@ -843,7 +843,7 @@ for arg in "$@"; do
arg=$(remove_quotes "$arg") # Remove the quotes

if [[ -f "$arg" || -d "$arg" ]]; then
arg_realpath="$(realpath "$arg")"
arg_realpath="$(readlink -m "$arg")"
if [[ -f "$arg_realpath" || -d "$arg_realpath" ]]; then
add_to_list "$arg_realpath" LIST_OF_FILE_ARGS
fi
Expand All @@ -854,13 +854,13 @@ done
#DEBUG "LIST_OF_FILE_ARGS[0]=${LIST_OF_FILE_ARGS[0]}"
DIRNAME="${LIST_OF_FILE_ARGS[0]}"
if [[ -f "$DIRNAME" ]]; then
HTML_OUT="${DIRNAME}.html"
HTML_OUT_MAYBE="${DIRNAME}.html"
DIRNAME=$(dirname "${DIRNAME}")
elif [[ -d "$DIRNAME" ]]; then
HTML_OUT="${DIRNAME}/Result.html"
HTML_OUT_MAYBE="${DIRNAME}/Result.html"
else
DIRNAME="${PWD}"
HTML_OUT="${DIRNAME}/Result.html"
HTML_OUT_MAYBE="${DIRNAME}/Result.html"
fi
DIR_RC="$DIRNAME/.mettalogrc"
#DEBUG "DIR_RC=$DIR_RC"
Expand Down Expand Up @@ -1001,7 +1001,7 @@ else
fi
fi

INTERP_SRC_DIR="$(realpath "${INTERP_SRC_DIR}")"
INTERP_SRC_DIR="$(readlink -m "${INTERP_SRC_DIR}")"

DEBUG "INTERP_SRC_DIR=$INTERP_SRC_DIR"

Expand Down Expand Up @@ -1114,12 +1114,13 @@ cleanup() {
else
METTA_CMD_EXIT_STATUS=${METTA_CMD_EXIT_STATUS:-$?}
fi
DEBUG "Exit code of METTA_CMD: $METTA_CMD_EXIT_STATUS"
do_DEBUG "Exit code of METTA_CMD: $METTA_CMD_EXIT_STATUS to $HTML_OUT or $file_html or $HTML_FILE"

if [ ! -z "$TEE_FILE" ];then
if [ ! -z "$HTML_OUT" ];then

if [ ! -z "$HTML_FILE" ];then
IF_REALLY_DO "ansi2html -u < '$TEE_FILE' > '$HTML_OUT'"
HTML_OUT=
export HTML_FILE=""
fi
IF_REALLY_DO "rm -f '$TEE_FILE'"
TEE_FILE=
Expand Down Expand Up @@ -1199,6 +1200,13 @@ execute_with_pipes() {
IF_REALLY_DO "$full_command"
}

if [ -z "$HTML_OUT" ];then
HTML_OUT=HTML_OUT_MAYBE
fi

if [ ! -z "$file_html" ];then
HTML_OUT="$file_html"
fi

# Conditional to check if html_flag is enabled
if [[ "$html_flag" == "enable" ]]; then
Expand All @@ -1208,14 +1216,19 @@ if [[ "$html_flag" == "enable" ]]; then
export TEE_FILE

if [ ! -z "$HTML_OUT" ];then
HTML_OUT=$(realpath --relative-to="$(pwd)" "$HTML_OUT")
HTML_OUT=$(readlink -m --relative-to="$(pwd)" "$HTML_OUT")
if [ ! -z "$OUTPUT_DIR" ] ;then
export METTALOG_OUTPUT="${OUTPUT_DIR}"
HTML_OUT="${OUTPUT_DIR}/${HTML_OUT}"
# HTML_OUT="${OUTPUT_DIR}/${HTML_OUT}"
fi
export HTML_FILE="${HTML_OUT}"
fi

if [ ! -z "$file_html" ];then
HTML_OUT="$file_html"
fi

export HTML_FILE="${HTML_OUT}"

export TYPESCRIPT=1
if [[ "$OS" == "Darwin" ]]; then # macOS
METTA_CMD="/usr/bin/script -q -f -a \"$TEE_FILE\" -c \\\"$(printf '%q ' ${METTA_CMD[@]})\\\""
Expand Down
24 changes: 18 additions & 6 deletions scripts/pass_fail_totals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ if [ -z "$1" ]; then
fi

start_dir="${1%/}"
#start_dir=$1

# Function to strip the start_dir prefix from directories
strip_start_dir() {
local dir=$1
echo "${dir#$start_dir/}"
}


reverse_path() {
# The original path is read from the first argument to the function
Expand Down Expand Up @@ -66,6 +72,7 @@ find "$start_dir" -type d -not -path '*/__pycache__*' | while read -r dir; do
echo "$dir $slash_count $(reverse_path $dir)" >> "$temp_file"
done

# cat "$temp_file" #debugging

file_info_tmp=$(mktemp)
chart1=$(mktemp)
Expand All @@ -81,8 +88,8 @@ sort -k1,1r -k2,2nr "$temp_file" | while read -r dir slash_count rvdir; do
# Process each .metta.html file in the directory
while read -r file; do
# Extract successes and failures
pass=$(tac "$file" | grep -oP 'Successes: \K\d+' | head -n 1 | bc || echo 0)
fail=$(tac "$file" | grep -oP 'Failures: \K\d+' | head -n 1 | bc || echo 0)
pass=$(tac "$file" | grep -oP 'Successes: \K\d+' | head -n 1 | bc || echo 0)
fail=$(tac "$file" | grep -oP 'Failures: \K\d+' | head -n 1 | bc || echo 0)

# Add to totals
total_pass=$((total_pass + pass))
Expand Down Expand Up @@ -112,7 +119,9 @@ sort -k1,1r -k2,2nr "$temp_file" | while read -r dir slash_count rvdir; do
show_total_as="${show_total_as}"
missing="${files_no_totals}"
fi
mdir="$(convert_path $dir)"

tdir="$(strip_start_dir "$dir")"
mdir="$(convert_path $tdir)"
mod="$(convert_mod $mdir)"
printf "|%4d|%-2s%3d|%4s| %3d%% | %-30s | %s |\n" "$total_pass" "$missing" "$total_fail" "$show_total_as" "$dir_percent" "$mod" "$mdir" >> $chart1

Expand Down Expand Up @@ -176,7 +185,9 @@ sort -k3,3 -k2,2nr "$temp_file" | while read -r dir slash_count rvdir; do
fail=-1
pass=-1
fi
relative_path=$(echo "$file" | sed 's/^\.\///' | sed -e 's|examples|reports|g' -e 's|-reports|-examples|g')

tfile="$(strip_start_dir "$file")"
relative_path=$(echo "$tfile" | sed 's/^\.\///' | sed -e 's|examples|reports|g' -e 's|-reports|-examples|g')
github_link="${base_url}${relative_path}"
printf "| %5d | %5d | %5d%% | [%s](%s) |\n" "$pass" "$fail" "$file_percent" "$(basename "${file%.html}")" "$github_link" >> $file_info_tmp
fi
Expand All @@ -185,7 +196,8 @@ sort -k3,3 -k2,2nr "$temp_file" | while read -r dir slash_count rvdir; do
# Calculate and print the directory percentage
total_tests=$((total_pass + total_fail))
dir_percent=0
mdir="$(convert_path $dir)"
tdir="$(strip_start_dir "$dir")"
mdir="$(convert_path $tdir)"
if [ "$total_tests" -ne 0 ]; then
dir_percent=$((100 * total_pass / total_tests))
if [ "$had_files" == "true" ]; then
Expand Down
82 changes: 59 additions & 23 deletions scripts/test_in_metta.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ process_file() {

export METTALOG_OUTPUT="${METTALOG_OUTPUT}"

export HTML_OUT="${file}.html"
export HTML_OUT="$file_html"

DEBUG "==========================================================================="
DEBUG "${BLUE}${BOLD}===========================================================================${NC}"
Expand All @@ -47,8 +47,8 @@ process_file() {
DEBUG "==========================================================================="

# Add unique absolute paths to PYTHONPATH
pp1=$(realpath "$(dirname "${file}")")
pp2=$(realpath "$(dirname "${pp1}")")
pp1=$(readlink -m "$(dirname "${file}")")
pp2=$(readlink -m "$(dirname "${pp1}")")
for pp in $pp1 $pp2; do
if [[ ":$PYTHONPATH:" != *":$pp:"* ]]; then
export PYTHONPATH="${PYTHONPATH:+"$PYTHONPATH:"}$pp"
Expand All @@ -69,7 +69,7 @@ process_file() {
DEBUG_WHY "${YELLOW}Regenerating answers: $file.answers${NC}"
#IF_REALLY_DO cat /dev/null > "${file}.answers"
IF_REALLY_DO rm -f "${file}.answers"
# git checkout "${file}.answers"
# git checkout "${file}.answers"

# Function to handle SIGINT
handle_sigint() {
Expand Down Expand Up @@ -163,12 +163,13 @@ process_file() {
sleep 0.1
IF_REALLY_DO touch "$file_html"

TEST_CMD="./mettalog '--output=$METTALOG_OUTPUT' --timeout=$METTALOG_MAX_TIME --html --repl=false ${extra_args[@]} ${passed_along_to_mettalog[@]} \"$file\" --halt=true"
TEST_CMD="$METTALOG_DIR/mettalog '--output=$METTALOG_OUTPUT' --timeout=$METTALOG_MAX_TIME --html --repl=false ${extra_args[@]} ${passed_along_to_mettalog[@]} \"$file\" --halt=true"
# DEBUG "${BOLD}$TEST_CMD${NC}"



EXTRA_INFO="Under $METTALOG_MAX_TIME seconds"

DEBUG "HTML_OUT=$HTML_OUT"

# Start the timer
local START_TIME=$(date +%s)

Expand Down Expand Up @@ -225,6 +226,21 @@ process_file() {
LOGFILE="$file_html"
fi

DEBUG "file_html=$file_html"

# Check if the file exists
if [ -f "$file_html" ]; then
# Get the file size in bytes
file_size=$(stat --format="%s" "$file_html")
if [ "$file_size" -lt 20 ]; then
DEBUG "${RED}(warning) The file '$file_html' is less than 20 bytes!${NC}"
else
DEBUG "The file '$file_html' exists and is $file_size bytes."
fi
else
DEBUG "${RED}The file '$file_html' does not exist.${NC}"
fi

# Redirect debug messages to both the logfile and console
echo "$DEBUG_MESSAGE" | tee -a "$LOGFILE"

Expand All @@ -247,21 +263,26 @@ process_file() {
# Check if the script is being sourced or run directly
IS_SOURCED=$( [[ "${BASH_SOURCE[0]}" != "${0}" ]] && echo 1 || echo 0)
# Save the directory one level above where this script resides
SCRIPTS="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SCRIPTS="$(readlink -m "$SCRIPTS")"

METTALOG_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
METTALOG_DIR="$(readlink -m "$METTALOG_DIR")"

SCRIPT_NAME=$(basename "$0")

passed_along_to_mettalog=()
METTALOG_MAX_TIME=45
RUST_METTA_MAX_TIME=60

SCRIPT_NAME=$(basename "$0")
run_tests_auto_reply=""
generate_report_auto_reply=""
METTALOG_OUTPUT="reports/tests_output/testrun_$(date +%Y%m%d_%H%M%S)"
METTALOG_OUTPUT="$METTALOG_DIR/reports/tests_output/testrun_$(date +%Y%m%d_%H%M%S)"
fresh=0
clean=0 # 0 means don't clean, 1 means do clean
if_failures=0
if_regressions=0
show_help=0
RUST_METTA_MAX_TIME=60
EXTRA_FIND_ARGS=" ! -path '*/.*' ! -path '*~*' ! -size +10M"
EXTRA_GREP_ARGS=""
dry_run=0
Expand Down Expand Up @@ -498,7 +519,7 @@ generate_final_MeTTaLog() {
cd "$METTALOG_DIR" || exit 1

if [ 1 -eq 0 ]; then
python3 ./scripts/into_junit.py "${SHARED_UNITS}" > "$METTALOG_OUTPUT/junit.xml"
python3 $SCRIPTS/into_junit.py "${SHARED_UNITS}" > "$METTALOG_OUTPUT/junit.xml"

junit2html "$METTALOG_OUTPUT/junit.xml"
junit2html "$METTALOG_OUTPUT/junit.xml" --summary-matrix
Expand All @@ -522,14 +543,22 @@ generate_final_MeTTaLog() {
{ echo "| TEST NAME | STATUS | URL LOCATION | TEST CONDITION | ACTUAL RESULT | EXPECTED RESULT |"
echo "|-----------|--------|--------------|----------------|---------------|-----------------|"
cat "${SHARED_UNITS}" | awk -F'\\(|\\) \\| \\(' '{ print $1 " " $0 }' | sort | cut -d' ' -f2- | tac | awk '!seen[$0]++' | tac
} > ./$METTALOG_OUTPUT/PASS_FAIL.md
} > $METTALOG_OUTPUT/PASS_FAIL.md


echo "$SCRIPTS/pass_fail_totals.sh $METTALOG_OUTPUT/ > $METTALOG_OUTPUT/TEST_LINKS.md"

METTALOG_OUTPUT_RELATIVE=$(realpath --no-symlinks --relative-to="$PWD" "$METTALOG_OUTPUT")
$SCRIPTS/pass_fail_totals.sh $METTALOG_OUTPUT_RELATIVE/ > $METTALOG_OUTPUT/TEST_LINKS.md

./scripts/pass_fail_totals.sh $METTALOG_OUTPUT/ > $METTALOG_OUTPUT/TEST_LINKS.md
printf '%s\n' "${@}" > "$METTALOG_OUTPUT/_REPORT_.md"

cat "$METTALOG_OUTPUT/_REPORT_.md"

cat $METTALOG_OUTPUT/TEST_LINKS.md | sed -e "s|$METTALOG_OUTPUT|reports|g" \
| sed -e "s|Directory: ./reports/tests/|D: |g" >> "$METTALOG_OUTPUT/_REPORT_.md"
./scripts/html_pass_fail.sh $METTALOG_OUTPUT/ > $METTALOG_OUTPUT/REPORT.html

$SCRIPTS/html_pass_fail.sh $METTALOG_OUTPUT/ > $METTALOG_OUTPUT/REPORT.html

}

Expand Down Expand Up @@ -682,7 +711,7 @@ while [ "$#" -gt 0 ]; do
shift
done

source ./scripts/ensure_venv
source $SCRIPTS/ensure_venv

extract_all_parent_directories

Expand All @@ -706,7 +735,7 @@ fi

if [ -z "$SHARED_UNITS" ]; then
if [ -d "$METTALOG_OUTPUT" ]; then
export SHARED_UNITS=$(realpath $METTALOG_OUTPUT)/SHARED.UNITS
export SHARED_UNITS=$(readlink -m $METTALOG_OUTPUT)/SHARED.UNITS
fi
fi
touch $SHARED_UNITS
Expand All @@ -725,24 +754,31 @@ else
REPLY=$run_tests_auto_reply
fi

# Directory containing the .pl files
PYSWIP_VERSION="$METTALOG_DIR/prolog/metta_lang"
# Directory containing the .pl files
PYSWIP_VERSION="${PYSWIP_VERSION%/}"
# Directory containing the .pl files
if [ -f "$PYSWIP_VERSION/metta_interp.pl" ]; then
INTERP_SRC_DIR="$PYSWIP_VERSION"
else
if [ -f "$PYSWIP_VERSION/src/canary/metta_interp.pl" ]; then
INTERP_SRC_DIR="$PYSWIP_VERSION/src/canary"
if [ -f "$PYSWIP_VERSION/prolog/metta_interp.pl" ]; then
INTERP_SRC_DIR="$PYSWIP_VERSION/prolog"
else
if [ -f "$METTALOG_DIR/src/$PYSWIP_VERSION/metta_interp.pl" ]; then
INTERP_SRC_DIR="$METTALOG_DIR/src/$PYSWIP_VERSION"
else
INTERP_SRC_DIR="$METTALOG_DIR/src/canary"
fi
INTERP_SRC_DIR="$METTALOG_DIR/.Attic/$PYSWIP_VERSION"
fi
fi



METTALOG_OUTPUT_RELATIVE="$METTALOG_OUTPUT"
METTALOG_OUTPUT="$(readlink -m "$METTALOG_OUTPUT")"
DEBUG "INTERP_SRC_DIR=$INTERP_SRC_DIR"
DEBUG "METTALOG_OUTPUT=$METTALOG_OUTPUT"
DEBUG "METTALOG_OUTPUT_RELATIVE=$METTALOG_OUTPUT_RELATIVE"
SHARED_UNITS="$(readlink -m "$SHARED_UNITS")"
DEBUG "SHARED_UNITS=$SHARED_UNITS"
export SHARED_UNITS

if [[ ! -f "${METTALOG_OUTPUT}/src/" ]]; then
:
Expand Down

0 comments on commit f7ff783

Please sign in to comment.