From ef28b2788ba5d269ce11649f0428afe8f727b6cb Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Wed, 18 Oct 2017 17:30:52 +0200 Subject: [PATCH 01/23] Added test cases for battery_percentage with pmset Need to figure out way to ensure that pmset is used every time... --- test/plugins/battery.plugin.bats | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 test/plugins/battery.plugin.bats diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats new file mode 100644 index 00000000..c6f62c92 --- /dev/null +++ b/test/plugins/battery.plugin.bats @@ -0,0 +1,36 @@ +#!/usr/bin/env bats + +load ../test_helper +load ../../lib/composure + +cite _about _param _example _group _author _version + +load ../../lib/helpers +load ../../plugins/available/battery.plugin + +@test 'plugins battery: battery-percentage with pmset, 100%' { + function pmset { + echo "-InternalBattery-0 (id=12345) 100%; discharging; 16:00 remaining present: true" + } + + run battery_percentage + assert_output "100" +} + +@test 'plugins battery: battery-percentage with pmset, 98%' { + function pmset { + echo "-InternalBattery-0 (id=12345) 98%; 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" + } + + run battery_percentage + assert_output "4" +} From d0b3205de0c8cf99da660feb488065a35ea29013 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Wed, 18 Oct 2017 18:19:53 +0200 Subject: [PATCH 02/23] 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" From c8d86858fdba7c019554880b684ff8ac4e353698 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 19 Oct 2017 07:30:44 +0200 Subject: [PATCH 03/23] Added setup function for _command_exists that works for pmset --- test/plugins/battery.plugin.bats | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index f5ac0c1d..90862432 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -5,10 +5,30 @@ load ../../lib/composure cite _about _param _example _group _author _version -load ../../lib/helpers load ../../plugins/available/battery.plugin +function setup_command_exists_pmset { + function _command_exists { + case "$1" in + "upower") + false + ;; + "acpi") + false + ;; + "pmset") + true + ;; + *) + false + ;; + esac + } +} + @test 'plugins battery: battery-percentage with pmset, 100%' { + setup_command_exists_pmset + function pmset { echo "-InternalBattery-0 (id=12345) 100%; discharging; 16:00 remaining present: true" } @@ -18,6 +38,8 @@ load ../../plugins/available/battery.plugin } @test 'plugins battery: battery-percentage with pmset, 98%' { + setup_command_exists_pmset + function pmset { echo "-InternalBattery-0 (id=12345) 98%; discharging; 16:00 remaining present: true" } @@ -27,6 +49,8 @@ load ../../plugins/available/battery.plugin } @test 'plugins battery: battery-percentage with pmset, 98.5%' { + setup_command_exists_pmset + function pmset { echo "-InternalBattery-0 (id=12345) 98.5%; discharging; 16:00 remaining present: true" } @@ -36,6 +60,8 @@ load ../../plugins/available/battery.plugin } @test 'plugins battery: battery-percentage with pmset, 4%' { + setup_command_exists_pmset + function pmset { echo "-InternalBattery-0 (id=12345) 4%; discharging; 16:00 remaining present: true" } From c221648a7ffb80da5eafb51a2d734357498c0f33 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 19 Oct 2017 07:34:12 +0200 Subject: [PATCH 04/23] Refactored setup function for pmset --- test/plugins/battery.plugin.bats | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 90862432..9016a7d9 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -26,12 +26,18 @@ function setup_command_exists_pmset { } } +function setup_pmset { + percent="$1" + + function pmset { + echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + } +} + @test 'plugins battery: battery-percentage with pmset, 100%' { setup_command_exists_pmset - function pmset { - echo "-InternalBattery-0 (id=12345) 100%; discharging; 16:00 remaining present: true" - } + setup_pmset "100%" run battery_percentage assert_output "100" @@ -40,9 +46,7 @@ function setup_command_exists_pmset { @test 'plugins battery: battery-percentage with pmset, 98%' { setup_command_exists_pmset - function pmset { - echo "-InternalBattery-0 (id=12345) 98%; discharging; 16:00 remaining present: true" - } + setup_pmset "98%" run battery_percentage assert_output "98" @@ -51,9 +55,7 @@ function setup_command_exists_pmset { @test 'plugins battery: battery-percentage with pmset, 98.5%' { setup_command_exists_pmset - function pmset { - echo "-InternalBattery-0 (id=12345) 98.5%; discharging; 16:00 remaining present: true" - } + setup_pmset "98.5%" run battery_percentage assert_output "98" @@ -62,9 +64,7 @@ function setup_command_exists_pmset { @test 'plugins battery: battery-percentage with pmset, 4%' { setup_command_exists_pmset - function pmset { - echo "-InternalBattery-0 (id=12345) 4%; discharging; 16:00 remaining present: true" - } + setup_pmset "4%" run battery_percentage assert_output "4" From 28c41efcbc3ae185bf7a10c07d68ed72d5d2c4b9 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 19 Oct 2017 07:34:58 +0200 Subject: [PATCH 05/23] Simplified setup fixture for pmset/_command_exists --- test/plugins/battery.plugin.bats | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 9016a7d9..e479f316 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -10,12 +10,6 @@ load ../../plugins/available/battery.plugin function setup_command_exists_pmset { function _command_exists { case "$1" in - "upower") - false - ;; - "acpi") - false - ;; "pmset") true ;; From b4c6029706535b6170b9819436e0910e38e1dc6f Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 19 Oct 2017 07:37:19 +0200 Subject: [PATCH 06/23] Made setup function for _command_exists generic --- test/plugins/battery.plugin.bats | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index e479f316..916855eb 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -7,10 +7,12 @@ cite _about _param _example _group _author _version load ../../plugins/available/battery.plugin -function setup_command_exists_pmset { +function setup_command_exists { + success_command="$1" + function _command_exists { case "$1" in - "pmset") + "${success_command}") true ;; *) @@ -29,7 +31,7 @@ function setup_pmset { } @test 'plugins battery: battery-percentage with pmset, 100%' { - setup_command_exists_pmset + setup_command_exists "pmset" setup_pmset "100%" @@ -38,7 +40,7 @@ function setup_pmset { } @test 'plugins battery: battery-percentage with pmset, 98%' { - setup_command_exists_pmset + setup_command_exists "pmset" setup_pmset "98%" @@ -47,7 +49,7 @@ function setup_pmset { } @test 'plugins battery: battery-percentage with pmset, 98.5%' { - setup_command_exists_pmset + setup_command_exists "pmset" setup_pmset "98.5%" @@ -56,7 +58,7 @@ function setup_pmset { } @test 'plugins battery: battery-percentage with pmset, 4%' { - setup_command_exists_pmset + setup_command_exists "pmset" setup_pmset "4%" From 5ab23e3ca01dd7277a3c9cf130e00c738a9f8d70 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 19 Oct 2017 08:05:52 +0200 Subject: [PATCH 07/23] Added test cases for acpi, upower and ioreg, need to fill in data The one for ioreg works fine, the others currently fail. --- test/plugins/battery.plugin.bats | 152 +++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 916855eb..d7313742 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -22,6 +22,11 @@ function setup_command_exists { } } +####################### +# +# pmset +# + function setup_pmset { percent="$1" @@ -65,3 +70,150 @@ function setup_pmset { run battery_percentage assert_output "4" } + +####################### +# +# acpi +# + +function setup_acpi { + percent="$1" + + function acpi { + echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + } +} + +@test 'plugins battery: battery-percentage with acpi, 100%' { + setup_command_exists "acpi" + + setup_acpi "100%" + + run battery_percentage + assert_output "100" +} + +@test 'plugins battery: battery-percentage with acpi, 98%' { + setup_command_exists "acpi" + + setup_acpi "98%" + + run battery_percentage + assert_output "98" +} + +@test 'plugins battery: battery-percentage with acpi, 98.5%' { + setup_command_exists "acpi" + + setup_acpi "98.5%" + + run battery_percentage + assert_output "98" +} + +@test 'plugins battery: battery-percentage with acpi, 4%' { + setup_command_exists "acpi" + + setup_acpi "4%" + + run battery_percentage + assert_output "4" +} + +####################### +# +# upower +# + +function setup_upower { + percent="$1" + + function upower { + echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + } +} + +@test 'plugins battery: battery-percentage with upower, 100%' { + setup_command_exists "upower" + + setup_upower "100%" + + run battery_percentage + assert_output "100" +} + +@test 'plugins battery: battery-percentage with upower, 98%' { + setup_command_exists "upower" + + setup_upower "98%" + + 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%" + + run battery_percentage + assert_output "4" +} + +####################### +# +# ioreg +# + +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 "4" +} From d39d14b1406a9d84914a235f73a6b98c33cc2dc8 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Fri, 20 Oct 2017 14:52:08 +0200 Subject: [PATCH 08/23] Refactored test run script so that you can run a single file Call it like this: test/run test/plugins/battery.plugin.bats If called without any parameters, all of the tests are run. --- test/run | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/run b/test/run index dab65268..da1aa061 100755 --- a/test/run +++ b/test/run @@ -10,4 +10,10 @@ if [ -z "${BASH_IT}" ]; then export BASH_IT fi -exec $bats_executable ${CI:+--tap} ${test_directory}/{bash_it,completion,install,lib,plugins,themes} +if [ -z "$1" ]; then + test_dirs=( ${test_directory}/{bash_it,completion,install,lib,plugins,themes} ) +else + test_dirs=( "$1" ) +fi + +exec $bats_executable ${CI:+--tap} "${test_dirs[@]}" From b7b8a0afe0f33ea9b5fdd04b19f68ea59e942c97 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 07:39:08 +0200 Subject: [PATCH 09/23] 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 From a4824cc8c85634ffc41b2cc8edfb8463ae7bd234 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 07:40:59 +0200 Subject: [PATCH 10/23] Changed pmset test to use printf --- test/plugins/battery.plugin.bats | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 9a1e56b7..b9229bde 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -31,7 +31,7 @@ function setup_pmset { percent="$1" function pmset { - echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + printf "\-InternalBattery-0 (id=12345) %s; discharging; 16:00 remaining present: true" "${percent}" } } From 812f49e4570261f0806e6ef26f4aaaf0b5121ce5 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 08:14:35 +0200 Subject: [PATCH 11/23] Started first tests for acpi command --- test/plugins/battery.plugin.bats | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index b9229bde..5e48dfba 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -80,7 +80,7 @@ function setup_acpi { percent="$1" function acpi { - echo "-InternalBattery-0 (id=12345) ""${percent}""; discharging; 16:00 remaining present: true" + printf "Battery 0: Charging, %s, 01:02:48 until charged" "${percent}" } } @@ -102,15 +102,6 @@ function setup_acpi { assert_output "98" } -@test 'plugins battery: battery-percentage with acpi, 98.5%' { - setup_command_exists "acpi" - - setup_acpi "98.5%" - - run battery_percentage - assert_output "98" -} - @test 'plugins battery: battery-percentage with acpi, 4%' { setup_command_exists "acpi" From 3aed38c79f1a9e17311b3e05306db79b0463f0a2 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 17:07:04 +0200 Subject: [PATCH 12/23] Updated acpi tests Trying to cover the currently implemented tests, but I don't have a system with acpi available for testing. Relying on assumptions so far. --- test/plugins/battery.plugin.bats | 42 ++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 5e48dfba..577164f0 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -78,39 +78,67 @@ function setup_pmset { function setup_acpi { percent="$1" + status="$2" function acpi { - printf "Battery 0: Charging, %s, 01:02:48 until charged" "${percent}" + printf "Battery 0: %s, %s, 01:02:48 until charged" "${status}" "${percent}" } } -@test 'plugins battery: battery-percentage with acpi, 100%' { +@test 'plugins battery: battery-percentage with acpi, 100% Full' { setup_command_exists "acpi" - setup_acpi "100%" + setup_acpi "100%" "Full" run battery_percentage assert_output "100" } -@test 'plugins battery: battery-percentage with acpi, 98%' { +@test 'plugins battery: battery-percentage with acpi, 98% Charging' { setup_command_exists "acpi" - setup_acpi "98%" + setup_acpi "98%" "Charging" run battery_percentage assert_output "98" } -@test 'plugins battery: battery-percentage with acpi, 4%' { +@test 'plugins battery: battery-percentage with acpi, 98% Discharging' { setup_command_exists "acpi" - setup_acpi "4%" + 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 "4" } +@test 'plugins battery: battery-percentage with acpi, 4% no status' { + setup_command_exists "acpi" + + setup_acpi "4%" "" + + run battery_percentage + assert_output "-1" +} + ####################### # # upower From 486c9e13824ec8319aa82c43c2129d3a940bee5e Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 17:14:53 +0200 Subject: [PATCH 13/23] Consolidated cases for acpi --- plugins/available/battery.plugin.bash | 26 ++------------------------ test/plugins/battery.plugin.bats | 9 +++++++++ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 4049a289..78f54b61 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -60,30 +60,8 @@ battery_percentage(){ elif _command_exists acpi; then local ACPI_OUTPUT=$(acpi -b) - case $ACPI_OUTPUT in - *" Unknown"*) - local PERC_OUTPUT=$(echo $ACPI_OUTPUT | head -c 22 | tail -c 2) - case $PERC_OUTPUT in - *%) - echo "0${PERC_OUTPUT}" | head -c 2 - ;; - *) - echo ${PERC_OUTPUT} - ;; - esac - ;; - - *" Charging"* | *" Discharging"*) - local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) - echo ${PERC_OUTPUT} - ;; - *" Full"*) - echo '100' - ;; - *) - echo '-1' - ;; - esac + local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) + echo ${PERC_OUTPUT:--1} elif _command_exists pmset; then local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p') diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 577164f0..369a8ef8 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -135,6 +135,15 @@ function setup_acpi { setup_acpi "4%" "" + run battery_percentage + assert_output "4" +} + +@test 'plugins battery: battery-percentage with acpi, no status' { + setup_command_exists "acpi" + + setup_acpi "" "" + run battery_percentage assert_output "-1" } From 6b97283cbfaf7104fb461e7da4389a95e0505143 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 23 Oct 2017 17:21:24 +0200 Subject: [PATCH 14/23] Consolidated cases for pmset and ioreg --- plugins/available/battery.plugin.bash | 28 ++++----------------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 78f54b61..33352a67 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -64,32 +64,12 @@ battery_percentage(){ echo ${PERC_OUTPUT:--1} elif _command_exists pmset; then - local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p') - case $PMSET_OUTPUT in - 100*) - echo '100' - ;; - *) - # 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 + local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) + 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: "?")}') - case $IOREG_OUTPUT in - 100*) - echo '100' - ;; - *) - echo $IOREG_OUTPUT | head -c 2 - ;; - esac + 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) + echo ${IOREG_OUTPUT:--1} elif _command_exists WMIC; then local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*') From 601202e8160c60b108c6371e9a3a5201a913590a Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:32:00 +0200 Subject: [PATCH 15/23] 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" +} From 7602b1da329b7a778f0e7a656dc47d141309ef07 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:40:42 +0200 Subject: [PATCH 16/23] Added tests for WMIC --- plugins/available/battery.plugin.bash | 11 +---- test/plugins/battery.plugin.bats | 58 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 55fa981f..7c035180 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -72,15 +72,8 @@ battery_percentage(){ echo ${IOREG_OUTPUT:--1} elif _command_exists WMIC; then - local WINPC=$(echo porcent=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List) | grep -o '[0-9]*') - case $WINPC in - 100*) - echo '100' - ;; - *) - echo $WINPC - ;; - esac + local WINPC=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1) + echo ${WINPC:--1} else echo "no" fi diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index fac42883..36eeab4f 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -272,3 +272,61 @@ function setup_ioreg { run battery_percentage assert_output "0" } + +####################### +# +# WMIC +# + +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 "4" +} + +@test 'plugins battery: battery-percentage with WMIC, no status' { + setup_command_exists "WMIC" + + setup_WMIC "" + + run battery_percentage + assert_output "-1" +} From a573b63da22c6c7e449ebecd106c86045b175152 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:42:37 +0200 Subject: [PATCH 17/23] Simplified acpi --- plugins/available/battery.plugin.bash | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 7c035180..2154c987 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -59,8 +59,7 @@ battery_percentage(){ echo ${UPOWER_OUTPUT:--1} elif _command_exists acpi; then - local ACPI_OUTPUT=$(acpi -b) - local PERC_OUTPUT=$(echo $ACPI_OUTPUT | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) + local PERC_OUTPUT=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) echo ${PERC_OUTPUT:--1} elif _command_exists pmset; then From 86a87a33b542d29d684fd6fbf00aed39dcf8f67f Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:45:04 +0200 Subject: [PATCH 18/23] Refactored command output handling --- plugins/available/battery.plugin.bash | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 2154c987..4079738d 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -53,29 +53,28 @@ battery_percentage(){ about 'displays battery charge as a percentage of full (100%)' group 'battery' + declare COMMAND_OUTPUT="no" + if _command_exists upower; then - local UPOWER_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | grep -o "[0-9]\+" | head -1) - echo ${UPOWER_OUTPUT:--1} + COMMAND_OUTPUT=$(upower --show-info $(upower --enumerate | grep BAT) | grep percentage | grep -o "[0-9]\+" | head -1) elif _command_exists acpi; then - local PERC_OUTPUT=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) - echo ${PERC_OUTPUT:--1} + COMMAND_OUTPUT=$(acpi -b | awk -F, '/,/{gsub(/ /, "", $0); gsub(/%/,"", $0); print $2}' ) elif _command_exists pmset; then - local PMSET_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) - echo ${PMSET_OUTPUT:--1} + COMMAND_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) elif _command_exists ioreg; then - 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} + COMMAND_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%d"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}') elif _command_exists WMIC; then - local WINPC=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1) - echo ${WINPC:--1} + COMMAND_OUTPUT=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1) else - echo "no" + COMMAND_OUTPUT="no" fi + + echo ${COMMAND_OUTPUT:--1} } battery_charge(){ From 629b8522e5bbe58537fdd366757b252008e63d87 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Tue, 24 Oct 2017 07:52:25 +0200 Subject: [PATCH 19/23] Ensuring that output is always two digits at least Show "04" instead of just "4". --- plugins/available/battery.plugin.bash | 2 +- test/plugins/battery.plugin.bats | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 4079738d..08da85b9 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -74,7 +74,7 @@ battery_percentage(){ COMMAND_OUTPUT="no" fi - echo ${COMMAND_OUTPUT:--1} + printf "%02d" "${COMMAND_OUTPUT:--1}" } battery_charge(){ diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 36eeab4f..43ddeace 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -68,7 +68,7 @@ function setup_pmset { setup_pmset "4%" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with pmset, no status' { @@ -136,7 +136,7 @@ function setup_acpi { setup_acpi "4%" "Charging" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with acpi, 4% no status' { @@ -145,7 +145,7 @@ function setup_acpi { setup_acpi "4%" "" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with acpi, no status' { @@ -203,7 +203,7 @@ function setup_upower { setup_upower "4.2345%" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with upower, no output' { @@ -261,7 +261,7 @@ function setup_ioreg { setup_ioreg "4%" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with ioreg, no status' { @@ -270,7 +270,7 @@ function setup_ioreg { setup_ioreg "" run battery_percentage - assert_output "0" + assert_output "00" } ####################### @@ -319,7 +319,7 @@ function setup_WMIC { setup_WMIC "4%" run battery_percentage - assert_output "4" + assert_output "04" } @test 'plugins battery: battery-percentage with WMIC, no status' { From fef8e9ed097828ad032aebf22dabc6f742f17543 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 30 Oct 2017 20:22:29 +0100 Subject: [PATCH 20/23] Fixed ioreg test case --- plugins/available/battery.plugin.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index 08da85b9..b9d00514 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -66,7 +66,7 @@ battery_percentage(){ COMMAND_OUTPUT=$(pmset -g ps | sed -n 's/.*[[:blank:]]+*\(.*%\).*/\1/p' | grep -o "[0-9]\+" | head -1) elif _command_exists ioreg; then - COMMAND_OUTPUT=$(ioreg -n AppleSmartBattery -r | awk '$1~/Capacity/{c[$1]=$3} END{OFMT="%d"; max=c["\"MaxCapacity\""]; print (max>0? 100*c["\"CurrentCapacity\""]/max: "?")}') + COMMAND_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) elif _command_exists WMIC; then COMMAND_OUTPUT=$(WMIC PATH Win32_Battery Get EstimatedChargeRemaining /Format:List | grep -o '[0-9]\+' | head -1) From 7ea1a582160d81fa9358a6b27a2a132581e39d04 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 2 Nov 2017 06:04:10 +0100 Subject: [PATCH 21/23] Added comments on the test functions --- test/plugins/battery.plugin.bats | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 43ddeace..0cced24b 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -7,6 +7,18 @@ 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" @@ -27,6 +39,8 @@ function setup_command_exists { # 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" @@ -85,6 +99,10 @@ function setup_pmset { # 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" @@ -162,6 +180,8 @@ function setup_acpi { # 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" @@ -220,6 +240,8 @@ function setup_upower { # 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" @@ -278,6 +300,8 @@ function setup_ioreg { # 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" From 5ab0cc53888e1129cc6254846bbc78ced730139d Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 2 Nov 2017 06:08:20 +0100 Subject: [PATCH 22/23] Fixed ioreg - no battery test case --- test/plugins/battery.plugin.bats | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 0cced24b..685b2072 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -289,10 +289,13 @@ function setup_ioreg { @test 'plugins battery: battery-percentage with ioreg, no status' { setup_command_exists "ioreg" - setup_ioreg "" + # Simulate that no battery is present + function ioreg { + printf "" + } run battery_percentage - assert_output "00" + assert_output "-1" } ####################### From 31990490fa4808e4e0bc23fa0308cd1ddbf22457 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Thu, 2 Nov 2017 06:15:34 +0100 Subject: [PATCH 23/23] Added test for the "no" energy tool case --- plugins/available/battery.plugin.bash | 6 +++++- test/plugins/battery.plugin.bats | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/plugins/available/battery.plugin.bash b/plugins/available/battery.plugin.bash index b9d00514..b9bee172 100644 --- a/plugins/available/battery.plugin.bash +++ b/plugins/available/battery.plugin.bash @@ -74,7 +74,11 @@ battery_percentage(){ COMMAND_OUTPUT="no" fi - printf "%02d" "${COMMAND_OUTPUT:--1}" + if [ "${COMMAND_OUTPUT}" != "no" ]; then + printf "%02d" "${COMMAND_OUTPUT:--1}" + else + echo "${COMMAND_OUTPUT}" + fi } battery_charge(){ diff --git a/test/plugins/battery.plugin.bats b/test/plugins/battery.plugin.bats index 685b2072..88e20f25 100644 --- a/test/plugins/battery.plugin.bats +++ b/test/plugins/battery.plugin.bats @@ -34,6 +34,18 @@ function setup_command_exists { } } +####################### +# +# no tool +# + +@test 'plugins battery: battery-percentage with no tool' { + setup_command_exists "fooooo" + + run battery_percentage + assert_output "no" +} + ####################### # # pmset