#! /bin/bash

#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#
# PURPOSE:       Test suite for testing the project scaffolding after executing the SETUP_TEMPLATE script.
# TITLE:         Project Scaffolding tests
# AUTHOR:        @Josee9988 | Jose Gracia
# VERSION:       See in ./../CHANGELOG.md
# NOTES:         This script is called by the TESTS_RUNNER.sh script. And it unit tests the newly generated scaffolding
#                generated by the SETUP_TEMPLATE script.
# BASH_VERSION:  5.1.4(1)-release (x86_64-pc-linux-gnu)
# LICENSE:       see in ../LICENSE (project root) or https://github.com/Josee9988/project-template/blob/master/LICENSE
# GITHUB:        https://github.com/Josee9988/
# REPOSITORY:    https://github.com/Josee9988/project-template
# ISSUES:        https://github.com/Josee9988/project-template/issues
# MAIL:          jgracia9988@gmail.com
#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#

TESTS_TRASH_DIR="tests/.ignore.tests_trash"
USERNAME="FAKE_USERNAME_TESTS"
NAME="FAKE_NAME_TESTS"
MAIL="FAKE_EMAIL_TESTS"
TYPE="FAKE_TYPE_TESTS"

oneTimeSetUp() {
    cp -r ./* $TESTS_TRASH_DIR --copy-content 2>/dev/null || :
    cp -r .github/ $TESTS_TRASH_DIR --copy-contents
    cp -r bin/ $TESTS_TRASH_DIR --copy-contents
    cp .gitignore $TESTS_TRASH_DIR --copy-contents
    rm -r $TESTS_TRASH_DIR/tests/ 2>/dev/null || :
    rm -r $TESTS_TRASH_DIR/.git/ 2>/dev/null || :
    cd $TESTS_TRASH_DIR || exit
    bash SETUP_TEMPLATE.sh --username=$USERNAME --projectName=$NAME --email=$MAIL --projectType=$TYPE --omit-commit --omit-verification --omit-test-check >/dev/null # run the setup script
}

oneTimeTearDown() {
    cd "../.." || exit
}

# TESTS
suite() {
    suite_addTest testDotGithubFolder
    suite_addTest testDotGithubISSUE_TEMPLATE
    suite_addTest testDotGithubISSUE_TEMPLATEFiles
    suite_addTest testDotGithubFiles
    suite_addTest testTestRemovedFiles
    suite_addTest testGlobalFiles
    suite_addTest testRemovedFiles
}

testDotGithubFolder() {
    if [ ! -e ".github/" ]; then
        assertEquals ".github file does not exist" 1 0 # error
    fi
}

testDotGithubISSUE_TEMPLATE() {
    if [ ! -e ".github/ISSUE_TEMPLATE" ]; then
        assertEquals ".github/ISSUE_TEMPLATE was not found" 1 0 # error
    fi
}

testDotGithubISSUE_TEMPLATEFiles() {
    declare -a files=(
        ".github/ISSUE_TEMPLATE/1-bug-report.md" ".github/ISSUE_TEMPLATE/2-failing-test.md"
        ".github/ISSUE_TEMPLATE/3-docs-bug.md" ".github/ISSUE_TEMPLATE/4-feature-request.md"
        ".github/ISSUE_TEMPLATE/5-enhancement-request.md" ".github/ISSUE_TEMPLATE/6-security-report.md"
        ".github/ISSUE_TEMPLATE/7-question-support.md")

    for file in "${files[@]}"; do
        assertTrue " $file does not exist" "[ -e \"$file\" ]"
    done
}

testDotGithubFiles() {
    declare -a files=(
        ".github/CODEOWNERS" ".github/CODE_OF_CONDUCT.md" ".github/CONTRIBUTING.md" ".github/ISSUE_TEMPLATE.md"
        ".github/pull_request_template.md" ".github/SECURITY.md" ".github/SUPPORT.md" ".github/issue_label_bot.yaml"
        ".github/config.yml" ".github/FUNDING.yml" ".github/settings.yml")

    for file in "${files[@]}"; do
        assertTrue " $file does not exist" "[ -e \"$file\" ]"
    done
}

testTestRemovedFiles() {
    filesFound=0
    if [ -e "tests/" ] && [ -e "tests/shunit2" ] && [ -e "tests/TESTS_RUNNER.sh" ]; then
        filesFound=1
    fi
    assertNotEquals " tests folder of some files were found" 1 $filesFound
}

testGlobalFiles() {
    filesFound=0
    if [ -e ".gitignore" ] && [ -e "CHANGELOG.md" ] && [ -e "README.md" ]; then
        filesFound=1
    fi
    assertEquals " gitignore, changelog or readme were not found" 1 $filesFound
}

testRemovedFiles() {
    filesFound=0
    if [ -e "LICENSE" ] && [ -e "bin" ] && [ -e "bin/FUNCTION_HELPERS.sh" ]; then
        filesFound=1
    fi
    assertNotEquals " LICENSE or the bin directory were found" 1 $filesFound
}

# Load and run shUnit2.
. tests/shunit2
