Skip to content

Commit ba1b90a

Browse files
committed
Revert "Remove sync.Map registry; tests run PreRunE to populate context"
This reverts commit e735470.
1 parent e735470 commit ba1b90a

5 files changed

Lines changed: 39 additions & 24 deletions

File tree

cmd/workspace/apps/overrides_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111

1212
func TestListTableConfig(t *testing.T) {
1313
cmd := newList()
14-
cmd.SetContext(t.Context())
15-
require.NoError(t, cmd.PreRunE(cmd, nil))
1614

17-
cfg := tableview.GetTableConfig(cmd.Context())
15+
cfg := tableview.GetTableConfigForCmd(cmd)
1816
require.NotNil(t, cfg)
1917
require.Len(t, cfg.Columns, 4)
2018

cmd/workspace/jobs/overrides_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111

1212
func TestListTableConfig(t *testing.T) {
1313
cmd := newList()
14-
cmd.SetContext(t.Context())
15-
require.NoError(t, cmd.PreRunE(cmd, nil))
1614

17-
cfg := tableview.GetTableConfig(cmd.Context())
15+
cfg := tableview.GetTableConfigForCmd(cmd)
1816
require.NotNil(t, cfg)
1917
require.Len(t, cfg.Columns, 2)
2018

cmd/workspace/pipelines/overrides_test.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ func TestLooksLikeUUID_resourceName(t *testing.T) {
2222

2323
func TestListPipelinesTableConfig(t *testing.T) {
2424
cmd := newListPipelines()
25-
cmd.SetContext(t.Context())
26-
require.NoError(t, cmd.PreRunE(cmd, nil))
2725

28-
cfg := tableview.GetTableConfig(cmd.Context())
26+
cfg := tableview.GetTableConfigForCmd(cmd)
2927
require.NotNil(t, cfg)
3028
require.Len(t, cfg.Columns, 3)
3129
require.NotNil(t, cfg.Search)
@@ -43,10 +41,8 @@ func TestListPipelinesTableConfig(t *testing.T) {
4341

4442
func TestListPipelinesSearchEscapesLikeWildcards(t *testing.T) {
4543
cmd := newListPipelines()
46-
cmd.SetContext(t.Context())
47-
require.NoError(t, cmd.PreRunE(cmd, nil))
4844

49-
cfg := tableview.GetTableConfig(cmd.Context())
45+
cfg := tableview.GetTableConfigForCmd(cmd)
5046
require.NotNil(t, cfg)
5147
require.NotNil(t, cfg.Search)
5248

@@ -63,26 +59,34 @@ func TestListPipelinesSearchEscapesLikeWildcards(t *testing.T) {
6359

6460
func TestListPipelinesSearchDisabledWhenFilterSet(t *testing.T) {
6561
cmd := newListPipelines()
66-
cmd.SetContext(t.Context())
6762

6863
err := cmd.Flags().Set("filter", "state = 'RUNNING'")
6964
require.NoError(t, err)
7065

71-
require.NoError(t, cmd.PreRunE(cmd, nil))
72-
cfg := tableview.GetTableConfig(cmd.Context())
66+
cfg := tableview.GetTableConfigForCmd(cmd)
7367
require.NotNil(t, cfg)
68+
require.NotNil(t, cfg.Search)
69+
70+
// Simulate context setup so disableSearchIfFilterSet can read the config.
71+
cmd.SetContext(tableview.SetTableConfig(t.Context(), cfg))
72+
disableSearchIfFilterSet(cmd)
7473

75-
// The config stored in context should have Search disabled (shallow-copied,
76-
// so the registered config itself is not mutated).
77-
assert.Nil(t, cfg.Search)
74+
// The original config must not be mutated.
75+
assert.NotNil(t, cfg.Search, "original config must not be mutated")
76+
// The config stored in context should have Search disabled.
77+
ctxCfg := tableview.GetTableConfig(cmd.Context())
78+
require.NotNil(t, ctxCfg)
79+
assert.Nil(t, ctxCfg.Search)
7880
}
7981

8082
func TestListPipelinesSearchNotDisabledWithoutFilter(t *testing.T) {
8183
cmd := newListPipelines()
82-
cmd.SetContext(t.Context())
83-
require.NoError(t, cmd.PreRunE(cmd, nil))
8484

85-
cfg := tableview.GetTableConfig(cmd.Context())
85+
cfg := tableview.GetTableConfigForCmd(cmd)
8686
require.NotNil(t, cfg)
87+
88+
// Simulate context setup so disableSearchIfFilterSet can read the config.
89+
cmd.SetContext(tableview.SetTableConfig(t.Context(), cfg))
90+
disableSearchIfFilterSet(cmd)
8791
assert.NotNil(t, cfg.Search)
8892
}

cmd/workspace/serving-endpoints/overrides_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import (
1111

1212
func TestListTableConfig(t *testing.T) {
1313
cmd := newList()
14-
cmd.SetContext(t.Context())
15-
require.NoError(t, cmd.PreRunE(cmd, nil))
1614

17-
cfg := tableview.GetTableConfig(cmd.Context())
15+
cfg := tableview.GetTableConfigForCmd(cmd)
1816
require.NotNil(t, cfg)
1917
require.Len(t, cfg.Columns, 3)
2018

libs/tableview/context.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tableview
22

33
import (
44
"context"
5+
"sync"
56

67
"github.com/spf13/cobra"
78
)
@@ -19,9 +20,14 @@ func GetTableConfig(ctx context.Context) *TableConfig {
1920
return cfg
2021
}
2122

23+
// configByCmd holds configs registered via SetTableConfigOnCmd so they can
24+
// be looked up before the command's PreRunE has executed (useful in tests).
25+
var configByCmd sync.Map
26+
2227
// SetTableConfigOnCmd arranges for cfg to be stored in the command's context
2328
// at execution time via PreRunE, preserving Cobra's context propagation.
2429
func SetTableConfigOnCmd(cmd *cobra.Command, cfg *TableConfig) {
30+
configByCmd.Store(cmd, cfg)
2531
prev := cmd.PreRunE
2632
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
2733
cmd.SetContext(SetTableConfig(cmd.Context(), cfg))
@@ -31,3 +37,14 @@ func SetTableConfigOnCmd(cmd *cobra.Command, cfg *TableConfig) {
3137
return nil
3238
}
3339
}
40+
41+
// GetTableConfigForCmd returns the config registered on cmd via
42+
// SetTableConfigOnCmd. Unlike GetTableConfig (which reads from context),
43+
// this works before the command's PreRunE has executed.
44+
func GetTableConfigForCmd(cmd *cobra.Command) *TableConfig {
45+
v, ok := configByCmd.Load(cmd)
46+
if !ok {
47+
return nil
48+
}
49+
return v.(*TableConfig)
50+
}

0 commit comments

Comments
 (0)