8 "github.com/stretchr/testify/require"
11 func TestDataDirFollowsTheOrder(t *testing.T) {
13 testCases := []struct {
21 name: "ONECHAT_DIR first",
22 dir: filepath.Join(root, "a"),
23 xdg: filepath.Join(root, "xdg"),
25 want: filepath.Join(root, "a"),
28 name: "absolute XDG_DATA_HOME",
30 xdg: filepath.Join(root, "xdg"),
32 want: filepath.Join(root, "xdg", "onechat"),
35 name: "relative XDG_DATA_HOME ignored",
39 want: filepath.Join(root, ".local", "share", "onechat"),
42 name: "home directory last",
46 want: filepath.Join(root, ".local", "share", "onechat"),
50 for _, tc := range testCases {
51 t.Run(tc.name, func(t *testing.T) {
52 t.Setenv("ONECHAT_DIR", tc.dir)
53 t.Setenv("XDG_DATA_HOME", tc.xdg)
54 t.Setenv("HOME", tc.home)
57 require.NoError(t, err)
58 require.Equal(t, tc.want, dir)
60 info, err := os.Stat(dir)
61 require.NoError(t, err)
62 require.Equal(t, os.FileMode(0o700), info.Mode().Perm())
67 func TestDataDirFailsWithoutAHome(t *testing.T) {
68 t.Setenv("ONECHAT_DIR", "")
69 t.Setenv("XDG_DATA_HOME", "")
73 require.ErrorContains(t, err, "ONECHAT_DIR")