From 1a6b9937b00a3d60dedc5ace1a72f9aaf92d9b25 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Wed, 8 Nov 2017 21:17:52 +0100 Subject: add test suite --- Makefile | 3 ++ t/necho.t | 46 ++++++++++++++++++++++++++ t/qecho.t | 40 ++++++++++++++++++++++ t/secho.t | 41 +++++++++++++++++++++++ t/tap3 | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ t/zecho.t | 46 ++++++++++++++++++++++++++ 6 files changed, 288 insertions(+) create mode 100755 t/necho.t create mode 100755 t/qecho.t create mode 100755 t/secho.t create mode 100755 t/tap3 create mode 100755 t/zecho.t diff --git a/Makefile b/Makefile index 8e656e0..c70f5ee 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ $(LINKS): necho $(LINKS:=.1): necho.1 ln -sf necho.1 $@ +check: FRC all + prove -v + install: FRC all mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(MANDIR)/man1 install -m0755 $(ALL) $(DESTDIR)$(BINDIR) diff --git a/t/necho.t b/t/necho.t new file mode 100755 index 0000000..27493ba --- /dev/null +++ b/t/necho.t @@ -0,0 +1,46 @@ +#!/bin/sh +export "PATH=.:t:$PATH" + +printf '1..6\n' +printf '# necho\n' + +tap3 "single string" <<'EOF' +necho hello +>>> +hello +EOF + +tap3 "two strings" <<'EOF' +necho hello random +>>> +hello +random +EOF + +tap3 "no arguments" <<'EOF' +necho +>>> +EOF + +tap3 "empty argument" <<'EOF' +necho foo '' bar +>>> +foo + +bar +EOF + +tap3 "string with minus" <<'EOF' +necho -n -e +>>> +-n +-e +EOF + +tap3 "string with double minus" <<'EOF' +necho -n -- -e +>>> +-n +-- +-e +EOF diff --git a/t/qecho.t b/t/qecho.t new file mode 100755 index 0000000..665d5be --- /dev/null +++ b/t/qecho.t @@ -0,0 +1,40 @@ +#!/bin/sh +export "PATH=.:t:$PATH" + +printf '1..6\n' +printf '# qecho\n' + +tap3 "single string" <<'EOF' +qecho hello +>>> +»hello« +EOF + +tap3 "two strings" <<'EOF' +qecho hello random +>>> +»hello« »random« +EOF + +tap3 "no arguments" <<'EOF' +qecho +>>> +EOF + +tap3 "empty argument" <<'EOF' +qecho foo '' bar +>>> +»foo« »« »bar« +EOF + +tap3 "string with minus" <<'EOF' +qecho -n -e +>>> +»-n« »-e« +EOF + +tap3 "string with double minus" <<'EOF' +qecho -n -- -e +>>> +»-n« »--« »-e« +EOF diff --git a/t/secho.t b/t/secho.t new file mode 100755 index 0000000..5b982e1 --- /dev/null +++ b/t/secho.t @@ -0,0 +1,41 @@ +#!/bin/sh +export "PATH=.:t:$PATH" + +printf '1..6\n' +printf '# secho\n' + +tap3 "single string" <<'EOF' +secho hello +>>> +hello +EOF + +tap3 "two strings" <<'EOF' +secho hello random +>>> +hello random +EOF + +tap3 "no arguments" <<'EOF' +secho +>>> + +EOF + +tap3 "empty argument" <<'EOF' +secho foo '' bar +>>> +foo bar +EOF + +tap3 "string with minus" <<'EOF' +secho -n -e +>>> +-n -e +EOF + +tap3 "string with double minus" <<'EOF' +secho -n -- -e +>>> +-n -- -e +EOF diff --git a/t/tap3 b/t/tap3 new file mode 100755 index 0000000..bf6f564 --- /dev/null +++ b/t/tap3 @@ -0,0 +1,112 @@ +#!/usr/bin/env perl +# tap3 [DESC] - check output/error/status of a command against a specification +# +# A tiny variant of shelltestrunner (format v1), just takes one test +# case and outputs a TAP line. +# +# Input format: +# +# CMD +# <<< +# INPUT +# >>> +# OUTPUT +# >>> /OUTPUT REGEX/ +# >>>2 +# STDERR +# >>>2 /STDERR REGEX/ +# >>>= STATUS +# >>>= !STATUS +# +# All but CMD are optional and can be put in any order, +# Regex variants can be repeated, all patterns must match. +# By default, STATUS is set to 0 and STDERR assumed empty. +# +# To the extent possible under law, the creator of this work has waived +# all copyright and related or neighboring rights to this work. +# http://creativecommons.org/publicdomain/zero/1.0/ + +use strict; +use warnings; +use Symbol 'gensym'; +use IPC::Open3; + +my $cmd = ""; +my ($input, $output, @output_rx, $stderr, @stderr_rx, $status, $status_not); +my $ignored = ""; + +my $var = \$cmd; +while () { + if (/^#!? /) { next; } + if (/^<<<$/) { $var = \$input; $input = ""; next; } + if (/^>>>$/) { $var = \$output; $output = ""; next; } + if (/^>>>2$/) { $var = \$stderr; $stderr = ""; next; } + if (/^>>>\s*\/(.*)\/$/) { push @output_rx, $1; next; } + if (/^>>>2\s*\/(.*)\/$/) { push @stderr_rx, $1; next; } + if (/^>>>=\s+(\d+)$/) { $var = \$ignored; $status = $1; next; } + if (/^>>>=\s+!(\d+)$/) { $var = \$ignored; $status_not = $1; next; } + $$var .= $_; +} + +chomp($cmd); +die "No command to check given\n" if !$cmd; + +my ($wtr, $rdr); +my $err = gensym; +my $pid = open3($wtr, $rdr, $err, "/bin/sh", "-c", $cmd); + +my $desc = shift || $cmd; +$desc =~ s/\n.*//; + +print $wtr $input if (defined($input)); +close $wtr; +my $real_output = do { local $/; <$rdr>; }; +my $real_stderr = do { local $/; <$err>; }; +waitpid($pid, 0); +my $real_status = $? >> 8; + +my $r = 0; + +sub not_ok { + print "not ok - $desc\n" if (!$r); + $r = 1; + $_[0] =~ s/^/# /mg; + print $_[0]; +} + +if (defined($output) && $real_output ne $output) { + not_ok("wrong output:\n$real_output"); +} +for my $rx (@output_rx) { + if ($real_output !~ $rx) { + not_ok("output doesn't match /$rx/:\n$real_output\n"); + } +} +if (defined($stderr) && $real_stderr ne $stderr) { + not_ok("wrong stderr:\n$real_stderr"); +} +for my $rx (@stderr_rx) { + if ($real_stderr !~ $rx) { + not_ok("stderr doesn't match /$rx/:\n$real_stderr\n"); + } +} +if (!defined($stderr) && !@stderr_rx && + !defined($status) && !defined($status_not) && + $real_stderr) { + not_ok("output to stderr:\n$real_stderr\n"); +} +if (defined($status) && $real_status != $status) { + not_ok("wrong status: $real_status (expected $status)\n"); +} +if (defined($status_not) && $real_status == $status_not) { + not_ok("wrong status: $real_status (expected anything else)\n"); +} +if (!defined($status) && !defined($status_not) && + !defined($stderr) && !@stderr_rx && + $real_status != 0) { + not_ok("wrong status: $real_status (command failed)\n"); +} + +print "ok - $desc\n" if (!$r); + +exit $r; diff --git a/t/zecho.t b/t/zecho.t new file mode 100755 index 0000000..dc2b00b --- /dev/null +++ b/t/zecho.t @@ -0,0 +1,46 @@ +#!/bin/sh +export "PATH=.:t:$PATH" + +printf '1..6\n' +printf '# zecho\n' + +tap3 "single string" <<'EOF' +zecho hello | tr '\000\012X' '\012X' +>>> +hello +EOF + +tap3 "two strings" <<'EOF' +zecho hello random | tr '\000\012X' '\012X' +>>> +hello +random +EOF + +tap3 "no arguments" <<'EOF' +zecho | tr '\000\012X' '\012X' +>>> +EOF + +tap3 "empty argument" <<'EOF' +zecho foo '' bar | tr '\000\012X' '\012X' +>>> +foo + +bar +EOF + +tap3 "string with minus" <<'EOF' +zecho -n -e | tr '\000\012X' '\012X' +>>> +-n +-e +EOF + +tap3 "string with double minus" <<'EOF' +zecho -n -- -e | tr '\000\012X' '\012X' +>>> +-n +-- +-e +EOF -- cgit 1.4.1