<commit_before>#!/usr/bin/env bash

# Test to verify that the installation script works as expected.
# This script needs to be adapted, if new packages, etc are added to the install script

# The absolute path to the folder which contains this script
PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOME_DIR="/home/pi"

tests=0
failed_tests=0

# Tool functions

check_chmod_chown() {
    local mod_expected=$1
    local user_expected=$2
    local group_expected=$3
    local dir=$4
    local files=$5

    for file in ${files};
    do
        mod_actual=$(stat --format '%a' "${dir}/${file}")
        user_actual=$(stat -c '%U' "${dir}/${file}")
        group_actual=$(stat -c '%G' "${dir}/${file}")
        test ! "${mod_expected}" -eq "${mod_actual}" && echo "  ERROR: ${file} actual mod (${mod_actual}) differs from expected (${mod_expected})!"
        test ! "${user_expected}" == "${user_actual}" && echo "  ERROR: ${file} actual owner (${user_actual}) differs from expected (${user_expected})!"
        test ! "${group_expected}" == "${group_actual}" && echo "  ERROR: ${file} actual group (${group_actual}) differs from expected (${group_expected})!"
    done
}

check_file_contains_string() {
    local string="$1"
    local file="$2"

    # sudo is required for checking /etc/mopidy/mopidy.conf
    if [[ ! $(sudo grep -iw "${string}" "${file}") ]]; then
        echo "  ERROR: '${string}' not found in ${file}"
        ((failed_tests++))
    fi
    ((tests++))
}

check_service_state() {
    local service="$1"
    local desired_state="$2"

    local actual_state=$(systemctl show -p ActiveState --value "${service}")
    if [[ ! "${actual_state}" == "${desired_state}" ]]; then
        echo "  ERROR: service ${service} is not ${desired_state} (state: ${actual_state})."
        ((failed_tests++))
    fi
    ((tests++))
}

check_service_enablement() {
    local service="$1"
    local desired_enablement="$2"

    local actual_enablement=$(systemctl is-enabled "${service}")
    if [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
        echo "  ERROR: service ${service} is not ${desired_enablement} (state: ${actual_enablement})."
        ((failed_tests++))
    fi
    ((tests++))
}

check_variable() {
  local variable=${1}
  # check if variable exist and if it's empty
  test -z "${!variable+x}" && echo "ERROR: \$${variable} is missing!" && fail=true && return
  test "${!variable}" == "" && echo "ERROR: \$${variable} is empty!" && fail=true
}

# Verify functions

verify_conf_file() {
    local install_conf="${PATHDATA}/PhonieboxInstall.conf"
    printf "\nTESTING PhonieboxInstall.conf file...\n\n"
    # check that PhonieboxInstall.conf exists and is not empty

    # check if config file exists
    if [[ -f "${install_conf}" ]]; then
        # Source config file
        source "${install_conf}"
        cat "${install_conf}"
        echo ""
    else
        echo "ERROR: ${install_conf} does not exist!"
        exit 1
    fi

    fail=false
    if [[ -z "${WIFIconfig+x}" ]]; then
        echo "  ERROR: \$WIFIconfig is missing or not set!" && fail=true
    else
        echo "\$WIFIconfig is set to '$WIFIconfig'"
        if [[ "$WIFIconfig" == "YES" ]]; then
            check_variable "WIFIcountryCode"<commit_msg>Look for PhonieboxInstall.conf in HOME_DIR (#929)<commit_after>#!/usr/bin/env bash

# Test to verify that the installation script works as expected.
# This script needs to be adapted, if new packages, etc are added to the install script

# The absolute path to the folder which contains this script
PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOME_DIR="/home/pi"

tests=0
failed_tests=0

# Tool functions

check_chmod_chown() {
    local mod_expected=$1
    local user_expected=$2
    local group_expected=$3
    local dir=$4
    local files=$5

    for file in ${files};
    do
        mod_actual=$(stat --format '%a' "${dir}/${file}")
        user_actual=$(stat -c '%U' "${dir}/${file}")
        group_actual=$(stat -c '%G' "${dir}/${file}")
        test ! "${mod_expected}" -eq "${mod_actual}" && echo "  ERROR: ${file} actual mod (${mod_actual}) differs from expected (${mod_expected})!"
        test ! "${user_expected}" == "${user_actual}" && echo "  ERROR: ${file} actual owner (${user_actual}) differs from expected (${user_expected})!"
        test ! "${group_expected}" == "${group_actual}" && echo "  ERROR: ${file} actual group (${group_actual}) differs from expected (${group_expected})!"
    done
}

check_file_contains_string() {
    local string="$1"
    local file="$2"

    # sudo is required for checking /etc/mopidy/mopidy.conf
    if [[ ! $(sudo grep -iw "${string}" "${file}") ]]; then
        echo "  ERROR: '${string}' not found in ${file}"
        ((failed_tests++))
    fi
    ((tests++))
}

check_service_state() {
    local service="$1"
    local desired_state="$2"

    local actual_state=$(systemctl show -p ActiveState --value "${service}")
    if [[ ! "${actual_state}" == "${desired_state}" ]]; then
        echo "  ERROR: service ${service} is not ${desired_state} (state: ${actual_state})."
        ((failed_tests++))
    fi
    ((tests++))
}

check_service_enablement() {
    local service="$1"
    local desired_enablement="$2"

    local actual_enablement=$(systemctl is-enabled "${service}")
    if [[ ! "${actual_enablement}" == "${desired_enablement}" ]]; then
        echo "  ERROR: service ${service} is not ${desired_enablement} (state: ${actual_enablement})."
        ((failed_tests++))
    fi
    ((tests++))
}

check_variable() {
  local variable=${1}
  # check if variable exist and if it's empty
  test -z "${!variable+x}" && echo "ERROR: \$${variable} is missing!" && fail=true && return
  test "${!variable}" == "" && echo "ERROR: \$${variable} is empty!" && fail=true
}

# Verify functions

verify_conf_file() {
    local install_conf="${HOME_DIR}/PhonieboxInstall.conf"
    printf "\nTESTING PhonieboxInstall.conf file...\n\n"
    # check that PhonieboxInstall.conf exists and is not empty

    # check if config file exists
    if [[ -f "${install_conf}" ]]; then
        # Source config file
        source "${install_conf}"
        cat "${install_conf}"
        echo ""
    else
        echo "ERROR: ${install_conf} does not exist!"
        exit 1
    fi

    fail=false
    if [[ -z "${WIFIconfig+x}" ]]; then
        echo "  ERROR: \$WIFIconfig is missing or not set!" && fail=true
    else
        echo "\$WIFIconfig is set to '$WIFIconfig'"
        if [[ "$WIFIconfig" == "YES" ]]; then
            check_variable "WIFIcountryCode"
{'fraction_non_alphanumeric': 0.17112462006079027, 'fraction_numerical': 0.005319148936170213, 'mean_word_length': 3.2984977139124756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2552823', 'n_tokens_mistral': 2046, 'n_tokens_neox': 2031, 'n_words': 600}