mirror of
https://github.com/derailed/k9s.git
synced 2026-10-11 21:39:54 +00:00
fix(ui): guard tree collapse toggle against a nil root (#4257)
This commit is contained in:
+6
-1
@@ -111,7 +111,12 @@ func (*Tree) noopCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||
|
||||
func (t *Tree) toggleCollapseCmd(*tcell.EventKey) *tcell.EventKey {
|
||||
t.expandNodes = !t.expandNodes
|
||||
t.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
|
||||
// The root is populated asynchronously and may not be set yet.
|
||||
root := t.GetRoot()
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
root.Walk(func(node, parent *tview.TreeNode) bool {
|
||||
if parent != nil {
|
||||
node.SetExpanded(t.expandNodes)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright Authors of K9s
|
||||
|
||||
package ui_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/derailed/k9s/internal/ui"
|
||||
"github.com/derailed/tcell/v2"
|
||||
"github.com/derailed/tview"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTreeToggleCollapse(t *testing.T) {
|
||||
uu := map[string]struct {
|
||||
root *tview.TreeNode
|
||||
}{
|
||||
"no-root": {},
|
||||
"root": {
|
||||
root: tview.NewTreeNode("root").AddChild(tview.NewTreeNode("child")),
|
||||
},
|
||||
}
|
||||
|
||||
for k := range uu {
|
||||
u := uu[k]
|
||||
t.Run(k, func(t *testing.T) {
|
||||
v := ui.NewTree()
|
||||
v.BindKeys()
|
||||
if u.root != nil {
|
||||
v.SetRoot(u.root)
|
||||
}
|
||||
a, ok := v.Actions().Get(ui.KeyX)
|
||||
require.True(t, ok)
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
a.Action(tcell.NewEventKey(tcell.KeyRune, 'x', tcell.ModNone))
|
||||
})
|
||||
assert.False(t, v.ExpandNodes())
|
||||
if u.root != nil {
|
||||
assert.False(t, u.root.GetChildren()[0].IsExpanded())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -510,7 +510,12 @@ func (x *Xray) gotoCmd(*tcell.EventKey) *tcell.EventKey {
|
||||
x.Start()
|
||||
}
|
||||
x.CmdBuff().SetActive(false)
|
||||
x.GetRoot().ExpandAll()
|
||||
// The root is populated asynchronously and may not be set yet.
|
||||
root := x.GetRoot()
|
||||
if root == nil {
|
||||
return nil
|
||||
}
|
||||
root.ExpandAll()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Copyright Authors of K9s
|
||||
|
||||
package view
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/derailed/k9s/internal/client"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestXrayGotoCmdBeforeTreeLoaded(t *testing.T) {
|
||||
v, ok := NewXray(client.NewGVR("deployments")).(*Xray)
|
||||
assert.True(t, ok)
|
||||
v.CmdBuff().SetActive(true)
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
v.gotoCmd(nil)
|
||||
})
|
||||
assert.False(t, v.CmdBuff().IsActive())
|
||||
}
|
||||
Reference in New Issue
Block a user