Skip to content

Commit

Permalink
guard against concurrent map writes in DeprecationTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
onsi committed Apr 26, 2022
1 parent ce3d373 commit 0976569
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions types/deprecation_support.go
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"strconv"
"strings"
"sync"
"unicode"

"github.com/onsi/ginkgo/v2/formatter"
Expand Down Expand Up @@ -84,11 +85,13 @@ func (d deprecations) Nodot() Deprecation {

type DeprecationTracker struct {
deprecations map[Deprecation][]CodeLocation
lock *sync.Mutex
}

func NewDeprecationTracker() *DeprecationTracker {
return &DeprecationTracker{
deprecations: map[Deprecation][]CodeLocation{},
lock: &sync.Mutex{},
}
}

Expand All @@ -102,6 +105,8 @@ func (d *DeprecationTracker) TrackDeprecation(deprecation Deprecation, cl ...Cod
}
}

d.lock.Lock()
defer d.lock.Unlock()
if len(cl) == 1 {
d.deprecations[deprecation] = append(d.deprecations[deprecation], cl[0])
} else {
Expand All @@ -110,10 +115,14 @@ func (d *DeprecationTracker) TrackDeprecation(deprecation Deprecation, cl ...Cod
}

func (d *DeprecationTracker) DidTrackDeprecations() bool {
d.lock.Lock()
defer d.lock.Unlock()
return len(d.deprecations) > 0
}

func (d *DeprecationTracker) DeprecationsReport() string {
d.lock.Lock()
defer d.lock.Unlock()
out := formatter.F("{{light-yellow}}You're using deprecated Ginkgo functionality:{{/}}\n")
out += formatter.F("{{light-yellow}}============================================={{/}}\n")
for deprecation, locations := range d.deprecations {
Expand Down

0 comments on commit 0976569

Please sign in to comment.