Skip to content

Commit dc0c1bb

Browse files
committed
feat(global_state): introduce global state management for initialization and reporter handling
- Added a new file for managing global state, including functions to set and get the global initialization state and reporter instance. - Replaced direct global state checks in qase.go with calls to the new global state management functions, improving code organization and readability. - Implemented a reset function for the global state, facilitating easier testing and state management. This update enhances the overall structure of the codebase by centralizing global state management, leading to improved maintainability and clarity.
1 parent 2df6bc2 commit dc0c1bb

File tree

2 files changed

+81
-29
lines changed

2 files changed

+81
-29
lines changed

pkg/qase-go/qase/global_state.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package qase
2+
3+
import (
4+
"sync"
5+
"sync/atomic"
6+
7+
"github.com/qase-tms/qase-go/pkg/qase-go/reporters"
8+
)
9+
10+
// Global state shared across all packages
11+
var (
12+
globalInitialized int32
13+
globalReporter *reporters.CoreReporter
14+
globalMutex sync.RWMutex
15+
)
16+
17+
// SetGlobalInitialized sets the global initialization state
18+
func SetGlobalInitialized(initialized bool) {
19+
if initialized {
20+
atomic.StoreInt32(&globalInitialized, 1)
21+
} else {
22+
atomic.StoreInt32(&globalInitialized, 0)
23+
}
24+
}
25+
26+
// IsGlobalInitialized returns true if qase has been globally initialized
27+
func IsGlobalInitialized() bool {
28+
return atomic.LoadInt32(&globalInitialized) == 1
29+
}
30+
31+
// SetGlobalReporter sets the global reporter instance
32+
func SetGlobalReporter(reporter *reporters.CoreReporter) {
33+
globalMutex.Lock()
34+
defer globalMutex.Unlock()
35+
globalReporter = reporter
36+
}
37+
38+
// GetGlobalReporter returns the global reporter instance if available
39+
func GetGlobalReporter() *reporters.CoreReporter {
40+
globalMutex.RLock()
41+
defer globalMutex.RUnlock()
42+
return globalReporter
43+
}
44+
45+
// ResetGlobalState resets the global state (useful for testing)
46+
func ResetGlobalState() {
47+
globalMutex.Lock()
48+
defer globalMutex.Unlock()
49+
atomic.StoreInt32(&globalInitialized, 0)
50+
globalReporter = nil
51+
}

pkg/qase-go/qase/qase.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@ var (
2828
stepStackMutex sync.RWMutex
2929
// Add this for automatic test run completion
3030
completeTestRunOnce sync.Once
31-
// Global initialization control
32-
globalInitialized bool
33-
globalMutex sync.RWMutex
3431
)
3532

3633
func init() {
3734
once.Do(func() {
3835
// Check if global initialization has already been done
39-
globalMutex.RLock()
40-
if globalInitialized {
41-
globalMutex.RUnlock()
42-
return // Already initialized globally
36+
if IsGlobalInitialized() {
37+
// Global initialization was done, skip local initialization
38+
return
4339
}
44-
globalMutex.RUnlock()
4540

4641
// Fallback to local initialization if global wasn't called
4742
cfg, err := config.Load()
@@ -200,8 +195,8 @@ func Test(t *testing.T, meta TestMetadata, fn func()) {
200195
result.Execution.Duration = &duration
201196

202197
// Add result to reporter
203-
if reporter != nil {
204-
_ = reporter.AddResult(result)
198+
if getActiveReporter() != nil {
199+
_ = getActiveReporter().AddResult(result)
205200
}
206201
}()
207202

@@ -307,7 +302,7 @@ func AddAttachments(path string) {
307302
result.Attachments = append(result.Attachments, *attachment)
308303

309304
// Log for debugging
310-
if reporter != nil && reporter.GetConfig().Debug {
305+
if getActiveReporter() != nil && getActiveReporter().GetConfig().Debug {
311306
logging.Info("Added attachment: %s to test: %s", path, result.Title)
312307
}
313308
}
@@ -329,7 +324,7 @@ func AttachFile(name, filePath, contentType string) {
329324
result.Attachments = append(result.Attachments, *attachment)
330325

331326
// Log for debugging
332-
if reporter != nil && reporter.GetConfig().Debug {
327+
if getActiveReporter() != nil && getActiveReporter().GetConfig().Debug {
333328
logging.Info("Added file attachment: %s (%s) - %s to test: %s", name, contentType, filePath, result.Title)
334329
}
335330
}
@@ -350,7 +345,7 @@ func AttachContent(name, content, contentType string) {
350345
result.Attachments = append(result.Attachments, *attachment)
351346

352347
// Log for debugging
353-
if reporter != nil && reporter.GetConfig().Debug {
348+
if getActiveReporter() != nil && getActiveReporter().GetConfig().Debug {
354349
contentPreview := content
355350
if len(content) > 50 {
356351
contentPreview = content[:50] + "..."
@@ -365,9 +360,9 @@ func AttachContent(name, content, contentType string) {
365360
func CompleteTestRun() error {
366361
var err error
367362
completeTestRunOnce.Do(func() {
368-
if reporter != nil {
363+
if getActiveReporter() != nil {
369364
logging.Info("Completing test run (will only happen once)")
370-
err = reporter.CompleteTestRun(context.Background())
365+
err = getActiveReporter().CompleteTestRun(context.Background())
371366
if err != nil {
372367
logging.Error("Error completing test run: %v", err)
373368
} else {
@@ -443,12 +438,14 @@ func InitializeGlobal() error {
443438
}
444439

445440
reporter = r
446-
if err := reporter.StartTestRun(context.Background()); err != nil {
441+
if err := getActiveReporter().StartTestRun(context.Background()); err != nil {
447442
logging.Error("Failed to start test run: %v", err)
448443
return err
449444
}
450445

451-
globalInitialized = true
446+
SetGlobalInitialized(true)
447+
SetGlobalReporter(r)
448+
452449
logging.Info("Qase test run started successfully (global initialization)")
453450
return nil
454451
}
@@ -486,33 +483,37 @@ func InitializeGlobalWithConfig(cfg *config.Config) error {
486483
}
487484

488485
reporter = r
489-
if err := reporter.StartTestRun(context.Background()); err != nil {
486+
if err := getActiveReporter().StartTestRun(context.Background()); err != nil {
490487
logging.Error("Failed to start test run: %v", err)
491488
return err
492489
}
493490

494-
globalInitialized = true
491+
SetGlobalInitialized(true)
492+
SetGlobalReporter(r)
493+
495494
logging.Info("Qase test run started successfully (global initialization with custom config)")
496495
return nil
497496
}
498497

499-
// IsGlobalInitialized returns true if qase has been globally initialized
500-
func IsGlobalInitialized() bool {
501-
globalMutex.RLock()
502-
defer globalMutex.RUnlock()
503-
return globalInitialized
504-
}
505-
506498
// ResetGlobal resets the global initialization state (useful for testing)
507499
func ResetGlobal() {
508-
globalMutex.Lock()
509-
defer globalMutex.Unlock()
510-
globalInitialized = false
500+
ResetGlobalState()
511501
reporter = nil
512502
once = sync.Once{}
513503
}
514504

515505
// Helper functions
506+
507+
// getActiveReporter returns the active reporter (global or local)
508+
func getActiveReporter() *reporters.CoreReporter {
509+
// First check if global reporter is available
510+
if GetGlobalReporter() != nil {
511+
return GetGlobalReporter()
512+
}
513+
// Fallback to local reporter
514+
return reporter
515+
}
516+
516517
func getTestFile() string {
517518
_, file, _, ok := runtime.Caller(2)
518519
if ok {

0 commit comments

Comments
 (0)