tools: display error context when it exists (#749)

For example when failing to decode toml, display the context around the
error and the location of the problem.
This commit is contained in:
Thomas Pelletier
2022-04-07 20:33:09 -04:00
committed by GitHub
parent 9804fc57e0
commit 88a8aecdd4
2 changed files with 29 additions and 1 deletions
+16
View File
@@ -10,6 +10,7 @@ import (
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -47,6 +48,21 @@ func TestProcessMainStdinErr(t *testing.T) {
assert.NotEmpty(t, stderr.String())
}
func TestProcessMainStdinDecodeErr(t *testing.T) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
input := strings.NewReader("this is the input")
exit := processMain([]string{}, input, stdout, stderr, func(r io.Reader, w io.Writer) error {
var v interface{}
return toml.Unmarshal([]byte(`qwe = 001`), &v)
})
assert.Equal(t, -1, exit)
assert.Empty(t, stdout.String())
assert.Contains(t, stderr.String(), "error occurred at")
}
func TestProcessMainFileExists(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "example")
require.NoError(t, err)