mirror of
https://github.com/cheat/cheat.git
synced 2024-11-23 14:31:36 +01:00
ce37b670c7
Squashed commit of the following: commit5c322e79b7
Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:56:56 2020 -0500 docs(README): update the `README` Update the `README` to document the improved config-generation mechanism. commit803e1f014c
Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 19:19:49 2020 -0500 feat(config-init): platform-specific pathing Update `--init` subcommand to rely upon the same platform-detection intelligence that was previously implemented by the "installer". The installer and `--init` should now produce identical config files. commit99c48097e2
Author: Chris Lane <chris@chris-allen-lane.com> Date: Fri Mar 6 18:26:33 2020 -0500 feat(installer): platform-correct config templating Modify the "installer" to populate cheatpaths with sensible defaults based on the detection of the user's operating system and environment. commit8e1580ff5a
Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 20:19:58 2020 -0500 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. commita08dca70d9
Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 18:14:27 2020 -0500 feat(installer): default path selection Modify the installer to improve default config and cheatsheet path selection. commite15bc6c966
Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:49:50 2020 -0500 fix(typo): correct comment typo in `main.go` commitefd09575df
Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:46:49 2020 -0500 feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. commitec10244ebe
Author: Chris Lane <chris@chris-allen-lane.com> Date: Thu Mar 5 17:15:28 2020 -0500 chore(installer): delete unused file Delete `installer/installer.go`, which (in hindsight) was unnecessary. commitebd9ec6287
Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:31:13 2020 -0500 wip(installer): stub experimental "installer" Stubs out an experimental "installer" that will help new users to quickly configure `cheat`. commitecac5a0971
Author: Chris Lane <chris@chris-allen-lane.com> Date: Wed Mar 4 19:30:12 2020 -0500 chore(dependencies): updates vendored dependencies
174 lines
3.8 KiB
Go
174 lines
3.8 KiB
Go
package config
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
)
|
|
|
|
// TestValidatePathsNix asserts that the proper config paths are returned on
|
|
// *nix platforms
|
|
func TestValidatePathsNix(t *testing.T) {
|
|
|
|
// mock the user's home directory
|
|
home := "/home/foo"
|
|
|
|
// mock some envvars
|
|
envvars := map[string]string{
|
|
"XDG_CONFIG_HOME": "/home/bar",
|
|
}
|
|
|
|
// specify the platforms to test
|
|
oses := []string{
|
|
"darwin",
|
|
"freebsd",
|
|
"linux",
|
|
}
|
|
|
|
// test each *nix os
|
|
for _, os := range oses {
|
|
// get the paths for the platform
|
|
paths, err := Paths(os, home, envvars)
|
|
if err != nil {
|
|
t.Errorf("paths returned an error: %v", err)
|
|
}
|
|
|
|
// specify the expected output
|
|
want := []string{
|
|
"/home/bar/cheat/conf.yml",
|
|
"/home/foo/.config/cheat/conf.yml",
|
|
"/home/foo/.cheat/conf.yml",
|
|
}
|
|
|
|
// assert that output matches expectations
|
|
if !reflect.DeepEqual(paths, want) {
|
|
t.Errorf(
|
|
"failed to return expected paths: want:\n%s, got:\n%s",
|
|
spew.Sdump(want),
|
|
spew.Sdump(paths),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestValidatePathsNixNoXDG asserts that the proper config paths are returned
|
|
// on *nix platforms when `XDG_CONFIG_HOME is not set
|
|
func TestValidatePathsNixNoXDG(t *testing.T) {
|
|
|
|
// mock the user's home directory
|
|
home := "/home/foo"
|
|
|
|
// mock some envvars
|
|
envvars := map[string]string{}
|
|
|
|
// specify the platforms to test
|
|
oses := []string{
|
|
"darwin",
|
|
"freebsd",
|
|
"linux",
|
|
}
|
|
|
|
// test each *nix os
|
|
for _, os := range oses {
|
|
// get the paths for the platform
|
|
paths, err := Paths(os, home, envvars)
|
|
if err != nil {
|
|
t.Errorf("paths returned an error: %v", err)
|
|
}
|
|
|
|
// specify the expected output
|
|
want := []string{
|
|
"/home/foo/.config/cheat/conf.yml",
|
|
"/home/foo/.cheat/conf.yml",
|
|
}
|
|
|
|
// assert that output matches expectations
|
|
if !reflect.DeepEqual(paths, want) {
|
|
t.Errorf(
|
|
"failed to return expected paths: want:\n%s, got:\n%s",
|
|
spew.Sdump(want),
|
|
spew.Sdump(paths),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestValidatePathsWindows asserts that the proper config paths are returned
|
|
// on Windows platforms
|
|
func TestValidatePathsWindows(t *testing.T) {
|
|
|
|
// mock the user's home directory
|
|
home := "not-used-on-windows"
|
|
|
|
// mock some envvars
|
|
envvars := map[string]string{
|
|
"APPDATA": "/apps",
|
|
"PROGRAMDATA": "/programs",
|
|
}
|
|
|
|
// get the paths for the platform
|
|
paths, err := Paths("windows", home, envvars)
|
|
if err != nil {
|
|
t.Errorf("paths returned an error: %v", err)
|
|
}
|
|
|
|
// specify the expected output
|
|
want := []string{
|
|
"/apps/cheat/conf.yml",
|
|
"/programs/cheat/conf.yml",
|
|
}
|
|
|
|
// assert that output matches expectations
|
|
if !reflect.DeepEqual(paths, want) {
|
|
t.Errorf(
|
|
"failed to return expected paths: want:\n%s, got:\n%s",
|
|
spew.Sdump(want),
|
|
spew.Sdump(paths),
|
|
)
|
|
}
|
|
}
|
|
|
|
// TestValidatePathsUnsupported asserts that an error is returned on
|
|
// unsupported platforms
|
|
func TestValidatePathsUnsupported(t *testing.T) {
|
|
_, err := Paths("unsupported", "", map[string]string{})
|
|
if err == nil {
|
|
t.Errorf("failed to return error on unsupported platform")
|
|
}
|
|
}
|
|
|
|
// TestValidatePathsCheatConfigPath asserts that the proper config path is
|
|
// returned when `CHEAT_CONFIG_PATH` is explicitly specified.
|
|
func TestValidatePathsCheatConfigPath(t *testing.T) {
|
|
|
|
// mock the user's home directory
|
|
home := "/home/foo"
|
|
|
|
// mock some envvars
|
|
envvars := map[string]string{
|
|
"XDG_CONFIG_HOME": "/home/bar",
|
|
"CHEAT_CONFIG_PATH": "/home/baz/conf.yml",
|
|
}
|
|
|
|
// get the paths for the platform
|
|
paths, err := Paths("linux", home, envvars)
|
|
if err != nil {
|
|
t.Errorf("paths returned an error: %v", err)
|
|
}
|
|
|
|
// specify the expected output
|
|
want := []string{
|
|
"/home/baz/conf.yml",
|
|
}
|
|
|
|
// assert that output matches expectations
|
|
if !reflect.DeepEqual(paths, want) {
|
|
t.Errorf(
|
|
"failed to return expected paths: want:\n%s, got:\n%s",
|
|
spew.Sdump(want),
|
|
spew.Sdump(paths),
|
|
)
|
|
}
|
|
}
|