-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from cli/jtmcg/accessibility-isEnabled
Add accessibility package and isEnabled function
- Loading branch information
Showing
8 changed files
with
159 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters