1
0
Fork 0
mirror of https://github.com/romkatv/powerlevel10k.git synced 2024-11-15 01:20:07 +00:00
powerlevel10k/test/segments/public_ip.spec

387 lines
10 KiB
RPMSpec
Raw Normal View History

2018-07-18 20:23:32 +00:00
#!/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
2019-01-21 18:46:53 +00:00
mkdir $FOLDER/bin
mkdir $FOLDER/sbin
2018-07-18 20:23:32 +00:00
cd $FOLDER
# Change cache file, so that the users environment don't
# interfere with the tests.
POWERLEVEL9K_PUBLIC_IP_FILE=$FOLDER/public_ip_file
}
function tearDown() {
# Go back to powerlevel9k folder
cd "${P9K_HOME}"
# Remove eventually created test-specific folder
rm -fr "${FOLDER}"
# At least remove test folder completely
rm -fr /tmp/powerlevel9k-test
unset FOLDER
unset P9K_HOME
# Unset cache file
unset POWERLEVEL9K_PUBLIC_IP_FILE
}
2019-01-23 00:01:49 +00:00
function fakeIfconfig() {
local INTERFACE="${1}"
[[ -z "${INTERFACE}" ]] && INTERFACE="tun0"
2019-01-23 00:01:49 +00:00
# Fake ifconfig
cat > $FOLDER/sbin/ifconfig <<EOF
#!/usr/bin/env zsh
2019-01-23 17:56:07 +00:00
if [[ "\$*" == "-l" ]]; then
echo "lo0 gif0 stf0 EHC250 EHC253 tun1 tun0 ${INTERFACE} fw0 en0 en2 en1 p2p0 bridge0 utun0"
exit 0
fi
if [[ "\$*" == "${INTERFACE}" ]]; then
2019-01-23 00:01:49 +00:00
cat <<INNER
${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
2019-01-23 00:01:49 +00:00
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
2019-01-23 17:56:07 +00:00
if [[ "\$#" == "0" ]]; then
2019-01-23 00:01:49 +00:00
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
${INTERFACE}: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
2019-01-23 00:01:49 +00:00
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
2019-01-23 17:56:07 +00:00
fi
2019-01-23 00:01:49 +00:00
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
#!/usr/bin/env zsh
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
2019-01-23 18:10:46 +00:00
if [[ "\$*" =~ '-brief.*show' ]]; then
cat <<INNER
lo UNKNOWN 127.0.0.1/8
${INTERFACE1} UP 1.2.3.4/24
${INTERFACE2} UP 5.4.3.2/16
docker0 DOWN 172.17.0.1/16
INNER
fi
2019-01-23 00:01:49 +00:00
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
}
2018-07-18 20:23:32 +00:00
function testPublicIpSegmentPrintsNothingByDefaultIfHostIsNotAvailable() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip custom_world)
2018-07-18 20:23:32 +00:00
local POWERLEVEL9K_PUBLIC_IP_HOST='http://unknown.xyz'
local POWERLEVEL9K_CUSTOM_WORLD='echo world'
# We need to overwrite dig, as this is a fallback method that
# uses an alternative host.
alias dig='nodig'
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{007} %F{000}world %k%F{007}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unalias dig
}
function testPublicIpSegmentPrintsNoticeIfNotConnected() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
local POWERLEVEL9K_PUBLIC_IP_HOST='http://unknown.xyz'
local POWERLEVEL9K_PUBLIC_IP_NONE="disconnected"
# We need to overwrite dig, as this is a fallback method that
# uses an alternative host.
alias dig='nodig'
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}disconnected %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unalias dig
}
function testPublicIpSegmentWorksWithWget() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
alias dig='nodig'
alias curl='nocurl'
wget() {
echo "wget 1.2.3.4"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}wget 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction wget
unalias dig
unalias curl
}
function testPublicIpSegmentUsesCurlAsFallbackMethodIfWgetIsNotAvailable() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
alias dig='nodig'
alias wget='nowget'
curl() {
echo "curl 1.2.3.4"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}curl 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction curl
unalias dig
unalias wget
}
function testPublicIpSegmentUsesDigAsFallbackMethodIfWgetAndCurlAreNotAvailable() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
alias curl='nocurl'
alias wget='nowget'
dig() {
echo "dig 1.2.3.4"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}dig 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction dig
unalias curl
unalias wget
}
function testPublicIpSegmentCachesFile() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
dig() {
echo "first"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}first %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
dig() {
echo "second"
}
# Segment should not have changed!
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}first %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction dig
}
function testPublicIpSegmentRefreshesCachesFileAfterTimeout() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
local POWERLEVEL9K_PUBLIC_IP_TIMEOUT=2
dig() {
echo "first"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}first %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
sleep 3
dig() {
echo "second"
}
# Segment should not have changed!
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}second %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction dig
}
function testPublicIpSegmentRefreshesCachesFileIfEmpty() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
dig() {
echo "first"
}
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}first %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
# Truncate cache file
echo "" >! $POWERLEVEL9K_PUBLIC_IP_FILE
dig() {
echo "second"
}
# Segment should not have changed!
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}second %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction dig
}
function testPublicIpSegmentWhenGoingOnline() {
2018-07-19 20:39:19 +00:00
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
2018-07-18 20:23:32 +00:00
local POWERLEVEL9K_PUBLIC_IP_METHODS="dig"
local POWERLEVEL9K_PUBLIC_IP_NONE="disconnected"
alias dig="nodig"
# Load Powerlevel9k
source ${P9K_HOME}/powerlevel9k.zsh-theme
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}disconnected %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unalias dig
dig() {
echo "second"
}
# Segment should not have changed!
2018-08-17 16:53:26 +00:00
assertEquals "%K{000} %F{007}second %k%F{000}%f " "$(build_left_prompt)"
2018-07-18 20:23:32 +00:00
unfunction dig
}
2019-01-21 18:46:53 +00:00
function testPublicIpSegmentWithVPNTurnedOnLinux() {
local OS='linux'
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun1"
2019-01-23 00:01:49 +00:00
# Fake ip command
fakeIp "tun1"
2019-01-21 18:46:53 +00:00
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
}
function testPublicIpSegmentWithVPNTurnedOnOsx() {
typeset -F now
now=$(date +%s)
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
fakeIfconfig "tun1"
2019-01-21 18:46:53 +00:00
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
unfunction stat
}
2019-01-23 17:56:07 +00:00
function testPublicIpSegmentWithVPNTurnedOnAndFuzzyMatchingOnOsx() {
typeset -F now
now=$(date +%s)
local OS='OSX'
echo "1.2.3.4" > $POWERLEVEL9K_PUBLIC_IP_FILE
local POWERLEVEL9K_PUBLIC_IP_VPN_INTERFACE="tun"
# Fake stat call
function stat() {
echo $now
}
# Fake ifconfig
fakeIfconfig "tun3"
assertEquals "%K{000} %F{007}(vpn) %f%F{007}1.2.3.4 " "$(prompt_public_ip left 1 false "$FOLDER")"
unfunction stat
}
2018-08-04 15:34:52 +00:00
source shunit2/shunit2