Skip to content

Commit

Permalink
#66 Add UI color system + add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ltoussaint committed Dec 9, 2012
1 parent f8ea1d4 commit ab6da70
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/.features_subject
/conf/phpunit.xml
/conf/phpunit.php
/conf/twgit.sh
/conf/twgit.sh
/conf/color/_*.sh
21 changes: 21 additions & 0 deletions conf/color/dark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CUI_COLORS=(
[error]='\033[1;31m'
[error.bold]='\033[1;33m'
[error.header]='\033[1m\033[4;33m/!\\\033[0;37m '
[feature_subject]='\033[1;34m'
[help]='\033[0;36m'
[help.bold]='\033[1;36m'
[help.header]='\033[1;36m(i) '
[help_detail]='\033[0;37m'
[help_detail.bold]='\033[1;37m'
[help_detail.header]=' '
[info]='\033[1;37m'
[normal]='\033[0;37m'
[ok]='\033[0;32m'
[processing]='\033[1;37m'
[question]='\033[1;33m'
[question.bold]='\033[1;37m'
[warning]='\033[0;33m'
[warning.bold]='\033[1;33m'
[warning.header]='\033[1m\033[4;33m/!\\\033[0;37m '
)
21 changes: 21 additions & 0 deletions conf/color/default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CUI_COLORS=(
[error]='\033[1;31m'
[error.bold]='\033[1;33m'
[error.header]='\033[1m\033[4;33m/!\\\033[0;37m '
[feature_subject]='\033[1;34m'
[help]='\033[0;36m'
[help.bold]='\033[1;36m'
[help.header]='\033[1;36m(i) '
[help_detail]='\033[0;37m'
[help_detail.bold]='\033[1;37m'
[help_detail.header]=' '
[info]='\033[1;37m'
[normal]='\033[0;37m'
[ok]='\033[0;32m'
[processing]='\033[1;30m'
[question]='\033[1;33m'
[question.bold]='\033[1;37m'
[warning]='\033[0;33m'
[warning.bold]='\033[1;33m'
[warning.header]='\033[1m\033[4;33m/!\\\033[0;37m '
)
21 changes: 21 additions & 0 deletions conf/color/light.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CUI_COLORS=(
[error]='\033[1;31m'
[error.bold]='\033[1;33m'
[error.header]='\033[1m\033[4;33m/!\\\033[0;37m '
[feature_subject]='\033[1;34m'
[help]='\033[0;36m'
[help.bold]='\033[1;36m'
[help.header]='\033[1;36m(i) '
[help_detail]='\033[0;30m'
[help_detail.bold]='\033[1;30m'
[help_detail.header]=' '
[info]='\033[1;30m'
[normal]='\033[0;37m'
[ok]='\033[0;32m'
[processing]='\033[1;37m'
[question]='\033[1;33m'
[question.bold]='\033[1;37m'
[warning]='\033[0;33m'
[warning.bold]='\033[1;33m'
[warning.header]='\033[1m\033[4;33m/!\\\033[0;37m '
)
57 changes: 57 additions & 0 deletions inc/coloredUI.inc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,63 @@
# @license http://www.apache.org/licenses/LICENSE-2.0
#

##
# Check if color file exists
#
# @param string $1 Name of the choose color
# @return 0 if exists, else 1
##
function CUI_existsColorFile () {
local colorName=$1
[ -f "$TWGIT_COLOR_DIR/$colorName.sh" ] && return 0 || return 1
}

##
# Load color file
#
# @param string $1 Name of the choose color
##
function CUI_loadColorFile () {
local colorName=$1
. "$TWGIT_COLOR_DIR/$colorName.sh"
}


##
# Initialize colors and decoration

# @param string $1 Name of the default color file
# @see $CUI_COLORS
# @testedby TwgitCUITest
# @return 0
#
function CUI_initColors () {
local defaultColor="default"

if ! [ $defaultColor = $TWGIT_COLOR_DIR ]; then
if CUI_existsColorFile "$TWGIT_COLOR_FILE" ; then
CUI_loadColorFile "$TWGIT_COLOR_FILE"
return 0
else
errorMessage="Can't load \"$TWGIT_COLOR_FILE\" color file, please make sure \"$TWGIT_COLOR_DIR/$TWGIT_COLOR_FILE.sh\" file exists or change TWGIT_COLOR_FILE configuration"
fi
fi


if CUI_existsColorFile $defaultColor; then
CUI_loadColorFile $defaultColor
CUI_displayMsg warning $errorMessage
return 0
else
echo "Can't load default color file, try to update twgit using'twgit update'"
exit 1
fi

}

# Need to declare CUI_COLORS here to allow accessiblity in next functions




##
Expand Down
58 changes: 58 additions & 0 deletions tests/TwgitCUITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
class TwgitCUITest extends TwgitTestCase
{

/**
* Color directory temporarily created for tests
* @var string
*/
protected $_sTmpColorDir;

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
Expand All @@ -18,6 +24,58 @@ public function setUp ()
$o->remove(TWGIT_REPOSITORY_LOCAL_DIR);
$o->mkdir(TWGIT_REPOSITORY_ORIGIN_DIR, '0777');
$o->mkdir(TWGIT_REPOSITORY_LOCAL_DIR, '0777');

$this->_sTmpColorDir = TWGIT_TMP_DIR . '/color';
$o->remove($this->_sTmpColorDir);
$o->mkdir($this->_sTmpColorDir, '0777');
}

/**
* @shcovers inc/coloredUI.inc.sh::CUI_initColors
* @shcovers inc/coloredUI.inc.sh::CUI_loadColorFile
* @shcovers inc/coloredUI.inc.sh::CUI_existsColorFile
*/
public function testLoadColorFile_ThrowExceptionWhenDefaultColorFileNotExists ()
{
$this->setExpectedException('RuntimeException', "Can't load default color file, try to update twgit using'twgit update'");
$sMsg = $this->_localShellCodeCall('TWGIT_COLOR_DIR="/";TWGIT_COLOR_FILE="default"; CUI_initColors', false);
}

/**
* @shcovers inc/coloredUI.inc.sh::CUI_initColors
* @shcovers inc/coloredUI.inc.sh::CUI_loadColorFile
* @shcovers inc/coloredUI.inc.sh::CUI_existsColorFile
*/
public function testLoadColorFile_DisplayWarningWhenCustomColorFileNotExistsButDefaultExists ()
{
$sMsg = $this->_localShellCodeCall('TWGIT_COLOR_FILE="custom"; CUI_initColors', false);
$this->assertEquals('\033[1m\033[4;33m/!\\\033[0;37m \033[0;33mCan\'t load "custom" color file, please make sure "' . TWGIT_ROOT_DIR . '/conf/color/custom.sh" file exists or change TWGIT_COLOR_FILE configuration\033[0m', $sMsg);
}

/**
* @shcovers inc/coloredUI.inc.sh::CUI_initColors
* @shcovers inc/coloredUI.inc.sh::CUI_loadColorFile
* @shcovers inc/coloredUI.inc.sh::CUI_existsColorFile
*/
public function testLoadColorFile_NoMessageWhenGoodCustomColorFile ()
{
$o = self::_getShellInstance();
$o->exec('touch ' . $this->_sTmpColorDir . '/custom.sh');
$sMsg = $this->_localShellCodeCall('TWGIT_COLOR_DIR="' . $this->_sTmpColorDir . '"; TWGIT_COLOR_FILE="custom"; CUI_initColors', false);
$this->assertEquals('', $sMsg);
}

/**
* @shcovers inc/coloredUI.inc.sh::CUI_initColors
* @shcovers inc/coloredUI.inc.sh::CUI_loadColorFile
* @shcovers inc/coloredUI.inc.sh::CUI_existsColorFile
*/
public function testLoadColorFile_NoMessageWhenDefaultColorFile ()
{
$o = self::_getShellInstance();
$o->exec('touch ' . $this->_sTmpColorDir . '/default.sh');
$sMsg = $this->_localShellCodeCall('TWGIT_COLOR_DIR="' . $this->_sTmpColorDir . '"; TWGIT_COLOR_FILE="default"; CUI_initColors', false);
$this->assertEquals('', $sMsg);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/inc/testFunction.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sCommonFunction="$1"; shift
# Includes:
. $(dirname $0)/../../conf/twgit.sh
. $TWGIT_INC_DIR/common.inc.sh
CUI_initColors

# Execution:
if [ ! -z "$sCommonFunction" ]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/inc/testShellCode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#



# Parameters:
sCmds="$1"; shift

# Includes:
. $(dirname $0)/../../conf/twgit.sh
. $TWGIT_INC_DIR/common.inc.sh
CUI_initColors

# Execution:
rFile="${TWGIT_TMP_DIR}/file.$$.$RANDOM"
Expand Down
1 change: 1 addition & 0 deletions twgit
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if [ ! -f "$config_file" ]; then
fi
. $config_file
. $TWGIT_INC_DIR/common.inc.sh
CUI_initColors



Expand Down

0 comments on commit ab6da70

Please sign in to comment.