summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreplanet <emeric.planet@gmail.com>2016-11-14 09:13:44 +0100
committerMichael Stapelberg <stapelberg@users.noreply.github.com>2016-11-14 00:13:44 -0800
commitb0af4e4c8543d8cdfd8bc7cd68b760d9d66e927c (patch)
treee9586c0909c71a33208b8d22661577a0e705d83a
parent220b9e156527463512862f59b2413702d3ef6a3f (diff)
Unit testing in Travis (#170)
-rw-r--r--.travis.yml1
-rw-r--r--i3status.c40
-rw-r--r--testcases/001-battery/BAT0_uevent4
-rw-r--r--testcases/001-battery/expected_output.txt1
-rw-r--r--testcases/001-battery/i3status.conf10
-rwxr-xr-xtravis/run-tests.pl42
6 files changed, 86 insertions, 12 deletions
diff --git a/.travis.yml b/.travis.yml
index 2f3f93d..fc58769 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -22,3 +22,4 @@ addons:
script:
- make -j
- clang-format-3.5 -i $(find . -name "*.[ch]" | tr '\n' ' ') && git diff --exit-code || (echo 'Code was not formatted using clang-format!'; false)
+ - ./travis/run-tests.pl
diff --git a/i3status.c b/i3status.c
index 85ada1b..f487612 100644
--- a/i3status.c
+++ b/i3status.c
@@ -63,6 +63,7 @@
int general_socket;
static bool exit_upon_signal = false;
+static bool run_once = false;
cfg_t *cfg, *cfg_general, *cfg_section;
@@ -479,11 +480,12 @@ int main(int argc, char *argv[]) {
CFG_END()};
char *configfile = NULL;
- int o, option_index = 0;
+ int opt, option_index = 0;
struct option long_options[] = {
{"config", required_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
+ {"run-once", no_argument, 0, 0},
{0, 0, 0, 0}};
struct sigaction action;
@@ -506,18 +508,28 @@ int main(int argc, char *argv[]) {
if (setlocale(LC_ALL, "") == NULL)
die("Could not set locale. Please make sure all your LC_* / LANG settings are correct.");
- while ((o = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1)
- if ((char)o == 'c')
- configfile = optarg;
- else if ((char)o == 'h') {
- printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n"
- "Syntax: %s [-c <configfile>] [-h] [-v]\n",
- argv[0]);
- return 0;
- } else if ((char)o == 'v') {
- printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n");
- return 0;
+ while ((opt = getopt_long(argc, argv, "c:hv", long_options, &option_index)) != -1) {
+ switch (opt) {
+ case 'c':
+ configfile = optarg;
+ break;
+ case 'h':
+ printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n"
+ "Syntax: %s [-c <configfile>] [-h] [-v]\n",
+ argv[0]);
+ return 0;
+ break;
+ case 'v':
+ printf("i3status " VERSION " © 2008 Michael Stapelberg and contributors\n");
+ return 0;
+ break;
+ case 0:
+ if (strcmp(long_options[option_index].name, "run-once") == 0) {
+ run_once = true;
+ }
+ break;
}
+ }
if (configfile == NULL)
configfile = get_config_path();
@@ -749,6 +761,10 @@ int main(int argc, char *argv[]) {
printf("\n");
fflush(stdout);
+ if (run_once) {
+ break;
+ }
+
/* To provide updates on every full second (as good as possible)
* we don’t use sleep(interval) but we sleep until the next
* second (with microsecond precision) plus (interval-1)
diff --git a/testcases/001-battery/BAT0_uevent b/testcases/001-battery/BAT0_uevent
new file mode 100644
index 0000000..b994324
--- /dev/null
+++ b/testcases/001-battery/BAT0_uevent
@@ -0,0 +1,4 @@
+POWER_SUPPLY_STATUS=Discharging
+POWER_SUPPLY_CURRENT_NOW=1107000
+POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
+POWER_SUPPLY_CHARGE_NOW=2390000
diff --git a/testcases/001-battery/expected_output.txt b/testcases/001-battery/expected_output.txt
new file mode 100644
index 0000000..6e1898b
--- /dev/null
+++ b/testcases/001-battery/expected_output.txt
@@ -0,0 +1 @@
+BAT 30.64% 02:09:32
diff --git a/testcases/001-battery/i3status.conf b/testcases/001-battery/i3status.conf
new file mode 100644
index 0000000..b55e768
--- /dev/null
+++ b/testcases/001-battery/i3status.conf
@@ -0,0 +1,10 @@
+general {
+ output_format = "none"
+}
+
+order += "battery all"
+
+battery all {
+ format = "%status %percentage %remaining"
+ path = "testcases/001-battery/BAT%d_uevent"
+}
diff --git a/travis/run-tests.pl b/travis/run-tests.pl
new file mode 100755
index 0000000..453a633
--- /dev/null
+++ b/travis/run-tests.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use v5.10;
+use strict;
+use warnings;
+use Term::ANSIColor qw(:constants);
+use File::Basename;
+
+sub TestCase {
+ my ($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`;
+ }
+
+ if ( "$testres" eq "$refres" ) {
+ say "Testing test case '", basename($dir), "'… ", BOLD, GREEN, "OK", RESET;
+ return 1;
+ } else {
+ say "Testing test case '", basename($dir), "'… ", BOLD, RED, "Failed!", RESET;
+ return 0;
+ }
+}
+
+
+my $testcases = 'testcases';
+my $testresults = 1;
+
+opendir(my $dir, $testcases) or die "Could not open directory $testcases: $!";
+
+while (my $entry = readdir($dir)) {
+ next unless (-d "$testcases/$entry");
+ next if ($entry =~ m/^\./);
+ $testresults = $testresults && TestCase("$testcases/$entry");
+}
+closedir($dir);
+exit 0;