From c8123fe6adb0fc2095131a09df1bb5a82dd3c2a3 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 18 Aug 2019 21:15:24 +0200 Subject: Add new test files --- t/conf/attr_clear.t | 31 +++++++++++++++++++++ t/conf/color.t | 33 ++++++++++++++++++++++ t/conf/fail.t | 59 ++++++++++++++++++++++++++++++++++++++++ t/conf/parse/fail.t | 63 ++++++++++++++++++++++++++++++++++++++++++ t/conf/parse/success.t | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ t/conf/use.t | 57 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 317 insertions(+) create mode 100755 t/conf/attr_clear.t create mode 100755 t/conf/color.t create mode 100755 t/conf/fail.t create mode 100755 t/conf/parse/fail.t create mode 100755 t/conf/parse/success.t create mode 100755 t/conf/use.t (limited to 't/conf') diff --git a/t/conf/attr_clear.t b/t/conf/attr_clear.t new file mode 100755 index 0000000..7bbe5f9 --- /dev/null +++ b/t/conf/attr_clear.t @@ -0,0 +1,31 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 1; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (attr clear)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->('foo'); + + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "attr=blink\n"; + close($fh); + + is(qx($program default --attr=bold $infile), "\e[1;39mfoo\e[0m", 'discard conf attr string'); + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/color.t b/t/conf/color.t new file mode 100755 index 0000000..3adfd5b --- /dev/null +++ b/t/conf/color.t @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 3; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (color)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->('foo'); + + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "color=green\n"; + close($fh); + + is(qx($program $infile), "\e[32mfoo\e[0m", 'color from config'); + is(qx(printf %s "foo" | $program none/none), 'foo', 'read from stdin'); + is(qx($program none/none $infile), 'foo', 'read from file'); + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/fail.t b/t/conf/fail.t new file mode 100755 index 0000000..d3b8852 --- /dev/null +++ b/t/conf/fail.t @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 7; + +my $run_program_fail = sub +{ + my ($program, $message, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 1 && $output =~ /$message/) ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config failure exit)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + my @set = ( + [ 'attr=:', 'attr conf option must be provided a string' ], + [ 'attr=bold:underscore', 'attr conf option must have strings separated by ,' ], + [ 'attr=b0ld', 'attr conf option attribute \'b0ld\' is not valid' ], + [ 'attr=b0ld,underscore', 'attr conf option attribute \'b0ld\' is not valid' ], # handle comma + [ 'attr=bold,bold', 'attr conf option has attribute \'bold\' twice or more' ], + [ 'exclude-random=random', 'exclude-random conf option must be provided a plain color' ], + [ 'omit-color-empty=unsure', 'omit-color-empty conf option is not valid' ], + ); + foreach my $set (@set) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $set->[0], "\n"; + close($fh); + ok($run_program_fail->($program, $set->[1], $infile), $set->[1]); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/parse/fail.t b/t/conf/parse/fail.t new file mode 100755 index 0000000..673153b --- /dev/null +++ b/t/conf/parse/fail.t @@ -0,0 +1,63 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 8; + +my $run_program_fail = sub +{ + my ($program, $message, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 1 && $output =~ /$message/) ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config parse failure)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + my $chars_exceed = 'x' x 256; + + my @set = ( + [ '[attr=bold', 'option \'\[attr\' cannot be made of non-option characters' ], + [ 'attr1=bold', 'option \'attr1\' not recognized' ], + [ 'color1=magenta', 'option \'color1\' not recognized' ], + [ 'exclude-random1=black', 'option \'exclude-random1\' not recognized' ], + [ 'omit-color-empty1=yes', 'option \'omit-color-empty1\' not recognized' ], + [ 'attr', 'option \'attr\' not followed by =' ], + [ 'attr bold', 'option \'attr\' not followed by =' ], + [ "color=$chars_exceed", 'line exceeds maximum of' ], + ); + + foreach my $set (@set) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $set->[0], "\n"; + close($fh); + ok($run_program_fail->($program, $set->[1], $infile), $set->[1]); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/parse/success.t b/t/conf/parse/success.t new file mode 100755 index 0000000..fe2d46d --- /dev/null +++ b/t/conf/parse/success.t @@ -0,0 +1,74 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); +use constant true => 1; +use constant false => 0; + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use IPC::Open3 qw(open3); +use Symbol qw(gensym); +use Test::More; + +my $tests = 21; + +my $conf = <<'EOT'; +# comment + # comment + # comment +attr=bold +attr =bold +attr= bold +attr =bold +attr= bold +attr = bold + color=green +color=green + color=green +color=green +exclude-random=black +omit-color-empty=yes +attr=bold # comment +attr=bold # comment +attr= +color= +exclude-random= +omit-color-empty= +EOT + +my $run_program_succeed = sub +{ + my ($program, $infile) = @_; + + my $err = gensym; + + my $pid = open3(gensym, gensym, $err, $program, qw(default), $infile); + waitpid($pid, 0); + + my $output = do { local $/; <$err> }; + + return ($? >> 8 == 0 && $output eq '') ? true : false; +}; + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (config parse success)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(''); + + foreach my $line (split /\n/, $conf) { + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $line, "\n"; + close($fh); + ok($run_program_succeed->($program, $infile), $line); + } + + unlink $program; + unlink $conf_file; +} diff --git a/t/conf/use.t b/t/conf/use.t new file mode 100755 index 0000000..0a695df --- /dev/null +++ b/t/conf/use.t @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 21; + +my $conf = <<'EOT'; +attr=underscore +color=yellow # tested also in color.t +omit-color-empty=yes +EOT + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + + skip 'compiling failed (use config)', $tests unless system(qq($compiler -DTEST -DCONF_FILE_TEST=\"$conf_file\" -o $program $source)) == 0; + + my $infile1 = $write_to_tmpfile->(<<'EOT'); +foo + +bar + +baz +EOT + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $conf; + close($fh); + + is(qx($program $infile1), <<"EOT", 'use config'); +\e[4;33mfoo\e[0m + +\e[4;33mbar\e[0m + +\e[4;33mbaz\e[0m +EOT + my $infile2 = $write_to_tmpfile->('foo'); + + open($fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} "exclude-random=black\n"; + close($fh); + + for (my $i = 1; $i <= 20; $i++) { + like(qx($program random $infile2), qr/^\e\[3[1-7]mfoo\e\[0m$/, 'use exclude-random'); + } + + unlink $program; + unlink $conf_file; +} -- cgit v1.2.3 From 946a888c6c7aea8af693089bed73673c193899a7 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Wed, 28 Aug 2019 13:57:36 +0200 Subject: Test --config --- t/conf/param.t | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 t/conf/param.t (limited to 't/conf') diff --git a/t/conf/param.t b/t/conf/param.t new file mode 100755 index 0000000..3fe5436 --- /dev/null +++ b/t/conf/param.t @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use lib qw(lib); + +use Colorize::Common qw(:defaults $write_to_tmpfile); +use File::Temp qw(tmpnam); +use Test::More; + +my $tests = 1; + +my $conf = <<'EOT'; +attr=bold +color=blue +omit-color-empty=yes +EOT + +plan tests => $tests; + +SKIP: { + my $program = tmpnam(); + my $conf_file = tmpnam(); + # -DTEST omitted on purpose + skip 'compiling failed (config param)', $tests unless system(qq($compiler -o $program $source)) == 0; + + my $infile = $write_to_tmpfile->(<<'EOT'); +foo + +bar + +baz +EOT + open(my $fh, '>', $conf_file) or die "Cannot open `$conf_file' for writing: $!\n"; + print {$fh} $conf; + close($fh); + + is(qx($program --config=$conf_file $infile), <<"EOT", 'config param'); +\e[1;34mfoo\e[0m + +\e[1;34mbar\e[0m + +\e[1;34mbaz\e[0m +EOT + unlink $program; + unlink $conf_file; +} -- cgit v1.2.3 From 31de42f6348099f80017e1b35abb407db01e6559 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Fri, 30 Aug 2019 20:41:16 +0200 Subject: Print line number when exceeding chars --- colorize.c | 4 +++- t/conf/parse/fail.t | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 't/conf') diff --git a/colorize.c b/colorize.c index dbeed25..bf7e3d6 100644 --- a/colorize.c +++ b/colorize.c @@ -663,6 +663,7 @@ init_opts_vars (void) static void parse_conf (const char *conf_file, struct conf *config) { + unsigned int cnt = 0; char line[256 + 1]; FILE *conf; @@ -674,8 +675,9 @@ parse_conf (const char *conf_file, struct conf *config) char *assign, *comment, *opt, *value; char *p; + cnt++; if (strlen (line) > (sizeof (line) - 2)) - vfprintf_fail ("%s: line exceeds maximum of %u characters", conf_file, (unsigned int)(sizeof (line) - 2)); + vfprintf_fail ("%s: line %u exceeds maximum of %u characters", conf_file, cnt, (unsigned int)(sizeof (line) - 2)); if ((p = strrchr (line, '\n'))) *p = '\0'; /* NAME PARSING (start) */ diff --git a/t/conf/parse/fail.t b/t/conf/parse/fail.t index 673153b..ea267f8 100755 --- a/t/conf/parse/fail.t +++ b/t/conf/parse/fail.t @@ -48,7 +48,7 @@ SKIP: { [ 'omit-color-empty1=yes', 'option \'omit-color-empty1\' not recognized' ], [ 'attr', 'option \'attr\' not followed by =' ], [ 'attr bold', 'option \'attr\' not followed by =' ], - [ "color=$chars_exceed", 'line exceeds maximum of' ], + [ "color=$chars_exceed", 'line 1 exceeds maximum of' ], ); foreach my $set (@set) { -- cgit v1.2.3 From d04e14ae7bef2af9121bc4880e36128acd3dd8ed Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 1 Sep 2019 15:51:59 +0200 Subject: Add config short option -c --- colorize.1 | 4 ++-- colorize.c | 20 ++++++++++++++------ t/conf/param.t | 18 +++++++++++------- 3 files changed, 27 insertions(+), 15 deletions(-) (limited to 't/conf') diff --git a/colorize.1 b/colorize.1 index 45a3ef0..02ffe2e 100644 --- a/colorize.1 +++ b/colorize.1 @@ -1,4 +1,4 @@ -.TH COLORIZE 1 "2019-08-27" "colorize v0.64" "User Commands" +.TH COLORIZE 1 "2019-09-01" "colorize v0.64" "User Commands" .SH NAME colorize \- colorize text on terminal with ANSI escape sequences .SH SYNOPSIS @@ -40,7 +40,7 @@ clean text from color escape sequences emitted by colorize .BR \-\-clean\-all clean text from all valid color escape sequences .TP -.BR \-\-config=\fIPATH\fR +.BR \-c ", " \-\-config=\fIPATH\fR alternate configuration file location .TP .BR \-\-exclude\-random=\fICOLOR\fR diff --git a/colorize.c b/colorize.c index bf7e3d6..147e684 100644 --- a/colorize.c +++ b/colorize.c @@ -488,6 +488,10 @@ print_tstamp (FILE *log) } #endif +#define DUP_CONFIG() \ + *conf_file = xstrdup (optarg); \ + break; + #define PRINT_HELP_EXIT() \ print_help (); \ exit (EXIT_SUCCESS); @@ -502,7 +506,7 @@ static void process_opts (int argc, char **argv, char **conf_file) { int opt; - while ((opt = getopt_long (argc, argv, "hV", long_opts, NULL)) != -1) + while ((opt = getopt_long (argc, argv, "c:hV", long_opts, NULL)) != -1) { switch (opt) { @@ -520,8 +524,7 @@ process_opts (int argc, char **argv, char **conf_file) clean_all = true; break; case OPT_CONFIG: - *conf_file = xstrdup (optarg); - break; + DUP_CONFIG (); case OPT_EXCLUDE_RANDOM: opts_set |= OPT_EXCLUDE_RANDOM_SET; opts_arg.exclude_random = xstrdup (optarg); @@ -537,6 +540,8 @@ process_opts (int argc, char **argv, char **conf_file) ABORT_TRACE (); } break; + case 'c': + DUP_CONFIG (); case 'h': PRINT_HELP_EXIT (); case 'V': @@ -799,7 +804,7 @@ print_help (void) }; const struct opt_data opts_data[] = { { "attr", NULL, "=ATTR1,ATTR2,..." }, - { "config", NULL, "=PATH" }, + { "config", "c", "=PATH" }, { "exclude-random", NULL, "=COLOR" }, { "help", "h", NULL }, { "version", "V", NULL }, @@ -839,9 +844,12 @@ print_help (void) if (opt_data) { if (opt_data->short_opt) - printf ("\t\t-%s, --%s\n", opt_data->short_opt, opt->name); + printf ("\t\t-%s, --%s", opt_data->short_opt, opt->name); else - printf ("\t\t --%s%s\n", opt->name, opt_data->arg); + printf ("\t\t --%s", opt->name); + if (opt_data->arg) + printf ("%s", opt_data->arg); + printf ("\n"); } else printf ("\t\t --%s\n", opt->name); diff --git a/t/conf/param.t b/t/conf/param.t index 3fe5436..520df2b 100755 --- a/t/conf/param.t +++ b/t/conf/param.t @@ -8,7 +8,7 @@ use Colorize::Common qw(:defaults $write_to_tmpfile); use File::Temp qw(tmpnam); use Test::More; -my $tests = 1; +my $tests = 2; my $conf = <<'EOT'; attr=bold @@ -16,6 +16,14 @@ color=blue omit-color-empty=yes EOT +my $expected = <<"EOT"; +\e[1;34mfoo\e[0m + +\e[1;34mbar\e[0m + +\e[1;34mbaz\e[0m +EOT + plan tests => $tests; SKIP: { @@ -35,13 +43,9 @@ EOT print {$fh} $conf; close($fh); - is(qx($program --config=$conf_file $infile), <<"EOT", 'config param'); -\e[1;34mfoo\e[0m - -\e[1;34mbar\e[0m + is(qx($program -c $conf_file $infile), $expected, 'short option'); + is(qx($program --config=$conf_file $infile), $expected, 'long option'); -\e[1;34mbaz\e[0m -EOT unlink $program; unlink $conf_file; } -- cgit v1.2.3