diff --git a/test/bash/config.sh b/test/bash/config.sh index e9e2aef..2898df9 100644 --- a/test/bash/config.sh +++ b/test/bash/config.sh @@ -7,23 +7,23 @@ cd $fixtures head "set config (Number)(refresh)" $pg set refresh 4000 > /dev/null -val=`$pg config | grep "refresh:" | egrep -oh "\d+"` +val=$(config "refresh:" "^[^0-9]*([0-9]+).*") -[ $val -eq 4000 ] || fail "expect the value to be 4000, but current is $val" +[ "$val" -eq 4000 ] || fail "expect the value to be 4000, but current is $val" success "the value should be 4000" head "set config (Number)(port)" $pg set port 9000 > /dev/null -val=`$pg config | grep "port:" | egrep -oh "\d+"` +val=$(config "port:" "^[^0-9]*([0-9]+).*") -[ $val -eq 9000 ] || fail "expect the value to be 9000, but current is $val" +[ "$val" -eq 9000 ] || fail "expect the value to be 9000, but current is $val" success "the value should be 9000" head "set config (Boolean)" $pg set manipulation false > /dev/null -val=`$pg config | grep "manipulation:" | egrep -oh "(true|false)"` +val=$(config "manipulation:" ".*(true|false).*") -[ $val = false ] || fail "expect the value to be false, but current is $val" +[ "$val" = false ] || fail "expect the value to be false, but current is $val" success "the value should be false" head "set config (String)" @@ -34,9 +34,9 @@ if [ ! -d "$tmpPM2" ]; then fi $pg set pm2 "$tmpPM2" > /dev/null -val=`$pg config | grep "pm2:" | egrep -oh "$tmpPM2$" | wc -c` +val=$(config "pm2:" ".*(\/.+).*") -[ $val -gt 0 ] || fail "expect the value to be /tmp/.pm2" +[ ! "$val" = "$tmpPM2" ] || fail "expect the value to be /tmp/.pm2" success "the value should be /tmp/.pm2" $pg set pm2 "~/.pm2" > /dev/null diff --git a/test/bash/include.sh b/test/bash/include.sh index 4969547..54a3eaa 100644 --- a/test/bash/include.sh +++ b/test/bash/include.sh @@ -7,6 +7,16 @@ pg="`type -P node` `pwd`/bin/pm2-gui" fixtures="test/fixtures" +function config(){ + local result="" + if [[ "$OSTYPE" =~ ^darwin ]]; then + result=`$pg config | grep "$1" | sed -E "s/$2/\1/"` + else + result=`$pg config | grep "$1" | sed -r "s/$2/\1/"` + fi + echo "$result" +} + function success { echo -e "\033[32m ✔ $1\033[0m" } diff --git a/test/bash/interface.sh b/test/bash/interface.sh index 055a7b5..547e5a6 100644 --- a/test/bash/interface.sh +++ b/test/bash/interface.sh @@ -5,92 +5,98 @@ source "${SRC}/include.sh" cd $fixtures +function port(){ + local result="" + result=`netstat -an | grep "$1" | egrep "tcp" | grep "LISTEN"` + echo "$result" +} + $pg set port 8088 > /dev/null head "run web server (default port)" nohup $pg start > /dev/null 2>&1 & pid=$! sleep 1 -ret=`nc 127.0.0.1 8088 < /dev/null; echo $?` -[ $ret -eq 0 ] || fail "expect 127.0.0.1:8088 can be connected" +ret=$(port 8088) +[ ! -z "$ret" ] || fail "expect 127.0.0.1:8088 can be connected" success "127.0.0.1:8088 should be connected" kill "$pid" sleep 1 -ret=`nc 127.0.0.1 8088 < /dev/null; echo $?` -[ $ret -eq 1 ] || fail "expect 127.0.0.1:8088 can not be connected" +ret=$(port 8088) +[ -z "$ret" ] || fail "expect 127.0.0.1:8088 can not be connected" success "127.0.0.1:8088 should be disconnected" head "run web server (customized port: 9000)" nohup $pg start 9000 > /dev/null 2>&1 & pid=$! sleep 1 -ret=`nc 127.0.0.1 9000 < /dev/null; echo $?` -[ $ret -eq 0 ] || fail "expect 127.0.0.1:9000 can be connected" +ret=$(port 9000) +[ ! -z "$ret" ] || fail "expect 127.0.0.1:9000 can be connected" success "127.0.0.1:9000 should be connected" kill "$pid" sleep 1 -ret=`nc 127.0.0.1 9000 < /dev/null; echo $?` -[ $ret -eq 1 ] || fail "expect 127.0.0.1:9000 can not be connected" +ret=$(port 9000) +[ -z "$ret" ] || fail "expect 127.0.0.1:9000 can not be connected" success "127.0.0.1:9000 should be disconnected" head "run web server (--config verify)" ret=`$pg start --config not_exist.json | grep "does not exist" | wc -c` -[ $ret -gt 0 ] || fail "expect throw out error message" +[ "$ret" -gt 0 ] || fail "expect throw out error message" success "JSON file does not exist" ret=`$pg start --config invalid.conf | grep "invalid" | wc -c` -[ $ret -gt 0 ] || fail "expect throw out error message" +[ "$ret" -gt 0 ] || fail "expect throw out error message" success "JSON file invalid" head "run web server (--config specific file)" nohup $pg start --config pm2-gui-cp.conf > /dev/null 2>&1 & pid=$! sleep 1 -ret=`nc 127.0.0.1 27130 < /dev/null; echo $?` -[ $ret -eq 0 ] || fail "expect 127.0.0.1:27130 can be connected" +ret=$(port 27130) +[ ! -z "$ret" ] || fail "expect 127.0.0.1:27130 can be connected" success "127.0.0.1:27130 should be connected" kill "$pid" sleep 1 -ret=`nc 127.0.0.1 27130 < /dev/null; echo $?` -[ $ret -eq 1 ] || fail "expect 127.0.0.1:27130 can not be connected" +ret=$(port 27130) +[ -z "$ret" ] || fail "expect 127.0.0.1:27130 can not be connected" success "127.0.0.1:27130 should be disconnected" -val=`$pg config | grep "refresh:" | egrep -oh "\d+"` -[ $val -eq 3000 ] || fail "expect the value of refresh to be 3000, but current is $val" +val=$(config "refresh:" "^[^0-9]*([0-9]+).*") +[ "$val" -eq 3000 ] || fail "expect the value of refresh to be 3000, but current is $val" success "the value of refresh should be 3000" -val=`$pg config | grep "manipulation:" | egrep -oh "(true|false)"` -[ $val = false ] || fail "expect the value of manipulation to be false, but current is $val" +val=$(config "manipulation:" ".*(true|false).*") +[ "$val" = false ] || fail "expect the value of manipulation to be false, but current is $val" success "the value of manipulation should be false" -val=`$pg config | grep "pm2:" | egrep -oh "/tmp/\.pm2$" | wc -c` -[ $val -gt 0 ] || fail "expect the value of pm2 to be /tmp/.pm2" +val=$(config "pm2:" ".*(\/.+).*") +[ ! "$val" = "/tmp/.pm2" ] || fail "expect the value of pm2 to be /tmp/.pm2" success "the value of pm2 should be /tmp/.pm2" head "run web server (--config default file)" nohup $pg start --config > /dev/null 2>&1 & pid=$! sleep 1 -ret=`nc 127.0.0.1 8088 < /dev/null; echo $?` -[ $ret -eq 0 ] || fail "expect 127.0.0.1:8088 can be connected" +ret=$(port 8088) +[ ! -z "$ret" ] || fail "expect 127.0.0.1:8088 can be connected" success "127.0.0.1:8088 should be connected" kill "$pid" sleep 1 -ret=`nc 127.0.0.1 8088 < /dev/null; echo $?` -[ $ret -eq 1 ] || fail "expect 127.0.0.1:8088 can not be connected" +ret=$(port 8088) +[ -z "$ret" ] || fail "expect 127.0.0.1:8088 can not be connected" success "127.0.0.1:8088 should be disconnected" -val=`$pg config | grep "refresh:" | egrep -oh "\d+"` -[ $val -eq 5000 ] || fail "expect the value of refresh to be 5000, but current is $val" +val=$(config "refresh:" "^[^0-9]*([0-9]+).*") +[ "$val" -eq 5000 ] || fail "expect the value of refresh to be 5000, but current is $val" success "the value of refresh should be 3000" -val=`$pg config | grep "manipulation:" | egrep -oh "(true|false)"` -[ $val = true ] || fail "expect the value of manipulation to be true, but current is $val" +val=$(config "manipulation:" ".*(true|false).*") +[ "$val" = true ] || fail "expect the value of manipulation to be true, but current is $val" success "the value of manipulation should be true" root="~/.pm2" if [ -z "$PM2_HOME" ] @@ -107,8 +113,8 @@ else fi fi fi -val=`$pg config | grep "pm2:" | egrep -oh "$root$" | wc -c` -[ $val -gt 0 ] || fail "expect the value of pm2 to be $root" +val=$(config "pm2:" ".*(\/.+).*") +[ ! "$val" = "$root" ] || fail "expect the value of pm2 to be $root" success "the value of pm2 should be $root" $pg set port 8088 > /dev/null \ No newline at end of file