aboutsummaryrefslogtreecommitdiff
path: root/internal/xdg/xdg_test.go
blob: 7c7de87b008ab844127bfccd5f04bca5ac71edbd (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package xdg

import "testing"

func TestBaseDirs(t *testing.T) {
	t.Setenv("HOME", "/home/u")
	t.Setenv("XDG_CONFIG_HOME", "")
	t.Setenv("XDG_STATE_HOME", "relative/ignored")
	t.Setenv("XDG_DATA_HOME", "/data/")
	if got := ConfigHome(); got != "/home/u/.config" {
		t.Errorf("ConfigHome() = %q", got)
	}
	if got := StateHome(); got != "/home/u/.local/state" {
		t.Errorf("StateHome() = %q, a relative value must be ignored", got)
	}
	if got := DataHome(); got != "/data" {
		t.Errorf("DataHome() = %q", got)
	}
}

// TestHomeTrailingSlash is item H: Home() must clean its result, or a
// trailing slash from $HOME breaks Abbrev's prefix check.
func TestHomeTrailingSlash(t *testing.T) {
	t.Setenv("HOME", "/home/u/")
	if got := Abbrev("/home/u/x"); got != "~/x" {
		t.Errorf("Abbrev(/home/u/x) = %q, want ~/x", got)
	}
	if got := Expand("~/x"); got != "/home/u/x" {
		t.Errorf("Expand(~/x) = %q, want /home/u/x", got)
	}
}

func TestExpandAbbrev(t *testing.T) {
	t.Setenv("HOME", "/home/u")
	expand := map[string]string{
		"~":        "/home/u",
		"~/d/x":    "/home/u/d/x",
		"~other/x": "~other/x",
		"/abs":     "/abs",
		"rel/x":    "rel/x",
	}
	for in, want := range expand {
		if got := Expand(in); got != want {
			t.Errorf("Expand(%q) = %q, want %q", in, got, want)
		}
	}
	abbrev := map[string]string{
		"/home/u":     "~",
		"/home/u/d/x": "~/d/x",
		"/home/ux/y":  "/home/ux/y",
		"/etc":        "/etc",
	}
	for in, want := range abbrev {
		if got := Abbrev(in); got != want {
			t.Errorf("Abbrev(%q) = %q, want %q", in, got, want)
		}
	}
}