#! /bin/sh
#
# How I run the sass-spec checker for rsass.
#
# Default run:
#     ./check-spec
# Just show which "basic" tests are still failing:
#     ./check-spec basic
# Generate stats to update lib.rs docs and readme:
#     ./check-spec stats
#
cd `dirname $0`

if [ -d sass-spec ]; then
   (cd sass-spec && git fetch >&2 && git rebase origin/master --autostash >&2)
else
   git clone https://github.com/sass/sass-spec.git >&2
fi

cat > sass-spec/spec/output_styles/options.yml <<EOF
---
:start_version: '3.4'
:only_on:
- libsass
- rsass
EOF

cat > sass-spec/spec/output_styles/compact/options.yml <<EOF
---
:output_style: :compact
:ignore_for:
- rsass
EOF

cat > sass-spec/spec/output_styles/nested/options.yml <<EOF
---
:output_style: :nested
:ignore_for:
- rsass
EOF

IMPL=rsass
check() {
    cargo build --release --features=commandline >&2
    LC_ALL=C sass-spec/sass-spec.rb -c target/release/rsass \
	  --impl $IMPL -V 4.0 sass-spec/spec/$1
}

list_fails() {
    grep ^SassSpec:: | sed -e 's#.*test__##' -e 's# .*##' | sort
}

case "$1" in
    "")
        check | list_fails
        ;;
    "-h"|"--help"|"help")
	echo "$0 stats ;: Give stats for passes / fails suitable for docs."
	echo "$0 ;: just run all the tests"
	echo "$0 --help ;: print this help"
	echo "Other args will be used as test subset specifications."
	echo "Examples: basic core_functions selector-functions"
	echo "          scss parser values"
	;;
    "impersonate")
        IMPL=libsass check | list_fails
        ;;
    "stats")
	check \
            | rg --no-line-number '^([0-9]+) runs, ([0-9]+) assertions.*' \
                 --replace 'Progress: ![$2](http://progressed.io/bar/$2?scale=$1&suffix=+) of $1 tests passed' -
        ;;
    "update-tests")
	cargo run --features=spectest --bin=spectest
	;;
    *)
        check $1 | list_fails
	;;
esac
