forked from liquidprompt/liquidprompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·331 lines (280 loc) · 6.73 KB
/
test.sh
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/sh
print_ok()
{
local OK="\\033[1;32m"
local RAZ="\\033[0;39m"
local cols=$1
local name=$2
# printf "\e${OK}%-${cols}s %-${cols}s\n${RAZ}" "$name" "OK"
printf "${OK}%-${cols}s %-${cols}s\n${RAZ}" "$name" "OK"
}
print_no()
{
local NOK="\\033[1;31m"
local RAZ="\\033[0;39m"
local cols=$1
local name=$2
local sub=$3
local line=$4
# printf "\e${NOK}%-${cols}s %-${cols}s #%-5s\n${RAZ}" "$name" "$sub" "$line"
printf "${NOK}%-${cols}s %-${cols}s #%-5s\n${RAZ}" "$name" "$sub" "$line"
}
assert()
{
local has=$1
local name=$2
local sub=$3
local line=$4
if [[ -z "$sub" ]]
then
return 1
fi
cols=20
if [[ $has == 1 ]] ; then
if [[ "$PS1" == *$sub* ]]
then
print_ok $cols "has $name"
else
print_no $cols "has $name" $sub $line
fi
elif [[ $has == 0 ]] ; then
if [[ "$PS1" != *$sub* ]]
then
print_ok $cols " no $name"
else
print_no $cols " no $name" $sub $line
fi
else
if [[ "$PS1" == $sub ]]
then
print_ok $cols " is $name"
else
print_no $cols " is $name" $sub $line
fi
fi
}
assert_has()
{
assert 1 "$@"
}
assert_not()
{
assert 0 "$@"
}
assert_is()
{
assert 2 "$@"
}
log_prompt()
{
echo -e "$PS1" 1>&2
}
#####################
# REDEFINE COMMANDS #
#####################
command()
{
echo "fake command $@" 1>&2
echo "/bin/fake"
}
uname()
{
echo "fake uname $@" 1>&2
echo Linux
}
nproc()
{
echo "fake nproc $@" 1>&2
echo 2
}
# battery
acpi()
{
echo "fake acpi $@" 1>&2
echo 'Battery 0: Discharging, 55%, 01:39:34 remaining'
}
git()
{
echo "fake git $@" 1>&2
case $1 in
"rev-parse" )
echo ".git";;
"branch" )
echo "* fake_test";;
"diff" )
echo "2 1 fake_file"
return 1;;
"status" )
echo "# Untracked";;
"rev-list" )
echo 111;;
esac
}
# global variables
export http_proxy="fake"
##########################
# Call the liquid prompt #
##########################
# As if we were in an interactive shell
export PS1="fake prompt \$"
# load functions
source ./liquidprompt
# Force liquid prompt function redefinition
_lp_cpu_load()
{
echo "fake _lp_cpu_load $@" 1>&2
echo "0.64"
}
################
# ADHOC CONFIG #
################
export LP_BATTERY_THRESHOLD=60
export LP_LOAD_THRESHOLD=1
export LP_MARK_PROXY="proxy"
export LP_MARK_BATTERY="BATT"
export LP_MARK_LOAD="LOAD"
export LP_MARK_UNTRACKED="untracked"
export LP_MARK_GIT="gitmark"
export LP_USER_ALWAYS=1
# Force erroneous command
fake_error
# Force set prompt
_lp_set_prompt
#########
# TESTS #
#########
echo "FULL PROMPT"
log_prompt
assert_has Battery_Mark BATT $LINENO
assert_has Battery_Level 55% $LINENO
assert_has Load_Mark LOAD $LINENO
assert_has Load_Level 32% $LINENO
assert_has User "[\\\u" $LINENO
if [[ $LP_HOSTNAME_ALWAYS == 0 ]] ; then
assert_not Hostname "\\\h" $LINENO
else
assert_has Hostname "\\\h" $LINENO
fi
assert_has Perms : $LINENO
assert_has Path $(pwd | sed -e "s|$HOME|~|") $LINENO
assert_has Proxy proxy $LINENO
assert_has Error 127 $LINENO
assert_has GIT_Branch fake_test $LINENO
assert_has GIT_Changes "+2/-1" $LINENO
assert_has GIT_Commits 111 $LINENO
assert_has GIT_Untrack untracked $LINENO
assert_has GIT_Mark gitmark $LINENO
# start hiding features
echo "DISABLE BATTERY"
export LP_ENABLE_BATT=0
_lp_set_prompt
log_prompt
assert_not Battery_Mark BATT $LINENO
assert_not Battery_level 55% $LINENO
assert_not Error 127 $LINENO
echo "HIDE BATTERY LEVEL"
export LP_ENABLE_BATT=1
export LP_BATTERY_THRESHOLD=50
_lp_set_prompt
log_prompt
assert_has Battery_Mark BATT $LINENO
assert_not Battery_level 55% $LINENO
assert_not Error 127 $LINENO
alias acpi="echo 'Battery 0: Full, 100%'"
_lp_set_prompt
log_prompt
assert_not Battery_Mark BATT $LINENO
echo "DISABLE LOAD"
export LP_ENABLE_LOAD=0
_lp_set_prompt
log_prompt
assert_not Load_Mark LOAD $LINENO
assert_not Load_Level 32% $LINENO
echo "HIDE LOAD"
export LP_ENABLE_LOAD=1
export LP_LOAD_THRESHOLD=40
_lp_set_prompt
log_prompt
assert_not Load_Mark LOAD $LINENO
assert_not Load_Level 32% $LINENO
echo "DISABLE PROXY"
export LP_ENABLE_PROXY=0
_lp_set_prompt
log_prompt
assert_not Proxy_Mark proxy $LINENO
echo "NO PROXY"
export LP_ENABLE_PROXY=1
export http_proxy=""
_lp_set_prompt
log_prompt
assert_not Proxy_Mark proxy $LINENO
echo "DISABLE GIT"
export LP_ENABLE_GIT=0
_lp_set_prompt
log_prompt
assert_not GIT_Branch fake_test $LINENO
assert_not GIT_Changes "+2/-1" $LINENO
assert_not GIT_Commits 111 $LINENO
assert_not GIT_Untrack untracked $LINENO
assert_not GIT_Mark gitmark $LINENO
assert_has User_Mark $ $LINENO
echo "NO GIT"
export LP_ENABLE_GIT=1
alias git="echo"
_lp_set_prompt
log_prompt
assert_not GIT_Branch fake_test $LINENO
assert_not GIT_Changes "+2/-1" $LINENO
assert_not GIT_Commits 111 $LINENO
assert_not GIT_Untrack untracked $LINENO
assert_not GIT_Mark gitmark $LINENO
assert_has User_Mark $ $LINENO
# create a deep dir tree
current=$PWD
for d in $(seq 20) ; do
dirname=""
for i in $(seq 5); do
dirname="$dirname$d"
done
mkdir -p $dirname
cd $dirname
done
echo "DISABLE SHORTEN PATH"
export LP_ENABLE_SHORTEN_PATH=0
_lp_set_prompt
log_prompt
assert_has Path "$(pwd | sed -e "s|$HOME|~|")" $LINENO
echo "ENABLE SHORTEN PATH"
export LP_ENABLE_SHORTEN_PATH=1
export LP_PATH_LENGTH=35
export LP_PATH_KEEP=1
_lp_set_prompt
log_prompt
assert_has Short_Path " … " $LINENO
# get back out of the deep tree
cd $current
echo "LOCAL HOST NAME"
_lp_set_prompt
log_prompt
# As the hostname is set once at the script start,
# and not re-interpret at each prompt,
# we cannot export the option in the test script.
# We thus rely on the existing config.
if [[ $LP_HOSTNAME_ALWAYS == 0 ]] ; then
assert_not Hostname "\\\h" $LINENO
else
assert_has Hostname "\\\h" $LINENO
fi
echo "prompt_OFF"
prompt_OFF
log_prompt
assert_is Prompt "$ " $LINENO
echo "prompt_on"
prompt_on
export LP_USER_ALWAYS=1
log_prompt
assert_has User "\\\u" $LINENO
assert_has Perms : $LINENO
assert_has Path $(pwd | sed -e "s|$HOME|~|") $LINENO
# assert_has Path "\\\w" $LINENO
assert_has Prompt "$ " $LINENO