|
1 | | -package testframework |
2 | | - |
3 | | -import ( |
4 | | - "context" |
5 | | - "github.com/cucumber/godog" |
6 | | - "os" |
7 | | - "sync" |
8 | | -) |
9 | | - |
10 | | -// All type definitions have been moved to types.go for better organization |
11 | | -var scenarioMutex sync.Mutex |
12 | | - |
13 | | -// InitializeScenario registers all step definitions for gherkin scenarios |
14 | | -func InitializeScenario(ctx *godog.ScenarioContext) { |
15 | | - |
16 | | - // Configuration steps (existing config_steps.go steps work fine with TestState via context) |
17 | | - InitializeConfigScenario(ctx) |
18 | | - |
19 | | - // Provider lifecycle steps |
20 | | - InitializeProviderSteps(ctx) |
21 | | - |
22 | | - // Flag evaluation steps |
23 | | - InitializeFlagSteps(ctx) |
24 | | - |
25 | | - // Context management steps |
26 | | - InitializeContextSteps(ctx) |
27 | | - |
28 | | - // Event handling steps |
29 | | - InitializeEventSteps(ctx) |
30 | | - |
31 | | - // Setup scenario hooks |
32 | | - ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { |
33 | | - scenarioMutex.Lock() |
34 | | - defer scenarioMutex.Unlock() |
35 | | - state := &TestState{ |
36 | | - EnvVars: make(map[string]string), |
37 | | - EvalContext: make(map[string]interface{}), |
38 | | - EventChannel: make(chan EventRecord, 100), |
39 | | - } |
40 | | - state.ProviderType = ctx.Value("resolver").(ProviderType) |
41 | | - state.FlagDir = ctx.Value("flagDir").(string) |
42 | | - |
43 | | - return context.WithValue(ctx, TestStateKey{}, state), nil |
44 | | - }) |
45 | | - |
46 | | - ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { |
47 | | - scenarioMutex.Lock() |
48 | | - defer scenarioMutex.Unlock() |
49 | | - if state, ok := ctx.Value(TestStateKey{}).(*TestState); ok { |
50 | | - state.clearEvents() |
51 | | - state.CleanupEnvironmentVariables() |
52 | | - state.cleanupProvider() |
53 | | - } |
54 | | - return ctx, nil |
55 | | - }) |
56 | | -} |
57 | | - |
58 | | -// Type conversion utilities are now centralized in utils.go |
59 | | -// Legacy compatibility wrappers |
60 | | -func convertValueForSteps(value string, valueType string) (interface{}, error) { |
61 | | - return DefaultConverter.ConvertForSteps(value, valueType) |
62 | | -} |
63 | | - |
64 | | -// applyDefaults sets default values for TestState fields |
65 | | -func (s *TestState) applyDefaults() { |
66 | | - if s.EnvVars == nil { |
67 | | - s.EnvVars = make(map[string]string) |
68 | | - } |
69 | | - if s.EvalContext == nil { |
70 | | - s.EvalContext = make(map[string]interface{}) |
71 | | - } |
72 | | - if s.EventChannel == nil { |
73 | | - s.EventChannel = make(chan EventRecord, 100) // Buffered channel to prevent blocking |
74 | | - } |
75 | | - if s.ProviderType == 0 { |
76 | | - s.ProviderType = RPC // Default to RPC |
77 | | - } |
78 | | -} |
79 | | - |
80 | | -// cleanupEnvironmentVariables restores original environment variables |
81 | | -func (s *TestState) CleanupEnvironmentVariables() { |
82 | | - for envVar, originalValue := range s.EnvVars { |
83 | | - if originalValue == "" { |
84 | | - os.Unsetenv(envVar) |
85 | | - } else { |
86 | | - os.Setenv(envVar, originalValue) |
87 | | - } |
88 | | - } |
89 | | - s.EnvVars = make(map[string]string) |
90 | | -} |
91 | | - |
92 | | -// cleanupProvider properly shuts down the provider and client to prevent event contamination |
93 | | -func (s *TestState) cleanupProvider() { |
94 | | - // Shutdown the provider if it has a shutdown method |
95 | | - if s.Provider != nil { |
96 | | - // Try to cast to common provider interfaces that might have shutdown methods |
97 | | - // This is defensive - not all providers will have explicit shutdown |
98 | | - |
99 | | - if shutdownable, ok := s.Provider.(interface{ Shutdown() }); ok { |
100 | | - shutdownable.Shutdown() |
101 | | - } |
102 | | - |
103 | | - s.Provider = nil |
104 | | - } |
105 | | -} |
| 1 | +package testframework |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/cucumber/godog" |
| 6 | + "os" |
| 7 | + "sync" |
| 8 | +) |
| 9 | + |
| 10 | +// All type definitions have been moved to types.go for better organization |
| 11 | +var scenarioMutex sync.Mutex |
| 12 | + |
| 13 | +// InitializeScenario registers all step definitions for gherkin scenarios |
| 14 | +func InitializeScenario(ctx *godog.ScenarioContext) { |
| 15 | + |
| 16 | + // Configuration steps (existing config_steps.go steps work fine with TestState via context) |
| 17 | + InitializeConfigScenario(ctx) |
| 18 | + |
| 19 | + // Provider lifecycle steps |
| 20 | + InitializeProviderSteps(ctx) |
| 21 | + |
| 22 | + // Flag evaluation steps |
| 23 | + InitializeFlagSteps(ctx) |
| 24 | + |
| 25 | + // Context management steps |
| 26 | + InitializeContextSteps(ctx) |
| 27 | + |
| 28 | + // Event handling steps |
| 29 | + InitializeEventSteps(ctx) |
| 30 | + |
| 31 | + // Setup scenario hooks |
| 32 | + ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) { |
| 33 | + scenarioMutex.Lock() |
| 34 | + defer scenarioMutex.Unlock() |
| 35 | + state := &TestState{ |
| 36 | + EnvVars: make(map[string]string), |
| 37 | + EvalContext: make(map[string]interface{}), |
| 38 | + EventChannel: make(chan EventRecord, 100), |
| 39 | + } |
| 40 | + state.ProviderType = ctx.Value("resolver").(ProviderType) |
| 41 | + state.FlagDir = ctx.Value("flagDir").(string) |
| 42 | + |
| 43 | + return context.WithValue(ctx, TestStateKey{}, state), nil |
| 44 | + }) |
| 45 | + |
| 46 | + ctx.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) { |
| 47 | + scenarioMutex.Lock() |
| 48 | + defer scenarioMutex.Unlock() |
| 49 | + if state, ok := ctx.Value(TestStateKey{}).(*TestState); ok { |
| 50 | + state.clearEvents() |
| 51 | + state.CleanupEnvironmentVariables() |
| 52 | + state.cleanupProvider() |
| 53 | + } |
| 54 | + return ctx, nil |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +// Type conversion utilities are now centralized in utils.go |
| 59 | +// Legacy compatibility wrappers |
| 60 | +func convertValueForSteps(value string, valueType string) (interface{}, error) { |
| 61 | + return DefaultConverter.ConvertForSteps(value, valueType) |
| 62 | +} |
| 63 | + |
| 64 | +// applyDefaults sets default values for TestState fields |
| 65 | +func (s *TestState) applyDefaults() { |
| 66 | + if s.EnvVars == nil { |
| 67 | + s.EnvVars = make(map[string]string) |
| 68 | + } |
| 69 | + if s.EvalContext == nil { |
| 70 | + s.EvalContext = make(map[string]interface{}) |
| 71 | + } |
| 72 | + if s.EventChannel == nil { |
| 73 | + s.EventChannel = make(chan EventRecord, 100) // Buffered channel to prevent blocking |
| 74 | + } |
| 75 | + if s.ProviderType == 0 { |
| 76 | + s.ProviderType = RPC // Default to RPC |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// cleanupEnvironmentVariables restores original environment variables |
| 81 | +func (s *TestState) CleanupEnvironmentVariables() { |
| 82 | + for envVar, originalValue := range s.EnvVars { |
| 83 | + if originalValue == "" { |
| 84 | + os.Unsetenv(envVar) |
| 85 | + } else { |
| 86 | + os.Setenv(envVar, originalValue) |
| 87 | + } |
| 88 | + } |
| 89 | + s.EnvVars = make(map[string]string) |
| 90 | +} |
| 91 | + |
| 92 | +// cleanupProvider properly shuts down the provider and client to prevent event contamination |
| 93 | +func (s *TestState) cleanupProvider() { |
| 94 | + // Shutdown the provider if it has a shutdown method |
| 95 | + if s.Provider != nil { |
| 96 | + // Try to cast to common provider interfaces that might have shutdown methods |
| 97 | + // This is defensive - not all providers will have explicit shutdown |
| 98 | + |
| 99 | + if shutdownable, ok := s.Provider.(interface{ Shutdown() }); ok { |
| 100 | + shutdownable.Shutdown() |
| 101 | + } |
| 102 | + |
| 103 | + s.Provider = nil |
| 104 | + } |
| 105 | +} |
0 commit comments