2019-10-20 16:02:28 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2019-11-08 01:59:18 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
2019-10-20 16:02:28 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-11-08 01:59:18 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-10-20 16:02:28 +02:00
|
|
|
|
2019-11-08 01:59:18 +01:00
|
|
|
"github.com/cheat/cheat/internal/cheatpath"
|
|
|
|
"github.com/cheat/cheat/internal/mock"
|
2019-10-20 16:02:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestConfig asserts that the configs are loaded correctly
|
|
|
|
func TestConfigSuccessful(t *testing.T) {
|
|
|
|
|
|
|
|
// initialize a config
|
2019-11-08 01:59:18 +01:00
|
|
|
conf, err := New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to parse config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert that the expected values were returned
|
|
|
|
if conf.Editor != "vim" {
|
|
|
|
t.Errorf("failed to set editor: want: vim, got: %s", conf.Editor)
|
|
|
|
}
|
|
|
|
if !conf.Colorize {
|
|
|
|
t.Errorf("failed to set colorize: want: true, got: %t", conf.Colorize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the user's home directory (with ~ expanded)
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to get homedir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert that the cheatpaths are correct
|
|
|
|
want := []cheatpath.Cheatpath{
|
|
|
|
cheatpath.Cheatpath{
|
2022-07-04 22:55:57 +02:00
|
|
|
Path: filepath.Join(home, ".dotfiles", "cheat", "community"),
|
2019-10-20 16:02:28 +02:00
|
|
|
ReadOnly: true,
|
|
|
|
Tags: []string{"community"},
|
|
|
|
},
|
|
|
|
cheatpath.Cheatpath{
|
2022-07-04 22:55:57 +02:00
|
|
|
Path: filepath.Join(home, ".dotfiles", "cheat", "work"),
|
2019-10-20 16:02:28 +02:00
|
|
|
ReadOnly: false,
|
|
|
|
Tags: []string{"work"},
|
|
|
|
},
|
|
|
|
cheatpath.Cheatpath{
|
2022-07-04 22:55:57 +02:00
|
|
|
Path: filepath.Join(home, ".dotfiles", "cheat", "personal"),
|
2019-10-20 16:02:28 +02:00
|
|
|
ReadOnly: false,
|
|
|
|
Tags: []string{"personal"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(conf.Cheatpaths, want) {
|
|
|
|
t.Errorf(
|
|
|
|
"failed to return expected results: want:\n%s, got:\n%s",
|
|
|
|
spew.Sdump(want),
|
|
|
|
spew.Sdump(conf.Cheatpaths),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestConfigFailure asserts that an error is returned if the config file
|
|
|
|
// cannot be read.
|
|
|
|
func TestConfigFailure(t *testing.T) {
|
|
|
|
|
|
|
|
// attempt to read a non-existent config file
|
2019-11-08 01:59:18 +01:00
|
|
|
_, err := New(map[string]interface{}{}, "/does-not-exit", false)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("failed to error on unreadable config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestEmptyEditor asserts that envvars are respected if an editor is not
|
|
|
|
// specified in the configs
|
|
|
|
func TestEmptyEditor(t *testing.T) {
|
|
|
|
|
|
|
|
// clear the environment variables
|
|
|
|
os.Setenv("VISUAL", "")
|
|
|
|
os.Setenv("EDITOR", "")
|
|
|
|
|
|
|
|
// initialize a config
|
2019-11-08 01:59:18 +01:00
|
|
|
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
2022-07-04 22:06:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to initialize test: %v", err)
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set editor, and assert that it is respected
|
|
|
|
os.Setenv("EDITOR", "foo")
|
2019-11-08 01:59:18 +01:00
|
|
|
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to init configs: %v", err)
|
|
|
|
}
|
|
|
|
if conf.Editor != "foo" {
|
|
|
|
t.Errorf("failed to respect editor: want: foo, got: %s", conf.Editor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set visual, and assert that it overrides editor
|
|
|
|
os.Setenv("VISUAL", "bar")
|
2019-11-08 01:59:18 +01:00
|
|
|
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to init configs: %v", err)
|
|
|
|
}
|
|
|
|
if conf.Editor != "bar" {
|
|
|
|
t.Errorf("failed to respect editor: want: bar, got: %s", conf.Editor)
|
|
|
|
}
|
|
|
|
}
|