From ceae80c91ce08cb0de3aa74a637f5fd1d50a5994 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Mon, 9 Jan 2023 12:54:45 +0100 Subject: [PATCH] fix invalid arg handling (io, stdin) and add tests for this --- Makefile | 1 + lib/io.go | 60 +++++++++++++++++++++++++++++++++-------------------- t/test.sh | 45 ++++++++++++++++++++++++++++++++++++++++ t/testtable | 6 ++++++ 4 files changed, 89 insertions(+), 23 deletions(-) create mode 100755 t/test.sh create mode 100644 t/testtable diff --git a/Makefile b/Makefile index 8029f15..2064c0a 100644 --- a/Makefile +++ b/Makefile @@ -67,6 +67,7 @@ clean: test: go test -v ./... + bash t/test.sh singletest: @echo "Call like this: ''make singletest TEST=TestPrepareColumns MOD=lib" diff --git a/lib/io.go b/lib/io.go index 1af9059..bd59bbf 100644 --- a/lib/io.go +++ b/lib/io.go @@ -55,40 +55,54 @@ func ProcessFiles(c *cfg.Config, args []string) error { func determineIO(c *cfg.Config, args []string) ([]io.Reader, string, error) { var pattern string var fds []io.Reader - var havefiles bool + var haveio bool - if len(args) > 0 { - // threre were args left, take a look - if _, err := os.Stat(args[0]); err != nil { - // first one is not a file, consider it as regexp and - // shift arg list + stat, _ := os.Stdin.Stat() + if (stat.Mode() & os.ModeCharDevice) == 0 { + // we're reading from STDIN, which takes precedence over file args + fds = append(fds, os.Stdin) + if len(args) > 0 { + // ignore any args > 1 pattern = args[0] c.Pattern = args[0] // used for colorization by printData() - args = args[1:] } - + haveio = true + } else { if len(args) > 0 { - // only files - for _, file := range args { - fd, err := os.OpenFile(file, os.O_RDONLY, 0755) - - if err != nil { - return nil, "", err + // threre were args left, take a look + if args[0] == "-" { + // in traditional unix programs a dash denotes STDIN (forced) + fds = append(fds, os.Stdin) + haveio = true + } else { + if _, err := os.Stat(args[0]); err != nil { + // first one is not a file, consider it as regexp and + // shift arg list + pattern = args[0] + c.Pattern = args[0] // used for colorization by printData() + args = args[1:] } - fds = append(fds, fd) + if len(args) > 0 { + // consider any other args as files + for _, file := range args { + + fd, err := os.OpenFile(file, os.O_RDONLY, 0755) + + if err != nil { + return nil, "", err + } + + fds = append(fds, fd) + haveio = true + } + } } - havefiles = true } } - if !havefiles { - stat, _ := os.Stdin.Stat() - if (stat.Mode() & os.ModeCharDevice) == 0 { - fds = append(fds, os.Stdin) - } else { - return nil, "", errors.New("No file specified and nothing to read on stdin!") - } + if !haveio { + return nil, "", errors.New("No file specified and nothing to read on stdin!") } return fds, pattern, nil diff --git a/t/test.sh b/t/test.sh new file mode 100755 index 0000000..dad383b --- /dev/null +++ b/t/test.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +# simple commandline unit test script + +t="../tablizer" +fail=0 + +ex() { + # execute a test, report+exit on error, stay silent otherwise + log="/tmp/test-tablizer.$$.log" + name=$1 + shift + + echo -n "TEST $name " + + $* > $log 2>&1 + + if test $? -ne 0; then + echo "failed, see $log" + fail=1 + else + echo "ok" + rm -f $log + fi +} + +# only use files in test dir +cd $(dirname $0) + +echo "Executing commandline tests ..." + +# io pattern tests +ex io-pattern-and-file $t bk7 testtable.kube +cat testtable.kube | ex io-pattern-and-stdin $t bk7 +cat testtable.kube | ex io-pattern-and-stdin-dash $t bk7 - + +# same w/o pattern +ex io-just-file $t testtable.kube +cat testtable.kube | ex io-just-stdin $t +cat testtable.kube | ex io-just-stdin-dash $t - + +if test $fail -ne 0; then + echo "!!! Some tests failed !!!" + exit 1 +fi diff --git a/t/testtable b/t/testtable new file mode 100644 index 0000000..6f64f3e --- /dev/null +++ b/t/testtable @@ -0,0 +1,6 @@ +NAME READY STATUS RESTARTS AGE +alertmanager-kube-prometheus-alertmanager-0 2/2 Running 35 (45m ago) 11d +grafana-fcc54cbc9-bk7s8 1/1 Running 17 (45m ago) 1d +kube-prometheus-blackbox-exporter-5d85b5d8f4-tskh7 1/1 Running 17 (45m ago) 1h44m +kube-prometheus-kube-state-metrics-b4cd9487-75p7f 1/1 Running 20 (45m ago) 45m +kube-prometheus-node-exporter-bfzpl 1/1 Running 17 (45m ago) 54s