1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 type CleanTest
struct {
16 var cleantests
= []CleanTest
{
20 {"abc/def", "abc/def"},
25 {"../../abc", "../../abc"},
29 // Remove trailing slash
31 {"abc/def/", "abc/def"},
38 // Remove doubled slash
39 {"abc//def//ghi", "abc/def/ghi"},
46 {"abc/./def", "abc/def"},
47 {"/./abc/def", "/abc/def"},
51 {"abc/def/ghi/../jkl", "abc/def/jkl"},
52 {"abc/def/../ghi/../jkl", "abc/jkl"},
53 {"abc/def/..", "abc"},
54 {"abc/def/../..", "."},
55 {"/abc/def/../..", "/"},
56 {"abc/def/../../..", ".."},
57 {"/abc/def/../../..", "/"},
58 {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
61 {"abc/./../def", "def"},
62 {"abc//./../def", "def"},
63 {"abc/../../././../def", "../../def"},
66 func TestClean(t
*testing
.T
) {
67 for _
, test
:= range cleantests
{
68 if s
:= Clean(test
.path
); s
!= test
.clean
{
69 t
.Errorf("Clean(%q) = %q, want %q", test
.path
, s
, test
.clean
)
74 type SplitTest
struct {
75 path
, dir
, file
string
78 var splittests
= []SplitTest
{
86 func TestSplit(t
*testing
.T
) {
87 for _
, test
:= range splittests
{
88 if d
, f
:= Split(test
.path
); d
!= test
.dir || f
!= test
.file
{
89 t
.Errorf("Split(%q) = %q, %q, want %q, %q", test
.path
, d
, f
, test
.dir
, test
.file
)
94 type JoinTest
struct {
99 var jointests
= []JoinTest
{
105 {[]string{"a"}, "a"},
108 {[]string{"a", "b"}, "a/b"},
109 {[]string{"a", ""}, "a"},
110 {[]string{"", "b"}, "b"},
111 {[]string{"/", "a"}, "/a"},
112 {[]string{"/", ""}, "/"},
113 {[]string{"a/", "b"}, "a/b"},
114 {[]string{"a/", ""}, "a"},
115 {[]string{"", ""}, ""},
118 // join takes a []string and passes it to Join.
119 func join(elem
[]string, args
...string) string {
124 func TestJoin(t
*testing
.T
) {
125 for _
, test
:= range jointests
{
126 if p
:= join(test
.elem
); p
!= test
.path
{
127 t
.Errorf("join(%q) = %q, want %q", test
.elem
, p
, test
.path
)
132 type ExtTest
struct {
136 var exttests
= []ExtTest
{
138 {"path.pb.go", ".go"},
140 {"a.dir/b.go", ".go"},
144 func TestExt(t
*testing
.T
) {
145 for _
, test
:= range exttests
{
146 if x
:= Ext(test
.path
); x
!= test
.ext
{
147 t
.Errorf("Ext(%q) = %q, want %q", test
.path
, x
, test
.ext
)
154 entries
[]*Node
// nil if the entry is a file
162 &Node
{"b", []*Node
{}, 0},
168 &Node
{"y", []*Node
{}, 0},
184 func walkTree(n
*Node
, path
string, f
func(path
string, n
*Node
)) {
186 for _
, e
:= range n
.entries
{
187 walkTree(e
, Join(path
, e
.name
), f
)
191 func makeTree(t
*testing
.T
) {
192 walkTree(tree
, tree
.name
, func(path
string, n
*Node
) {
193 if n
.entries
== nil {
194 fd
, err
:= os
.Open(path
, os
.O_CREAT
, 0660)
196 t
.Errorf("makeTree: %v", err
)
205 func markTree(n
*Node
) { walkTree(n
, "", func(path
string, n
*Node
) { n
.mark
++ }) }
207 func checkMarks(t
*testing
.T
) {
208 walkTree(tree
, tree
.name
, func(path
string, n
*Node
) {
210 t
.Errorf("node %s mark = %d; expected 1", path
, n
.mark
)
216 // Assumes that each node name is unique. Good enough for a test.
217 func mark(name
string) {
218 walkTree(tree
, tree
.name
, func(path
string, n
*Node
) {
225 type TestVisitor
struct{}
227 func (v
*TestVisitor
) VisitDir(path
string, f
*os
.FileInfo
) bool {
232 func (v
*TestVisitor
) VisitFile(path
string, f
*os
.FileInfo
) {
236 func TestWalk(t
*testing
.T
) {
239 // 1) ignore error handling, expect none
241 Walk(tree
.name
, v
, nil)
244 // 2) handle errors, expect none
245 errors
:= make(chan os
.Error
, 64)
246 Walk(tree
.name
, v
, errors
)
247 if err
, ok
:= <-errors
; ok
{
248 t
.Errorf("no error expected, found: s", err
)
252 if os
.Getuid() != 0 {
253 // introduce 2 errors: chmod top-level directories to 0
254 os
.Chmod(Join(tree
.name
, tree
.entries
[1].name
), 0)
255 os
.Chmod(Join(tree
.name
, tree
.entries
[3].name
), 0)
256 // mark respective subtrees manually
257 markTree(tree
.entries
[1])
258 markTree(tree
.entries
[3])
259 // correct double-marking of directory itself
260 tree
.entries
[1].mark
--
261 tree
.entries
[3].mark
--
263 // 3) handle errors, expect two
264 errors
= make(chan os
.Error
, 64)
265 os
.Chmod(Join(tree
.name
, tree
.entries
[1].name
), 0)
266 Walk(tree
.name
, v
, errors
)
267 for i
:= 1; i
<= 2; i
++ {
268 if _
, ok
:= <-errors
; !ok
{
269 t
.Errorf("%d. error expected, none found", i
)
273 if err
, ok
:= <-errors
; ok
{
274 t
.Errorf("only two errors expected, found 3rd: %v", err
)
276 // the inaccessible subtrees were marked manually
281 os
.Chmod(Join(tree
.name
, tree
.entries
[1].name
), 0770)
282 os
.Chmod(Join(tree
.name
, tree
.entries
[3].name
), 0770)
283 if err
:= os
.RemoveAll(tree
.name
); err
!= nil {
284 t
.Errorf("removeTree: %v", err
)
288 var basetests
= []CleanTest
{
303 func TestBase(t
*testing
.T
) {
304 for _
, test
:= range basetests
{
305 if s
:= Base(test
.path
); s
!= test
.clean
{
306 t
.Errorf("Base(%q) = %q, want %q", test
.path
, s
, test
.clean
)
311 type IsAbsTest
struct {
316 var isAbsTests
= []IsAbsTest
{
319 {"/usr/bin/gcc", true},
327 func TestIsAbs(t
*testing
.T
) {
328 for _
, test
:= range isAbsTests
{
329 if r
:= IsAbs(test
.path
); r
!= test
.isAbs
{
330 t
.Errorf("IsAbs(%q) = %v, want %v", test
.path
, r
, test
.isAbs
)