From 9dd2a49d9b0b57c748254ea5f84adcc3f6fe263d Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Wed, 19 Oct 2022 19:32:41 +0200 Subject: [PATCH] adapted version generation to cfg module, added and fixed unit tests --- CHANGELOG.md | 28 +++++++++++++++- Makefile | 4 +-- cfg/config.go | 11 +++---- cfg/config_test.go | 80 ++++++++++++++++++++++++++++++++++++++++++++- cmd/root.go | 3 +- lib/parser_test.go | 24 ++++++++++---- lib/printer_test.go | 6 ++-- 7 files changed, 135 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dc552a..6f78b5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,34 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org). +## [v1.0.11](https://github.com/TLINDEN/tablizer/tree/v1.0.11) - 2022-10-19 + +[Full Changelog](https://github.com/TLINDEN/tablizer/compare/v1.0.10...v1.0.11) + +### Added + +- Added CI job golinter to regularly check for common mistakes. + +- Added YAML output mode. + +- Added more unit tests, we're over 95% in the lib module. + +### Changed + +- do not use any global variables anymore, makes the code easier to + maintain, understand and test + +- using io.Writer in print* functions, which is easier to test, also + re-implemented the print tests. + +- replaced go-str2duration with my own implementation `duration2int()`. + + + ## [v1.0.10](https://github.com/TLINDEN/tablizer/tree/v1.0.10) - 2022-10-15 +[Full Changelog](https://github.com/TLINDEN/tablizer/compare/v1.0.9...v1.0.10) + ### Added - Added various sort modes: sort by time, by duration, numerical (-a -t -i) @@ -44,7 +70,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a ### Added -- Added sort support with the new parameter -k (like sort(1). +- Added sort support with the new parameter -k (like sort(1)). diff --git a/Makefile b/Makefile index c92bee7..879de3b 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ # # no need to modify anything below tool = tablizer -version = $(shell egrep "= .v" lib/common.go | cut -d'=' -f2 | cut -d'"' -f 2) +version = $(shell egrep "= .v" cfg/config.go | cut -d'=' -f2 | cut -d'"' -f 2) archs = android darwin freebsd linux netbsd openbsd windows PREFIX = /usr/local UID = root @@ -42,7 +42,7 @@ cmd/%.go: %.pod echo "\`" >> cmd/$*.go buildlocal: - go build -ldflags "-X 'github.com/tlinden/tablizer/lib.VERSION=$(VERSION)'" + go build -ldflags "-X 'github.com/tlinden/tablizer/cfg.VERSION=$(VERSION)'" release: ./mkrel.sh $(tool) $(version) diff --git a/cfg/config.go b/cfg/config.go index 7556b78..5b5c5a1 100644 --- a/cfg/config.go +++ b/cfg/config.go @@ -24,7 +24,7 @@ import ( ) const DefaultSeparator string = `(\s\s+|\t)` -const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml)" +const ValidOutputModes string = "(orgtbl|markdown|extended|ascii|yaml|shell)" const Version string = "v1.0.11" var VERSION string // maintained by -x @@ -117,17 +117,14 @@ func (conf *Config) PrepareModeFlags(flag Modeflag, mode string) error { conf.OutputMode = "ascii" } } else { - r, err := regexp.Compile(ValidOutputModes) - - if err != nil { - return errors.New("Failed to validate output mode spec!") - } - + r, _ := regexp.Compile(ValidOutputModes) // hardcoded, no fail expected match := r.MatchString(mode) if !match { return errors.New("Invalid output mode!") } + + conf.OutputMode = mode } return nil diff --git a/cfg/config_test.go b/cfg/config_test.go index b2f1df8..56b2f23 100644 --- a/cfg/config_test.go +++ b/cfg/config_test.go @@ -17,4 +17,82 @@ along with this program. If not, see . package cfg -// FIXME: add tests +import ( + "fmt" + // "reflect" + "testing" +) + +func TestPrepareModeFlags(t *testing.T) { + var tests = []struct { + flag Modeflag + mode string // input, if any + expect string // output + want bool + }{ + // short commandline flags like -M + {Modeflag{X: true}, "", "extended", false}, + {Modeflag{S: true}, "", "shell", false}, + {Modeflag{O: true}, "", "orgtbl", false}, + {Modeflag{Y: true}, "", "yaml", false}, + {Modeflag{M: true}, "", "markdown", false}, + {Modeflag{}, "", "ascii", false}, + + // long flags like -o yaml + {Modeflag{}, "extended", "extended", false}, + {Modeflag{}, "shell", "shell", false}, + {Modeflag{}, "orgtbl", "orgtbl", false}, + {Modeflag{}, "yaml", "yaml", false}, + {Modeflag{}, "markdown", "markdown", false}, + + // failing + {Modeflag{}, "blah", "", true}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("PrepareModeFlags-flags-mode-%s-expect-%s-want-%t", + tt.mode, tt.expect, tt.want) + t.Run(testname, func(t *testing.T) { + c := Config{OutputMode: tt.mode} + + // check either flag or pre filled mode, whatever is defined in tt + err := c.PrepareModeFlags(tt.flag, tt.mode) + if err != nil { + if !tt.want { + // expect to fail + t.Fatalf("PrepareModeFlags returned unexpected error: %s", err) + } + } else { + if c.OutputMode != tt.expect { + t.Errorf("got: %s, expect: %s", c.OutputMode, tt.expect) + } + } + }) + } +} + +func TestPrepareSortFlags(t *testing.T) { + var tests = []struct { + flag Sortmode + expect string // output + }{ + // short commandline flags like -M + {Sortmode{Numeric: true}, "numeric"}, + {Sortmode{Age: true}, "duration"}, + {Sortmode{Time: true}, "time"}, + {Sortmode{}, "string"}, + } + + for _, tt := range tests { + testname := fmt.Sprintf("PrepareSortFlags-expect-%s", tt.expect) + t.Run(testname, func(t *testing.T) { + c := Config{} + + c.PrepareSortFlags(tt.flag) + + if c.SortMode != tt.expect { + t.Errorf("got: %s, expect: %s", c.SortMode, tt.expect) + } + }) + } +} diff --git a/cmd/root.go b/cmd/root.go index 8a4b592..58ef950 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -61,6 +61,7 @@ func Execute() { RunE: func(cmd *cobra.Command, args []string) error { if ShowVersion { fmt.Println(cfg.Getversion()) + return nil } if ShowManual { @@ -112,7 +113,7 @@ func Execute() { _ = rootCmd.Flags().MarkHidden("yaml") // same thing but more common, takes precedence over above group - rootCmd.PersistentFlags().StringVarP(&Outputmode, "output", "o", "ascii", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)") + rootCmd.PersistentFlags().StringVarP(&Outputmode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)") err := rootCmd.Execute() if err != nil { diff --git a/lib/parser_test.go b/lib/parser_test.go index 332c52f..154f5a0 100644 --- a/lib/parser_test.go +++ b/lib/parser_test.go @@ -67,6 +67,7 @@ func TestParserPatternmatching(t *testing.T) { entries [][]string pattern string invert bool + want bool }{ { entries: [][]string{ @@ -86,6 +87,15 @@ func TestParserPatternmatching(t *testing.T) { pattern: "ig", invert: true, }, + { + entries: [][]string{ + { + "asd", "igig", "cxxxncnc", + }, + }, + pattern: "[a-z", + want: true, + }, } table := `ONE TWO THREE @@ -101,12 +111,14 @@ asd igig cxxxncnc gotdata, err := parseFile(c, readFd, tt.pattern) if err != nil { - t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata) - } - - if !reflect.DeepEqual(tt.entries, gotdata.entries) { - t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n", - tt.pattern, tt.invert, tt.entries, gotdata.entries) + if !tt.want { + t.Errorf("Parser returned error: %s\nData processed so far: %+v", err, gotdata) + } + } else { + if !reflect.DeepEqual(tt.entries, gotdata.entries) { + t.Errorf("Parser returned invalid data (pattern: %s, invert: %t)\nExp: %+v\nGot: %+v\n", + tt.pattern, tt.invert, tt.entries, gotdata.entries) + } } }) } diff --git a/lib/printer_test.go b/lib/printer_test.go index 0f1c6f6..dbb1f1f 100644 --- a/lib/printer_test.go +++ b/lib/printer_test.go @@ -91,13 +91,13 @@ ceta 33d12h 9 06/Jan/2008 15:04:05 -0700`, name: "default", mode: "orgtbl", expect: ` -|---------+-------------+----------+----------------------------| ++---------+-------------+----------+----------------------------+ | NAME(1) | DURATION(2) | COUNT(3) | WHEN(4) | -|---------+-------------+----------+----------------------------| ++---------+-------------+----------+----------------------------+ | beta | 1d10h5m1s | 33 | 3/1/2014 | | alpha | 4h35m | 170 | 2013-Feb-03 | | ceta | 33d12h | 9 | 06/Jan/2008 15:04:05 -0700 | -|---------+-------------+----------+----------------------------|`, ++---------+-------------+----------+----------------------------+`, }, { name: "default",