File: base_directory_test.go

package info (click to toggle)
golang-go-xdg 0~bzr20140219-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 92 kB
  • sloc: makefile: 3
file content (139 lines) | stat: -rw-r--r-- 3,099 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
// (c) 2014 John R. Lenton. See LICENSE.

package xdg

import (
	. "gopkg.in/check.v1"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestXDGd(t *testing.T) { TestingT(t) }

type xdgdSuite struct {
	home string
	env1 string
	val1 string
	env2 string
	val2 string
	dir  *XDGDir
}

var _ = Suite(&xdgdSuite{})

func (s *xdgdSuite) SetUpTest(c *C) {
	s.home = os.Getenv("HOME")
	s.env1 = "go_xdg_one"
	s.env2 = "go_xdg_two"
	s.val1 = "something"
	s.val2 = "one:two:three"
	s.dir = &XDGDir{s.env1, s.val1, s.env2, s.val2}
}

func (s *xdgdSuite) TestHomePrefersEnviron(c *C) {
	err := os.Setenv(s.env1, "algo")
	c.Assert(err, IsNil)
	defer os.Setenv(s.env1, "")
	h := s.dir.Home()
	c.Check(h, Equals, "algo")
}

func (s *xdgdSuite) TestDirsPrefersEnviron(c *C) {
	err := os.Setenv(s.env1, "cero")
	c.Assert(err, IsNil)
	defer os.Setenv(s.env1, "")
	err = os.Setenv(s.env2, "uno:dos")
	c.Assert(err, IsNil)
	defer os.Setenv(s.env2, "")
	hs := s.dir.Dirs()
	c.Check(hs, DeepEquals, []string{"cero", "uno", "dos"})
}

func (s *xdgdSuite) TestDirsSkipsEmpty(c *C) {
	err := os.Setenv(s.env2, "::")
	c.Assert(err, IsNil)
	defer os.Setenv(s.env2, "")
	hs := s.dir.Dirs()
	c.Check(hs, HasLen, 1)
}

// now repeat all the tests, but without the HOME environ.
type xdgdNoHomeSuite struct {
	xdgdSuite
}

var _ = Suite(&xdgdNoHomeSuite{})

func (s *xdgdNoHomeSuite) SetUpTest(c *C) {
	s.xdgdSuite.SetUpTest(c)
	os.Setenv("HOME", "")
}

func (s *xdgdNoHomeSuite) TearDownTest(c *C) {
	os.Setenv("HOME", s.home)
}

// and for these tests, an entirely fake HOME
type xdgdFHSuite struct {
	xdgdSuite
	real_home string
}

var _ = Suite(&xdgdFHSuite{})

func (s *xdgdFHSuite) SetUpTest(c *C) {
	s.real_home = os.Getenv("HOME")
	home := c.MkDir()
	os.Setenv("HOME", home)
	s.xdgdSuite.SetUpTest(c)
	s.val2 = c.MkDir() + ":" + c.MkDir() + ":" + c.MkDir()
	s.dir = &XDGDir{s.env1, s.val1, s.env2, s.val2}
}

func (s *xdgdFHSuite) TearDownTest(c *C) {
	os.Setenv("HOME", s.real_home)
}

func (s *xdgdFHSuite) TestFind(c *C) {
	vs := strings.Split(s.val2, ":")
	res1 := "stuff"
	exp1 := filepath.Join(s.home, s.val1, res1)
	res2 := "things/that"
	exp2 := filepath.Join(vs[1], res2)
	res3 := "more"
	exp3 := filepath.Join(vs[2], res3)
	for _, d := range []string{exp1, exp2, exp3} {
		err := os.MkdirAll(d, 0700)
		c.Assert(err, IsNil, Commentf(d))
	}
	for _, it := range []struct {
		res string
		exp string
	}{{res1, exp1}, {res2, exp2}, {res3, exp3}} {
		rv, err := s.dir.Find(it.res)
		c.Assert(err, IsNil)
		c.Check(rv, Equals, it.exp)
	}
	_, err := s.dir.Find("missing")
	c.Check(err, NotNil)
}

func (s *xdgdFHSuite) TestEnsureFirst(c *C) {
	// creates it if missing
	rv1, err := s.dir.Ensure("missing/file")
	c.Assert(err, IsNil)
	_, err = os.Stat(rv1)
	c.Check(err, IsNil)
	c.Check(rv1, Matches, s.home+".*"+"missing/file")
	// just gets it if existing
	rv2, err := s.dir.Ensure("missing/file")
	c.Assert(err, IsNil)
	c.Check(rv2, Equals, rv1)
}

func (s *xdgdFHSuite) TestEnsureFirstFailures(c *C) {
	_, err := s.dir.Ensure(strings.Repeat("*", 1<<9) + "/" + strings.Repeat("*", 1<<9))
	c.Assert(err, NotNil)
}