diff --git a/env/env.go b/env/env.go index 4029ca122..68af0b11c 100644 --- a/env/env.go +++ b/env/env.go @@ -1,7 +1,10 @@ // Package env provides types to interact with environment setup. package env -import "os" +import ( + "os" + "strconv" +) // Execution specific. const ( @@ -66,7 +69,7 @@ const ( type LookupFunc func(key string) (string, bool) // EmptyLookup is a LookupFunc that always returns "" and false. -func EmptyLookup(key string) (string, bool) { return "", false } +func EmptyLookup(_ string) (string, bool) { return "", false } // Lookup is a LookupFunc that uses os.LookupEnv. func Lookup(key string) (string, bool) { return os.LookupEnv(key) } @@ -82,3 +85,17 @@ func ConstLookup(k, v string) LookupFunc { return EmptyLookup(key) } } + +// LookupBool is a LookupFunc that returns the result of os.Lookup as a bool. +// Returns false either if the key does not exist or the value is not a bool. +func LookupBool(key string) bool { + v, ok := os.LookupEnv(key) + if !ok { + return false + } + bv, err := strconv.ParseBool(v) + if err != nil { + return false + } + return bv +} diff --git a/tests/frame_test.go b/tests/frame_test.go index 7aedab24a..282e76f4e 100644 --- a/tests/frame_test.go +++ b/tests/frame_test.go @@ -3,7 +3,6 @@ package tests import ( "context" "net/http" - "strconv" "testing" "time" @@ -72,15 +71,13 @@ func TestFrameDismissDialogBox(t *testing.T) { func TestFrameNoPanicWithEmbeddedIFrame(t *testing.T) { t.Parallel() - if s, ok := env.Lookup(env.BrowserHeadless); ok { - if v, err := strconv.ParseBool(s); err == nil && v { - // We're skipping this when running in headless - // environments since the bug that the test fixes - // only surfaces when in headfull mode. - // Remove this skip once we have headfull mode in - // CI: https://github.com/grafana/xk6-browser/issues/678 - t.Skip("skipped when in headless mode") - } + // We're skipping this when running in headless + // environments since the bug that the test fixes + // only surfaces when in headfull mode. + // Remove this skip once we have headfull mode in + // CI: https://github.com/grafana/xk6-browser/issues/678 + if env.LookupBool(env.BrowserHeadless) { + t.Skip("skipped when in headless mode") } // run the browser in headfull mode. @@ -110,15 +107,13 @@ func TestFrameNoPanicWithEmbeddedIFrame(t *testing.T) { func TestFrameNoPanicNavigateAndClickOnPageWithIFrames(t *testing.T) { t.Parallel() - if s, ok := env.Lookup(env.BrowserHeadless); ok { - if v, err := strconv.ParseBool(s); err == nil && v { - // We're skipping this when running in headless - // environments since the bug that the test fixes - // only surfaces when in headfull mode. - // Remove this skip once we have headfull mode in - // CI: https://github.com/grafana/xk6-browser/issues/678 - t.Skip("skipped when in headless mode") - } + // We're skipping this when running in headless + // environments since the bug that the test fixes + // only surfaces when in headfull mode. + // Remove this skip once we have headfull mode in + // CI: https://github.com/grafana/xk6-browser/issues/678 + if env.LookupBool(env.BrowserHeadless) { + t.Skip("skipped when in headless mode") } tb := newTestBrowser(