Skip to content

Commit

Permalink
Add a LookupTrue utility function
Browse files Browse the repository at this point in the history
It makes it a little bit clearer to make these queries.
  • Loading branch information
inancgumus committed Aug 17, 2023
1 parent e7cbc5c commit 783314e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
21 changes: 19 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package env provides types to interact with environment setup.
package env

import "os"
import (
"os"
"strconv"
)

// Execution specific.
const (
Expand Down Expand Up @@ -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) }
Expand All @@ -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
}
33 changes: 14 additions & 19 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tests
import (
"context"
"net/http"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 783314e

Please sign in to comment.