summaryrefslogtreecommitdiff
path: root/paths
diff options
context:
space:
mode:
Diffstat (limited to 'paths')
-rw-r--r--paths/path.go20
-rw-r--r--paths/paths.go53
-rw-r--r--paths/paths_test.go43
-rw-r--r--paths/paths_testutil.go26
4 files changed, 90 insertions, 52 deletions
diff --git a/paths/path.go b/paths/path.go
new file mode 100644
index 0000000..5706d29
--- /dev/null
+++ b/paths/path.go
@@ -0,0 +1,20 @@
+package paths
+
+type Path struct {
+ Val *string
+}
+
+func (p *Path) Set(val string) {
+ p.Val = &val
+}
+
+func (p *Path) String() string {
+ if p == nil || p.Val == nil {
+ return "<empty>"
+ }
+ return *p.Val
+}
+
+func Empty() *Path {
+ return &Path{}
+}
diff --git a/paths/paths.go b/paths/paths.go
index cc0c878..b714a23 100644
--- a/paths/paths.go
+++ b/paths/paths.go
@@ -3,23 +3,39 @@ package paths
import (
"github.com/adrg/xdg"
"os"
+ "path"
"path/filepath"
"strings"
)
var (
- Ifs string
- Config string
- Cache string
- This string
- Home string
+ Ifs string
+ ConfigDir = Empty()
+ CacheDir = Empty()
+ ContextsFile = Empty()
+ HomeDir = Empty()
+ System = Empty()
+ This = "pik"
)
+var Paths = []*Path{
+ ConfigDir,
+ CacheDir,
+ ContextsFile,
+ HomeDir,
+ System,
+}
+
+var DirectoriesToMake = []*Path{
+ ConfigDir,
+ CacheDir,
+}
+
type paths struct {
Initialized bool
}
-var Paths = &paths{
+var Component = &paths{
Initialized: false,
}
@@ -31,20 +47,21 @@ func (p *paths) Init() error {
xdg.Reload()
}
- Home = xdg.Home
- This = "pik"
- Cache = filepath.Join(xdg.CacheHome, This)
- Config = filepath.Join(xdg.ConfigHome, This)
+ HomeDir.Set(xdg.Home)
+ CacheDir.Set(filepath.Join(xdg.CacheHome, This))
+ ConfigDir.Set(filepath.Join(xdg.ConfigHome, This))
+ ContextsFile.Set(path.Join(CacheDir.String(), "contexts"))
+ System.Set(path.Join(ConfigDir.String()))
+
Ifs = os.Getenv("IFS")
- err := os.MkdirAll(Cache, 0700)
- if err != nil {
- return err
- }
- err = os.MkdirAll(Config, 0700)
- if err != nil {
- return err
+ for _, d := range DirectoriesToMake {
+ err := os.MkdirAll(d.String(), 0700)
+ if err != nil {
+ return err
+ }
}
+
if Ifs == "" {
Ifs = "\n"
}
@@ -53,5 +70,5 @@ func (p *paths) Init() error {
}
func ReplaceHome(input string) string {
- return strings.Replace(input, Home, "~", 1)
+ return strings.Replace(input, HomeDir.String(), "~", 1)
}
diff --git a/paths/paths_test.go b/paths/paths_test.go
index 8ab3461..1dd9034 100644
--- a/paths/paths_test.go
+++ b/paths/paths_test.go
@@ -4,42 +4,17 @@ package paths
import (
"github.com/stretchr/testify/assert"
+ "strconv"
"testing"
)
-var bHome, bThis, bCache, bConfig string
-
-// Set temporarily sets the paths for unit test purposes
-// remember to defer Reset
-func Set(home, this, cache, config string) {
- bHome = Home
- bThis = This
- bCache = Cache
- bConfig = Config
-
- Home = home
- This = this
- Cache = cache
- Config = config
-}
-
-// Reset sets the path variables back to before the unit test
-func Reset() {
- Home = bHome
- This = bThis
- Cache = bCache
- Config = bConfig
-}
-
func TestSetAndReset(t *testing.T) {
- Set("1", "2", "3", "4")
- assert.Equal(t, Home, "1")
- assert.Equal(t, This, "2")
- assert.Equal(t, Cache, "3")
- assert.Equal(t, Config, "4")
- Reset()
- assert.NotEqual(t, Home, "1")
- assert.NotEqual(t, This, "2")
- assert.NotEqual(t, Cache, "3")
- assert.NotEqual(t, Config, "4")
+ for i, pt := range Paths {
+ old := pt.String()
+ val := strconv.Itoa(i)
+ Set(pt, val)
+ assert.Equal(t, pt.String(), val)
+ Reset()
+ assert.Equal(t, pt.String(), old)
+ }
}
diff --git a/paths/paths_testutil.go b/paths/paths_testutil.go
new file mode 100644
index 0000000..5b8af48
--- /dev/null
+++ b/paths/paths_testutil.go
@@ -0,0 +1,26 @@
+//go:build test
+
+package paths
+
+var backups = make(map[*Path]string)
+
+func SetAll(folder string) {
+ for _, p := range Paths {
+ Set(p, folder)
+ }
+}
+
+// Set temporarily sets the paths for unit test purposes
+// remember to defer Reset
+func Set(target *Path, value string) {
+ backups[target] = target.String()
+ target.Set(value)
+}
+
+// Reset sets the path variables back to before the unit test
+func Reset() {
+ for path, oldValue := range backups {
+ path.Set(oldValue)
+ }
+ backups = make(map[*Path]string)
+}