File: test_sysfs.sh

package info (click to toggle)
liquidprompt 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,572 kB
  • sloc: sh: 3,621; python: 33; makefile: 15
file content (130 lines) | stat: -rwxr-xr-x 3,868 bytes parent folder | download
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
# Error on unset variables
set -u

if [ -n "${ZSH_VERSION-}" ]; then
  SHUNIT_PARENT="$0"
  setopt shwordsplit ksh_arrays
fi

# Load Linux version of _lp_battery()
uname() { printf 'Linux'; }

. ../liquidprompt --no-activate
unset -f uname

LP_ENABLE_BATT=1
_LP_BATTERY_FUNCTION=__lp_battery_sysfs

LP_ENABLE_TEMP=1
_LP_TEMP_FUNCTION=__lp_temp_sysfs

typeset -a battery_types battery_presents battery_status battery_capacities battery_out_statuses battery_values

# Add test cases to these arrays like below

# Empty ("") means file doesn't exist

# Linux 5.11.0-38-generic #42~20.04.1-Ubuntu SMP Tue Sep 28 20:41:07 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
battery_types+=("Battery")
battery_presents+=("1")
battery_statuses+=("Discharging")
battery_capacities+=("67")
battery_out_statuses+=(0)
battery_values+=("67")
battery_scopes+=("")

# Full, not charging
battery_types+=("Battery")
battery_presents+=("")
battery_statuses+=("")
battery_capacities+=("")
battery_out_statuses+=(4)
battery_values+=("")
battery_scopes+=("System")

# Not a battery
battery_types+=("Mains")
battery_presents+=("")
battery_statuses+=("")
battery_capacities+=("")
battery_out_statuses+=(4)
battery_values+=("")
battery_scopes+=("")

# Wrong type of battery (Wireless mouse, some other)
battery_types+=("Battery")
battery_presents+=("1")
battery_statuses+=("")
battery_capacities+=("0")
battery_out_statuses+=(4)
battery_values+=("")
battery_scopes+=("Device")


test_sysfs_battery() {
  _LP_LINUX_POWERSUPPLY_PATH="$SHUNIT_TMPDIR"

  for (( index=0; index < ${#battery_values[@]}; index++ )); do
    typeset power_supply="${_LP_LINUX_POWERSUPPLY_PATH}/${index}"
    mkdir "$power_supply"

    if [[ -n ${battery_types[index]-} ]]; then
      printf '%s\n' "${battery_types[index]}" > "${power_supply}/type"
    fi
    if [[ -n ${battery_presents[index]-} ]]; then
      printf '%s\n' "${battery_presents[index]}" > "${power_supply}/present"
    fi
    if [[ -n ${battery_statuses[index]-} ]]; then
      printf '%s\n' "${battery_statuses[index]}" > "${power_supply}/status"
    fi
    if [[ -n ${battery_capacities[index]-} ]]; then
      printf '%s\n' "${battery_capacities[index]}" > "${power_supply}/capacity"
    fi
    if [[ -n ${battery_scopes[index]} ]]; then
      printf '%s\n' "${battery_scopes[index]}" > "${power_supply}/scope"
    fi

    LP_BATTERY_THRESHOLD=100
    _lp_battery
    assertEquals "sysfs battery below returns at index ${index}" "${battery_out_statuses[$index]}" "$?"
    assertEquals "sysfs battery value at index ${index}" "${battery_values[$index]}" "${lp_battery-}"

    _status=${battery_out_statuses[$index]}
    (( _status < 4 )) && _status=$(( _status + 1 ))

    LP_BATTERY_THRESHOLD=0
    _lp_battery
    assertEquals "sysfs battery above returns at index ${index}" "$_status" "$?"
    assertEquals "sysfs battery value at index ${index}" "${battery_values[$index]}" "${lp_battery-}"

    # Must delete the "device", or liquidpropmt will find the first one again.
    rm -r "$power_supply"
  done
}

test_sysfs_temperature() {
  _LP_LINUX_TEMPERATURE_FILES=(
    "${SHUNIT_TMPDIR}/hwmon0_temp1_input"
    "${SHUNIT_TMPDIR}/hwmon1_temp1_input"
    "${SHUNIT_TMPDIR}/hwmon2_temp1_input"
    "${SHUNIT_TMPDIR}/thermal_zone0_temp"
  )
  typeset -i i=0
  printf '%s\n' 27000 > "${_LP_LINUX_TEMPERATURE_FILES[i++]}"
  printf '%s\n' 12000 > "${_LP_LINUX_TEMPERATURE_FILES[i++]}"
  printf '%s\n' 17000 > "${_LP_LINUX_TEMPERATURE_FILES[i++]}"
  printf '%s\n' 27000 > "${_LP_LINUX_TEMPERATURE_FILES[i++]}"

  LP_TEMP_THRESHOLD=100
  _lp_temperature
  assertEquals "sysfs temperature below returns" 1 "$?"
  assertEquals "sysfs temperature value" 27 "${lp_temperature-}"


  LP_TEMP_THRESHOLD=0
  _lp_temperature
  assertEquals "sysfs temperature above returns at index" 0 "$?"
  assertEquals "sysfs temperature value" 27 "${lp_temperature-}"
}

. ./shunit2