mirror of https://github.com/knative/func.git
test: confirm repositories search path (#431)
This commit is contained in:
parent
8cfb448382
commit
66c26115b8
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
fn "knative.dev/kn-plugin-func"
|
||||
|
@ -32,6 +33,42 @@ func TestCreateValidatesName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestCreateRepositoriesPath ensures that the create command utilizes the
|
||||
// expected repositories path, respecting the setting for XDG_CONFIG_PATH
|
||||
// when deriving the default
|
||||
func TestCreateRepositoriesPath(t *testing.T) {
|
||||
defer fromTempDir(t)()
|
||||
|
||||
// Update XDG_CONFIG_HOME to point to some arbitrary location.
|
||||
xdgConfigHome, err := ioutil.TempDir("", "alice")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
os.Setenv("XDG_CONFIG_HOME", xdgConfigHome)
|
||||
|
||||
// The expected full path to repositories:
|
||||
expected := filepath.Join(xdgConfigHome, "func", "repositories")
|
||||
|
||||
// Create command takes a function which will be invoked with the final
|
||||
// state of the createConfig, usually used to do fn.Client instantiation
|
||||
// after flags, environment variables, etc. are calculated. In this case it
|
||||
// will validate the test condition: that config reflects the value of
|
||||
// XDG_CONFIG_HOME, and secondarily the path suffix `func/repositories`.
|
||||
cmd := NewCreateCmd(func(cfg createConfig) *fn.Client {
|
||||
if cfg.Repositories != expected {
|
||||
t.Fatalf("expected repositories default path to be '%v', got '%v'", expected, cfg.Repositories)
|
||||
}
|
||||
return fn.New()
|
||||
})
|
||||
|
||||
// Invoke the command, which is an airball, but does invoke the client constructor, which
|
||||
// which evaluates the aceptance condition of ensuring the default repositories path was
|
||||
// updated based on the value of XDG_CONFIG_HOME.
|
||||
if err = cmd.Execute(); err != nil {
|
||||
t.Fatalf("unexpected error running 'create' with a default (noop) client instance: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Helpers ----
|
||||
|
||||
// change directory into a new temp directory.
|
||||
|
|
32
cmd/root.go
32
cmd/root.go
|
@ -110,21 +110,33 @@ func cwd() (cwd string) {
|
|||
return cwd
|
||||
}
|
||||
|
||||
// The name of the config directory within ~/.config (or configured location)
|
||||
const configDirName = "func"
|
||||
|
||||
// configPath is the effective path to the optional config directory used for
|
||||
// function defaults and extensible templates.
|
||||
func configPath() (path string) {
|
||||
if path = os.Getenv("XDG_CONFIG_HOME"); path != "" {
|
||||
path = filepath.Join(path, "func")
|
||||
return
|
||||
func configPath() string {
|
||||
// Use XDG_CONFIG_HOME/func if defined
|
||||
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
|
||||
// TODO: create if not exist
|
||||
return filepath.Join(xdg, configDirName)
|
||||
}
|
||||
|
||||
// Expand and use ~/.config/func
|
||||
home, err := homedir.Expand("~")
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "could not derive home directory for use as default templates path: %v", err)
|
||||
path = filepath.Join(".config", "func")
|
||||
} else {
|
||||
path = filepath.Join(home, ".config", "func")
|
||||
if err == nil {
|
||||
// TODO: ensureConfigPath(home)
|
||||
return filepath.Join(home, ".config", configDirName)
|
||||
}
|
||||
return
|
||||
|
||||
// default is .config in current working directory, used when there is no
|
||||
// available home in which to find a .`.config/func` directory.
|
||||
// A case could be made that a panic is in order in this scenario, but
|
||||
// currently this seems like a nonfatal situation, as in the scenario
|
||||
// "there is no home directory", the fallback of using `.config` if extant
|
||||
// may very well be the optimal choice.
|
||||
fmt.Fprintf(os.Stderr, "Error locating ~/.config: %v", err)
|
||||
return filepath.Join(".config", configDirName)
|
||||
}
|
||||
|
||||
// bindFunc which conforms to the cobra PreRunE method signature
|
||||
|
|
Loading…
Reference in New Issue