diff --git a/internal/testutils/config_stub.go b/internal/testutils/config_stub.go new file mode 100644 index 0000000..ad5eeb2 --- /dev/null +++ b/internal/testutils/config_stub.go @@ -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 + }) +} diff --git a/pkg/accessibility/accessibility.go b/pkg/accessibility/accessibility.go new file mode 100644 index 0000000..b99e7e3 --- /dev/null +++ b/pkg/accessibility/accessibility.go @@ -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" +} diff --git a/pkg/accessibility/accessibility_test.go b/pkg/accessibility/accessibility_test.go new file mode 100644 index 0000000..1bd4f80 --- /dev/null +++ b/pkg/accessibility/accessibility_test.go @@ -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 +` +} diff --git a/pkg/api/client_options_test.go b/pkg/api/client_options_test.go index 237b9d5..35cfb34 100644 --- a/pkg/api/client_options_test.go +++ b/pkg/api/client_options_test.go @@ -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 diff --git a/pkg/api/graphql_client_test.go b/pkg/api/graphql_client_test.go index b27d38f..15cdc7a 100644 --- a/pkg/api/graphql_client_test.go +++ b/pkg/api/graphql_client_test.go @@ -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"). @@ -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"). @@ -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"). @@ -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"). diff --git a/pkg/api/http_client_test.go b/pkg/api/http_client_test.go index 9fe0934..19c911d 100644 --- a/pkg/api/http_client_test.go +++ b/pkg/api/http_client_test.go @@ -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"). @@ -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 { diff --git a/pkg/api/rest_client_test.go b/pkg/api/rest_client_test.go index bc2917e..51ac16a 100644 --- a/pkg/api/rest_client_test.go +++ b/pkg/api/rest_client_test.go @@ -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"). diff --git a/pkg/repository/repository_test.go b/pkg/repository/repository_test.go index dfcdca7..0430eab 100644 --- a/pkg/repository/repository_test.go +++ b/pkg/repository/repository_test.go @@ -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 @@ -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) @@ -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 - }) -}