From d0b3205de0c8cf99da660feb488065a35ea29013 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Wed, 18 Oct 2017 18:19:53 +0200 Subject: [PATCH] Added an additional test case and a fix for using the pmset command The other commands still need to be mocked and tested/fixed, too. --- plugins/available/battery.plugin.bash | 8 +++++++- test/plugins/battery.plugin.bats | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 0391ca10..75a2f0d4 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -92,7 +92,13 @@ battery_percentage(){ echo '100' ;; *) - echo $PMSET_OUTPUT | head -c 2 + # This will cut off any decimals, and will get rid of the optional percentage sign at the end, too. + # Works for: + # - 100% + # - 100.0% + # - 99.8% + # - 4% + echo $PMSET_OUTPUT | grep -o "[0-9]\+" | head -1 ;; esac elif _command_exists ioreg; diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index c6f62c92..f5ac0c1d 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -26,6 +26,15 @@ load ../../plugins/available/battery.plugin assert_output "98" } +@test 'plugins battery: battery-percentage with pmset, 98.5%' { + function pmset { + echo "-InternalBattery-0 (id=12345) 98.5%; discharging; 16:00 remaining present: true" + } + + run battery_percentage + assert_output "98" +} + @test 'plugins battery: battery-percentage with pmset, 4%' { function pmset { echo "-InternalBattery-0 (id=12345) 4%; discharging; 16:00 remaining present: true"