mirror of https://github.com/cheat/cheat.git
fix: repairs broken config unit-tests
Repairs the `config` unit-tests which were broken with `3.0.4`. It does so by providing a simple switch that allows us to disable the resolution of filesystem symlinks when running tests.
This commit is contained in:
parent
51aaaf3423
commit
d6ebe0799d
|
@ -39,7 +39,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize the configs
|
// initialize the configs
|
||||||
conf, err := config.New(opts, confpath)
|
conf, err := config.New(opts, confpath, true)
|
||||||
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)
|
||||||
|
|
|
@ -22,7 +22,7 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Config struct
|
// New returns a new Config struct
|
||||||
func New(opts map[string]interface{}, confPath string) (Config, error) {
|
func New(opts map[string]interface{}, confPath string, resolve bool) (Config, error) {
|
||||||
|
|
||||||
// read the config file
|
// read the config file
|
||||||
buf, err := ioutil.ReadFile(confPath)
|
buf, err := ioutil.ReadFile(confPath)
|
||||||
|
@ -49,13 +49,22 @@ func New(opts map[string]interface{}, confPath string) (Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// follow symlinks
|
// follow symlinks
|
||||||
expanded, err = filepath.EvalSymlinks(expanded)
|
//
|
||||||
if err != nil {
|
// NB: `resolve` is an ugly kludge that exists for the sake of unit-tests.
|
||||||
return Config{}, fmt.Errorf(
|
// It's necessary because `EvalSymlinks` will error if the symlink points
|
||||||
"failed to resolve symlink: %s, %v",
|
// to a non-existent location on the filesystem. When unit-testing,
|
||||||
expanded,
|
// however, we don't want to have dependencies on the filesystem. As such,
|
||||||
err,
|
// `resolve` is a switch that allows us to turn off symlink resolution when
|
||||||
)
|
// running the config tests.
|
||||||
|
if resolve {
|
||||||
|
expanded, err = filepath.EvalSymlinks(expanded)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, fmt.Errorf(
|
||||||
|
"failed to resolve symlink: %s, %v",
|
||||||
|
expanded,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.Cheatpaths[i].Path = expanded
|
conf.Cheatpaths[i].Path = expanded
|
||||||
|
|
|
@ -1,26 +1,23 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"os"
|
"os"
|
||||||
//"path/filepath"
|
"path/filepath"
|
||||||
//"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
//"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
//"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
|
|
||||||
//"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
//"github.com/cheat/cheat/internal/mock"
|
"github.com/cheat/cheat/internal/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BUG: changes pertaining to symlink handling introduced in 3.0.4 break this
|
|
||||||
// test.
|
|
||||||
/*
|
|
||||||
// TestConfig asserts that the configs are loaded correctly
|
// TestConfig asserts that the configs are loaded correctly
|
||||||
func TestConfigSuccessful(t *testing.T) {
|
func TestConfigSuccessful(t *testing.T) {
|
||||||
|
|
||||||
// initialize a config
|
// initialize a config
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/conf.yml"))
|
conf, err := New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to parse config file: %v", err)
|
t.Errorf("failed to parse config file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -66,14 +63,13 @@ func TestConfigSuccessful(t *testing.T) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// TestConfigFailure asserts that an error is returned if the config file
|
// TestConfigFailure asserts that an error is returned if the config file
|
||||||
// cannot be read.
|
// cannot be read.
|
||||||
func TestConfigFailure(t *testing.T) {
|
func TestConfigFailure(t *testing.T) {
|
||||||
|
|
||||||
// attempt to read a non-existent config file
|
// attempt to read a non-existent config file
|
||||||
_, err := New(map[string]interface{}{}, "/does-not-exit")
|
_, err := New(map[string]interface{}{}, "/does-not-exit", false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("failed to error on unreadable config")
|
t.Errorf("failed to error on unreadable config")
|
||||||
}
|
}
|
||||||
|
@ -83,20 +79,19 @@ func TestConfigFailure(t *testing.T) {
|
||||||
// specified in the configs
|
// specified in the configs
|
||||||
func TestEmptyEditor(t *testing.T) {
|
func TestEmptyEditor(t *testing.T) {
|
||||||
|
|
||||||
/*
|
|
||||||
// clear the environment variables
|
// clear the environment variables
|
||||||
os.Setenv("VISUAL", "")
|
os.Setenv("VISUAL", "")
|
||||||
os.Setenv("EDITOR", "")
|
os.Setenv("EDITOR", "")
|
||||||
|
|
||||||
// initialize a config
|
// initialize a config
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"))
|
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("failed to return an error on empty editor")
|
t.Errorf("failed to return an error on empty editor")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set editor, and assert that it is respected
|
// set editor, and assert that it is respected
|
||||||
os.Setenv("EDITOR", "foo")
|
os.Setenv("EDITOR", "foo")
|
||||||
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"))
|
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to init configs: %v", err)
|
t.Errorf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -106,12 +101,11 @@ func TestEmptyEditor(t *testing.T) {
|
||||||
|
|
||||||
// set visual, and assert that it overrides editor
|
// set visual, and assert that it overrides editor
|
||||||
os.Setenv("VISUAL", "bar")
|
os.Setenv("VISUAL", "bar")
|
||||||
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"))
|
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to init configs: %v", err)
|
t.Errorf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
if conf.Editor != "bar" {
|
if conf.Editor != "bar" {
|
||||||
t.Errorf("failed to respect editor: want: bar, got: %s", conf.Editor)
|
t.Errorf("failed to respect editor: want: bar, got: %s", conf.Editor)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue