mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-16 20:20:57 +01:00
fix invalid arg handling (io, stdin) and add tests for this
This commit is contained in:
1
Makefile
1
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"
|
||||
|
||||
60
lib/io.go
60
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
|
||||
|
||||
45
t/test.sh
Executable file
45
t/test.sh
Executable file
@@ -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
|
||||
6
t/testtable
Normal file
6
t/testtable
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user