Skip to content

Commit

Permalink
fix panic at config root dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryooooooga committed Apr 25, 2021
1 parent 2a8d762 commit e63cb04
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ deps:
zouch: $(shell find . -name "*.go")
go build -v

.PHONY: test
test: deps
go test -v ./...

.PHONY: clean
clean:
${RM} zouch
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func runCommand(c *cli.Context) error {
}

logger := newLogger(verboseFlag)
rootDir := config.GetRootDir()
rootDir := config.NewConfig().RootDir()

app := newApp(logger, rootDir, createDirFlag, forceFlag)

Expand Down
27 changes: 20 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ import (
)

const (
rootEnvKey = "ZOUCH_ROOT"
RootEnvKey = "ZOUCH_ROOT"
XdgConfigHomeEnvKey = "XDG_COFNIG_HOME"
)

func GetRootDir() string {
type Config struct {
userHomeDir func() (string, error)
lookupEnv func(key string) (string, bool)
}

func NewConfig() *Config {
return &Config{
userHomeDir: os.UserHomeDir,
lookupEnv: os.LookupEnv,
}
}

func (c *Config) RootDir() string {
// "$ZOUCH_ROOT"
if root, ok := os.LookupEnv(rootEnvKey); ok {
if root, ok := c.lookupEnv(RootEnvKey); ok {
return root
}
// "$XDG_CONFIG_HOME/zouch"
if xdgConfigHome, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
if xdgConfigHome, ok := c.lookupEnv(XdgConfigHomeEnvKey); ok {
return path.Join(xdgConfigHome, "zouch")
}
// "$HOME/.config/zouch"
home, err := os.UserHomeDir()
home, err := c.userHomeDir()
if err != nil {
return path.Join(home, ".config", "zouch")
panic(err)
}
panic(err)
return path.Join(home, ".config", "zouch")
}
74 changes: 74 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package config

import (
"testing"
)

func TestRootDir(t *testing.T) {
type envs struct {
home string
xdgConfigHome string
zouchRoot string
}
type scenario struct {
expectedRootDir string
envs envs
}

scenarios := []scenario{
{
expectedRootDir: "/home/USER/.config/zouch",
envs: envs{
home: "/home/USER",
xdgConfigHome: "",
zouchRoot: "",
},
},
{
expectedRootDir: "/home/USER/xdgConfig/zouch",
envs: envs{
home: "/home/USER",
xdgConfigHome: "/home/USER/xdgConfig",
zouchRoot: "",
},
},
{
expectedRootDir: "/tmp/zouch",
envs: envs{
home: "/home/USER",
xdgConfigHome: "/home/USER/xdgConfig",
zouchRoot: "/tmp/zouch",
},
},
}

for _, s := range scenarios {
t.Run(s.expectedRootDir, func(t *testing.T) {
c := newTestConfig(s.envs.home, s.envs.xdgConfigHome, s.envs.zouchRoot)

if rootDir := c.RootDir(); rootDir != s.expectedRootDir {
t.Fatalf("expected rootDir == %v, actual %v", s.expectedRootDir, rootDir)
}
})
}
}

func newTestConfig(home string, xdgConfigHome string, zouchRoot string) *Config {
return &Config{
// Stub of os.UserHomeDir
userHomeDir: func() (string, error) {
return home, nil
},
// Stub of os.LookupEnv
lookupEnv: func(key string) (string, bool) {
switch key {
case XdgConfigHomeEnvKey:
return xdgConfigHome, len(xdgConfigHome) > 0
case RootEnvKey:
return zouchRoot, len(zouchRoot) > 0
default:
return "", false
}
},
}
}

0 comments on commit e63cb04

Please sign in to comment.