Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add accessibility package and isEnabled function #180

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
})
}
29 changes: 29 additions & 0 deletions pkg/accessibility/accessibility.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package accessibility

import (
"os"

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

const ACCESSIBILITY_ENV = "ACCESSIBILITY"

// 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: "0",
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: "false",
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("ACCESSIBILITY", 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
})
}
Loading