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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#!/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"
}
function testIpSegmentPrintsNothingOnOsxIfNotConnected() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip custom_world)
alias networksetup='echo "not connected"'
local POWERLEVEL9K_CUSTOM_WORLD='echo world'
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS="OSX" # Fake OSX
assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)"
unalias networksetup
}
function testIpSegmentPrintsNothingOnLinuxIfNotConnected() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip custom_world)
alias ip='echo "not connected"'
local POWERLEVEL9K_CUSTOM_WORLD='echo world'
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS="Linux" # Fake Linux
assertEquals "%K{white} %F{black}world %k%F{white}%f " "$(build_left_prompt)"
unalias ip
}
function testIpSegmentWorksOnOsxWithNoInterfaceSpecified() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
alias networksetup="echo 'An asterisk (*) denotes that a network service is disabled.
(1) Ethernet
(Hardware Port: Ethernet, Device: en0)
(2) FireWire
(Hardware Port: FireWire, Device: fw0)
(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en1)
(4) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en3)
(5) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
(6) Apple USB Ethernet Adapter
(Hardware Port: Apple USB Ethernet Adapter, Device: en4)
'"
alias ipconfig="_(){ echo '1.2.3.4'; };_"
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='OSX' # Fake OSX
assertEquals "%K{cyan} %F{black%}IP %f%F{black}1.2.3.4 %k%F{cyan}%f " "$(build_left_prompt)"
unalias ipconfig
unalias networksetup
}
# There could be more than one confiured network interfaces.
# `networksetup -listnetworkserviceorder` lists the interfaces
# in hierarchical order, but from outside this is not obvious
# (implementation detail). So we need a test for this case.
function testIpSegmentWorksOnOsxWithMultipleInterfacesSpecified() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
alias networksetup="echo 'An asterisk (*) denotes that a network service is disabled.
(1) Ethernet
(Hardware Port: Ethernet, Device: en0)
(2) FireWire
(Hardware Port: FireWire, Device: fw0)
(3) Wi-Fi
(Hardware Port: Wi-Fi, Device: en1)
(4) Bluetooth PAN
(Hardware Port: Bluetooth PAN, Device: en3)
(5) Thunderbolt Bridge
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
(6) Apple USB Ethernet Adapter
(Hardware Port: Apple USB Ethernet Adapter, Device: en4)
'"
# Return a unique IP address for every interface
ipconfig() {
case "${2}" {
en0)
echo 1.2.3.4
;;
fw0)
echo 2.3.4.5
;;
en1)
echo 3.4.5.6
;;
en3)
echo 4.5.6.7
;;
}
}
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='OSX' # Fake OSX
assertEquals "%K{cyan} %F{black%}IP %f%F{black}1.2.3.4 %k%F{cyan}%f " "$(build_left_prompt)"
unfunction ipconfig
unalias networksetup
}
function testIpSegmentWorksOnOsxWithInterfaceSpecified() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
local POWERLEVEL9K_IP_INTERFACE='xxx'
alias ipconfig="_(){ echo '1.2.3.4'; };_"
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='OSX' # Fake OSX
assertEquals "%K{cyan} %F{black%}IP %f%F{black}1.2.3.4 %k%F{cyan}%f " "$(build_left_prompt)"
unalias ipconfig
}
function testIpSegmentWorksOnLinuxWithNoInterfaceSpecified() {
setopt aliases
local POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
# That command is harder to test, as it is used at first
# to get all relevant network interfaces and then for
# getting the configuration of that segment..
ip(){
if [[ "$*" == 'link ls up' ]]; then
echo "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: eth0: <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";
fi
if [[ "$*" == '-4 a show eth0' ]]; then
echo '2: eth0: <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';
fi
}
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='Linux' # Fake Linux
assertEquals "%K{cyan} %F{black%}IP %f%F{black}10.0.2.15 %k%F{cyan}%f " "$(build_left_prompt)"
unfunction ip
}
function testIpSegmentWorksOnLinuxWithMultipleInterfacesSpecified() {
setopt aliases
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
# That command is harder to test, as it is used at first
# to get all relevant network interfaces and then for
# getting the configuration of that segment..
ip(){
if [[ "$*" == 'link ls up' ]]; then
echo "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: eth0: <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: eth1: <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";
fi
if [[ "$*" == '-4 a show eth1' ]]; then
echo '3: eth1: <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';
fi
}
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='Linux' # Fake Linux
assertEquals "%K{cyan} %F{black%}IP %f%F{black}10.0.2.15 %k%F{cyan}%f " "$(build_left_prompt)"
unfunction ip
}
function testIpSegmentWorksOnLinuxWithInterfaceSpecified() {
local -a POWERLEVEL9K_LEFT_PROMPT_ELEMENTS
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(ip)
local POWERLEVEL9K_IP_INTERFACE='xxx'
ip(){
echo '2: eth0: <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';
}
# Load Powerlevel9k
source powerlevel9k.zsh-theme
local OS='Linux' # Fake Linux
assertEquals "%K{cyan} %F{black%}IP %f%F{black}10.0.2.15 %k%F{cyan}%f " "$(build_left_prompt)"
unfunction ip
}
source shunit2/source/2.1/src/shunit2
|