From b7b8a0afe0f33ea9b5fdd04b19f68ea59e942c97 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 07:39:08 +0200 Subject: [PATCH] Completed tests for upower command --- plugins/available/battery.plugin.bash | 4 ++-- test/plugins/battery.plugin.bats | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 75a2f0d4..4049a289 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -55,8 +55,8 @@ battery_percentage(){ if _command_exists upower; then - local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | tail --bytes 5) - echo ${UPOWER_OUTPUT: : -1} + local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | grep -o "[0-9]\+" | head -1) + echo ${UPOWER_OUTPUT:--1} elif _command_exists acpi; then local ACPI_OUTPUT=$(acpi -b) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index d7313742..9a1e56b7 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -129,14 +129,14 @@ function setup_upower { percent="$1" function upower { - echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + 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%" + setup_upower "100.00%" run battery_percentage assert_output "100" @@ -145,7 +145,7 @@ function setup_upower { @test 'plugins battery: battery-percentage with upower, 98%' { setup_command_exists "upower" - setup_upower "98%" + setup_upower "98.4567%" run battery_percentage assert_output "98" @@ -163,12 +163,21 @@ function setup_upower { @test 'plugins battery: battery-percentage with upower, 4%' { setup_command_exists "upower" - setup_upower "4%" + setup_upower "4.2345%" run battery_percentage assert_output "4" } +@test 'plugins battery: battery-percentage with upower, no output' { + setup_command_exists "upower" + + setup_upower "" + + run battery_percentage + assert_output "-1" +} + ####################### # # ioreg