mirror of https://github.com/cheat/cheat.git
fix(tests): fix `config.Paths` tests
Refactor `config.Paths` (by externalizing a call to `homedir.Dir`) to decouple it from filesystem paths, thus facilitating cleaner unit-tests.
This commit is contained in:
parent
a08dca70d9
commit
8e1580ff5a
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docopt/docopt-go"
|
"github.com/docopt/docopt-go"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
@ -34,6 +35,13 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the user's home directory
|
||||||
|
home, err := homedir.Dir()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// read the envvars into a map of strings
|
// read the envvars into a map of strings
|
||||||
envvars := map[string]string{}
|
envvars := map[string]string{}
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
|
@ -42,7 +50,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the os-specifc paths at which the config file may be located
|
// load the os-specifc paths at which the config file may be located
|
||||||
confpaths, err := config.Paths(runtime.GOOS, envvars)
|
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -9,13 +9,11 @@ import (
|
||||||
|
|
||||||
// Paths returns config file paths that are appropriate for the operating
|
// Paths returns config file paths that are appropriate for the operating
|
||||||
// system
|
// system
|
||||||
func Paths(sys string, envvars map[string]string) ([]string, error) {
|
func Paths(
|
||||||
|
sys string,
|
||||||
// get the user's home directory
|
home string,
|
||||||
home, err := homedir.Dir()
|
envvars map[string]string,
|
||||||
if err != nil {
|
) ([]string, error) {
|
||||||
return []string{}, fmt.Errorf("failed to get user home directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if `CHEAT_CONFIG_PATH` is set, expand ~ and return it
|
// if `CHEAT_CONFIG_PATH` is set, expand ~ and return it
|
||||||
if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok {
|
if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok {
|
||||||
|
|
|
@ -11,9 +11,11 @@ import (
|
||||||
// *nix platforms
|
// *nix platforms
|
||||||
func TestValidatePathsNix(t *testing.T) {
|
func TestValidatePathsNix(t *testing.T) {
|
||||||
|
|
||||||
|
// mock the user's home directory
|
||||||
|
home := "/home/foo"
|
||||||
|
|
||||||
// mock some envvars
|
// mock some envvars
|
||||||
envvars := map[string]string{
|
envvars := map[string]string{
|
||||||
"HOME": "/home/foo",
|
|
||||||
"XDG_CONFIG_HOME": "/home/bar",
|
"XDG_CONFIG_HOME": "/home/bar",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ func TestValidatePathsNix(t *testing.T) {
|
||||||
// test each *nix os
|
// test each *nix os
|
||||||
for _, os := range oses {
|
for _, os := range oses {
|
||||||
// get the paths for the platform
|
// get the paths for the platform
|
||||||
paths, err := Paths(os, envvars)
|
paths, err := Paths(os, home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("paths returned an error: %v", err)
|
t.Errorf("paths returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -54,10 +56,11 @@ func TestValidatePathsNix(t *testing.T) {
|
||||||
// on *nix platforms when `XDG_CONFIG_HOME is not set
|
// on *nix platforms when `XDG_CONFIG_HOME is not set
|
||||||
func TestValidatePathsNixNoXDG(t *testing.T) {
|
func TestValidatePathsNixNoXDG(t *testing.T) {
|
||||||
|
|
||||||
|
// mock the user's home directory
|
||||||
|
home := "/home/foo"
|
||||||
|
|
||||||
// mock some envvars
|
// mock some envvars
|
||||||
envvars := map[string]string{
|
envvars := map[string]string{}
|
||||||
"HOME": "/home/foo",
|
|
||||||
}
|
|
||||||
|
|
||||||
// specify the platforms to test
|
// specify the platforms to test
|
||||||
oses := []string{
|
oses := []string{
|
||||||
|
@ -69,7 +72,7 @@ func TestValidatePathsNixNoXDG(t *testing.T) {
|
||||||
// test each *nix os
|
// test each *nix os
|
||||||
for _, os := range oses {
|
for _, os := range oses {
|
||||||
// get the paths for the platform
|
// get the paths for the platform
|
||||||
paths, err := Paths(os, envvars)
|
paths, err := Paths(os, home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("paths returned an error: %v", err)
|
t.Errorf("paths returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -95,6 +98,9 @@ func TestValidatePathsNixNoXDG(t *testing.T) {
|
||||||
// on Windows platforms
|
// on Windows platforms
|
||||||
func TestValidatePathsWindows(t *testing.T) {
|
func TestValidatePathsWindows(t *testing.T) {
|
||||||
|
|
||||||
|
// mock the user's home directory
|
||||||
|
home := "not-used-on-windows"
|
||||||
|
|
||||||
// mock some envvars
|
// mock some envvars
|
||||||
envvars := map[string]string{
|
envvars := map[string]string{
|
||||||
"APPDATA": "/apps",
|
"APPDATA": "/apps",
|
||||||
|
@ -102,7 +108,7 @@ func TestValidatePathsWindows(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the paths for the platform
|
// get the paths for the platform
|
||||||
paths, err := Paths("windows", envvars)
|
paths, err := Paths("windows", home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("paths returned an error: %v", err)
|
t.Errorf("paths returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -126,7 +132,7 @@ func TestValidatePathsWindows(t *testing.T) {
|
||||||
// TestValidatePathsUnsupported asserts that an error is returned on
|
// TestValidatePathsUnsupported asserts that an error is returned on
|
||||||
// unsupported platforms
|
// unsupported platforms
|
||||||
func TestValidatePathsUnsupported(t *testing.T) {
|
func TestValidatePathsUnsupported(t *testing.T) {
|
||||||
_, err := Paths("unsupported", map[string]string{})
|
_, err := Paths("unsupported", "", map[string]string{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("failed to return error on unsupported platform")
|
t.Errorf("failed to return error on unsupported platform")
|
||||||
}
|
}
|
||||||
|
@ -136,15 +142,17 @@ func TestValidatePathsUnsupported(t *testing.T) {
|
||||||
// returned when `CHEAT_CONFIG_PATH` is explicitly specified.
|
// returned when `CHEAT_CONFIG_PATH` is explicitly specified.
|
||||||
func TestValidatePathsCheatConfigPath(t *testing.T) {
|
func TestValidatePathsCheatConfigPath(t *testing.T) {
|
||||||
|
|
||||||
|
// mock the user's home directory
|
||||||
|
home := "/home/foo"
|
||||||
|
|
||||||
// mock some envvars
|
// mock some envvars
|
||||||
envvars := map[string]string{
|
envvars := map[string]string{
|
||||||
"HOME": "/home/foo",
|
|
||||||
"XDG_CONFIG_HOME": "/home/bar",
|
"XDG_CONFIG_HOME": "/home/bar",
|
||||||
"CHEAT_CONFIG_PATH": "/home/baz/conf.yml",
|
"CHEAT_CONFIG_PATH": "/home/baz/conf.yml",
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the paths for the platform
|
// get the paths for the platform
|
||||||
paths, err := Paths("linux", envvars)
|
paths, err := Paths("linux", home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("paths returned an error: %v", err)
|
t.Errorf("paths returned an error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue