Files
bash-it/test/plugins/battery.plugin.bats
John D Pell c2c76a380a plugin/base: improvements (#1930)
* plugins/base: code style improvements

Quote variables, use $@ and $array[@] instead of $*, typeset some integers, remove unneccesasary binary invocation, use shell features when possible, remove `eval`, &c.

* plugins/base: conditional function definitions

Instead of functions failing when required tools aren't installed, just don't define the function.
Alsö, don't redefine del() if it already exists.

* plugins/base: rewrite `usage()`

Reimplement disk usage function using Bash syntax and simpler layout, without having to invoke an external binary.

* plugins/base: revamp `quiet()`

New implementation that is even quieter.

* plugins/base: `myip()`

* plugins/base: `pickfrom()`

* plugins/base: `passgen()`

Fix `passgen()` to not need `tr`, remove one subshell, and eliminate a useless `echo`.

* plugins/base: `mkcd()`

* plugins/base: `mkiso()`

* plugins/base: remove `banish-cookies()`

Adobe Flash is gone with the wind. Alsö, this would be something someone would do *once* and shouldn't be a function...

* plugins/base: `lsgrep` is SC2010

The `lsgrep()` function is *itself* explicitly forbidden by `shellcheck` rule SC2010.

Alsö, s/`$*`/`$@`

* plugins/base: `mkiso()`

Expressly handle unbound parameters.

* plugins/base: remove `command_exists`

* plugin/base: lint SC2154 && SC2144

Newly undisabled `shellcheck` rules

* plugin/base: import libs for tests

* plugin/base: `shfmt`

Apply `shfmt` using current project settings. My apologies to future `git blame` hunters. ♥
2021-09-28 15:13:27 +03:00

374 lines
7.5 KiB
Bash
Executable File

#!/usr/bin/env bats
load ../test_helper
load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh"
load "${BASH_IT}/lib/log.bash"
load "${BASH_IT}/lib/helpers.bash"
cite _about _param _example _group _author _version
load ../../plugins/available/battery.plugin
# Sets up the `_command_exists` function so that it only responds `true` if called with
# the name of the function that was passed in as an argument to `setup_command_exists`.
# This is used to ensure that the test cases can test the various energy management tools
# without actually having them. When called like
#
# setup_command_exists "pmset"
#
# then calling `_command_exists "pmset"` will return `true`,
# while calling `_command_exists "ioreg"` (or other commands) will return `false`.
#
# It's cool that Bash allows to define functions within functions, works almost like
# a closure in JavaScript.
function setup_command_exists {
success_command="$1"
function _command_exists {
case "$1" in
"${success_command}")
true
;;
*)
false
;;
esac
}
}
#######################
#
# no tool
#
@test 'plugins battery: battery-percentage with no tool' {
setup_command_exists "fooooo"
run battery_percentage
assert_output "no"
}
#######################
#
# pmset
#
# Creates a `pmset` function that simulates output like the real `pmset` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_pmset {
percent="$1"
function pmset {
printf "\-InternalBattery-0 (id=12345) %s; discharging; 16:00 remaining present: true" "${percent}"
}
}
@test 'plugins battery: battery-percentage with pmset, 100%' {
setup_command_exists "pmset"
setup_pmset "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with pmset, 98%' {
setup_command_exists "pmset"
setup_pmset "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 98.5%' {
setup_command_exists "pmset"
setup_pmset "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with pmset, 4%' {
setup_command_exists "pmset"
setup_pmset "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with pmset, no status' {
setup_command_exists "pmset"
setup_pmset ""
run battery_percentage
assert_output "-1"
}
#######################
#
# acpi
#
# Creates a `acpi` function that simulates output like the real `acpi` command.
# The passed in parameters are used for
# 1) the remaining battery percentage.
# 2) the battery status
function setup_acpi {
percent="$1"
status="$2"
function acpi {
printf "Battery 0: %s, %s, 01:02:48 until charged" "${status}" "${percent}"
}
}
@test 'plugins battery: battery-percentage with acpi, 100% Full' {
setup_command_exists "acpi"
setup_acpi "100%" "Full"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with acpi, 98% Charging' {
setup_command_exists "acpi"
setup_acpi "98%" "Charging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Discharging' {
setup_command_exists "acpi"
setup_acpi "98%" "Discharging"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 98% Unknown' {
setup_command_exists "acpi"
setup_acpi "98%" "Unknown"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with acpi, 4% Charging' {
setup_command_exists "acpi"
setup_acpi "4%" "Charging"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, 4% no status' {
setup_command_exists "acpi"
setup_acpi "4%" ""
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with acpi, no status' {
setup_command_exists "acpi"
setup_acpi "" ""
run battery_percentage
assert_output "-1"
}
#######################
#
# upower
#
# Creates a `upower` function that simulates output like the real `upower` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_upower {
percent="$1"
function upower {
printf "voltage: 12.191 V\n time to full: 57.3 minutes\n percentage: %s\n capacity: 84.6964" "${percent}"
}
}
@test 'plugins battery: battery-percentage with upower, 100%' {
setup_command_exists "upower"
setup_upower "100.00%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with upower, 98%' {
setup_command_exists "upower"
setup_upower "98.4567%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 98.5%' {
setup_command_exists "upower"
setup_upower "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with upower, 4%' {
setup_command_exists "upower"
setup_upower "4.2345%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with upower, no output' {
setup_command_exists "upower"
setup_upower ""
run battery_percentage
assert_output "-1"
}
#######################
#
# ioreg
#
# Creates a `ioreg` function that simulates output like the real `ioreg` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_ioreg {
percent="$1"
function ioreg {
printf "\"MaxCapacity\" = 100\n\"CurrentCapacity\" = %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with ioreg, 100%' {
setup_command_exists "ioreg"
setup_ioreg "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with ioreg, 98%' {
setup_command_exists "ioreg"
setup_ioreg "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 98.5%' {
setup_command_exists "ioreg"
setup_ioreg "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with ioreg, 4%' {
setup_command_exists "ioreg"
setup_ioreg "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with ioreg, no status' {
setup_command_exists "ioreg"
# Simulate that no battery is present
function ioreg {
printf ""
}
run battery_percentage
assert_output "-1"
}
#######################
#
# WMIC
#
# Creates a `WMIC` function that simulates output like the real `WMIC` command.
# The passed in parameter is used for the remaining battery percentage.
function setup_WMIC {
percent="$1"
function WMIC {
printf "Charge: %s" "${percent}"
}
}
@test 'plugins battery: battery-percentage with WMIC, 100%' {
setup_command_exists "WMIC"
setup_WMIC "100%"
run battery_percentage
assert_output "100"
}
@test 'plugins battery: battery-percentage with WMIC, 98%' {
setup_command_exists "WMIC"
setup_WMIC "98%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 98.5%' {
setup_command_exists "WMIC"
setup_WMIC "98.5%"
run battery_percentage
assert_output "98"
}
@test 'plugins battery: battery-percentage with WMIC, 4%' {
setup_command_exists "WMIC"
setup_WMIC "4%"
run battery_percentage
assert_output "04"
}
@test 'plugins battery: battery-percentage with WMIC, no status' {
setup_command_exists "WMIC"
setup_WMIC ""
run battery_percentage
assert_output "-1"
}