@@ -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
3633func 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) {
365360func 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)
507499func 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+
516517func getTestFile () string {
517518 _ , file , _ , ok := runtime .Caller (2 )
518519 if ok {
0 commit comments