From f8aa82f4f324032262339f8e9a62f6d79e6e0498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 17 Aug 2023 13:34:29 +0300 Subject: [PATCH] Add a LookupBool utility function It makes it a little bit clearer to make these queries. --- env/env.go | 22 ++++++++++++++++++++-- tests/frame_test.go | 33 ++++++++++++++------------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/env/env.go b/env/env.go index 4029ca122..5fb648671 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,18 @@ func ConstLookup(k, v string) LookupFunc { return EmptyLookup(key) } } + +// LookupBool returns the result of Lookup as a bool. +// Returns false if the key does not exist or the value +// is not a bool. +func LookupBool(key string) bool { + v, ok := Lookup(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(