Fix #4707: Add nil check in hideRootFlags (#4880)

* Add test for #4707: hideRootFlags should handle non-existent flags

* Fix #4707: Add nil check in hideRootFlags
This commit is contained in:
Nathan Wallace
2025-11-16 00:16:53 +08:00
committed by GitHub
parent 0b0b330a27
commit 79ab1c9b69
2 changed files with 22 additions and 1 deletions

View File

@@ -80,7 +80,9 @@ func InitCmd() {
func hideRootFlags(flags ...string) {
for _, flag := range flags {
rootCmd.Flag(flag).Hidden = true
if f := rootCmd.Flag(flag); f != nil {
f.Hidden = true
}
}
}

19
cmd/root_test.go Normal file
View File

@@ -0,0 +1,19 @@
package cmd
import (
"testing"
"github.com/stretchr/testify/assert"
)
// TestHideRootFlags_NonExistentFlag tests that hideRootFlags handles non-existent flags gracefully
// Bug #4707: hideRootFlags panics when called with a flag that doesn't exist
func TestHideRootFlags_NonExistentFlag(t *testing.T) {
// Initialize the root command
InitCmd()
// Test that calling hideRootFlags with a non-existent flag should NOT panic
assert.NotPanics(t, func() {
hideRootFlags("non-existent-flag")
}, "hideRootFlags should handle non-existent flags without panicking")
}