#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: package-html
#
# This test should always be run together with 022_html. run_tests.sh will
# take care of this automatically
#
# * Does package-html correctly build?
# * Does the name retrieved with package-html-dir-name match the actual result?
# * Does the tarball have all the necessary files?
# * Is the HTML result directory cleaned before "tar"ing it's content?
# * Are the desktopfiles generated ?
# * Is the document file generated?
# * Is the page file generated?
# * Is the --name option correctly implemented?
# * Does the --set-date option work correctly?

_PHTML_NAME_PATH=""
_PHTML_BUILD_RESULT=""
_PHTML_TEMPDIR=""
_LOGFILE=""

source lib/common_functions

header "package-html"

function oneTimeSetUp() {
    local _LOG_DIR

    # Clean up the build directory
    clean_build_dir all
    # get the result directory
    _PHTML_NAME_PATH=$($_DAPSEXEC -d $_DCFILE package-html-dir-name 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the path to the resulting directory package failed. Skipping tests"
    fi
    _PHTML_BUILD_RESULT=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=PACKAGE_HTML_RESULT 2>/dev/null)
    if [ $? -ne 0 ]; then
       exit_on_error "The command to determine the tarball name failed. Skipping tests"
    fi
    _LOG_DIR=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=LOG_DIR 2>/dev/null)
    if [ $? -ne 0 ]; then
	exit_on_error " The initial DAPS call to determine the LOG file failed. Skipping tests"
    fi
    _LOGFILE=${_LOG_DIR}/make_package-html.log
    # create temp dir
    #
    make_tempdir "_PHTML_TEMPDIR"
    _PHTML_TEMPDIR="${_PHTML_TEMPDIR}/packagehtml"
    mkdir -p "$_PHTML_TEMPDIR" || exit_on_error " Creating a temporary directory failed. Skipping tests"
}

# Post
# this function is run _after_ the tests are executed
#
function oneTimeTearDown() {
    stats
    # Clean up the build directory
    clean_build_dir all
}

#---------------------------------------------------------------
# TESTS
#---------------------------------------------------------------

#--------------------------------
# * Does package-html correctly build?
# * Does the name retrieved with package-html-dir-name match the actual result?
#
function test_packagehtml () {
    local _PHTML_BUILD_PATH
    _PHTML_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE package-html 2>/dev/null)
    assertTrue \
        ' └─ The package-html command itself failed' \
        "$?"
    assertEquals \
	" └─ The resulting filename does not match the one retrieved with --package-src-name" \
	"$_PHTML_NAME_PATH" "$_PHTML_BUILD_PATH"
    assertTrue \
	" └─ The resulting file (${_PHTML_BUILD_RESULT}) does not exist." \
	"[ -s $_PHTML_BUILD_RESULT ]"
}

#--------------------------------
# * Does the tarball have all the necessary files?
#

function test_packagehtmlFilelist () {
    local _HTML_DIR_NAME _TEMPDIR 

    _HTML_DIR_NAME=$($_DAPSEXEC -v0 -d $_DCFILE html-dir-name 2>/dev/null)
    assertTrue \
        ' └─ The command to determine the HTML build directory failed' \
        "$?"

    # unpack tarball into temp dir
    #
    (cd $_PHTML_TEMPDIR && tar xfj $_PHTML_BUILD_RESULT --strip-components 1)

    # diff the HTML result dir and the extracted tarball - both should
    # not differ

    diff -r $_HTML_DIR_NAME $_PHTML_TEMPDIR
    assertTrue \
        " └─ The tar archive does not contain all files from $_HTML_DIR_NAME" \
        "$?"    
}
#--------------------------------
# * Is the HTML result directory cleaned before "tar"ing it's content?
# * Are the desktopfiles generated ?
# * Is the document file generated?
# * Is the page file generated?
#
function test_packagehtmlClean_and_Helpfiles () {
    local _DUMMYFILE _PHTML_DESKTOPFILES _PHTML_DOCUMENTFILE _PHTML_PAGEFILE

    _DUMMYFILE=dummy.file
    touch ${_PHTML_NAME_PATH}/$_DUMMYFILE 
    
    $_DAPSEXEC -v0 -d $_DCFILE package-html --desktopfiles --documentfiles \
        --pagefiles >/dev/null
    assertTrue \
        ' └─ The package-html command with --[desktop|document|page]files failed' \
        "$?"
    # test if dummy file has been deleted
    #
    assertFalse \
        " └─ The HTML build directory was not cleaned before the build (\$CLEAN_DIR)" \
        "[[ -s $_PHTML_NAME_PATH/$_DUMMYFILE ]]"
    _PHTML_DESKTOPFILES=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=DESKTOPFILES_RESULT)
    assertTrue \
        ' └─ The command to determine the desktopfiles path failed' \
        "$?"
    assertTrue \
        ' └─ The desktopfiles tarball does not exist.' \
        "[[ -s $_PHTML_DESKTOPFILES ]]"
    _PHTML_DOCUMENTFILE=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=DOCUMENTFILES_RESULT)
    assertTrue \
        ' └─ The command to determine the documentfile path failed' \
        "$?"
    assertTrue \
        ' └─ The documentfile does not exist.' \
        "[[ -s $_PHTML_DOCUMENTFILE ]]"
    _PHTML_PAGEFILE=$($_DAPSEXEC -v0 -d $_DCFILE showvariable VARIABLE=PAGEFILES_RESULT)
    assertTrue \
        ' └─ The command to determine the pagesfiles path failed' \
        "$?"
    assertTrue \
        ' └─ The pagefile does not exist.' \
        "[[ -s $_PHTML_PAGEFILE ]]"

}

#--------------------------------
# * Does the --set-date option work correctly?
#
# The original DocBook stylesheets ignore the date in HTML outputs, so there is
# currently no way to properly test, since the SUSE stylesheets are no longer
# part of the repo
#
#function test_packagehtmlSetDate () {
#
#    local _DATE _PHTML_BUILD_PATH _STYLEROOT
#
#    _DATE="Jun 01 2013"
#    # the DocBook stylesheets do not print a publication date, therefore
#    # using the SUSE stylesheets
#    ##
#    _STYLEROOT="../suse/xslt2013"
#    clean_build_dir results
#
#    $_DAPSEXEC -v0 -d $_DCFILE --styleroot "$_STYLEROOT" package-html --set-date=\"$_DATE\" >/dev/null
#    assertTrue \
#	" └─ Building package-html with --set-date failed " \
#	"$?"
#
#    # clean tmp and unpack new archive
#    [[ -d ${_PHTML_TEMPDIR} ]] && rm -rf "${PHTML_TEMPDIR}" && mkdir -p "${_PHTML_TEMPDIR}"
#    (cd $_PHTML_TEMPDIR && tar xfj $_PHTML_BUILD_RESULT --strip-components 1)
#
#    grep -q "$_DATE" ${_PHTML_TEMPDIR}/index.html 2>/dev/null
#
#    assertTrue \
#	" └─ The date set with --set-date does not appear in index.html." \
#	"$?"
#}
#

#--------------------------------
# * Is the --name option correctly implemented?
#
function test_packagehtmlNAME () {

    local _PHTML_BUILD_PATH _PHTML_NAME_PATH _NAME
    _NAME="$RANDOM"

    clean_build_dir results

    _PHTML_BUILD_PATH=$($_DAPSEXEC -v0 -d $_DCFILE package-html --name $_NAME 2>/dev/null)
    assertTrue \
	" └─ Building package-html with --name failed " \
       "$?"

    _PHTML_NAME_PATH=$($_DAPSEXEC -v0 -d $_DCFILE package-html-dir-name --name $_NAME 2>/dev/null)
   assertTrue \
	' └─  Getting the filename for package-html with --name failed ' \
       "$?"
   assertEquals \
       " └─ The resulting filename does not match the one retrieved with --package-src-name: " \
       "$_PHTML_NAME_PATH" "$_PHTML_BUILD_PATH"

   # expr match does not work with Variables as search term, needs regexp
   echo "$_PHTML_BUILD_PATH" | grep -q $_NAME
   assertTrue \
       " └─ String passed with --name ($_NAME) does not appear in the package filename" \
       "$?"
   assertEquals \
       " └─ The build subdirectory does not have the name supplied with --name: " \
       "$(basename $_PHTML_NAME_PATH)" "$(basename $_PHTML_BUILD_PATH)"
}

source $_SHUNIT2SRC
