#!/bin/bash
#
# Copyright (C) 2012-2015 SUSE Linux GmbH
#
# Author:
# Frank Sundermeyer <fsundermeyer at opensuse dot org>
#
# Testing DAPS: --builddir-option
#
# * Is the make variable for BUILD_DIR correctly set?
# * Is the specified build directory realy used?


_BUILD_DIR=""

source lib/common_functions

header "Build Directory"

function oneTimeSetUp () {
    # Clean up the build directory
    clean_build_dir all
    _BUILD_DIR=$(mktemp -d -q ${SHUNIT_TMPDIR}/daps_testsuite_XXXXXXX)
    if [ $? -ne 0 ]; then
	exit_on_error " Cannot create temporary directory $_BUILD_DIR. Skipping builddir tests"
    fi
}

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

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

#--------------------------------
# * Is the make variable for BUILD_DIR correctly set?
#
function test_builddirVariable () {
    local _BD_VAR

    _BD_VAR=$($_DAPSEXEC -v0 --main $_MAINPATH --builddir $_BUILD_DIR showvariable VARIABLE=BUILD_DIR 2>/dev/null)
    assertTrue \
        ' └─ The showvariable command for BUILD_DIR failed' \
        "$?"
    assertEquals \
	" └─ The value for BUILD_DIR does not match the specified build directory:" \
	"$_BUILD_DIR" "$_BD_VAR"
}

#--------------------------------
# * Is the specified build directory realy used?
#
function test_builddirResult () {

    $_DAPSEXEC -v0 --main $_MAINPATH --builddir $_BUILD_DIR profile >/dev/null 2>&1
    readlink -e ${_BUILD_DIR}/.profiled/*/$_MAIN >/dev/null 2>&1
    assertTrue \
        ' └─ The build directory is not used.' \
        "$?"
}

# source shUnit2 test
source $_SHUNIT2SRC
