courgette/internal/commands/configuration_test.go

47 lines
1.2 KiB
Go

package commands
import (
"os/user"
"path/filepath"
"strings"
"testing"
)
func TestGetCacheDirDefault(t *testing.T) {
configuration, _ := NewConfigFromYamlBytes([]byte(""))
parts := strings.Split(defaultCacheDirectory, "/")
user, _ := user.Current()
defaultPathParts := []string{user.HomeDir}
defaultPathParts = append(defaultPathParts, parts[1:]...)
defaultPath := filepath.Join(defaultPathParts...)
if configuration.GetCacheDir() != defaultPath {
t.Errorf("Expected default %s, got %s instead.", defaultPath, configuration.GetCacheDir())
}
}
func TestGetCacheDirExpandsTilde(t *testing.T) {
configuration, _ := NewConfigFromYamlBytes([]byte(""))
user, _ := user.Current()
cacheDir := configuration.GetCacheDir()
if filepath.HasPrefix(configuration.GetCacheDir(), "~") {
t.Errorf("Expected tilde to be expanded, was not.")
}
if !filepath.HasPrefix(cacheDir, user.HomeDir) {
t.Errorf("Expected tilder to be expanded, was not.")
}
}
func TestGetCacheDirReturnsCachePath(t *testing.T) {
sample := `
cache:
dir: "/tmp/cache-dir"
`
configuration, _ := NewConfigFromYamlBytes([]byte(sample))
cacheDir := configuration.GetCacheDir()
if cacheDir != "/tmp/cache-dir" {
t.Errorf("Unexpected cache path: %s", cacheDir)
}
}