commands: Fix data race in test

Note that this is a test fix only.
This commit is contained in:
Bjørn Erik Pedersen 2023-03-14 12:18:42 +01:00
parent f5eddf89bf
commit 0fbab7cbc5
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
6 changed files with 15 additions and 11 deletions

View file

@ -302,6 +302,7 @@ func (c *commandeer) loadConfig() error {
cfg := c.DepsCfg cfg := c.DepsCfg
c.configured = false c.configured = false
cfg.Running = c.running cfg.Running = c.running
loggers.PanicOnWarning.Store(c.h.panicOnWarning)
var dir string var dir string
if c.h.source != "" { if c.h.source != "" {

View file

@ -209,6 +209,7 @@ type hugoBuilderCommon struct {
environment string environment string
buildWatch bool buildWatch bool
panicOnWarning bool
poll string poll string
clock string clock string
@ -299,7 +300,7 @@ func (cc *hugoBuilderCommon) handleFlags(cmd *cobra.Command) {
cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date, author, and CODEOWNERS info to the pages") cmd.Flags().Bool("enableGitInfo", false, "add Git revision, date, author, and CODEOWNERS info to the pages")
cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build") cmd.Flags().BoolVar(&cc.gc, "gc", false, "enable to run some cleanup tasks (remove unused cache files) after the build")
cmd.Flags().StringVar(&cc.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes") cmd.Flags().StringVar(&cc.poll, "poll", "", "set this to a poll interval, e.g --poll 700ms, to use a poll based approach to watch for file system changes")
cmd.Flags().BoolVar(&loggers.PanicOnWarning, "panicOnWarning", false, "panic on first WARNING log") cmd.Flags().BoolVar(&cc.panicOnWarning, "panicOnWarning", false, "panic on first WARNING log")
cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions") cmd.Flags().Bool("templateMetrics", false, "display metrics about template executions")
cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics") cmd.Flags().Bool("templateMetricsHints", false, "calculate some improvement hints when combined with --templateMetrics")
cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.") cmd.Flags().BoolP("forceSyncStatic", "", false, "copy all files when static is changed.")

View file

@ -255,6 +255,7 @@ func initializeFlags(cmd *cobra.Command, cfg config.Provider) {
setValueFromFlag(cmd.Flags(), "destination", cfg, "publishDir", false) setValueFromFlag(cmd.Flags(), "destination", cfg, "publishDir", false)
setValueFromFlag(cmd.Flags(), "printI18nWarnings", cfg, "logI18nWarnings", false) setValueFromFlag(cmd.Flags(), "printI18nWarnings", cfg, "logI18nWarnings", false)
setValueFromFlag(cmd.Flags(), "printPathWarnings", cfg, "logPathWarnings", false) setValueFromFlag(cmd.Flags(), "printPathWarnings", cfg, "logPathWarnings", false)
} }
func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider, targetKey string, force bool) { func setValueFromFlag(flags *flag.FlagSet, key string, cfg config.Provider, targetKey string, force bool) {

View file

@ -21,6 +21,7 @@ import (
"os" "os"
"regexp" "regexp"
"runtime" "runtime"
"sync/atomic"
"time" "time"
"github.com/gohugoio/hugo/common/terminal" "github.com/gohugoio/hugo/common/terminal"
@ -31,7 +32,7 @@ import (
var ( var (
// Counts ERROR logs to the global jww logger. // Counts ERROR logs to the global jww logger.
GlobalErrorCounter *jww.Counter GlobalErrorCounter *jww.Counter
PanicOnWarning bool PanicOnWarning atomic.Bool
) )
func init() { func init() {
@ -136,14 +137,14 @@ const panicOnWarningMessage = "Warning trapped. Remove the --panicOnWarning flag
func (l *logger) Warnf(format string, v ...any) { func (l *logger) Warnf(format string, v ...any) {
l.WARN.Printf(format, v...) l.WARN.Printf(format, v...)
if PanicOnWarning { if PanicOnWarning.Load() {
panic(panicOnWarningMessage) panic(panicOnWarningMessage)
} }
} }
func (l *logger) Warnln(v ...any) { func (l *logger) Warnln(v ...any) {
l.WARN.Println(v...) l.WARN.Println(v...)
if PanicOnWarning { if PanicOnWarning.Load() {
panic(panicOnWarningMessage) panic(panicOnWarningMessage)
} }
} }

View file

@ -415,7 +415,7 @@ func Deprecated(item, alternative string, err bool) {
DistinctErrorLog.Errorf("%s is deprecated and will be removed in Hugo %s. %s", item, hugo.CurrentVersion.Next().ReleaseVersion(), alternative) DistinctErrorLog.Errorf("%s is deprecated and will be removed in Hugo %s. %s", item, hugo.CurrentVersion.Next().ReleaseVersion(), alternative)
} else { } else {
var warnPanicMessage string var warnPanicMessage string
if !loggers.PanicOnWarning { if !loggers.PanicOnWarning.Load() {
warnPanicMessage = "\n\nRe-run Hugo with the flag --panicOnWarning to get a better error message." warnPanicMessage = "\n\nRe-run Hugo with the flag --panicOnWarning to get a better error message."
} }
DistinctWarnLog.Warnf("%s is deprecated and will be removed in a future release. %s%s", item, alternative, warnPanicMessage) DistinctWarnLog.Warnf("%s is deprecated and will be removed in a future release. %s%s", item, alternative, warnPanicMessage)

View file

@ -65,10 +65,10 @@ func TestDistinctLoggerDoesNotLockOnWarningPanic(t *testing.T) {
// Set PanicOnWarning to true to reproduce issue 9380 // Set PanicOnWarning to true to reproduce issue 9380
// Ensure global variable loggers.PanicOnWarning is reset to old value after test // Ensure global variable loggers.PanicOnWarning is reset to old value after test
if loggers.PanicOnWarning == false { if !loggers.PanicOnWarning.Load() {
loggers.PanicOnWarning = true loggers.PanicOnWarning.Store(true)
defer func() { defer func() {
loggers.PanicOnWarning = false loggers.PanicOnWarning.Store(false)
}() }()
} }