diff --git a/LICENSE b/LICENSE index 6b156fe..610ce97 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,6 @@ -GNU GENERAL PUBLIC LICENSE + + + GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. @@ -631,8 +633,8 @@ to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. - {one line to give the program's name and a brief idea of what it does.} - Copyright (C) {year} {name of author} + JuNest: The Arch Linux based distro that runs upon any Linux distros without root access + Copyright (C) 2014 Filippo Squillace This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -652,7 +654,7 @@ Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: - {project} Copyright (C) {year} {fullname} + JuNest Copyright (C) 2014 Filippo Squillace This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. @@ -673,3 +675,4 @@ the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . + diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..b3ae5c7 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +5.6.7 diff --git a/bin/jchroot b/bin/jchroot index 98f8680..8eaa8d4 100755 --- a/bin/jchroot +++ b/bin/jchroot @@ -1,30 +1,14 @@ #!/bin/bash # -# This file is part of JuNest (https://github.com/fsquillace/junest) -# -# Copyright (c) 2015 -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU Library General Public License as published -# by the Free Software Foundation; either version 2, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - # This script is the simplified and portable version of arch-chroot # (https://wiki.archlinux.org/index.php/Change_root#Using_arch-chroot) +# set -e -################################ IMPORTS ################################## -source "$(dirname $0)/../lib/util.sh" +JUNEST_BASE="$(readlink -f $(dirname $(readlink -f "$0"))/..)" + +source "${JUNEST_BASE}/lib/utils.sh" ################################ MAIN FUNCTIONS ########################### diff --git a/bin/junest b/bin/junest index 9ac3736..b081e71 100755 --- a/bin/junest +++ b/bin/junest @@ -2,24 +2,10 @@ # # This file is part of JuNest (https://github.com/fsquillace/junest). # -# Copyright (c) 2015 -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU Library General Public License as published -# by the Free Software Foundation; either version 2, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# -JUNEST_BASE="$(dirname $0)/.." +JUNEST_BASE="$(readlink -f $(dirname $(readlink -f "$0"))/..)" +source "${JUNEST_BASE}/lib/utils.sh" source "${JUNEST_BASE}/lib/core.sh" ################################### @@ -27,7 +13,8 @@ source "${JUNEST_BASE}/lib/core.sh" ################################### usage() { - echo -e "$NAME: $DESCRIPTION" + echo -e "$NAME (v$(cat $JUNEST_BASE/VERSION)): $DESCRIPTION" + echo echo -e "Usage: $CMD [options] [--] [command]" echo echo -e "Setup options:" @@ -54,9 +41,7 @@ usage() { } version() { - echo -e "$NAME $VERSION ($CODE_NAME): $DESCRIPTION" - echo -e "Copyright (c) $COPYRIGHT $AUTHOR" - echo -e "Homepage: $HOMEPAGE" + echo -e "$NAME $(cat $JUNEST_BASE/VERSION)" } check_cli(){ @@ -225,7 +210,11 @@ function execute_operation(){ fi } -parse_arguments "$@" -check_cli -execute_operation +function cli() { + parse_arguments "$@" + check_cli + execute_operation +} + +cli "$@" # vim: set ts=4 sw=4 noet: diff --git a/lib/core.sh b/lib/core.sh index 4cd08f0..451d4ca 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -1,41 +1,17 @@ #!/usr/bin/env bash # -# Copyright (c) 2015 +# This module contains all core functionalities for JuNest. # -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU Library General Public License as published -# by the Free Software Foundation; either version 2, or (at your option) -# any later version. +# Dependencies: +# - lib/utils.sh # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . -# - -# References: -# https://wiki.archlinux.org/index.php/PKGBUILD -# https://wiki.archlinux.org/index.php/Creating_Packages +# vim: ft=sh set -e -################################ IMPORTS ################################# -source "$(dirname ${BASH_ARGV[0]})/util.sh" - -################################# VARIABLES ############################## - NAME='JuNest' CMD='junest' -VERSION='5.6.7' -CODE_NAME='Nitida' DESCRIPTION='The Arch Linux based distro that runs upon any Linux distros without root access' -AUTHOR='Filippo Squillace ' -HOMEPAGE="https://github.com/fsquillace/${CMD}" -COPYRIGHT='2012-2016' - if [ "$JUNEST_ENV" == "1" ] then @@ -101,6 +77,7 @@ WGET="wget --no-check-certificate" CURL="curl -L -J -O -k" TAR=tar CHOWN="chown" +LN=ln LD_EXEC="$LD_LIB --library-path ${JUNEST_HOME}/usr/lib:${JUNEST_HOME}/lib" @@ -109,7 +86,7 @@ LD_EXEC="$LD_LIB --library-path ${JUNEST_HOME}/usr/lib:${JUNEST_HOME}/lib" # image. function ln_cmd(){ - ln $@ || $LD_EXEC ${JUNEST_HOME}/usr/bin/ln $@ + $LN $@ || $LD_EXEC ${JUNEST_HOME}/usr/bin/$LN $@ } function rm_cmd(){ @@ -148,6 +125,24 @@ function chroot_cmd(){ ################################# MAIN FUNCTIONS ############################## +NOT_AVAILABLE_ARCH=102 +NOT_EXISTING_FILE=103 +ARCHITECTURE_MISMATCH=104 +ROOT_ACCESS_ERROR=105 + +####################################### +# Check if the JuNest system is installed in JUNEST_HOME. +# +# Globals: +# JUNEST_HOME (RO) : Contains the JuNest home directory. +# Arguments: +# None +# Returns: +# 0 : If JuNest is installed +# 1 : If JuNest is not installed +# Output: +# None +####################################### function is_env_installed(){ [ -d "$JUNEST_HOME" ] && [ "$(ls -A $JUNEST_HOME)" ] && return 0 return 1 @@ -155,8 +150,8 @@ function is_env_installed(){ function _cleanup_build_directory(){ -# $1: maindir (optional) - str: build directory to get rid local maindir=$1 + check_not_null "$maindir" builtin cd $ORIGIN_WD trap - QUIT EXIT ABRT KILL TERM INT rm_cmd -fr "$maindir" @@ -164,15 +159,20 @@ function _cleanup_build_directory(){ function _prepare_build_directory(){ + local maindir=$1 + check_not_null "$maindir" trap - QUIT EXIT ABRT KILL TERM INT trap "rm_cmd -rf ${maindir}; die \"Error occurred when installing ${NAME}\"" EXIT QUIT ABRT KILL TERM INT } function _setup_env(){ + local imagepath=$1 + check_not_null "$imagepath" + is_env_installed && die "Error: ${NAME} has been already installed in $JUNEST_HOME" + mkdir_cmd -p "${JUNEST_HOME}" - imagepath=$1 $TAR -zxpf ${imagepath} -C ${JUNEST_HOME} mkdir_cmd -p ${JUNEST_HOME}/run/lock info "The default mirror URL is ${DEFAULT_MIRROR}." @@ -182,14 +182,33 @@ function _setup_env(){ } +####################################### +# Setup JuNest. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory in which JuNest needs +# to be installed. +# ARCH (RO) : The host architecture. +# JUNEST_TEMPDIR (RO) : The JuNest temporary directory for building +# the JuNest system from the image. +# ENV_REPO (RO) : URL of the site containing JuNest images. +# NAME (RO) : The JuNest name. +# DEFAULT_MIRROR (RO) : Arch Linux URL mirror. +# Arguments: +# arch ($1?) : The JuNest architecture image to download. +# Defaults to the host architecture +# Returns: +# $NOT_AVAILABLE_ARCH : If the architecture is not one of the available ones. +# Output: +# None +####################################### function setup_env(){ - local arch=$ARCH - [ -z $1 ] || arch="$1" + local arch=${1:-$ARCH} contains_element $arch "${ARCH_LIST[@]}" || \ - die "The architecture is not one of: ${ARCH_LIST[@]}" + die_on_status $NOT_AVAILABLE_ARCH "The architecture is not one of: ${ARCH_LIST[@]}" local maindir=$(TMPDIR=$JUNEST_TEMPDIR mktemp -d -t ${CMD}.XXXXXXXXXX) - _prepare_build_directory + _prepare_build_directory $maindir info "Downloading ${NAME}..." builtin cd ${maindir} @@ -202,21 +221,51 @@ function setup_env(){ _cleanup_build_directory ${maindir} } - +####################################### +# Setup JuNest from file. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory in which JuNest needs +# to be installed. +# NAME (RO) : The JuNest name. +# DEFAULT_MIRROR (RO) : Arch Linux URL mirror. +# Arguments: +# imagefile ($1) : The JuNest image file. +# Returns: +# $NOT_EXISTING_FILE : If the image file does not exist. +# Output: +# None +####################################### function setup_env_from_file(){ local imagefile=$1 - [ ! -e ${imagefile} ] && die "Error: The ${NAME} image file ${imagefile} does not exist" + check_not_null "$imagefile" + [ ! -e ${imagefile} ] && die_on_status $NOT_EXISTING_FILE "Error: The ${NAME} image file ${imagefile} does not exist" info "Installing ${NAME} from ${imagefile}..." _setup_env ${imagefile} - - builtin cd $ORIGIN_WD } +####################################### +# Run JuNest as real root. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory. +# UID (RO) : The user ID. +# SUDO_USER (RO) : The sudo user ID. +# SUDO_GID (RO) : The sudo group ID. +# SH (RO) : Contains the default command to run in JuNest. +# Arguments: +# cmd ($@?) : The command to run inside JuNest environment. +# Default command is defined by SH variable. +# Returns: +# $ARCHITECTURE_MISMATCH : If host and JuNest architecture are different. +# Output: +# - : The command output. +####################################### function run_env_as_root(){ source ${JUNEST_HOME}/etc/junest/info [ "$JUNEST_ARCH" != "$ARCH" ] && \ - die "The host system architecture is not correct: $ARCH != $JUNEST_ARCH" + die_on_status $ARCHITECTURE_MISMATCH "The host system architecture is not correct: $ARCH != $JUNEST_ARCH" local uid=$UID # SUDO_USER is more reliable compared to SUDO_UID @@ -268,18 +317,48 @@ function _run_env_with_qemu(){ _run_env_with_proot "$proot_args" "${@}" } +####################################### +# Run JuNest as fakeroot. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory. +# EUID (RO) : The user ID. +# SH (RO) : Contains the default command to run in JuNest. +# Arguments: +# cmd ($@?) : The command to run inside JuNest environment. +# Default command is defined by SH variable. +# Returns: +# $ROOT_ACCESS_ERROR : If the user is the real root. +# Output: +# - : The command output. +####################################### function run_env_as_fakeroot(){ (( EUID == 0 )) && \ - die "You cannot access with root privileges. Use --root option instead." + die_on_status $ROOT_ACCESS_ERROR "You cannot access with root privileges. Use --root option instead." # The mtab file should already be available in the image. # This following instruction will be deleted [ ! -e ${JUNEST_HOME}/etc/mtab ] && ln_cmd -s /proc/self/mounts ${JUNEST_HOME}/etc/mtab _run_env_with_qemu "-S ${JUNEST_HOME} $1" "${@:2}" } +####################################### +# Run JuNest as normal user. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory. +# EUID (RO) : The user ID. +# SH (RO) : Contains the default command to run in JuNest. +# Arguments: +# cmd ($@?) : The command to run inside JuNest environment. +# Default command is defined by SH variable. +# Returns: +# $ROOT_ACCESS_ERROR : If the user is the real root. +# Output: +# - : The command output. +####################################### function run_env_as_user(){ (( EUID == 0 )) && \ - die "You cannot access with root privileges. Use --root option instead." + die_on_status $ROOT_ACCESS_ERROR "You cannot access with root privileges. Use --root option instead." # The mtab file should already be available in the image. # This following instruction will be deleted [ ! -e ${JUNEST_HOME}/etc/mtab ] && ln_cmd -s /proc/self/mounts ${JUNEST_HOME}/etc/mtab @@ -298,6 +377,18 @@ function _provide_bindings_as_user(){ echo $existing_bindings } +####################################### +# Remove an existing JuNest system. +# +# Globals: +# JUNEST_HOME (RO) : The JuNest home directory to remove. +# Arguments: +# None +# Returns: +# None +# Output: +# None +####################################### function delete_env(){ ! ask "Are you sure to delete ${NAME} located in ${JUNEST_HOME}" "N" && return if mountpoint -q ${JUNEST_HOME} diff --git a/lib/util.sh b/lib/util.sh deleted file mode 100644 index 6865c16..0000000 --- a/lib/util.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env bash -# -# This file is part of JuNest (https://github.com/fsquillace/junest) -# -# Copyright (c) 2015 -# -# This program is free software; you can redistribute it and/or modify it -# under the terms of the GNU Library General Public License as published -# by the Free Software Foundation; either version 2, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -function echoerr(){ - echo "$@" 1>&2 -} -function die(){ -# $@: msg (mandatory) - str: Message to print - error $@ - exit 1 -} -function error(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;31m$@\033[0m" -} -function warn(){ -# $@: msg (mandatory) - str: Message to print - echoerr -e "\033[1;33m$@\033[0m" -} -function info(){ -# $@: msg (mandatory) - str: Message to print - echo -e "\033[1;37m$@\033[0m" -} - -function ask(){ - # $1: question string - # $2: default value - can be either Y, y, N, n (by default Y) - - local default="Y" - [ -z $2 ] || default=$(echo "$2" | tr '[:lower:]' '[:upper:]') - - local other="n" - [ "$default" == "N" ] && other="y" - - local prompt="$1 (${default}/${other})> " - - local res="none" - while [ "$res" != "Y" ] && [ "$res" != "N" ] && [ "$res" != "" ]; - do - read -p "$prompt" res - res=$(echo "$res" | tr '[:lower:]' '[:upper:]') - done - - [ "$res" == "" ] && res="$default" - - if [ "$res" == "Y" ] - then - return 0 - else - return 1 - fi - -} - -function insert_quotes_on_spaces(){ -# It inserts quotes between arguments. -# Useful to preserve quotes on command -# to be used inside sh -c/bash -c - C='' - whitespace="[[:space:]]" - for i in "$@" - do - if [[ $i =~ $whitespace ]] - then - C="$C \"$i\"" - else - C="$C $i" - fi - done - echo $C -} - -contains_element () { - local e - for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done - return 1 -} diff --git a/lib/utils.sh b/lib/utils.sh new file mode 100644 index 0000000..840fbe9 --- /dev/null +++ b/lib/utils.sh @@ -0,0 +1,195 @@ +#!/usr/bin/env bash + +NULL_EXCEPTION=11 +WRONG_ANSWER=33 + +####################################### +# Check if the argument is null. +# +# Globals: +# None +# Arguments: +# argument ($1) : Argument to check. +# Returns: +# 0 : If argument is not null. +# NULL_EXCEPTION : If argument is null. +# Output: +# None +####################################### +function check_not_null() { + [ -z "$1" ] && { error "Error: null argument $1"; return $NULL_EXCEPTION; } + return 0 +} + +####################################### +# Redirect message to stderr. +# +# Globals: +# None +# Arguments: +# msg ($@): Message to print. +# Returns: +# None +# Output: +# Message printed to stderr. +####################################### +function echoerr() { + echo "$@" 1>&2; +} + +####################################### +# Print an error message to stderr and exit program. +# +# Globals: +# None +# Arguments: +# msg ($@) : Message to print. +# Returns: +# 1 : The unique exit status printed. +# Output: +# Message printed to stderr. +####################################### +function die() { + error $@ + exit 1 +} + +####################################### +# Print an error message to stderr and exit program with a given status. +# +# Globals: +# None +# Arguments: +# status ($1) : The exit status to use. +# msg ($2-) : Message to print. +# Returns: +# $? : The $status exit status. +# Output: +# Message printed to stderr. +####################################### +function die_on_status() { + status=$1 + shift + error $@ + exit $status +} + +####################################### +# Print an error message to stderr. +# +# Globals: +# None +# Arguments: +# msg ($@): Message to print. +# Returns: +# None +# Output: +# Message printed to stderr. +####################################### +function error() { + echoerr -e "\033[1;31m$@\033[0m" +} + +####################################### +# Print a warn message to stderr. +# +# Globals: +# None +# Arguments: +# msg ($@): Message to print. +# Returns: +# None +# Output: +# Message printed to stderr. +####################################### +function warn() { + # $@: msg (mandatory) - str: Message to print + echoerr -e "\033[1;33m$@\033[0m" +} + +####################################### +# Print an info message to stdout. +# +# Globals: +# None +# Arguments: +# msg ($@): Message to print. +# Returns: +# None +# Output: +# Message printed to stdout. +####################################### +function info(){ + echo -e "\033[1;36m$@\033[0m" +} + +####################################### +# Ask a question and wait to receive an answer from stdin. +# It returns $default_answer if no answer has be received from stdin. +# +# Globals: +# None +# Arguments: +# question ($1) : The question to ask. +# default_answer ($2) : Possible values: 'Y', 'y', 'N', 'n' (default: 'Y') +# Returns: +# 0 : If user replied with either 'Y' or 'y'. +# 1 : If user replied with either 'N' or 'n'. +# WRONG_ANSWER : If `default_answer` is not one of the possible values. +# Output: +# Print the question to ask. +####################################### +function ask(){ + local question=$1 + local default_answer=$2 + check_not_null $question + + if [ ! -z "$default_answer" ] + then + local answers="Y y N n" + [[ "$answers" =~ "$default_answer" ]] || { error "The default answer: $default_answer is wrong."; return $WRONG_ANSWER; } + fi + + local default="Y" + [ -z "$default_answer" ] || default=$(echo "$default_answer" | tr '[:lower:]' '[:upper:]') + + local other="n" + [ "$default" == "N" ] && other="y" + + local prompt=$(info "$question (${default}/${other})> ") + + local res="none" + while [ "$res" != "Y" ] && [ "$res" != "N" ] && [ "$res" != "" ]; + do + read -p "$prompt" res + res=$(echo "$res" | tr '[:lower:]' '[:upper:]') + done + + [ "$res" == "" ] && res="$default" + + [ "$res" == "Y" ] +} + +function insert_quotes_on_spaces(){ +# It inserts quotes between arguments. +# Useful to preserve quotes on command +# to be used inside sh -c/bash -c + C='' + whitespace="[[:space:]]" + for i in "$@" + do + if [[ $i =~ $whitespace ]] + then + C="$C \"$i\"" + else + C="$C $i" + fi + done + echo $C +} + +contains_element () { + local e + for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done + return 1 +} diff --git a/tests/test-cli.sh b/tests/test-cli.sh index 46d38d1..c7795dc 100755 --- a/tests/test-cli.sh +++ b/tests/test-cli.sh @@ -1,9 +1,15 @@ #!/bin/bash +source "$(dirname $0)/utils.sh" + source $(dirname $0)/../bin/junest -h &> /dev/null # Disable the exiterr set +e +function oneTimeSetUp(){ + setUpUnitTests +} + function setUp(){ function is_env_installed(){ return 0 @@ -51,152 +57,125 @@ function run_env_as_user(){ echo "run_env_as_user($proot_args,$@)" } -function wrap_env(){ - parse_arguments "$@" - check_cli - execute_operation -} - function test_help(){ - local output=$(wrap_env -h) - assertEquals $output "usage" - local output=$(wrap_env --help) - assertEquals $output "usage" + assertCommandSuccess cli -h + assertEquals "usage" "$(cat $STDOUTF)" + assertCommandSuccess cli --help + assertEquals "usage" "$(cat $STDOUTF)" } function test_version(){ - local output=$(wrap_env -v) - assertEquals $output "version" - local output=$(wrap_env --version) - assertEquals $output "version" + assertCommandSuccess cli -v + assertEquals "version" "$(cat $STDOUTF)" + assertCommandSuccess cli --version + assertEquals "version" "$(cat $STDOUTF)" } function test_build_image_env(){ - local output=$(wrap_env -b) - assertEquals $output "build_image_env(false,false)" - local output=$(wrap_env --build-image) - assertEquals $output "build_image_env(false,false)" - local output=$(wrap_env -b -s) - assertEquals $output "build_image_env(false,true)" - local output=$(wrap_env -b -n) - assertEquals $output "build_image_env(true,false)" - local output=$(wrap_env -b -n -s) - assertEquals $output "build_image_env(true,true)" - local output=$(wrap_env --build-image --disable-validation --skip-root-tests) - assertEquals $output "build_image_env(true,true)" + assertCommandSuccess cli -b + assertEquals "build_image_env(false,false)" "$(cat $STDOUTF)" + assertCommandSuccess cli --build-image + assertEquals "build_image_env(false,false)" "$(cat $STDOUTF)" + assertCommandSuccess cli -b -s + assertEquals "build_image_env(false,true)" "$(cat $STDOUTF)" + assertCommandSuccess cli -b -n + assertEquals "build_image_env(true,false)" "$(cat $STDOUTF)" + assertCommandSuccess cli -b -n -s + assertEquals "build_image_env(true,true)" "$(cat $STDOUTF)" + assertCommandSuccess cli --build-image --disable-validation --skip-root-tests + assertEquals "build_image_env(true,true)" "$(cat $STDOUTF)" } function test_check_env(){ - local output=$(wrap_env -c myscript) - assertEquals $output "check_env(${JUNEST_HOME},myscript,false)" - local output=$(wrap_env --check myscript) - assertEquals $output "check_env(${JUNEST_HOME},myscript,false)" - local output=$(wrap_env -c myscript -s) - assertEquals $output "check_env(${JUNEST_HOME},myscript,true)" - local output=$(wrap_env --check myscript --skip-root-tests) - assertEquals $output "check_env(${JUNEST_HOME},myscript,true)" + assertCommandSuccess cli -c myscript + assertEquals "check_env(${JUNEST_HOME},myscript,false)" "$(cat $STDOUTF)" + assertCommandSuccess cli --check myscript + assertEquals "check_env(${JUNEST_HOME},myscript,false)" "$(cat $STDOUTF)" + assertCommandSuccess cli -c myscript -s + assertEquals "check_env(${JUNEST_HOME},myscript,true)" "$(cat $STDOUTF)" + assertCommandSuccess cli --check myscript --skip-root-tests + assertEquals "check_env(${JUNEST_HOME},myscript,true)" "$(cat $STDOUTF)" } function test_delete_env(){ - local output=$(wrap_env -d) - assertEquals $output "delete_env" - local output=$(wrap_env --delete) - assertEquals $output "delete_env" + assertCommandSuccess cli -d + assertEquals "delete_env" "$(cat $STDOUTF)" + assertCommandSuccess cli --delete + assertEquals "delete_env" "$(cat $STDOUTF)" } -#function test_setup_env_from_file(){ - #local output=$(wrap_env -i myimage) - #assertEquals $output "setup_env_from_file(myimage)" - #local output=$(wrap_env --setup-from-file myimage) - #assertEquals $output "setup_env_from_file(myimage)" -#} function test_setup_env_from_file(){ is_env_installed(){ return 1 } - local output=$(wrap_env -i myimage) - assertEquals "$output" "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" - local output=$(wrap_env --setup-from-file myimage) - assertEquals "$output" "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" + assertCommandSuccess cli -i myimage + assertEquals "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" "$(cat $STDOUTF)" + assertCommandSuccess cli --setup-from-file myimage + assertEquals "$(echo -e "setup_env_from_file(myimage)\nrun_env_as_user(,)")" "$(cat $STDOUTF)" is_env_installed(){ return 0 } - $(wrap_env -i myimage 2> /dev/null) - assertEquals 1 $? + assertCommandFail cli -i myimage } function test_setup_env(){ is_env_installed(){ return 1 } - local output=$(wrap_env -a arm) - assertEquals "$output" "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" - local output=$(wrap_env --arch arm) - assertEquals "$output" "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" - local output=$(wrap_env) - assertEquals "$output" "$(echo -e "setup_env()\nrun_env_as_user(,)")" + assertCommandSuccess cli -a arm + assertEquals "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" "$(cat $STDOUTF)" + assertCommandSuccess cli --arch arm + assertEquals "$(echo -e "setup_env(arm)\nrun_env_as_user(,)")" "$(cat $STDOUTF)" + assertCommandSuccess cli + assertEquals "$(echo -e "setup_env()\nrun_env_as_user(,)")" "$(cat $STDOUTF)" is_env_installed(){ return 0 } - $(wrap_env -a arm 2> /dev/null) - assertEquals 1 $? + assertCommandFail cli -a arm } function test_run_env_as_fakeroot(){ - local output=$(wrap_env -f) - assertEquals $output "run_env_as_fakeroot(,)" - local output=$(wrap_env --fakeroot) - assertEquals $output "run_env_as_fakeroot(,)" + assertCommandSuccess cli -f + assertEquals "run_env_as_fakeroot(,)" "$(cat $STDOUTF)" + assertCommandSuccess cli --fakeroot + assertEquals "run_env_as_fakeroot(,)" "$(cat $STDOUTF)" - local output=$(wrap_env -f -p "-b arg") - assertEquals "${output[@]}" "run_env_as_fakeroot(-b arg,)" - local output=$(wrap_env -f -p "-b arg" -- command -kv) - assertEquals "${output[@]}" "run_env_as_fakeroot(-b arg,command -kv)" - local output=$(wrap_env -f command --as) - assertEquals "${output[@]}" "run_env_as_fakeroot(,command --as)" - $(wrap_env -a "myarch" -f command --as 2> /dev/null) - assertEquals 1 $? + assertCommandSuccess cli -f -p "-b arg" + assertEquals "run_env_as_fakeroot(-b arg,)" "$(cat $STDOUTF)" + assertCommandSuccess cli -f -p "-b arg" -- command -kv + assertEquals "run_env_as_fakeroot(-b arg,command -kv)" "$(cat $STDOUTF)" + assertCommandSuccess cli -f command --as + assertEquals "run_env_as_fakeroot(,command --as)" "$(cat $STDOUTF)" + assertCommandFail cli -a "myarch" -f command --as } function test_run_env_as_user(){ - local output=$(wrap_env) - assertEquals $output "run_env_as_user(,)" + assertCommandSuccess cli + assertEquals "run_env_as_user(,)" "$(cat $STDOUTF)" - local output=$(wrap_env -p "-b arg") - assertEquals "$output" "run_env_as_user(-b arg,)" - local output=$(wrap_env -p "-b arg" -- command -ll) - assertEquals "$output" "run_env_as_user(-b arg,command -ll)" - local output=$(wrap_env command -ls) - assertEquals "$output" "run_env_as_user(,command -ls)" - $(wrap_env -a "myarch" -- command -ls 2> /dev/null) - assertEquals 1 $? + assertCommandSuccess cli -p "-b arg" + assertEquals "run_env_as_user(-b arg,)" "$(cat $STDOUTF)" + assertCommandSuccess cli -p "-b arg" -- command -ll + assertEquals "run_env_as_user(-b arg,command -ll)" "$(cat $STDOUTF)" + assertCommandSuccess cli command -ls + assertEquals "run_env_as_user(,command -ls)" "$(cat $STDOUTF)" + + assertCommandFail cli -a "myarch" -- command -ls } function test_run_env_as_root(){ - local output=$(wrap_env -r) - assertEquals $output "run_env_as_root" - - local output=$(wrap_env -r command) - assertEquals "${output[@]}" "run_env_as_root command" + assertCommandSuccess cli -r + assertEquals "run_env_as_root " "$(cat $STDOUTF)" + assertCommandSuccess cli -r command + assertEquals "run_env_as_root command" "$(cat $STDOUTF)" } function test_check_cli(){ - $(wrap_env -b -h 2> /dev/null) - assertEquals $? 1 - $(wrap_env -b -c 2> /dev/null) - assertEquals $? 1 - $(wrap_env -d -s 2> /dev/null) - assertEquals $? 1 - $(wrap_env -n -v 2> /dev/null) - assertEquals $? 1 - $(wrap_env -d -r 2> /dev/null) - assertEquals $? 1 - $(wrap_env -h -f 2> /dev/null) - assertEquals $? 1 - $(wrap_env -v -i fsd 2> /dev/null) - assertEquals $? 1 - $(wrap_env -f -r 2> /dev/null) - assertEquals $? 1 - $(wrap_env -p args -v 2> /dev/null) - assertEquals $? 1 - $(wrap_env -a arch -v 2> /dev/null) - assertEquals $? 1 - $(wrap_env -d args 2> /dev/null) - assertEquals $? 1 + assertCommandFail cli -b -h + assertCommandFail cli -b -c + assertCommandFail cli -d -s + assertCommandFail cli -n -v + assertCommandFail cli -d -r + assertCommandFail cli -h -f + assertCommandFail cli -v -i fsd + assertCommandFail cli -f -r + assertCommandFail cli -p args -v + assertCommandFail cli -a arch -v + assertCommandFail cli -d args } source $(dirname $0)/shunit2 diff --git a/tests/test-core.sh b/tests/test-core.sh index bd57032..2724eee 100755 --- a/tests/test-core.sh +++ b/tests/test-core.sh @@ -1,12 +1,17 @@ #!/bin/bash +source "$(dirname $0)/utils.sh" + +# Disable the exiterr +set +e function oneTimeSetUp(){ [ -z "$SKIP_ROOT_TESTS" ] && SKIP_ROOT_TESTS=0 CURRPWD=$PWD - ENV_MAIN_HOME=/tmp/envtesthome - [ -e $ENV_MAIN_HOME ] || JUNEST_HOME=$ENV_MAIN_HOME bash --rcfile "$(dirname $0)/../lib/core.sh" -ic "setup_env" + ENV_MAIN_HOME=/tmp/junest-test-home + [ -e $ENV_MAIN_HOME ] || JUNEST_HOME=$ENV_MAIN_HOME bash -ic "source $CURRPWD/$(dirname $0)/../lib/utils.sh; source $CURRPWD/$(dirname $0)/../lib/core.sh; setup_env" JUNEST_HOME="" + setUpUnitTests } function install_mini_env(){ @@ -17,6 +22,7 @@ function setUp(){ cd $CURRPWD JUNEST_HOME=$(TMPDIR=/tmp mktemp -d -t envhome.XXXXXXXXXX) JUNEST_BASE="$CURRPWD/$(dirname $0)/.." + source "${JUNEST_BASE}/lib/utils.sh" source "${JUNEST_BASE}/lib/core.sh" ORIGIN_WD=$(TMPDIR=/tmp mktemp -d -t envowd.XXXXXXXXXX) cd $ORIGIN_WD @@ -40,42 +46,36 @@ function tearDown(){ function test_is_env_installed(){ - is_env_installed - assertEquals $? 1 + assertCommandFail is_env_installed touch $JUNEST_HOME/just_file - is_env_installed - assertEquals $? 0 + assertCommandSuccess is_env_installed } function test_download(){ WGET=/bin/true CURL=/bin/false - download_cmd - assertEquals $? 0 + assertCommandSuccess download_cmd WGET=/bin/false CURL=/bin/true - download_cmd - assertEquals $? 0 + assertCommandSuccess download_cmd - $(WGET=/bin/false CURL=/bin/false download_cmd something 2> /dev/null) - assertEquals $? 1 + WGET=/bin/false CURL=/bin/false assertCommandFail download_cmd } function test_ln(){ install_mini_env touch ln_file - ln_cmd -s ln_file new_file - assertEquals $? 0 + assertCommandSuccess ln_cmd -s ln_file new_file assertTrue "[ -e new_file ]" rm new_file touch ln_file OLDPATH="$PATH" PATH="" - ln_cmd -s ln_file new_file 2> /dev/null + $(ln_cmd -s ln_file new_file 2> /dev/null) local ret=$? PATH="$OLDPATH" assertEquals $ret 0 @@ -86,8 +86,7 @@ function test_rm(){ install_mini_env touch rm_file - rm_cmd rm_file - assertEquals $? 0 + assertCommandSuccess rm_cmd rm_file assertTrue "[ ! -e rm_file ]" touch rm_file @@ -106,8 +105,7 @@ function test_chown(){ local id=$(id -u) touch chown_file - chown_cmd $id chown_file - assertEquals $? 0 + assertCommandSuccess chown_cmd $id chown_file touch chown_file OLDPATH="$PATH" @@ -121,8 +119,7 @@ function test_chown(){ function test_mkdir(){ install_mini_env - mkdir_cmd -p new_dir/new_dir - assertEquals $? 0 + assertCommandSuccess mkdir_cmd -p new_dir/new_dir assertTrue "[ -d new_dir/new_dir ]" rm -rf new_dir @@ -150,8 +147,7 @@ function test_setup_env(){ assertTrue "[ -e $JUNEST_HOME/file ]" assertTrue "[ -e $JUNEST_HOME/run/lock ]" - $(setup_env "noarch" 2> /dev/null) - assertEquals 1 $? + assertCommandFailOnStatus 102 setup_env "noarch" } @@ -162,8 +158,7 @@ function test_setup_env_from_file(){ assertTrue "[ -e $JUNEST_HOME/file ]" assertTrue "[ -e $JUNEST_HOME/run/lock ]" - $(setup_env_from_file noexist.tar.gz 2> /dev/null) - assertEquals $? 1 + assertCommandFailOnStatus 103 setup_env_from_file noexist.tar.gz } function test_setup_env_from_file_with_absolute_path(){ @@ -182,29 +177,23 @@ function test_run_env_as_root(){ CLASSIC_CHROOT="sudo $CLASSIC_CHROOT" CHOWN="sudo $CHOWN" - local output=$(run_env_as_root pwd) - assertEquals "/" "$output" - run_env_as_root [ -e /run/lock ] - assertEquals 0 $? - run_env_as_root [ -e $HOME ] - assertEquals 0 $? + assertCommandSuccess run_env_as_root pwd + assertEquals "/" "$(cat $STDOUTF)" + assertCommandSuccess run_env_as_root [ -e /run/lock ] + assertCommandSuccess run_env_as_root [ -e $HOME ] # test that normal user has ownership of the files created by root - run_env_as_root touch /a_root_file - # This ensure that the trap will be executed - kill -TERM $$ - local output=$(run_env_as_root stat -c '%u' /a_root_file) - assertEquals "$UID" "$output" + assertCommandSuccess run_env_as_root touch /a_root_file + assertCommandSuccess run_env_as_root stat -c '%u' /a_root_file + assertEquals "$UID" "$(cat $STDOUTF)" SH=("sh" "--login" "-c" "type -t type") - local output=$(run_env_as_root) - assertEquals "builtin" "$output" + assertCommandSuccess run_env_as_root + assertEquals "builtin" "$(cat $STDOUTF)" SH=("sh" "--login" "-c" "[ -e /run/lock ]") - run_env_as_root - assertEquals 0 $? + assertCommandSuccess run_env_as_root SH=("sh" "--login" "-c" "[ -e $HOME ]") - run_env_as_root - assertEquals 0 $? + assertCommandSuccess run_env_as_root } function test_run_env_as_root_different_arch(){ @@ -212,8 +201,7 @@ function test_run_env_as_root_different_arch(){ install_mini_env echo "JUNEST_ARCH=XXX" > ${JUNEST_HOME}/etc/junest/info - $(run_env_as_root pwd 2> /dev/null) - assertEquals 1 $? + assertCommandFailOnStatus 104 run_env_as_root pwd } function test_run_env_as_classic_root(){ @@ -224,10 +212,9 @@ function test_run_env_as_classic_root(){ CLASSIC_CHROOT="sudo $CLASSIC_CHROOT" CHOWN="sudo $CHOWN" - local output=$(run_env_as_root pwd 2> /dev/null) - assertEquals "/" "$output" - run_env_as_root [ -e /run/lock ] 2> /dev/null - assertEquals 0 $? + assertCommandSuccess run_env_as_root pwd + assertEquals "/" "$(cat $STDOUTF)" + assertCommandSuccess run_env_as_root [ -e /run/lock ] } function test_run_env_as_junest_root(){ @@ -239,30 +226,28 @@ function test_run_env_as_junest_root(){ LD_EXEC="sudo $LD_EXEC" CHOWN="sudo $CHOWN" - local output=$(run_env_as_root pwd 2> /dev/null) - assertEquals "/" "$output" - run_env_as_root [ -e /run/lock ] 2> /dev/null - assertEquals 0 $? - run_env_as_root [ -e $HOME ] 2> /dev/null - assertEquals 1 $? + assertCommandSuccess run_env_as_root pwd + assertEquals "/" "$(cat $STDOUTF)" + assertCommandSuccess run_env_as_root [ -e /run/lock ] + assertCommandFail run_env_as_root [ -e $HOME ] } function test_run_env_as_user(){ install_mini_env - local output=$(run_env_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2" | awk -F: '{print $1}') - assertEquals "$output" "/usr/bin/mkdir" + assertCommandSuccess run_env_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2" + assertEquals "/usr/bin/mkdir" "$(cat $STDOUTF| awk -F: '{print $1}')" assertTrue "[ -e $JUNEST_HOME/newdir2 ]" SH=("/usr/bin/echo") - local output=$(run_env_as_user "-k 3.10") - assertEquals "-c :" "$output" + assertCommandSuccess run_env_as_user "-k 3.10" + assertEquals "-c :" "$(cat $STDOUTF)" } function test_run_env_as_proot_mtab(){ install_mini_env - $(run_env_as_fakeroot "-k 3.10" "echo") + assertCommandSuccess run_env_as_fakeroot "-k 3.10" "echo" assertTrue "[ -e $JUNEST_HOME/etc/mtab ]" - $(run_env_as_user "-k 3.10" "echo") + assertCommandSuccess run_env_as_user "-k 3.10" "echo" assertTrue "[ -e $JUNEST_HOME/etc/mtab ]" } @@ -273,38 +258,33 @@ function test_run_env_as_root_mtab(){ CHROOT="sudo $CHROOT" CLASSIC_CHROOT="sudo $CLASSIC_CHROOT" CHOWN="sudo $CHOWN" - $(run_env_as_root "echo") + assertCommandSuccess run_env_as_root "echo" assertTrue "[ ! -e $JUNEST_HOME/etc/mtab ]" } function test_run_env_with_quotes(){ install_mini_env - local output=$(run_env_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2" | awk -F: '{print $1}') - assertEquals "/usr/bin/mkdir" "$output" + assertCommandSuccess run_env_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2" + assertEquals "/usr/bin/mkdir" "$(cat $STDOUTF| awk -F: '{print $1}')" assertTrue "[ -e $JUNEST_HOME/newdir2 ]" } function test_run_env_as_user_proot_args(){ install_mini_env - run_env_as_user "--help" "" &> /dev/null - assertEquals 0 $? + assertCommandSuccess run_env_as_user "--help" "" mkdir $JUNEST_TEMPDIR/newdir touch $JUNEST_TEMPDIR/newdir/newfile - run_env_as_user "-b $JUNEST_TEMPDIR/newdir:/newdir -k 3.10" "ls" "-l" "/newdir/newfile" &> /dev/null - assertEquals 0 $? + assertCommandSuccess run_env_as_user "-b $JUNEST_TEMPDIR/newdir:/newdir -k 3.10" "ls" "-l" "/newdir/newfile" - $(_run_env_with_proot --helps 2> /dev/null) - assertEquals 1 $? + assertCommandFail _run_env_with_proot --helps } function test_run_env_with_proot_compat(){ PROOT="/bin/true" - _run_env_with_proot "" "" &> /dev/null - assertEquals 0 $? + assertCommandSuccess _run_env_with_proot "" "" - $(PROOT="/bin/false" _run_env_with_proot --helps 2> /dev/null) - assertEquals 1 $? + PROOT="/bin/false" assertCommandFail _run_env_with_proot --helps } function test_run_env_with_proot_as_root(){ @@ -312,10 +292,8 @@ function test_run_env_with_proot_as_root(){ install_mini_env - $(sudo run_env_as_user 2> /dev/null) - assertEquals 1 $? - $(sudo run_env_as_fakeroot 2> /dev/null) - assertEquals 1 $? + assertCommandFail sudo run_env_as_user + assertCommandFail sudo run_env_as_fakeroot } function test_run_proot_seccomp(){ @@ -323,39 +301,38 @@ function test_run_proot_seccomp(){ env } PROOT=envv - local output=$(proot_cmd | grep "^PROOT_NO_SECCOMP") - assertEquals "" "$output" + assertCommandSuccess proot_cmd cmd + assertEquals "" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")" envv(){ env | grep "^PROOT_NO_SECCOMP" } PROOT=envv local output=$(proot_cmd | grep "^PROOT_NO_SECCOMP") + assertCommandSuccess proot_cmd cmd # The variable PROOT_NO_SECCOMP will be produced # twice due to the fallback mechanism assertEquals "PROOT_NO_SECCOMP=1 -PROOT_NO_SECCOMP=1" "$output" +PROOT_NO_SECCOMP=1" "$(cat $STDOUTF | grep "^PROOT_NO_SECCOMP")" } function test_run_env_as_fakeroot(){ install_mini_env - local output=$(run_env_as_fakeroot "-k 3.10" "id" | awk '{print $1}') - assertEquals "uid=0(root)" "$output" + assertCommandSuccess run_env_as_fakeroot "-k 3.10" "id" + assertEquals "uid=0(root)" "$(cat $STDOUTF | awk '{print $1}')" } function test_delete_env(){ install_mini_env echo "N" | delete_env 1> /dev/null - is_env_installed - assertEquals 0 $? + assertCommandSuccess is_env_installed echo "Y" | delete_env 1> /dev/null - is_env_installed - assertEquals 1 $? + assertCommandFail is_env_installed } function test_nested_env(){ install_mini_env - JUNEST_ENV=1 bash -ic "source $CURRPWD/$(dirname $0)/../lib/core.sh" &> /dev/null + JUNEST_ENV=1 bash -ic "source $CURRPWD/$(dirname $0)/../lib/utils.sh; source $CURRPWD/$(dirname $0)/../lib/core.sh" &> /dev/null assertEquals 1 $? } diff --git a/tests/test-util.sh b/tests/test-util.sh deleted file mode 100755 index 80fd660..0000000 --- a/tests/test-util.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash -source "$(dirname $0)/../lib/util.sh" - -# Disable the exiterr -set +e - -function test_echoerr(){ - local actual=$(echoerr "Test" 2>&1) - assertEquals "$actual" "Test" -} - -function test_error(){ - local actual=$(error "Test" 2>&1) - local expected=$(echo -e "\033[1;31mTest\033[0m") - assertEquals "$actual" "$expected" -} - -function test_warn(){ - local actual=$(warn "Test" 2>&1) - local expected=$(echo -e "\033[1;33mTest\033[0m") - assertEquals "$actual" "$expected" -} - -function test_info(){ - local actual=$(info "Test") - local expected=$(echo -e "\033[1;37mTest\033[0m") - assertEquals "$actual" "$expected" -} - -function test_die(){ - local actual=$(die "Test" 2>&1) - local expected=$(echo -e "\033[1;31mTest\033[0m") - assertEquals "$actual" "$expected" - $(die Dying 2> /dev/null) - assertEquals $? 1 -} - -function test_ask(){ - echo "Y" | ask "Test" &> /dev/null - assertEquals $? 0 - echo "y" | ask "Test" &> /dev/null - assertEquals $? 0 - echo "N" | ask "Test" &> /dev/null - assertEquals $? 1 - echo "n" | ask "Test" &> /dev/null - assertEquals $? 1 - echo -e "\n" | ask "Test" &> /dev/null - assertEquals $? 0 - echo -e "\n" | ask "Test" "N" &> /dev/null - assertEquals $? 1 - echo -e "asdf\n\n" | ask "Test" "N" &> /dev/null - assertEquals $? 1 -} - -function test_insert_quotes_on_spaces(){ - local actual=$(insert_quotes_on_spaces this is "a test") - assertEquals "this is \"a test\"" "$actual" - - local actual=$(insert_quotes_on_spaces this is 'a test') - assertEquals "this is \"a test\"" "$actual" -} - -function test_contains_element(){ - array=("something to search for" "a string" "test2000") - contains_element "a string" "${array[@]}" - assertEquals "$?" "0" - - contains_element "blabla" "${array[@]}" - assertEquals "$?" "1" -} - -source $(dirname $0)/shunit2 diff --git a/tests/test-utils.sh b/tests/test-utils.sh new file mode 100755 index 0000000..437ae5a --- /dev/null +++ b/tests/test-utils.sh @@ -0,0 +1,97 @@ +#!/bin/bash +source "$(dirname $0)/utils.sh" + +unset HOME +export HOME=$(TMPDIR=/tmp mktemp -d -t pearl-user-home.XXXXXXX) + +source "$(dirname $0)/../lib/utils.sh" + +# Disable the exiterr +set +e + +function oneTimeSetUp(){ + setUpUnitTests +} + +function test_check_not_null(){ + assertCommandFailOnStatus 11 check_not_null "" "" + assertCommandSuccess check_not_null "bla" "" +} + +function test_echoerr(){ + assertCommandSuccess echoerr "Test" + assertEquals "Test" "$(cat $STDERRF)" +} + +function test_error(){ + assertCommandSuccess error "Test" + local expected=$(echo -e "\033[1;31mTest\033[0m") + assertEquals "$expected" "$(cat $STDERRF)" +} + +function test_warn(){ + assertCommandSuccess warn "Test" + local expected=$(echo -e "\033[1;33mTest\033[0m") + assertEquals "$expected" "$(cat $STDERRF)" +} + +function test_info(){ + assertCommandSuccess info "Test" + local expected=$(echo -e "\033[1;36mTest\033[0m") + assertEquals "$expected" "$(cat $STDOUTF)" +} + +function test_die(){ + assertCommandFail die "Test" + local expected=$(echo -e "\033[1;31mTest\033[0m") + assertEquals "$expected" "$(cat $STDERRF)" +} + +function test_die_on_status(){ + assertCommandFailOnStatus 222 die_on_status 222 "Test" + local expected=$(echo -e "\033[1;31mTest\033[0m") + assertEquals "$expected" "$(cat $STDERRF)" +} + +function test_ask_null_question(){ + assertCommandFailOnStatus 11 ask "" "Y" +} + +function test_ask(){ + echo "Y" | ask "Test" &> /dev/null + assertEquals 0 $? + echo "y" | ask "Test" &> /dev/null + assertEquals 0 $? + echo "N" | ask "Test" &> /dev/null + assertEquals 1 $? + echo "n" | ask "Test" &> /dev/null + assertEquals 1 $? + echo -e "\n" | ask "Test" &> /dev/null + assertEquals 0 $? + echo -e "\n" | ask "Test" "N" &> /dev/null + assertEquals 1 $? + echo -e "asdf\n\n" | ask "Test" "N" &> /dev/null + assertEquals 1 $? +} + +function test_ask_wrong_default_answer() { + echo "Y" | ask "Test" G &> /dev/null + assertEquals 33 $? +} + +function test_insert_quotes_on_spaces(){ + assertCommandSuccess insert_quotes_on_spaces this is "a test" + assertEquals "this is \"a test\"" "$(cat $STDOUTF)" + + assertCommandSuccess insert_quotes_on_spaces this is 'a test' + assertEquals "this is \"a test\"" "$(cat $STDOUTF)" +} + +function test_contains_element(){ + array=("something to search for" "a string" "test2000") + assertCommandSuccess contains_element "a string" "${array[@]}" + + assertCommandFailOnStatus 1 contains_element "blabla" "${array[@]}" +} + +source $(dirname $0)/shunit2 diff --git a/tests/utils.sh b/tests/utils.sh new file mode 100644 index 0000000..b12420e --- /dev/null +++ b/tests/utils.sh @@ -0,0 +1,32 @@ + +function setUpUnitTests(){ + OUTPUT_DIR="${SHUNIT_TMPDIR}/output" + mkdir "${OUTPUT_DIR}" + STDOUTF="${OUTPUT_DIR}/stdout" + STDERRF="${OUTPUT_DIR}/stderr" +} + +function assertCommandSuccess(){ + $(set -e + "$@" > $STDOUTF 2> $STDERRF + ) + assertTrue "The command $1 did not return 0 exit status" $? +} + +function assertCommandFail(){ + $(set -e + "$@" > $STDOUTF 2> $STDERRF + ) + assertFalse "The command $1 returned 0 exit status" $? +} + +# $1: expected exit status +# $2-: The command under test +function assertCommandFailOnStatus(){ + local status=$1 + shift + $(set -e + "$@" > $STDOUTF 2> $STDERRF + ) + assertEquals $status $? +}