Add tests

pull/22/head
Dominik Ritter 6 years ago
parent a8fa0332d2
commit 9b73c8bd9c

@ -533,7 +533,9 @@ prompt_battery() {
# * $1 Alignment: string - left|right
# * $2 Index: integer
# * $3 Joined: bool - If the segment should be joined
# * $4 Root Prefix: string - Root prefix for testing purposes
prompt_public_ip() {
local ROOT_PREFIX="${4}"
# set default values for segment
set_default POWERLEVEL9K_PUBLIC_IP_TIMEOUT "300"
set_default POWERLEVEL9K_PUBLIC_IP_NONE ""
@ -561,7 +563,7 @@ prompt_public_ip() {
# grab a fresh IP if needed
local fresh_ip
if [[ $refresh_ip =~ true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
if [[ $refresh_ip == true && -w $POWERLEVEL9K_PUBLIC_IP_FILE ]]; then
for method in "${POWERLEVEL9K_PUBLIC_IP_METHODS[@]}"; do
case $method in
'dig')
@ -598,7 +600,7 @@ prompt_public_ip() {
# Check VPN is on if VPN interface is set
if [[ -n $POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE ]]; then
if [[ "$OS" == "OSX" ]]; then
for vpn_iface in $(/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1)
for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e ^"$POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE" | cut -d":" -f1)
do
icon='VPN_ICON'
break
@ -1142,16 +1144,17 @@ prompt_ip() {
set_default POWERLEVEL9K_VPN_IP_INTERFACE "tun"
# prompt if vpn active
prompt_vpn_ip() {
local ROOT_PREFIX="${4}"
if [[ "$OS" == "OSX" ]]; then
for vpn_iface in $(/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1)
for vpn_iface in $(${ROOT_PREFIX}/sbin/ifconfig | grep -e "^${POWERLEVEL9K_VPN_IP_INTERFACE}" | cut -d":" -f1)
do
ip=$(/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2)
ip=$(${ROOT_PREFIX}/sbin/ifconfig "$vpn_iface" | grep -o "inet\s.*" | cut -d' ' -f2)
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON'
done
else
for vpn_iface in $(ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+" | grep -o -E "^${POWERLEVEL9K_VPN_IP_INTERFACE}.*")
for vpn_iface in $(${ROOT_PREFIX}/sbin/ip link ls up | grep -o -E ":\s+[a-z0-9]+:" | grep -v "lo" | grep -o -E "[a-z0-9]+" | grep -o -E "^${POWERLEVEL9K_VPN_IP_INTERFACE}.*")
do
ip=$(/sbin/ip -4 a show "$vpn_iface" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+")
ip=$(${ROOT_PREFIX}/sbin/ip -4 a show "$vpn_iface" | grep -o "inet\s*[0-9.]*" | grep -o -E "[0-9.]+")
"$1_prompt_segment" "$0" "$2" "cyan" "$DEFAULT_COLOR" "$ip" 'VPN_ICON'
done
fi

@ -12,6 +12,8 @@ function setUp() {
P9K_HOME=$(pwd)
FOLDER=/tmp/powerlevel9k-test
mkdir -p $FOLDER
mkdir $FOLDER/bin
mkdir $FOLDER/sbin
cd $FOLDER
# Change cache file, so that the users environment don't
@ -219,4 +221,85 @@ function testPublicIpSegmentWhenGoingOnline() {
unfunction dig
}
function testPublicIpSegmentWithVPNTurnedOnLinux() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
local OS='linux'
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1"
ip() {
cat <<EOF
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether 8c:16:45:7d:0c:9a brd ff:ff:ff:ff:ff:ff
3: tun1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
link/ether b4:6b:fc:9d:c6:bc brd ff:ff:ff:ff:ff:ff
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:8f:5c:ed:39 brd ff:ff:ff:ff:ff:ff
EOF
}
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
unfunction ip
rm -f $POWERLEVEL9K_PUBLIC_IP_FILE
}
function testPublicIpSegmentWithVPNTurnedOnOsx() {
typeset -F now
now=$(date +%s)
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
local OS='OSX'
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1"
# Fake stat call
function stat() {
echo $now
}
# Fake ifconfig
cat > $FOLDER/sbin/ifconfig <<EOF
#!/bin/sh
cat <<INNER
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether 8c:16:45:7d:0c:9a txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xe8200000-e8220000
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 5136 bytes 328651 (320.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5136 bytes 328651 (320.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
INNER
EOF
chmod +x $FOLDER/sbin/ifconfig
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
rm -f $POWERLEVEL9K_PUBLIC_IP_FILE
unfunction stat
}
source shunit2/shunit2

@ -0,0 +1,181 @@
#!/usr/bin/env zsh
#vim:ft=zsh ts=2 sw=2 sts=2 et fenc=utf-8
# Required for shunit2 to run correctly
setopt shwordsplit
SHUNIT_PARENT=$0
function setUp() {
export TERM="xterm-256color"
# Test specific
P9K_HOME=$(pwd)
FOLDER=/tmp/powerlevel9k-test
mkdir -p $FOLDER
mkdir $FOLDER/bin
mkdir $FOLDER/sbin
}
function tearDown() {
# Go back to powerlevel9k folder
cd "${P9K_HOME}"
# Remove eventually created test-specific folder
rm -fr "${FOLDER}"
unset FOLDER
unset P9K_HOME
}
function fakeIfconfig() {
# Fake ifconfig
cat > $FOLDER/sbin/ifconfig <<EOF
#!/bin/sh
if [[ "\$#" -gt 0 ]]; then
cat <<INNER
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xe8200000-e8220000
INNER
exit 0
fi
cat <<INNER
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:8f:5c:ed:51 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 1.2.3.4 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xe8200000-e8220000
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 5136 bytes 328651 (320.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5136 bytes 328651 (320.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
INNER
EOF
chmod +x $FOLDER/sbin/ifconfig
}
function fakeIp() {
local INTERFACE1="${1}"
[[ -z "${INTERFACE1}" ]] && INTERFACE1="tun0"
local INTERFACE2="${2}"
[[ -z "${INTERFACE2}" ]] && INTERFACE2="disabled-if2"
cat > $FOLDER/sbin/ip <<EOF
#!/bin/sh
if [[ "\$*" == 'link ls up' ]]; then
cat <<INNER
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
4: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:7e:84:45 brd ff:ff:ff:ff:ff:ff
INNER
fi
if [[ "\$*" =~ 'show ${INTERFACE1}' ]]; then
cat <<INNER
3: ${INTERFACE1}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
valid_lft forever preferred_lft forever
INNER
fi
if [[ "\$*" =~ 'show ${INTERFACE2}' ]]; then
cat <<INNER
3: ${INTERFACE2}: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 1.2.3.4 brd 10.0.2.255 scope global eth0
valid_lft forever preferred_lft forever
INNER
fi
EOF
chmod +x $FOLDER/sbin/ip
}
function testVpnIpSegmentPrintsNothingOnOsxIfNotConnected() {
alias ifconfig='echo "not connected"'
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS="OSX" # Fake OSX
assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")"
unalias ifconfig
}
function testVpnIpSegmentPrintsNothingOnLinuxIfNotConnected() {
alias ip='echo "not connected"'
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS="Linux" # Fake Linux
assertEquals "" "$(prompt_vpn_ip left 1 false "$FOLDER")"
unalias ip
}
function testVpnIpSegmentWorksOnOsxWithInterfaceSpecified() {
local POWERLEVEL9K_VPN_IP_INTERFACE='tun1'
fakeIfconfig
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='OSX' # Fake OSX
assertEquals "%K{006} %F{000}(vpn) %f%F{000}1.2.3.4 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
}
function testVpnIpSegmentWorksOnLinuxWithInterfaceSpecified() {
local POWERLEVEL9K_VPN_IP_INTERFACE='tun1'
fakeIp "tun1"
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='Linux' # Fake Linux
assertEquals "%K{006} %F{000}(vpn) %f%F{000}10.0.2.15 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
}
# vpn_ip is not capable of handling multiple vpn interfaces ATM.
# function testVpnIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() {
# local POWERLEVEL9K_VPN_IP_INTERFACE=(tun0 tun1)
# fakeIp "tun0" "tun1"
# # Load Powerlevel9k
# source powerlevel9k.zsh-theme
# local OS='Linux' # Fake Linux
# setopt xtrace
# assertEquals "%K{006} %F{000}(vpn) %f%F{000}10.0.2.15 " "$(prompt_vpn_ip left 1 false "$FOLDER")"
# unsetopt xtrace
# }
source shunit2/shunit2
Loading…
Cancel
Save