Skip to content

Commit 2d301d8

Browse files
committed
test(promslog): refactor to set style via exposed Set() method
Previously this was cheating and setting the internal value of the style because it's in the same package, but consumers of this library obviously can't do that. To test the same usage flow as users, this switches tests to use the Set() method when setting logging style in tests. Signed-off-by: TJ Hoplock <[email protected]>
1 parent 1062f39 commit 2d301d8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

promslog/slog_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"log/slog"
2323
"regexp"
24+
"slices"
2425
"strings"
2526
"testing"
2627

@@ -114,7 +115,17 @@ func TestDynamicLevels(t *testing.T) {
114115
for name, tc := range tests {
115116
t.Run(name, func(t *testing.T) {
116117
buf.Reset() // Ensure buf is reset prior to tests
117-
config := &Config{ioWriter: &buf, Style: &AllowedStyle{s: tc.logStyle}}
118+
config := &Config{ioWriter: &buf, Style: &AllowedStyle{}}
119+
err := config.Style.Set(tc.logStyle)
120+
// AllowedStyle.Set() returns an error on invalid style
121+
// values and the tests use a mixture of valid and
122+
// invalid values. We must expect no error for valid
123+
// values, and expect an error for the invalid values.
124+
if slices.Contains(StyleOptions, tc.logStyle) {
125+
require.NoError(t, err, "log style returned an error setting a valid value", "value", tc.logStyle)
126+
} else {
127+
require.ErrorContains(t, err, fmt.Sprintf("unrecognized log style %s", tc.logStyle), "expected error setting log style to invalid value", "value", tc.logStyle)
128+
}
118129
logger := New(config)
119130

120131
// Test that log level can be adjusted on-the-fly to debug and that a

0 commit comments

Comments
 (0)