From 601202e8160c60b108c6371e9a3a5201a913590a Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:32:00 +0200 Subject: [PATCH] Simplified ioreg, added tests for no status --- plugins/available/battery.plugin.bash | 2 +- test/plugins/battery.plugin.bats | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 33352a67..55fa981f 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -68,7 +68,7 @@ battery_percentage(){ echo ${PMSET_OUTPUT:--1} elif _command_exists ioreg; then - local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%05.2f%%"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}' | grep -o "[0-9]\+" | head -1) + local IOREG_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%d"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}') echo ${IOREG_OUTPUT:--1} elif _command_exists WMIC; then diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 369a8ef8..fac42883 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -71,6 +71,15 @@ function setup_pmset { assert_output "4" } +@test 'plugins battery: battery-percentage with pmset, no status' { + setup_command_exists "pmset" + + setup_pmset "" + + run battery_percentage + assert_output "-1" +} + ####################### # # acpi @@ -254,3 +263,12 @@ function setup_ioreg { run battery_percentage assert_output "4" } + +@test 'plugins battery: battery-percentage with ioreg, no status' { + setup_command_exists "ioreg" + + setup_ioreg "" + + run battery_percentage + assert_output "0" +}