summaryrefslogtreecommitdiff
path: root/test/segments/public_ip.spec
blob: a9c0a770678d2da21de4c0b2ea85fd19a0ff0224 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/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"
  # Load Powerlevel9k
  source powerlevel9k.zsh-theme

  # 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 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'

  assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)"

  unalias dig
}

function testPublicIpSegmentPrintsNoticeIfNotConnected() {
  local 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'

  assertEquals "%K{black} %F{white}disconnected %k%F{black}%f " "$(build_left_prompt)"

  unalias dig
}

function testPublicIpSegmentWorksWithWget() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  alias dig='nodig'
  alias curl='nocurl'
  wget() {
      echo "wget 1.2.3.4"
  }

  assertEquals "%K{black} %F{white}wget 1.2.3.4 %k%F{black}%f " "$(build_left_prompt)"

  unfunction wget
  unalias dig
  unalias curl
}

function testPublicIpSegmentUsesCurlAsFallbackMethodIfWgetIsNotAvailable() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  alias dig='nodig'
  alias wget='nowget'
  curl() {
      echo "curl 1.2.3.4"
  }

  assertEquals "%K{black} %F{white}curl 1.2.3.4 %k%F{black}%f " "$(build_left_prompt)"

  unfunction curl
  unalias dig
  unalias wget
}

function testPublicIpSegmentUsesDigAsFallbackMethodIfWgetAndCurlAreNotAvailable() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  alias curl='nocurl'
  alias wget='nowget'
  dig() {
      echo "dig 1.2.3.4"
  }

  assertEquals "%K{black} %F{white}dig 1.2.3.4 %k%F{black}%f " "$(build_left_prompt)"

  unfunction dig
  unalias curl
  unalias wget
}

function testPublicIpSegmentCachesFile() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  dig() {
      echo "first"
  }

  assertEquals "%K{black} %F{white}first %k%F{black}%f " "$(build_left_prompt)"

  dig() {
      echo "second"
  }

  # Segment should not have changed!
  assertEquals "%K{black} %F{white}first %k%F{black}%f " "$(build_left_prompt)"

  unfunction dig
}

function testPublicIpSegmentRefreshesCachesFileAfterTimeout() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  local POWERLEVEL9K_PUBLIC_IP_TIMEOUT=2
  dig() {
      echo "first"
  }

  assertEquals "%K{black} %F{white}first %k%F{black}%f " "$(build_left_prompt)"

  sleep 3
  dig() {
      echo "second"
  }

  # Segment should not have changed!
  assertEquals "%K{black} %F{white}second %k%F{black}%f " "$(build_left_prompt)"

  unfunction dig
}

function testPublicIpSegmentRefreshesCachesFileIfEmpty() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  dig() {
      echo "first"
  }

  assertEquals "%K{black} %F{white}first %k%F{black}%f " "$(build_left_prompt)"

  # Truncate cache file
  echo "" >! $POWERLEVEL9K_PUBLIC_IP_FILE

  dig() {
      echo "second"
  }

  # Segment should not have changed!
  assertEquals "%K{black} %F{white}second %k%F{black}%f " "$(build_left_prompt)"

  unfunction dig
}

function testPublicIpSegmentWhenGoingOnline() {
  local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(public_ip)
  local POWERLEVEL9K_PUBLIC_IP_METHODS="dig"
  local POWERLEVEL9K_PUBLIC_IP_NONE="disconnected"
  alias dig="nodig"

  assertEquals "%K{black} %F{white}disconnected %k%F{black}%f " "$(build_left_prompt)"

  unalias dig

  dig() {
      echo "second"
  }

  # Segment should not have changed!
  assertEquals "%K{black} %F{white}second %k%F{black}%f " "$(build_left_prompt)"

  unfunction dig
}

source shunit2/source/2.1/src/shunit2