-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-suite
executable file
·183 lines (148 loc) · 5.27 KB
/
test-suite
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
#!/usr/bin/env sh
# shellcheck disable=SC2046
EXE="$(pwd)/$RUNCMD"
TESTDIR=/tmp/enlighten_testing
export BACKLIGHT_SEARCH_PATH="$TESTDIR:/sys/class/backlight:/sys/class/leds"
export BACKLIGHT_DEVICE=dev4
# setup the test env
for i in $(seq 1 6); do
mkdir -p "$TESTDIR"/dev"$i"
done
echo 0 > "$TESTDIR"/dev2/brightness
echo 1 > "$TESTDIR"/dev3/max_brightness
echo 250 > "$TESTDIR"/dev4/brightness
echo 1347 > "$TESTDIR"/dev4/max_brightness
mkdir -p "$TESTDIR"/dev5/max_brightness
mkdir -p "$TESTDIR"/dev5/brightness
echo 0 > "$TESTDIR"/dev6/brightness
echo 10 > "$TESTDIR"/dev6/max_brightness
touch "$TESTDIR"/notdev
echo 0 > "$TESTDIR"/TESTCOUNT
echo 0 > "$TESTDIR"/TESTSFAILED
# helper functions
FAIL() {
count=0
count=$(cat "$TESTDIR"/TESTCOUNT)
count=$((count + 1))
echo "$count" > "$TESTDIR"/TESTCOUNT
failed=0
failed=$(cat "$TESTDIR"/TESTSFAILED)
failed=$((failed + 1))
echo "$failed" > "$TESTDIR"/TESTSFAILED
echo 'FAIL'
}
PASS() {
count=0
count=$(cat "$TESTDIR"/TESTCOUNT)
count=$((count + 1))
echo "$count" > "$TESTDIR"/TESTCOUNT
echo 'PASS'
}
contains() {
case "$1" in
*"$2"*) echo 0;;
*) echo 1;;
esac
}
try() {
if test "$@"; then
PASS
else
FAIL
fi
}
# non-brightness commands (device detection, help and version output)
LIST=$(eval "$EXE" l)
printf 'ignore non-directories: [ %s ]\n' $(try $(contains "$LIST" 'notdev') -eq 1)
printf 'ignore empty directories: [ %s ]\n' $(try $(contains "$LIST" 'dev1') -eq 1)
printf 'ignore directories without max_brightness: [ %s ]\n' $(try $(contains "$LIST" 'dev2') -eq 1)
printf 'ignore directories without brightness: [ %s ]\n' $(try $(contains "$LIST" 'dev3') -eq 1)
printf 'ignore directories whose brightness are not regular files: [ %s ]\n' $(try $(contains "$LIST" 'dev5') -eq 1)
printf 'detect directories with both brightness files: [ %s ]\n' $(try $(contains "$LIST" 'dev4') -eq 0)
GET=$(eval "$EXE" h 2>&1)
printf 'properly gets help output: [ %s ]\n' $(try $(contains "$GET" 'Usage') -eq 0)
GET=$(eval "$EXE" v 2>&1)
printf 'properly gets version output: [ %s ]\n' $(try $(contains "$GET" 'v') -eq 0)
GET=$(eval "$EXE" e 2>&1)
printf 'properly gets environment configuration: [ %s ]\n' $(try $(contains "$GET" 'BACKLIGHT_') -eq 0)
# brightness fetching
GET=$(eval "$EXE")
printf 'properly fetches brightness: [ %s ]\n' $(try "$GET" = '250 / 1347 (18%)')
# brightness manipulation
eval "$EXE" '=7%'
GET=$(eval "$EXE")
printf 'properly sets brightness (with =): [ %s ]\n' $(try "$GET" = '94 / 1347 (6%)')
eval "$EXE" 0
GET=$(eval "$EXE")
printf 'properly sets brightness (without =): [ %s ]\n' $(try "$GET" = '0 / 1347 (0%)')
eval "$EXE" '+500'
GET=$(eval "$EXE")
printf 'properly increments brightness: [ %s ]\n' $(try "$GET" = '500 / 1347 (37%)')
eval "$EXE" '-100'
GET=$(eval "$EXE")
printf 'properly decrements brightness: [ %s ]\n' $(try "$GET" = '400 / 1347 (29%)')
eval "$EXE" '50%'
GET=$(eval "$EXE")
printf 'properly sets brightness by percent: [ %s ]\n' $(try "$GET" = '673 / 1347 (49%)')
eval "$EXE" '+7%'
GET=$(eval "$EXE")
printf 'properly increments brightness by percent: [ %s ]\n' $(try "$GET" = '767 / 1347 (56%)')
eval "$EXE" '-12%'
GET=$(eval "$EXE")
printf 'properly decrements brightness by percent: [ %s ]\n' $(try "$GET" = '606 / 1347 (44%)')
# very-small step counts
export BACKLIGHT_DEVICE=dev6
eval "$EXE" '+1%'
GET=$(eval "$EXE")
printf 'ensures percentage-changes have an effect if allowable: [ %s ]\n' $(try "$GET" = '1 / 10 (10%)')
export BACKLIGHT_DEVICE=dev4
# thresholds
export BACKLIGHT_THRESHOLD_MIN='1'
export BACKLIGHT_THRESHOLD_MAX='25%'
eval "$EXE" '75%'
GET=$(eval "$EXE")
printf 'respects threshold maximum: [ %s ]\n' $(try "$GET" = '336 / 1347 (24%)')
eval "$EXE" '0'
GET=$(eval "$EXE")
printf 'respects threshold minimum: [ %s ]\n' $(try "$GET" = '1 / 1347 (0%)')
unset BACKLIGHT_THRESHOLD_MIN
unset BACKLIGHT_THRESHOLD_MAX
# transitions
eval "$EXE" 0 # reset
export BACKLIGHT_TRANSITION_STEPS=20
export BACKLIGHT_TRANSITION_PAUSE=50000000
START=$(date +'%s')
eval "$EXE" '+10%'
STOP=$(date +'%s')
printf 'respects transition steps and pause: [ %s ]\n' $(try $((STOP - START)) -eq 1)
unset START
unset STOP
unset BACKLIGHT_TRANSITION_STEPS
unset BACKLIGHT_TRANSITION_PAUSE
# garbage input
eval "$EXE" 0 # reset
GET=$(eval "$EXE")
eval "$EXE" '+pants' > /dev/null 2>&1
GET=$(eval "$EXE")
printf 'ignores basic garbage input: [ %s ]\n' $(try "$GET" = '0 / 1347 (0%)')
# garbage environment
export BACKLIGHT_SEARCH_PATH="$TESTDIR:::/sys/class/backlight::/sys/class/leds:"
GET=$(eval "$EXE")
printf 'ignores empty paths in SEARCH_PATH: [ %s ]\n' $(try "$GET" = '0 / 1347 (0%)')
eval "$EXE" l > /dev/null 2>&1
GET=$?
printf 'ignores errant, repeated delimiters in SEARCH_PATH: [ %s ]\n' $(try "$GET" = 0)
export BACKLIGHT_DEVICE=nonexistant
GET=$(eval "$EXE" 2>&1)
printf 'errors on basic garbage device name: [ %s ]\n' $(try $(contains "$GET" "Failed to find a match for the specified device") -eq 0)
unset BACKLIGHT_DEVICE
export BACKLIGHT_SEARCH_PATH=":::"
eval "$EXE" l > /dev/null 2>&1
GET=$?
printf 'ignores (effectively) empty SEARCH_PATH: [ %s ]\n' $(try "$GET" -eq 0)
# summary
count=$(cat "$TESTDIR"/TESTCOUNT)
failed=$(cat "$TESTDIR"/TESTSFAILED)
printf '\n%d of %d tests failed\n' "$failed" "$count"
rm -r "$TESTDIR"
exit "$failed"