diff --git a/properties.go b/properties.go index 6e350a2..0d0fc28 100644 --- a/properties.go +++ b/properties.go @@ -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" @@ -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()) } } diff --git a/properties_test.go b/properties_test.go index ad68ef4..6969cf7 100644 --- a/properties_test.go +++ b/properties_test.go @@ -11,6 +11,7 @@ import ( "math" "os" "reflect" + "regexp" "runtime" "strings" "testing" @@ -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"}, @@ -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())) } } @@ -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) {