adapted version generation to cfg module, added and fixed unit tests

This commit is contained in:
2022-10-19 19:32:41 +02:00
parent 90872e0c60
commit 9dd2a49d9b
7 changed files with 135 additions and 21 deletions

View File

@@ -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)).

View File

@@ -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)

View File

@@ -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

View File

@@ -17,4 +17,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
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)
}
})
}
}

View File

@@ -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 {

View File

@@ -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)
}
}
})
}

View File

@@ -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",