#!/usr/bin/env bash
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Script which prints tests coverage for a particular pack.
#

ACTION_TEST_FILE_NAME_GLOB="test_action_*.py"

if [ $# -ne 1 ]; then
    echo "Usage: ${0} <path to pack directory>"
    exit 1
fi

if [ $# -ne 1 ]; then
    echo "Usage: ${0} <path to pack directory>"
    exit 1
fi

PACK=${1}

echo -e "\n====== ${PACK} ======\n"

if [ -d ${PACK}/actions ]; then
    ACTIONS=$(find ${PACK}/actions -maxdepth 1 -name "*.py" -printf "%f\n")
    ACTION_COUNT=$(echo "${ACTIONS}" | grep -v ^$ | wc -l)
else
    ACTIONS=""
    ACTION_COUNT=0
fi

if [ -d ${PACK}/tests/ ]; then
    ACTION_TESTS=$(find ${PACK}/tests -maxdepth 1 -name ${ACTION_TEST_FILE_NAME_GLOB} -printf "%f\n")
    ACTION_TEST_COUNT=$(echo "${ACTION_TESTS}" | grep -v ^$ | wc -l)
else
    ACTION_TEST_COUNT=0
    ACTION_TESTS=""
fi

MISSING_COUNT=0

echo "Python Actions missing tests:"
for ACTION in ${ACTIONS}; do
    TEST_FILE_PATH=${PACK}/tests/test_action_${ACTION}
    if [ ! -f  ${TEST_FILE_PATH} ]; then
        echo -e "\t${ACTION} (${TEST_FILE_PATH} not found)"
        MISSING_COUNT=$((MISSING_COUNT+1))
    fi
done

echo
echo "Python Tests with no actions:"
for TEST in ${ACTION_TESTS}; do
    if [ ! -f ${PACK}/actions/${TEST#test_action_} ]; then
        echo -e "\t$TEST"
    fi
done

echo
echo -e "Python Test Coverage:"
echo -e "\tActions: ${ACTION_TEST_COUNT}/${ACTION_COUNT}"
