summaryrefslogtreecommitdiff
path: root/test/segments/public_ip.spec
diff options
context:
space:
mode:
authorBen Hilburn <bhilburn@gmail.com>2018-08-31 22:43:20 +0300
committerGitHub <noreply@github.com>2018-08-31 22:43:20 +0300
commite207dec8de036112fcac8b8face41e87e1d0b7ec (patch)
treecf455c0cb6d45464008d1b14877cef910a368ea6 /test/segments/public_ip.spec
parent571a859413866897cf962396f02f65a288f677ac (diff)
parent69c69b9cd91d77178324a86d8924efccadaa7247 (diff)
Merge pull request #944 from dritter/prepare_066
Preparing for v0.6.6 release!
Diffstat (limited to 'test/segments/public_ip.spec')
-rwxr-xr-xtest/segments/public_ip.spec222
1 files changed, 222 insertions, 0 deletions
diff --git a/test/segments/public_ip.spec b/test/segments/public_ip.spec
new file mode 100755
index 00000000..80c46ac5
--- /dev/null
+++ b/test/segments/public_ip.spec
@@ -0,0 +1,222 @@
+#!/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
+ 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
+}
+
+function testPublicIpSegmentPrintsNothingByDefaultIfHostIsNotAvailable() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip custom_world)
+ 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
+
+ assertEquals "%K{015} %F{000}world %k%F{015}%f " "$(build_left_prompt)"
+
+ unalias dig
+}
+
+function testPublicIpSegmentPrintsNoticeIfNotConnected() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ 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
+
+ assertEquals "%K{000} %F{015}disconnected %k%F{000}%f " "$(build_left_prompt)"
+
+ unalias dig
+}
+
+function testPublicIpSegmentWorksWithWget() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ alias dig='nodig'
+ alias curl='nocurl'
+ wget() {
+ echo "wget 1.2.3.4"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}wget 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction wget
+ unalias dig
+ unalias curl
+}
+
+function testPublicIpSegmentUsesCurlAsFallbackMethodIfWgetIsNotAvailable() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ alias dig='nodig'
+ alias wget='nowget'
+ curl() {
+ echo "curl 1.2.3.4"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}curl 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction curl
+ unalias dig
+ unalias wget
+}
+
+function testPublicIpSegmentUsesDigAsFallbackMethodIfWgetAndCurlAreNotAvailable() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ alias curl='nocurl'
+ alias wget='nowget'
+ dig() {
+ echo "dig 1.2.3.4"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}dig 1.2.3.4 %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction dig
+ unalias curl
+ unalias wget
+}
+
+function testPublicIpSegmentCachesFile() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ dig() {
+ echo "first"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}first %k%F{000}%f " "$(build_left_prompt)"
+
+ dig() {
+ echo "second"
+ }
+
+ # Segment should not have changed!
+ assertEquals "%K{000} %F{015}first %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction dig
+}
+
+function testPublicIpSegmentRefreshesCachesFileAfterTimeout() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ local POWERLEVEL9K_PUBLIC_IP_TIMEOUT=2
+ dig() {
+ echo "first"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}first %k%F{000}%f " "$(build_left_prompt)"
+
+ sleep 3
+ dig() {
+ echo "second"
+ }
+
+ # Segment should not have changed!
+ assertEquals "%K{000} %F{015}second %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction dig
+}
+
+function testPublicIpSegmentRefreshesCachesFileIfEmpty() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ dig() {
+ echo "first"
+ }
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}first %k%F{000}%f " "$(build_left_prompt)"
+
+ # Truncate cache file
+ echo "" >! $POWERLEVEL9K_PUBLIC_IP_FILE
+
+ dig() {
+ echo "second"
+ }
+
+ # Segment should not have changed!
+ assertEquals "%K{000} %F{015}second %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction dig
+}
+
+function testPublicIpSegmentWhenGoingOnline() {
+ local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
+ POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
+ local POWERLEVEL9K_PUBLIC_IP_METHODS="dig"
+ local POWERLEVEL9K_PUBLIC_IP_NONE="disconnected"
+ alias dig="nodig"
+
+ # Load Powerlevel9k
+ source ${P9K_HOME}/powerlevel9k.zsh-theme
+
+ assertEquals "%K{000} %F{015}disconnected %k%F{000}%f " "$(build_left_prompt)"
+
+ unalias dig
+
+ dig() {
+ echo "second"
+ }
+
+ # Segment should not have changed!
+ assertEquals "%K{000} %F{015}second %k%F{000}%f " "$(build_left_prompt)"
+
+ unfunction dig
+}
+
+source shunit2/shunit2 \ No newline at end of file