File: dirs_test.go

package info (click to toggle)
golang-github-apparentlymart-go-userdirs 0.0~git20200915.b0c018a-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 184 kB
  • sloc: makefile: 2
file content (162 lines) | stat: -rw-r--r-- 4,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package userdirs

import (
	"path/filepath"
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestDirsNewPath(t *testing.T) {
	dirs := Dirs{
		ConfigDirs: []string{
			"/user/config",
			"/global/config",
		},
		DataDirs: []string{
			"/user/data",
			"/global/data",
		},
		CacheDir: "/cache",
	}

	t.Run("NewConfigPath", func(t *testing.T) {
		got := dirs.NewConfigPath("foo", "bar")
		want := filepath.Join("/user", "config", "foo", "bar")
		if got != want {
			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
		}
	})
	t.Run("NewDataPath", func(t *testing.T) {
		got := dirs.NewDataPath("foo", "bar")
		want := filepath.Join("/user", "data", "foo", "bar")
		if got != want {
			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
		}
	})
	t.Run("CachePath", func(t *testing.T) {
		got := dirs.CachePath("foo", "bar")
		want := filepath.Join("/cache", "foo", "bar")
		if got != want {
			t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
		}
	})
}

func TestDirsFindConfigFiles(t *testing.T) {
	t.Run("both", func(t *testing.T) {
		prefix, dirs := testdataDirs(t)
		got := dirs.FindConfigFiles("foo.conf")
		want := []string{
			filepath.Join(prefix, "user", "config", "foo.conf"),
			filepath.Join(prefix, "global", "config", "foo.conf"),
		}
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
	t.Run("only one", func(t *testing.T) {
		prefix, dirs := testdataDirs(t)
		got := dirs.FindConfigFiles("bar.conf")
		want := []string{
			filepath.Join(prefix, "global", "config", "bar.conf"),
		}
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
	t.Run("none", func(t *testing.T) {
		_, dirs := testdataDirs(t)
		got := dirs.FindConfigFiles("nonexist.conf")
		want := []string(nil)
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
}

func TestDirsFindDataFiles(t *testing.T) {
	t.Run("both", func(t *testing.T) {
		prefix, dirs := testdataDirs(t)
		got := dirs.FindDataFiles("baz.dat")
		want := []string{
			filepath.Join(prefix, "user", "data", "baz.dat"),
			filepath.Join(prefix, "global", "data", "baz.dat"),
		}
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
	t.Run("none", func(t *testing.T) {
		_, dirs := testdataDirs(t)
		got := dirs.FindDataFiles("nonexist.dat")
		want := []string(nil)
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
}

func TestDirsGlobConfigFiles(t *testing.T) {
	t.Run("all", func(t *testing.T) {
		prefix, dirs := testdataDirs(t)
		got := dirs.GlobConfigFiles("*.conf")
		want := []string{
			filepath.Join(prefix, "user", "config", "foo.conf"),
			filepath.Join(prefix, "global", "config", "bar.conf"),
			filepath.Join(prefix, "global", "config", "foo.conf"),
		}
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
	t.Run("none", func(t *testing.T) {
		_, dirs := testdataDirs(t)
		got := dirs.GlobConfigFiles("*.nope")
		want := []string(nil)
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
}

func TestDirsGlobDataFiles(t *testing.T) {
	t.Run("all", func(t *testing.T) {
		prefix, dirs := testdataDirs(t)
		got := dirs.GlobDataFiles("*.dat")
		want := []string{
			filepath.Join(prefix, "user", "data", "baz.dat"),
			filepath.Join(prefix, "global", "data", "baz.dat"),
		}
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
	t.Run("none", func(t *testing.T) {
		_, dirs := testdataDirs(t)
		got := dirs.GlobDataFiles("*.nope")
		want := []string(nil)
		if diff := cmp.Diff(want, got); diff != "" {
			t.Errorf("wrong result\n%s", diff)
		}
	})
}

func testdataDirs(t *testing.T) (prefix string, dirs Dirs) {
	prefix, err := filepath.Abs("testdata")
	if err != nil {
		t.Fatalf("cannot find absolute path for testdata dir: %s", err)
	}

	return prefix, Dirs{
		ConfigDirs: []string{
			filepath.Join(prefix, "user", "config"),
			filepath.Join(prefix, "global", "config"),
		},
		DataDirs: []string{
			filepath.Join(prefix, "user", "data"),
			filepath.Join(prefix, "global", "data"),
		},
		CacheDir: filepath.Join(prefix, "cache"),
	}
}