Skip to content

test: add TestLevelStringUnknown and TestParseLevelVariations in level_test.go - #1581

Open
aoright wants to merge 1 commit into
sirupsen:masterfrom
aoright:test/level-string-unknown-and-parselevel
Open

test: add TestLevelStringUnknown and TestParseLevelVariations in level_test.go#1581
aoright wants to merge 1 commit into
sirupsen:masterfrom
aoright:test/level-string-unknown-and-parselevel

Conversation

@aoright

@aoright aoright commented Aug 19, 2026

Copy link
Copy Markdown

Summary

  • In level_test.go, add unit test TestLevelStringUnknown to verify Level.String() for unknown level values.
  • Add TestParseLevelVariations to verify case-insensitive matching and alias handling (e.g., warn/warning, uppercase, invalid input).

…l_test.go

Signed-off-by: aoright <102943475+aoright@users.noreply.github.com>

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I had a look and it looks like most lines are already covered by tests, except for these 3;

logrus/level.go

Lines 88 to 91 in 134c80f

p, ok := prefix[level]
if !ok {
p = unknown
}

Left suggestions to replace the existing tests with your table approach; let me know if you need more info.

When updating, please amend / squash your commit 😅

Comment thread level_test.go
Comment on lines +63 to +81

func TestLevelStringUnknown(t *testing.T) {
unknownLevel := logrus.Level(99)
require.Equal(t, "unknown", unknownLevel.String())
}

func TestParseLevelVariations(t *testing.T) {
tests := []struct {
input string
expected logrus.Level
hasErr bool
}{
{"panic", logrus.PanicLevel, false},
{"PANIC", logrus.PanicLevel, false},
{"fatal", logrus.FatalLevel, false},
{"error", logrus.ErrorLevel, false},
{"warn", logrus.WarnLevel, false},
{"WARN", logrus.WarnLevel, false},
{"warning", logrus.WarnLevel, false},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the table-test, but we should instead make this replace the existing test(s) (and move it to that file, because ParseLevel is defined in logrus.go; I actually wanted to update those to use a table-test, so perhaps you can update those so that we don't duplicate the exact tests?

Possibly the same test could cover text -> level and level text within the same table so that we don't end up with 2 table tests, but I haven't tried.

logrus/logrus_test.go

Lines 530 to 613 in 134c80f

func TestConvertLevelToString(t *testing.T) {
assert.Equal(t, "trace", TraceLevel.String())
assert.Equal(t, "debug", DebugLevel.String())
assert.Equal(t, "info", InfoLevel.String())
assert.Equal(t, "warning", WarnLevel.String())
assert.Equal(t, "error", ErrorLevel.String())
assert.Equal(t, "fatal", FatalLevel.String())
assert.Equal(t, "panic", PanicLevel.String())
}
func TestParseLevel(t *testing.T) {
l, err := ParseLevel("panic")
require.NoError(t, err)
assert.Equal(t, PanicLevel, l)
l, err = ParseLevel("PANIC")
require.NoError(t, err)
assert.Equal(t, PanicLevel, l)
l, err = ParseLevel("fatal")
require.NoError(t, err)
assert.Equal(t, FatalLevel, l)
l, err = ParseLevel("FATAL")
require.NoError(t, err)
assert.Equal(t, FatalLevel, l)
l, err = ParseLevel("error")
require.NoError(t, err)
assert.Equal(t, ErrorLevel, l)
l, err = ParseLevel("ERROR")
require.NoError(t, err)
assert.Equal(t, ErrorLevel, l)
l, err = ParseLevel("warn")
require.NoError(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("WARN")
require.NoError(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("warning")
require.NoError(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("WARNING")
require.NoError(t, err)
assert.Equal(t, WarnLevel, l)
l, err = ParseLevel("info")
require.NoError(t, err)
assert.Equal(t, InfoLevel, l)
l, err = ParseLevel("INFO")
require.NoError(t, err)
assert.Equal(t, InfoLevel, l)
l, err = ParseLevel("debug")
require.NoError(t, err)
assert.Equal(t, DebugLevel, l)
l, err = ParseLevel("DEBUG")
require.NoError(t, err)
assert.Equal(t, DebugLevel, l)
l, err = ParseLevel("trace")
require.NoError(t, err)
assert.Equal(t, TraceLevel, l)
l, err = ParseLevel("TRACE")
require.NoError(t, err)
assert.Equal(t, TraceLevel, l)
_, err = ParseLevel("invalid")
assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
}
func TestLevelString(t *testing.T) {
var loggerlevel Level = 32000
_ = loggerlevel.String()
}

Oh! One thing worth mentioning; that logrus_test.go uses dot-imports for logrus; that's not consistent with other tests-files we have, but changing it would cause too much code-churn, so I left that for now (and we probably should .. for now);

logrus/logrus_test.go

Lines 18 to 19 in 134c80f

. "github.com/sirupsen/logrus"
. "github.com/sirupsen/logrus/internal/testutils"

Comment thread level_test.go
Comment on lines +85 to +87
{"debug", logrus.DebugLevel, false},
{"trace", logrus.TraceLevel, false},
{"TRACE", logrus.TraceLevel, false},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When updating, could you avoid implicit fields here?

{input: "debug", expected: logrus.DebugLevel, hasErr: false},

Comment thread level_test.go
{"invalid", logrus.Level(0), true},
}

for _, tt := range tests {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit; we mostly converged on using tc (for "test case") for these tables; can you update it to use the same for consistency?

for _, tc := range tests {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants