Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance error message for circular references #50

Merged
merged 3 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package properties
// BUG(frank): Write() does not allow to configure the newline character. Therefore, on Windows LF is used.

import (
"bytes"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -766,7 +767,12 @@ func expand(s string, keys []string, prefix, postfix string, values map[string]s

for _, k := range keys {
if key == k {
return "", fmt.Errorf("circular reference in %q", key + " = " + prefix + k + postfix)
var b bytes.Buffer
b.WriteString("circular reference in:\n")
for _, k1 := range keys {
fmt.Fprintf(&b, "%s=%s\n", k1, values[k1])
}
return "", fmt.Errorf(b.String())
}
}

Expand Down
13 changes: 8 additions & 5 deletions properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"os"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -134,8 +135,8 @@ var errorTests = []struct {
{"key\\u123", "invalid unicode literal"},

// circular references
{"key=${key}", "circular reference"},
{"key1=${key2}\nkey2=${key1}", "circular reference"},
{"key=${key}", `circular reference in:\nkey=\$\{key\}`},
{"key1=${key2}\nkey2=${key1}", `circular reference in:\n(key1=\$\{key2\}\nkey2=\$\{key1\}|key2=\$\{key1\}\nkey1=\$\{key2\})`},

// malformed expressions
{"key=${ke", "malformed expression"},
Expand Down Expand Up @@ -450,8 +451,9 @@ func TestComplex(t *testing.T) {
func TestErrors(t *testing.T) {
for _, test := range errorTests {
_, err := Load([]byte(test.input), ISO_8859_1)
assert.Equal(t, err != nil, true, "want error")
assert.Equal(t, strings.Contains(err.Error(), test.msg), true)
assert.Equal(t, err != nil, true, fmt.Sprintf("want error: %s", test.input))
re := regexp.MustCompile(test.msg)
assert.Equal(t, re.MatchString(err.Error()), true, fmt.Sprintf("expected %s, got %s", test.msg, err.Error()))
}
}

Expand Down Expand Up @@ -796,7 +798,8 @@ func TestSetValue(t *testing.T) {
func TestMustSet(t *testing.T) {
input := "key=${key}"
p := mustParse(t, input)
assert.Panic(t, func() { p.MustSet("key", "${key}") }, `circular reference in "key = \$\{key\}"`)
e := `circular reference in:\nkey=\$\{key\}`
assert.Panic(t, func() { p.MustSet("key", "${key}") }, e)
}

func TestWrite(t *testing.T) {
Expand Down