From 62d0905c7f636254161076a835a17e72251f8460 Mon Sep 17 00:00:00 2001 From: kousu Date: Thu, 24 Aug 2017 04:11:18 -0400 Subject: Be more cautious about handling invalid battery measurements. --- src/print_battery_info.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/print_battery_info.c b/src/print_battery_info.c index e9077ec..e22ca3e 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -175,11 +175,19 @@ static bool slurp_battery_info(struct battery_info *batt_info, yajl_gen json_gen * POWER_SUPPLY_CHARGE_NOW is the unit of measurement. The energy is * given in mWh, the charge in mAh. So calculate every value given in * ampere to watt */ - if (!watt_as_unit && voltage != -1) { - batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0)); - batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0)); - batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0)); - batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0)); + if (!watt_as_unit && voltage >= 0) { + if (batt_info->present_rate > 0) { + batt_info->present_rate = (((float)voltage / 1000.0) * ((float)batt_info->present_rate / 1000.0)); + } + if (batt_info->remaining > 0) { + batt_info->remaining = (((float)voltage / 1000.0) * ((float)batt_info->remaining / 1000.0)); + } + if (batt_info->full_design > 0) { + batt_info->full_design = (((float)voltage / 1000.0) * ((float)batt_info->full_design / 1000.0)); + } + if (batt_info->full_last > 0) { + batt_info->full_last = (((float)voltage / 1000.0) * ((float)batt_info->full_last / 1000.0)); + } } #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) int state; @@ -499,8 +507,19 @@ void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char return; } - int full = (last_full_capacity ? batt_info.full_last : batt_info.full_design); - if (full < 0 && batt_info.percentage_remaining < 0) { + // *Choose* a measure of the 'full' battery. It is whichever is better of + // the battery's (hardware-given) design capacity (batt_info.full_design) + // and the battery's last known good charge (batt_info.full_last). + // We prefer the design capacity, but use the last capacity if we don't have it, + // or if we are asked to (last_full_capacity == true); but similarly we use + // the design capacity if we don't have the last capacity. + // If we don't have either then both full_design and full_last < 0, + // which implies full < 0, which bails out on the following line. + int full = batt_info.full_design; + if (full < 0 || (last_full_capacity && batt_info.full_last >= 0)) { + full = batt_info.full_last; + } + if (full < 0 && batt_info.remaining < 0 && batt_info.percentage_remaining < 0) { /* We have no physical measurements and no estimates. Nothing * much we can report, then. */ OUTPUT_FULL_TEXT(format_down); -- cgit v1.2.3 From aefa784882debe98f88799f0e8fdb544cb8ec3a9 Mon Sep 17 00:00:00 2001 From: kousu Date: Thu, 24 Aug 2017 17:57:16 -0400 Subject: Repair test case. This was introduced in #236. It looks like Travis never ran on that PR? Anyway it's blocking me submitting this now so I'm fixing it. --- testcases/014-battery-capacity/expected_output.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testcases/014-battery-capacity/expected_output.txt b/testcases/014-battery-capacity/expected_output.txt index 3f97d1b..d08aa58 100644 --- a/testcases/014-battery-capacity/expected_output.txt +++ b/testcases/014-battery-capacity/expected_output.txt @@ -1 +1 @@ -Touchpad: 100.00% BAT \ No newline at end of file +Touchpad: 100.00% BAT -- cgit v1.2.3 From cb9b55217d40a50f89f0445294a0b2650fc3dc26 Mon Sep 17 00:00:00 2001 From: Emeric Planet Date: Tue, 29 Aug 2017 19:01:30 +0200 Subject: Fix CPU unit tests (#239) Support any amount of available cores on testing machine. --- testcases/010-cpu-usage/cleanup.pl | 15 +++++++++++++++ testcases/010-cpu-usage/expected_output.pl | 12 ++++++++++++ testcases/010-cpu-usage/expected_output.txt | 1 - testcases/010-cpu-usage/setup.pl | 23 +++++++++++++++++++++++ testcases/010-cpu-usage/stat | 3 --- testcases/011-cpu-usage/cleanup.pl | 15 +++++++++++++++ testcases/011-cpu-usage/expected_output.pl | 12 ++++++++++++ testcases/011-cpu-usage/expected_output.txt | 1 - testcases/011-cpu-usage/setup.pl | 23 +++++++++++++++++++++++ testcases/011-cpu-usage/stat | 3 --- testcases/012-cpu-usage-error/stat | 1 - travis/run-tests.pl | 15 ++++++++++++--- 12 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 testcases/010-cpu-usage/cleanup.pl create mode 100755 testcases/010-cpu-usage/expected_output.pl delete mode 100644 testcases/010-cpu-usage/expected_output.txt create mode 100755 testcases/010-cpu-usage/setup.pl delete mode 100644 testcases/010-cpu-usage/stat create mode 100644 testcases/011-cpu-usage/cleanup.pl create mode 100644 testcases/011-cpu-usage/expected_output.pl delete mode 100644 testcases/011-cpu-usage/expected_output.txt create mode 100644 testcases/011-cpu-usage/setup.pl delete mode 100644 testcases/011-cpu-usage/stat diff --git a/testcases/010-cpu-usage/cleanup.pl b/testcases/010-cpu-usage/cleanup.pl new file mode 100644 index 0000000..ac341be --- /dev/null +++ b/testcases/010-cpu-usage/cleanup.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +if ($#ARGV != 0 || ! -d $ARGV[0]) { + say "Error with cleanup script: argument not provided or not a directory"; + exit 1; +} + +my $output_file = "$ARGV[0]/stat"; +if (-f $output_file) { + unlink $output_file; +} diff --git a/testcases/010-cpu-usage/expected_output.pl b/testcases/010-cpu-usage/expected_output.pl new file mode 100755 index 0000000..d14c908 --- /dev/null +++ b/testcases/010-cpu-usage/expected_output.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +chomp(my $cpu_count = `grep -c -P '^processor\\s+:' /proc/cpuinfo`); +if ($cpu_count == 1) { + print "all: 100% CPU_0: 100% CPU_1: \n"; +} else { + print "all: 75% CPU_0: 100% CPU_1: 50%\n"; +} diff --git a/testcases/010-cpu-usage/expected_output.txt b/testcases/010-cpu-usage/expected_output.txt deleted file mode 100644 index 336596e..0000000 --- a/testcases/010-cpu-usage/expected_output.txt +++ /dev/null @@ -1 +0,0 @@ -all: 75% CPU_0: 100% CPU_1: 50% diff --git a/testcases/010-cpu-usage/setup.pl b/testcases/010-cpu-usage/setup.pl new file mode 100755 index 0000000..8123374 --- /dev/null +++ b/testcases/010-cpu-usage/setup.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +if ($#ARGV != 0 || ! -d $ARGV[0]) { + say "Error with setup script: argument not provided or not a directory"; + exit 1; +} + +chomp(my $cpu_count = `grep -c -P '^processor\\s+:' /proc/cpuinfo`); +my $output_file = "$ARGV[0]/stat"; +open(my $fh, '>', $output_file) or die "Could not open file '$output_file' $!"; +print $fh "cpu 0 0 0 0 0 0 0 0 0 0\n"; +print $fh "cpu0 100 0 0 0 0 0 0 0 0 0\n"; +if ($cpu_count > 1) { + print $fh "cpu1 50 0 0 50 0 0 0 0 0 0\n"; + for (my $i = 2; $i <= $cpu_count; $i++) { + print $fh "cpu$i 0 0 0 0 0 0 0 0 0 0\n"; + } +} +close $fh; diff --git a/testcases/010-cpu-usage/stat b/testcases/010-cpu-usage/stat deleted file mode 100644 index 6fbc94e..0000000 --- a/testcases/010-cpu-usage/stat +++ /dev/null @@ -1,3 +0,0 @@ -cpu 0 0 0 0 0 0 0 0 0 0 -cpu0 100 0 0 0 0 0 0 0 0 0 -cpu1 50 0 0 50 0 0 0 0 0 0 diff --git a/testcases/011-cpu-usage/cleanup.pl b/testcases/011-cpu-usage/cleanup.pl new file mode 100644 index 0000000..ac341be --- /dev/null +++ b/testcases/011-cpu-usage/cleanup.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +if ($#ARGV != 0 || ! -d $ARGV[0]) { + say "Error with cleanup script: argument not provided or not a directory"; + exit 1; +} + +my $output_file = "$ARGV[0]/stat"; +if (-f $output_file) { + unlink $output_file; +} diff --git a/testcases/011-cpu-usage/expected_output.pl b/testcases/011-cpu-usage/expected_output.pl new file mode 100644 index 0000000..d8bf5bd --- /dev/null +++ b/testcases/011-cpu-usage/expected_output.pl @@ -0,0 +1,12 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +chomp(my $cpu_count = `grep -c -P '^processor\\s+:' /proc/cpuinfo`); +if ($cpu_count == 1) { + print "all: 00% CPU_0: 00% CPU_1: \n"; +} else { + print "all: 50% CPU_0: 00% CPU_1: 100%\n"; +} diff --git a/testcases/011-cpu-usage/expected_output.txt b/testcases/011-cpu-usage/expected_output.txt deleted file mode 100644 index 930a2b5..0000000 --- a/testcases/011-cpu-usage/expected_output.txt +++ /dev/null @@ -1 +0,0 @@ -all: 50% CPU_0: 00% CPU_1: 100% diff --git a/testcases/011-cpu-usage/setup.pl b/testcases/011-cpu-usage/setup.pl new file mode 100644 index 0000000..d1e1512 --- /dev/null +++ b/testcases/011-cpu-usage/setup.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use v5.10; +use strict; +use warnings; + +if ($#ARGV != 0 || ! -d $ARGV[0]) { + say "Error with setup script: argument not provided or not a directory"; + exit 1; +} + +chomp(my $cpu_count = `grep -c -P '^processor\\s+:' /proc/cpuinfo`); +my $output_file = "$ARGV[0]/stat"; +open(my $fh, '>', $output_file) or die "Could not open file '$output_file' $!"; +print $fh "cpu 0 0 0 0 0 0 0 0 0 0\n"; +print $fh "cpu0 0 0 0 300 0 0 0 0 0 0\n"; +if ($cpu_count > 1) { + print $fh "cpu1 100 100 100 0 0 0 0 0 0 0\n"; +} +for (my $i = 2; $i <= $cpu_count; $i++) { + print $fh "cpu$i 0 0 0 0 0 0 0 0 0 0\n"; +} +close $fh; diff --git a/testcases/011-cpu-usage/stat b/testcases/011-cpu-usage/stat deleted file mode 100644 index 9c77e7e..0000000 --- a/testcases/011-cpu-usage/stat +++ /dev/null @@ -1,3 +0,0 @@ -cpu 0 0 0 0 0 0 0 0 0 0 -cpu0 0 0 0 300 0 0 0 0 0 0 -cpu1 100 100 100 0 0 0 0 0 0 0 diff --git a/testcases/012-cpu-usage-error/stat b/testcases/012-cpu-usage-error/stat index cb9b6e3..9fb7b5c 100644 --- a/testcases/012-cpu-usage-error/stat +++ b/testcases/012-cpu-usage-error/stat @@ -1,2 +1 @@ cpu 0 0 0 0 0 0 0 0 0 0 -cpu0 100 0 0 0 0 0 0 0 0 0 diff --git a/travis/run-tests.pl b/travis/run-tests.pl index 453a633..5936b7e 100755 --- a/travis/run-tests.pl +++ b/travis/run-tests.pl @@ -3,19 +3,29 @@ use v5.10; use strict; use warnings; +use English; use Term::ANSIColor qw(:constants); use File::Basename; sub TestCase { my ($dir) = @_; + + if ( -f "@_/setup.pl") { + system($EXECUTABLE_NAME, "@_/setup.pl", ($dir)); + } + my $conf = "$dir/i3status.conf"; my $testres = `./i3status --run-once -c $conf`; my $refres = ""; if ( -f "@_/expected_output.txt") { $refres = `cat "@_/expected_output.txt"`; - } elsif ( -f "@_/expected_output.sh") { - $refres = `bash @_/expected_output.sh`; + } elsif ( -f "@_/expected_output.pl") { + $refres = `$EXECUTABLE_NAME @_/expected_output.pl`; + } + + if ( -f "@_/cleanup.pl") { + system($EXECUTABLE_NAME, "@_/cleanup.pl", ($dir)); } if ( "$testres" eq "$refres" ) { @@ -27,7 +37,6 @@ sub TestCase { } } - my $testcases = 'testcases'; my $testresults = 1; -- cgit v1.2.3