Unit testing in Travis (#170)

This commit is contained in:
eplanet 2016-11-14 09:13:44 +01:00 committed by Michael Stapelberg
parent 220b9e1565
commit b0af4e4c85
6 changed files with 86 additions and 12 deletions

View File

@ -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

View File

@ -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 dont use sleep(interval) but we sleep until the next
* second (with microsecond precision) plus (interval-1)

View File

@ -0,0 +1,4 @@
POWER_SUPPLY_STATUS=Discharging
POWER_SUPPLY_CURRENT_NOW=1107000
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000
POWER_SUPPLY_CHARGE_NOW=2390000

View File

@ -0,0 +1 @@
BAT 30.64% 02:09:32

View File

@ -0,0 +1,10 @@
general {
output_format = "none"
}
order += "battery all"
battery all {
format = "%status %percentage %remaining"
path = "testcases/001-battery/BAT%d_uevent"
}

42
travis/run-tests.pl Executable file
View File

@ -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;