diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-21 18:55:43 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-21 18:55:43 +0300 |
commit | f0b082d3be6126ee0f6af85d9b51c31b4bc8c4d4 (patch) | |
tree | 5c65dd44ab69df67e7b60a56cd407a369104aa52 | |
parent | ec78df167efca7feba360b29700ef052eccf4b69 (diff) | |
download | posixruncapture-f0b082d3be6126ee0f6af85d9b51c31b4bc8c4d4.tar.gz posixruncapture-f0b082d3be6126ee0f6af85d9b51c31b4bc8c4d4.tar.bz2 |
Write more tests.
* runcap: Update.
* Makefile.PL (test): Generate custom subdirs-test_ rules.
* capture.c (capture_new): Fix type checking.
* t/POSIX-Run-Capture.t: Rename to t/00use.t
* t/01simple.t: New file.
* t/02lines.t: New file.
* t/03two.t: New file.
* t/TestCapture.pm: New file.
-rw-r--r-- | Makefile.PL | 9 | ||||
-rw-r--r-- | capture.c | 33 | ||||
m--------- | runcap | 0 | ||||
-rw-r--r-- | t/00use.t (renamed from t/POSIX-Run-Capture.t) | 10 | ||||
-rw-r--r-- | t/01simple.t | 14 | ||||
-rw-r--r-- | t/02lines.t | 17 | ||||
-rw-r--r-- | t/03two.t | 23 | ||||
-rw-r--r-- | t/TestCapture.pm | 152 |
8 files changed, 240 insertions, 18 deletions
diff --git a/Makefile.PL b/Makefile.PL index 004a192..86b969f 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -43,13 +43,14 @@ sub test { my $val = shift->SUPER::test(@_); open(my $fd, '<', \$val); my @ret; my $ignore; while (<$fd>) { chomp; - if (/^subdirs-test_.*$/) { + if (/^(subdirs-test_.+?)\s*:.*$/) { + push @ret, "$1:: subdirs-test-prepare", ''; $ignore = 1; } elsif ($ignore) { if (/^$/) { $ignore = 0; } elsif (/^\S/) { $ignore = 0; @@ -57,8 +58,14 @@ sub test { } } else { push @ret, $_; } } close $fd; + + push @ret, '', + "subdirs-test-prepare::", + "\t\@\$(MAKE) -C runcap/t genout O='\$(CCFLAGS)'", + ''; + return join("\n", @ret); } @@ -64,13 +64,15 @@ capture_new(AV *av, unsigned timeout, SV *cb[2]) goto nomem; for (i = 0; i <= n; i++) { SV *sv, **psv = av_fetch(av, i, 0); if (!psv) croak("element %d doesn't exist", i); sv = *psv; - if (SvTYPE(sv) == SVt_PV) { + if (SvROK(sv)) { + croak("argument #%d is not a scalar", i); + } else { char *s = SvPV_nolen(sv); if ((argv[i] = strdup(s)) == NULL) goto nomem; } } argv[i] = NULL; @@ -145,25 +147,30 @@ capture_next_line(struct capture *cp, int fd) return realloc(buf, n + 1); } int capture_run(struct capture *cp) { + int res; + if (!cp->rc.rc_argv) croak("no command line given"); - return runcap(&cp->rc, cp->flags); -} + res = runcap(&cp->rc, cp->flags); -void -capture_wd() -{ - volatile int _st=0; - printf("ENTER\n"); - while (!_st) { - printf("WAIING\n"); - sleep(1); - _st = _st; + if (cp->flags & RCF_STDOUT_LINEMON && cp->closure[0].len) { + call_monitor(cp->closure[0].cv, + cp->closure[0].str, + cp->closure[0].len); + cp->closure[0].len = 0; } - printf("EXIT\n"); + + if (cp->flags & RCF_STDERR_LINEMON && cp->closure[1].len) { + call_monitor(cp->closure[1].cv, + cp->closure[1].str, + cp->closure[1].len); + cp->closure[1].len = 0; + } + + return res; } diff --git a/runcap b/runcap -Subproject 0c0ae29bc7a71fc2cb6d0acf403b1e72715fbe4 +Subproject 969dab4c1af666d381124fd481821bc936fc94e diff --git a/t/POSIX-Run-Capture.t b/t/00use.t index 9b57812..aefe48e 100644 --- a/t/POSIX-Run-Capture.t +++ b/t/00use.t @@ -1,18 +1,20 @@ # Before 'make install' is performed this script should be runnable with # 'make test'. After 'make install' it should work as 'perl POSIX-Run-Capture.t' ######################### -# change 'tests => 1' to 'tests => last_test_to_print'; - use strict; use warnings; -use Test::More tests => 1; -BEGIN { use_ok('POSIX::Run::Capture') }; +use Test::More tests => 3; +BEGIN { use POSIX::Run::Capture }; +ok(eval { new POSIX::Run::Capture(['cat', 'file']) }); +ok(eval { new POSIX::Run::Capture(argv => ['cat', 'file'], timeout => 3) }); +ok(!eval { new POSIX::Run::Capture(1, 2) }); + ######################### # Insert your test code below, the Test::More module is use()ed here so read # its man page ( perldoc Test::More ) for help writing this test script. diff --git a/t/01simple.t b/t/01simple.t new file mode 100644 index 0000000..36b4221 --- /dev/null +++ b/t/01simple.t @@ -0,0 +1,14 @@ +use lib 't'; + +use strict; +use warnings; +use TestCapture; +use Test::More tests => 1; + +our($catbin, $input); + +ok(TestCapture([[$catbin, $input]], + stdout => { nlines => 71, length => 4051 }, + stderr => { nlines => 0, length => 0 })); + + diff --git a/t/02lines.t b/t/02lines.t new file mode 100644 index 0000000..917921e --- /dev/null +++ b/t/02lines.t @@ -0,0 +1,17 @@ +use lib 't'; + +use strict; +use warnings; +use TestCapture; +use Test::More tests => 1; + +our($catbin, $input, $content); + +ok(TestCapture([[$catbin, $input]], + stdout => { + nlines => 71, + length => 4051, + content => $content + })); + + diff --git a/t/03two.t b/t/03two.t new file mode 100644 index 0000000..9b0ce0f --- /dev/null +++ b/t/03two.t @@ -0,0 +1,23 @@ +use lib 't'; + +use strict; +use warnings; +use TestCapture; +use Test::More tests => 1; + +our($catbin, $input, $content); + +ok(TestCapture([[$catbin, '-l', 337, '-o', $input, '-s', 628, '-l', 734, '-e', $input]], + stdout => { + nlines => 8, + length => 337, + content => substr($content, 0, 337) + }, + stderr => { + nlines => 11, + length => 734, + content => substr($content, 628, 734) + } + )); + + diff --git a/t/TestCapture.pm b/t/TestCapture.pm new file mode 100644 index 0000000..fda48d8 --- /dev/null +++ b/t/TestCapture.pm @@ -0,0 +1,152 @@ +use strict; +use warnings; +use Carp; +use Cwd qw(abs_path); +use File::Basename qw(dirname); +use POSIX::Run::Capture qw(:std); +use POSIX ":sys_wait_h"; + +my $dir = dirname(dirname(abs_path(__FILE__))); + +our $catbin = $dir . '/runcap/t/genout'; +our $input = $dir . '/runcap/t/INPUT'; + +sub mismatch { + my ($what, $exp, $got) = @_; + diag("$what mismatch: $exp <=> $got"); +} + +sub test_stream { + my ($cap, $kw, $fd) = @_; + + if (exists($kw->{length}) && $kw->{length} != $cap->length($fd)) { + mismatch("stdout length", $kw->{length}, $cap->length($fd)); + return 0; + } + if (exists($kw->{nlines}) && $kw->{nlines} != $cap->nlines($fd)) { + mismatch("number of stdout lines", $kw->{nlines}, $cap->nlines($fd)); + return 0; + } + if (exists($kw->{content})) { + my $lines = $cap->get_lines($fd); + my $content = join('',@$lines); + if ($kw->{content} ne $content) { + diag("stdout content mismatch"); + return 0; + } + } + return 1; +} + +sub TestCapture { + my ($argv, %kw) = @_; + + $kw{result} = 0 unless exists $kw{result}; + $kw{code} = 0 unless exists $kw{code}; + + my $cap = new POSIX::Run::Capture @{$argv}; + + return 0 unless $cap; + + my $res = $cap->run; + + unless ($kw{result} == $res) { + mismatch("result code", $res, $kw{result}); + return 0; + } + + unless (WIFEXITED($cap->status)) { + diag("unexpected termination status: ".$cap->status); + return 0; + } + + unless (WEXITSTATUS($cap->status) == $kw{code}) { + mismatch("exit code", WEXITSTATUS($cap->status), $kw{code}); + return 0; + } + + if (exists($kw{stdout}) && !test_stream($cap, $kw{stdout}, SD_STDOUT)) { + return 0; + } + + if (exists($kw{stderr}) && !test_stream($cap, $kw{stderr}, SD_STDERR)) { + return 0; + } + + return 1; +} + +our $content =<<'_INLINE_'; +CHAPTER I. Down the Rabbit-Hole + +Alice was beginning to get very tired of sitting by her sister on the +bank, and of having nothing to do: once or twice she had peeped into the +book her sister was reading, but it had no pictures or conversations +in it, 'and what is the use of a book,' thought Alice 'without +pictures or conversations?' + +So she was considering in her own mind (as well as she could, for the +hot day made her feel very sleepy and stupid), whether the pleasure of +making a daisy-chain would be worth the trouble of getting up and picking +the daisies, when suddenly a White Rabbit with pink eyes ran close by her. + +There was nothing so very remarkable in that; nor did Alice think it +so very much out of the way to hear the Rabbit say to itself, 'Oh +dear! Oh dear! I shall be late!' (when she thought it over afterwards, +it occurred to her that she ought to have wondered at this, but at the +time it all seemed quite natural); but when the Rabbit actually took a +watch out of its waistcoat-pocket, and looked at it, and then hurried on, +Alice started to her feet, for it flashed across her mind that she had +never before seen a rabbit with either a waistcoat-pocket, or a watch +to take out of it, and burning with curiosity, she ran across the field +after it, and fortunately was just in time to see it pop down a large +rabbit-hole under the hedge. + +In another moment down went Alice after it, never once considering how +in the world she was to get out again. + +The rabbit-hole went straight on like a tunnel for some way, and then +dipped suddenly down, so suddenly that Alice had not a moment to think +about stopping herself before she found herself falling down a very +deep well. + +Either the well was very deep, or she fell very slowly, for she had plenty +of time as she went down to look about her and to wonder what was going +to happen next. First, she tried to look down and make out what she was +coming to, but it was too dark to see anything; then she looked at the +sides of the well, and noticed that they were filled with cupboards +and book-shelves; here and there she saw maps and pictures hung upon +pegs. She took down a jar from one of the shelves as she passed; it was +labelled 'ORANGE MARMALADE', but to her great disappointment it was +empty: she did not like to drop the jar for fear of killing somebody, +so managed to put it into one of the cupboards as she fell past it. + +'Well!' thought Alice to herself, 'after such a fall as this, +I shall think nothing of tumbling down stairs! How brave they'll all +think me at home! Why, I wouldn't say anything about it, even if I +fell off the top of the house!' (Which was very likely true.) + +Down, down, down. Would the fall never come to an end! 'I wonder how +many miles I've fallen by this time?' she said aloud. 'I must be +getting somewhere near the centre of the earth. Let me see: that would be +four thousand miles down, I think--' (for, you see, Alice had learnt +several things of this sort in her lessons in the schoolroom, and though +this was not a very good opportunity for showing off her knowledge, +as there was no one to listen to her, still it was good practice to +say it over) '--yes, that's about the right distance--but then I +wonder what Latitude or Longitude I've got to?' (Alice had no idea +what Latitude was, or Longitude either, but thought they were nice grand +words to say.) + +Presently she began again. 'I wonder if I shall fall right through +the earth! How funny it'll seem to come out among the people that +walk with their heads downward! The Antipathies, I think--' (she was +rather glad there was no one listening, this time, as it didn't sound +at all the right word) '--but I shall have to ask them what the name +of the country is, you know. Please, Ma'am, is this New Zealand or +Australia?' (and she tried to curtsey as she spoke--fancy curtseying +as you're falling through the air! Do you think you could manage +it?) 'And what an ignorant little girl she'll think me for asking! No, +it'll never do to ask: perhaps I shall see it written up somewhere.' +_INLINE_ +; |