From 65a91c0137c4eb10d05b02ab659b031992eff29f Mon Sep 17 00:00:00 2001 From: Jan Heuermann Date: Fri, 6 Oct 2023 10:50:43 +0200 Subject: [PATCH 1/2] Add test for error output --- klog/app/cli/main/cli_test.go | 22 ++++++++++++++++++++++ klog/app/cli/main/testutil_test.go | 3 ++- klog/app/error.go | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/klog/app/cli/main/cli_test.go b/klog/app/cli/main/cli_test.go index 69247e3..7d87f29 100644 --- a/klog/app/cli/main/cli_test.go +++ b/klog/app/cli/main/cli_test.go @@ -23,6 +23,28 @@ func TestHandleInputFiles(t *testing.T) { assert.True(t, strings.Contains(out[1], "#foo 2h"), out) } +func TestPrintAppErrors(t *testing.T) { + out := (&Env{ + files: map[string]string{ + "invalid.klg": "2020-01-01asdf", + "valid.klg": "2020-01-01", + }, + }).run( + []string{"print", "invalid.klg"}, + []string{"start", "valid.klg"}, + []string{"start", "valid.klg"}, + ) + // Out 0 should contain pretty-printed parsing errors. + assert.True(t, strings.Contains(out[0], "ERROR in line 1:"), out) + assert.True(t, strings.Contains(out[0], "2020-01-01asdf"), out) + assert.True(t, strings.Contains(out[0], "^^^^^^^^^^^^^^"), out) + assert.True(t, strings.Contains(out[0], "Invalid date"), out) + // Out 1 should go through without errors. + // Out 2 should then display logical error, since there is an open-range already. + assert.True(t, strings.Contains(out[2], "Error: Manipulation failed"), out) + assert.True(t, strings.Contains(out[2], "There is already an open range in this record"), out) +} + func TestBookmarkFile(t *testing.T) { klog := &Env{ files: map[string]string{ diff --git a/klog/app/cli/main/testutil_test.go b/klog/app/cli/main/testutil_test.go index 2fcd815..54fe8f5 100644 --- a/klog/app/cli/main/testutil_test.go +++ b/klog/app/cli/main/testutil_test.go @@ -2,6 +2,7 @@ package klog import ( "github.com/jotaen/klog/klog/app" + "github.com/jotaen/klog/klog/app/cli/lib" "github.com/jotaen/klog/klog/app/cli/lib/terminalformat" "io" "os" @@ -42,7 +43,7 @@ func (e *Env) run(invocation ...[]string) []string { _ = w.Close() if runErr != nil { - outs[i] = runErr.Error() + outs[i] = lib.PrettifyError(runErr, false).Error() continue } out, _ := io.ReadAll(r) diff --git a/klog/app/error.go b/klog/app/error.go index 2608d5d..a106bad 100644 --- a/klog/app/error.go +++ b/klog/app/error.go @@ -98,11 +98,11 @@ func NewParserErrors(errs []txt.Error) ParserErrors { } func (pe parserErrors) Error() string { - return fmt.Sprintf("%d parsing errors", len(pe.errors)) + return fmt.Sprintf("%d parsing error(s)", len(pe.errors)) } func (pe parserErrors) Details() string { - return fmt.Sprintf("%d parsing errors", len(pe.errors)) + return fmt.Sprintf("%d parsing error(s)", len(pe.errors)) } func (pe parserErrors) Original() error { From c695c400c27d911f29fd6fa38b2ebdfdde361191 Mon Sep 17 00:00:00 2001 From: Jan Heuermann Date: Fri, 6 Oct 2023 11:01:21 +0200 Subject: [PATCH 2/2] Downgrade kong to 0.7.1 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c4d0c70..2779989 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( cloud.google.com/go v0.110.8 - github.com/alecthomas/kong v0.8.0 + github.com/alecthomas/kong v0.7.1 github.com/jotaen/genie v0.0.1 github.com/jotaen/kong-completion v0.0.5 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 diff --git a/go.sum b/go.sum index 9316a45..ef3f837 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ cloud.google.com/go v0.110.8 h1:tyNdfIxjzaWctIiLYOTalaLKZ17SI44SKFW26QbOhME= cloud.google.com/go v0.110.8/go.mod h1:Iz8AkXJf1qmxC3Oxoep8R1T36w8B92yU29PcBhHO5fk= github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0= github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA= -github.com/alecthomas/kong v0.8.0 h1:ryDCzutfIqJPnNn0omnrgHLbAggDQM2VWHikE1xqK7s= -github.com/alecthomas/kong v0.8.0/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= +github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4= +github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U= github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE= github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=