Skip to content

Commit

Permalink
Merge pull request #180 from cli/jtmcg/accessibility-isEnabled
Browse files Browse the repository at this point in the history
Add accessibility package and isEnabled function
  • Loading branch information
jtmcg authored Feb 7, 2025
2 parents 13104ed + 50d7ff4 commit a5c6b5e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 33 deletions.
21 changes: 21 additions & 0 deletions internal/testutils/config_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package testutils

import (
"testing"

"github.com/cli/go-gh/v2/pkg/config"
)

// StubConfig replaces the config.Read function with a function that returns a config object
// created from the given config string. It also sets up a cleanup function that restores the
// original config.Read function.
func StubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func(_ *config.Config) (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}
33 changes: 33 additions & 0 deletions pkg/accessibility/accessibility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package accessibility

import (
"os"

"github.com/cli/go-gh/v2/pkg/config"
)

// ACCESSIBILITY_ENV is the name of the environment variable that can be used to enable
// accessibility features. If the value is empty, "0", or "false", the accessibility
// features are disabled. Any other value enables the accessibility features. Note that
// this environment variable supercedes the configuration file's accessible setting.
const ACCESSIBILITY_ENV = "ACCESSIBILE"

// IsEnabled returns true if accessibility features are enabled via the ACCESSIBILITY
// environment variable or the configuration file.
func IsEnabled() bool {
envVar := os.Getenv(ACCESSIBILITY_ENV)
if envVar != "" {
return isEnvVarEnabled(envVar)
}

// We are not handling errors because we don't want to fail if the config is not
// read. Instead, we assume an empty configuration is equivalent to "disabled".
cfg, _ := config.Read(nil)
accessibleConfigValue, _ := cfg.Get([]string{"accessible"})

return accessibleConfigValue == "enabled"
}

func isEnvVarEnabled(envVar string) bool {
return envVar != "" && envVar != "0" && envVar != "false"
}
91 changes: 91 additions & 0 deletions pkg/accessibility/accessibility_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package accessibility

import (
"testing"

"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
)

func TestIsEnabled(t *testing.T) {
tests := []struct {
name string
envVarValue string
cfgStr string
wantOut bool
}{
{
name: "When the accessibility configuration and env var are both unset, it should return false",
envVarValue: "",
cfgStr: "",
wantOut: false,
},
{
name: "When the accessibility configuration is unset but the ACCESSIBLE env var is set to something truthy (not '0' or 'false'), it should return true",
envVarValue: "1",
cfgStr: "",
wantOut: true,
},
{
name: "When the accessibility configuration is unset and the ACCESSIBLE env var returns '0', it should return false",
envVarValue: "0",
cfgStr: "",
wantOut: false,
},
{
name: "When the accessibility configuration is unset and the ACCESSIBLE env var returns 'false', it should return false",
envVarValue: "false",
cfgStr: "",
wantOut: false,
},
{
name: "When the accessibility configuration is set to enabled and the env var is unset, it should return true",
envVarValue: "",
cfgStr: accessibilityEnabledConfig(),
wantOut: true,
},
{
name: "When the accessibility configuration is set to disabled and the env var is unset, it should return false",
envVarValue: "",
cfgStr: accessibilityDisabledConfig(),
wantOut: false,
},
{
name: "When the accessibility configuration is set to disabled and the env var is set to something truthy (not '0' or 'false'), it should return true",
envVarValue: "true",
cfgStr: accessibilityDisabledConfig(),
wantOut: true,
},
{
name: "When the accessibility configuration is set to enabled and the env var is set to '0', it should return false",
envVarValue: "0",
cfgStr: accessibilityEnabledConfig(),
wantOut: false,
},
{
name: "When the accessibility configuration is set to enabled and the env var is set to 'false', it should return false",
envVarValue: "false",
cfgStr: accessibilityEnabledConfig(),
wantOut: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("ACCESSIBILE", tt.envVarValue)
testutils.StubConfig(t, tt.cfgStr)
assert.Equal(t, tt.wantOut, IsEnabled())
})
}
}

func accessibilityEnabledConfig() string {
return `
accessible: enabled
`
}

func accessibilityDisabledConfig() string {
return `
accessible: disabled
`
}
3 changes: 2 additions & 1 deletion pkg/api/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"net/http"
"testing"

"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
)

func TestResolveOptions(t *testing.T) {
stubConfig(t, testConfigWithSocket())
testutils.StubConfig(t, testConfigWithSocket())

tests := []struct {
name string
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/graphql_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"testing"
"time"

"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestGraphQLClient(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand All @@ -34,7 +35,7 @@ func TestGraphQLClient(t *testing.T) {
}

func TestGraphQLClientDoError(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand All @@ -56,7 +57,7 @@ func TestGraphQLClientDoError(t *testing.T) {
}

func TestGraphQLClientQueryError(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand All @@ -78,7 +79,7 @@ func TestGraphQLClientQueryError(t *testing.T) {
}

func TestGraphQLClientMutateError(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand Down
15 changes: 2 additions & 13 deletions pkg/api/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"strings"
"testing"

"github.com/cli/go-gh/v2/pkg/config"
"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestHTTPClient(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand Down Expand Up @@ -177,17 +177,6 @@ func defaultHeaders() http.Header {
return h
}

func stubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func(_ *config.Config) (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}

func printPendingMocks(mocks []gock.Mock) string {
paths := []string{}
for _, mock := range mocks {
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/rest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"testing"
"time"

"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
"gopkg.in/h2non/gock.v1"
)

func TestRESTClient(t *testing.T) {
stubConfig(t, testConfig())
testutils.StubConfig(t, testConfig())
t.Cleanup(gock.Off)

gock.New("https://api.github.com").
Expand Down
17 changes: 3 additions & 14 deletions pkg/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package repository
import (
"testing"

"github.com/cli/go-gh/v2/pkg/config"
"github.com/cli/go-gh/v2/internal/testutils"
"github.com/stretchr/testify/assert"
)

func TestParse(t *testing.T) {
stubConfig(t, "")
testutils.StubConfig(t, "")

tests := []struct {
name string
Expand Down Expand Up @@ -106,7 +106,7 @@ hosts:
oauth_token: yyyyyyyyyyyyyyyyyyyy
git_protocol: https
`
stubConfig(t, cfgStr)
testutils.StubConfig(t, cfgStr)
r, err := Parse("OWNER/REPO")
assert.NoError(t, err)
assert.Equal(t, "enterprise.com", r.Host)
Expand Down Expand Up @@ -189,14 +189,3 @@ func TestParseWithHost(t *testing.T) {
})
}
}

func stubConfig(t *testing.T, cfgStr string) {
t.Helper()
old := config.Read
config.Read = func(_ *config.Config) (*config.Config, error) {
return config.ReadFromString(cfgStr), nil
}
t.Cleanup(func() {
config.Read = old
})
}

0 comments on commit a5c6b5e

Please sign in to comment.