OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [path/] [filepath/] [path_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package filepath_test
6
 
7
import (
8
        "io/ioutil"
9
        "os"
10
        "path/filepath"
11
        "reflect"
12
        "runtime"
13
        "testing"
14
)
15
 
16
type PathTest struct {
17
        path, result string
18
}
19
 
20
var cleantests = []PathTest{
21
        // Already clean
22
        {"", "."},
23
        {"abc", "abc"},
24
        {"abc/def", "abc/def"},
25
        {"a/b/c", "a/b/c"},
26
        {".", "."},
27
        {"..", ".."},
28
        {"../..", "../.."},
29
        {"../../abc", "../../abc"},
30
        {"/abc", "/abc"},
31
        {"/", "/"},
32
 
33
        // Remove trailing slash
34
        {"abc/", "abc"},
35
        {"abc/def/", "abc/def"},
36
        {"a/b/c/", "a/b/c"},
37
        {"./", "."},
38
        {"../", ".."},
39
        {"../../", "../.."},
40
        {"/abc/", "/abc"},
41
 
42
        // Remove doubled slash
43
        {"abc//def//ghi", "abc/def/ghi"},
44
        {"//abc", "/abc"},
45
        {"///abc", "/abc"},
46
        {"//abc//", "/abc"},
47
        {"abc//", "abc"},
48
 
49
        // Remove . elements
50
        {"abc/./def", "abc/def"},
51
        {"/./abc/def", "/abc/def"},
52
        {"abc/.", "abc"},
53
 
54
        // Remove .. elements
55
        {"abc/def/ghi/../jkl", "abc/def/jkl"},
56
        {"abc/def/../ghi/../jkl", "abc/jkl"},
57
        {"abc/def/..", "abc"},
58
        {"abc/def/../..", "."},
59
        {"/abc/def/../..", "/"},
60
        {"abc/def/../../..", ".."},
61
        {"/abc/def/../../..", "/"},
62
        {"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
63
 
64
        // Combinations
65
        {"abc/./../def", "def"},
66
        {"abc//./../def", "def"},
67
        {"abc/../../././../def", "../../def"},
68
}
69
 
70
var wincleantests = []PathTest{
71
        {`c:`, `c:.`},
72
        {`c:\`, `c:\`},
73
        {`c:\abc`, `c:\abc`},
74
        {`c:abc\..\..\.\.\..\def`, `c:..\..\def`},
75
        {`c:\abc\def\..\..`, `c:\`},
76
        {`c:\..\abc`, `c:\abc`},
77
        {`c:..\abc`, `c:..\abc`},
78
        {`\`, `\`},
79
        {`/`, `\`},
80
        {`\\i\..\c$`, `\c$`},
81
        {`\\i\..\i\c$`, `\i\c$`},
82
        {`\\i\..\I\c$`, `\I\c$`},
83
        {`\\host\share\foo\..\bar`, `\\host\share\bar`},
84
        {`//host/share/foo/../baz`, `\\host\share\baz`},
85
        {`\\a\b\..\c`, `\\a\b\c`},
86
        {`\\a\b`, `\\a\b`},
87
}
88
 
89
func TestClean(t *testing.T) {
90
        tests := cleantests
91
        if runtime.GOOS == "windows" {
92
                for i := range tests {
93
                        tests[i].result = filepath.FromSlash(tests[i].result)
94
                }
95
                tests = append(tests, wincleantests...)
96
        }
97
        for _, test := range tests {
98
                if s := filepath.Clean(test.path); s != test.result {
99
                        t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
100
                }
101
        }
102
}
103
 
104
const sep = filepath.Separator
105
 
106
var slashtests = []PathTest{
107
        {"", ""},
108
        {"/", string(sep)},
109
        {"/a/b", string([]byte{sep, 'a', sep, 'b'})},
110
        {"a//b", string([]byte{'a', sep, sep, 'b'})},
111
}
112
 
113
func TestFromAndToSlash(t *testing.T) {
114
        for _, test := range slashtests {
115
                if s := filepath.FromSlash(test.path); s != test.result {
116
                        t.Errorf("FromSlash(%q) = %q, want %q", test.path, s, test.result)
117
                }
118
                if s := filepath.ToSlash(test.result); s != test.path {
119
                        t.Errorf("ToSlash(%q) = %q, want %q", test.result, s, test.path)
120
                }
121
        }
122
}
123
 
124
type SplitListTest struct {
125
        list   string
126
        result []string
127
}
128
 
129
const lsep = filepath.ListSeparator
130
 
131
var splitlisttests = []SplitListTest{
132
        {"", []string{}},
133
        {string([]byte{'a', lsep, 'b'}), []string{"a", "b"}},
134
        {string([]byte{lsep, 'a', lsep, 'b'}), []string{"", "a", "b"}},
135
}
136
 
137
func TestSplitList(t *testing.T) {
138
        for _, test := range splitlisttests {
139
                if l := filepath.SplitList(test.list); !reflect.DeepEqual(l, test.result) {
140
                        t.Errorf("SplitList(%q) = %s, want %s", test.list, l, test.result)
141
                }
142
        }
143
}
144
 
145
type SplitTest struct {
146
        path, dir, file string
147
}
148
 
149
var unixsplittests = []SplitTest{
150
        {"a/b", "a/", "b"},
151
        {"a/b/", "a/b/", ""},
152
        {"a/", "a/", ""},
153
        {"a", "", "a"},
154
        {"/", "/", ""},
155
}
156
 
157
var winsplittests = []SplitTest{
158
        {`c:`, `c:`, ``},
159
        {`c:/`, `c:/`, ``},
160
        {`c:/foo`, `c:/`, `foo`},
161
        {`c:/foo/bar`, `c:/foo/`, `bar`},
162
        {`//host/share`, `//host/share`, ``},
163
        {`//host/share/`, `//host/share/`, ``},
164
        {`//host/share/foo`, `//host/share/`, `foo`},
165
        {`\\host\share`, `\\host\share`, ``},
166
        {`\\host\share\`, `\\host\share\`, ``},
167
        {`\\host\share\foo`, `\\host\share\`, `foo`},
168
}
169
 
170
func TestSplit(t *testing.T) {
171
        var splittests []SplitTest
172
        splittests = unixsplittests
173
        if runtime.GOOS == "windows" {
174
                splittests = append(splittests, winsplittests...)
175
        }
176
        for _, test := range splittests {
177
                if d, f := filepath.Split(test.path); d != test.dir || f != test.file {
178
                        t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
179
                }
180
        }
181
}
182
 
183
type JoinTest struct {
184
        elem []string
185
        path string
186
}
187
 
188
var jointests = []JoinTest{
189
        // zero parameters
190
        {[]string{}, ""},
191
 
192
        // one parameter
193
        {[]string{""}, ""},
194
        {[]string{"a"}, "a"},
195
 
196
        // two parameters
197
        {[]string{"a", "b"}, "a/b"},
198
        {[]string{"a", ""}, "a"},
199
        {[]string{"", "b"}, "b"},
200
        {[]string{"/", "a"}, "/a"},
201
        {[]string{"/", ""}, "/"},
202
        {[]string{"a/", "b"}, "a/b"},
203
        {[]string{"a/", ""}, "a"},
204
        {[]string{"", ""}, ""},
205
}
206
 
207
var winjointests = []JoinTest{
208
        {[]string{`directory`, `file`}, `directory\file`},
209
        {[]string{`C:\Windows\`, `System32`}, `C:\Windows\System32`},
210
        {[]string{`C:\Windows\`, ``}, `C:\Windows`},
211
        {[]string{`C:\`, `Windows`}, `C:\Windows`},
212
        {[]string{`C:`, `Windows`}, `C:\Windows`},
213
        {[]string{`\\host\share`, `foo`}, `\\host\share\foo`},
214
        {[]string{`//host/share`, `foo/bar`}, `\\host\share\foo\bar`},
215
}
216
 
217
// join takes a []string and passes it to Join.
218
func join(elem []string, args ...string) string {
219
        args = elem
220
        return filepath.Join(args...)
221
}
222
 
223
func TestJoin(t *testing.T) {
224
        if runtime.GOOS == "windows" {
225
                jointests = append(jointests, winjointests...)
226
        }
227
        for _, test := range jointests {
228
                if p := join(test.elem); p != filepath.FromSlash(test.path) {
229
                        t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
230
                }
231
        }
232
}
233
 
234
type ExtTest struct {
235
        path, ext string
236
}
237
 
238
var exttests = []ExtTest{
239
        {"path.go", ".go"},
240
        {"path.pb.go", ".go"},
241
        {"a.dir/b", ""},
242
        {"a.dir/b.go", ".go"},
243
        {"a.dir/", ""},
244
}
245
 
246
func TestExt(t *testing.T) {
247
        for _, test := range exttests {
248
                if x := filepath.Ext(test.path); x != test.ext {
249
                        t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext)
250
                }
251
        }
252
}
253
 
254
type Node struct {
255
        name    string
256
        entries []*Node // nil if the entry is a file
257
        mark    int
258
}
259
 
260
var tree = &Node{
261
        "testdata",
262
        []*Node{
263
                {"a", nil, 0},
264
                {"b", []*Node{}, 0},
265
                {"c", nil, 0},
266
                {
267
                        "d",
268
                        []*Node{
269
                                {"x", nil, 0},
270
                                {"y", []*Node{}, 0},
271
                                {
272
                                        "z",
273
                                        []*Node{
274
                                                {"u", nil, 0},
275
                                                {"v", nil, 0},
276
                                        },
277
                                        0,
278
                                },
279
                        },
280
                        0,
281
                },
282
        },
283
        0,
284
}
285
 
286
func walkTree(n *Node, path string, f func(path string, n *Node)) {
287
        f(path, n)
288
        for _, e := range n.entries {
289
                walkTree(e, filepath.Join(path, e.name), f)
290
        }
291
}
292
 
293
func makeTree(t *testing.T) {
294
        walkTree(tree, tree.name, func(path string, n *Node) {
295
                if n.entries == nil {
296
                        fd, err := os.Create(path)
297
                        if err != nil {
298
                                t.Errorf("makeTree: %v", err)
299
                                return
300
                        }
301
                        fd.Close()
302
                } else {
303
                        os.Mkdir(path, 0770)
304
                }
305
        })
306
}
307
 
308
func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
309
 
310
func checkMarks(t *testing.T, report bool) {
311
        walkTree(tree, tree.name, func(path string, n *Node) {
312
                if n.mark != 1 && report {
313
                        t.Errorf("node %s mark = %d; expected 1", path, n.mark)
314
                }
315
                n.mark = 0
316
        })
317
}
318
 
319
// Assumes that each node name is unique. Good enough for a test.
320
// If clear is true, any incoming error is cleared before return. The errors
321
// are always accumulated, though.
322
func mark(path string, info os.FileInfo, err error, errors *[]error, clear bool) error {
323
        if err != nil {
324
                *errors = append(*errors, err)
325
                if clear {
326
                        return nil
327
                }
328
                return err
329
        }
330
        name := info.Name()
331
        walkTree(tree, tree.name, func(path string, n *Node) {
332
                if n.name == name {
333
                        n.mark++
334
                }
335
        })
336
        return nil
337
}
338
 
339
func TestWalk(t *testing.T) {
340
        makeTree(t)
341
        errors := make([]error, 0, 10)
342
        clear := true
343
        markFn := func(path string, info os.FileInfo, err error) error {
344
                return mark(path, info, err, &errors, clear)
345
        }
346
        // Expect no errors.
347
        err := filepath.Walk(tree.name, markFn)
348
        if err != nil {
349
                t.Fatalf("no error expected, found: %s", err)
350
        }
351
        if len(errors) != 0 {
352
                t.Fatalf("unexpected errors: %s", errors)
353
        }
354
        checkMarks(t, true)
355
        errors = errors[0:0]
356
 
357
        // Test permission errors.  Only possible if we're not root
358
        // and only on some file systems (AFS, FAT).  To avoid errors during
359
        // all.bash on those file systems, skip during gotest -short.
360
        if os.Getuid() > 0 && !testing.Short() {
361
                // introduce 2 errors: chmod top-level directories to 0
362
                os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0)
363
                os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0)
364
 
365
                // 3) capture errors, expect two.
366
                // mark respective subtrees manually
367
                markTree(tree.entries[1])
368
                markTree(tree.entries[3])
369
                // correct double-marking of directory itself
370
                tree.entries[1].mark--
371
                tree.entries[3].mark--
372
                err := filepath.Walk(tree.name, markFn)
373
                if err != nil {
374
                        t.Fatalf("expected no error return from Walk, got %s", err)
375
                }
376
                if len(errors) != 2 {
377
                        t.Errorf("expected 2 errors, got %d: %s", len(errors), errors)
378
                }
379
                // the inaccessible subtrees were marked manually
380
                checkMarks(t, true)
381
                errors = errors[0:0]
382
 
383
                // 4) capture errors, stop after first error.
384
                // mark respective subtrees manually
385
                markTree(tree.entries[1])
386
                markTree(tree.entries[3])
387
                // correct double-marking of directory itself
388
                tree.entries[1].mark--
389
                tree.entries[3].mark--
390
                clear = false // error will stop processing
391
                err = filepath.Walk(tree.name, markFn)
392
                if err == nil {
393
                        t.Fatalf("expected error return from Walk")
394
                }
395
                if len(errors) != 1 {
396
                        t.Errorf("expected 1 error, got %d: %s", len(errors), errors)
397
                }
398
                // the inaccessible subtrees were marked manually
399
                checkMarks(t, false)
400
                errors = errors[0:0]
401
 
402
                // restore permissions
403
                os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0770)
404
                os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0770)
405
        }
406
 
407
        // cleanup
408
        if err := os.RemoveAll(tree.name); err != nil {
409
                t.Errorf("removeTree: %v", err)
410
        }
411
}
412
 
413
var basetests = []PathTest{
414
        {"", "."},
415
        {".", "."},
416
        {"/.", "."},
417
        {"/", "/"},
418
        {"////", "/"},
419
        {"x/", "x"},
420
        {"abc", "abc"},
421
        {"abc/def", "def"},
422
        {"a/b/.x", ".x"},
423
        {"a/b/c.", "c."},
424
        {"a/b/c.x", "c.x"},
425
}
426
 
427
var winbasetests = []PathTest{
428
        {`c:\`, `\`},
429
        {`c:.`, `.`},
430
        {`c:\a\b`, `b`},
431
        {`c:a\b`, `b`},
432
        {`c:a\b\c`, `c`},
433
        {`\\host\share\`, `\`},
434
        {`\\host\share\a`, `a`},
435
        {`\\host\share\a\b`, `b`},
436
}
437
 
438
func TestBase(t *testing.T) {
439
        tests := basetests
440
        if runtime.GOOS == "windows" {
441
                // make unix tests work on windows
442
                for i, _ := range tests {
443
                        tests[i].result = filepath.Clean(tests[i].result)
444
                }
445
                // add windows specific tests
446
                tests = append(tests, winbasetests...)
447
        }
448
        for _, test := range tests {
449
                if s := filepath.Base(test.path); s != test.result {
450
                        t.Errorf("Base(%q) = %q, want %q", test.path, s, test.result)
451
                }
452
        }
453
}
454
 
455
var dirtests = []PathTest{
456
        {"", "."},
457
        {".", "."},
458
        {"/.", "/"},
459
        {"/", "/"},
460
        {"////", "/"},
461
        {"/foo", "/"},
462
        {"x/", "x"},
463
        {"abc", "."},
464
        {"abc/def", "abc"},
465
        {"a/b/.x", "a/b"},
466
        {"a/b/c.", "a/b"},
467
        {"a/b/c.x", "a/b"},
468
}
469
 
470
var windirtests = []PathTest{
471
        {`c:\`, `c:\`},
472
        {`c:.`, `c:.`},
473
        {`c:\a\b`, `c:\a`},
474
        {`c:a\b`, `c:a`},
475
        {`c:a\b\c`, `c:a\b`},
476
        {`\\host\share\`, `\\host\share\`},
477
        {`\\host\share\a`, `\\host\share\`},
478
        {`\\host\share\a\b`, `\\host\share\a`},
479
}
480
 
481
func TestDir(t *testing.T) {
482
        tests := dirtests
483
        if runtime.GOOS == "windows" {
484
                // make unix tests work on windows
485
                for i, _ := range tests {
486
                        tests[i].result = filepath.Clean(tests[i].result)
487
                }
488
                // add windows specific tests
489
                tests = append(tests, windirtests...)
490
        }
491
        for _, test := range tests {
492
                if s := filepath.Dir(test.path); s != test.result {
493
                        t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result)
494
                }
495
        }
496
}
497
 
498
type IsAbsTest struct {
499
        path  string
500
        isAbs bool
501
}
502
 
503
var isabstests = []IsAbsTest{
504
        {"", false},
505
        {"/", true},
506
        {"/usr/bin/gcc", true},
507
        {"..", false},
508
        {"/a/../bb", true},
509
        {".", false},
510
        {"./", false},
511
        {"lala", false},
512
}
513
 
514
var winisabstests = []IsAbsTest{
515
        {`C:\`, true},
516
        {`c\`, false},
517
        {`c::`, false},
518
        {`c:`, false},
519
        {`/`, false},
520
        {`\`, false},
521
        {`\Windows`, false},
522
        {`c:a\b`, false},
523
        {`\\host\share\foo`, true},
524
        {`//host/share/foo/bar`, true},
525
}
526
 
527
func TestIsAbs(t *testing.T) {
528
        var tests []IsAbsTest
529
        if runtime.GOOS == "windows" {
530
                tests = append(tests, winisabstests...)
531
                // All non-windows tests should fail, because they have no volume letter.
532
                for _, test := range isabstests {
533
                        tests = append(tests, IsAbsTest{test.path, false})
534
                }
535
                // All non-windows test should work as intended if prefixed with volume letter.
536
                for _, test := range isabstests {
537
                        tests = append(tests, IsAbsTest{"c:" + test.path, test.isAbs})
538
                }
539
        } else {
540
                tests = isabstests
541
        }
542
 
543
        for _, test := range tests {
544
                if r := filepath.IsAbs(test.path); r != test.isAbs {
545
                        t.Errorf("IsAbs(%q) = %v, want %v", test.path, r, test.isAbs)
546
                }
547
        }
548
}
549
 
550
type EvalSymlinksTest struct {
551
        // If dest is empty, the path is created; otherwise the dest is symlinked to the path.
552
        path, dest string
553
}
554
 
555
var EvalSymlinksTestDirs = []EvalSymlinksTest{
556
        {"test", ""},
557
        {"test/dir", ""},
558
        {"test/dir/link3", "../../"},
559
        {"test/link1", "../test"},
560
        {"test/link2", "dir"},
561
}
562
 
563
var EvalSymlinksTests = []EvalSymlinksTest{
564
        {"test", "test"},
565
        {"test/dir", "test/dir"},
566
        {"test/dir/../..", "."},
567
        {"test/link1", "test"},
568
        {"test/link2", "test/dir"},
569
        {"test/link1/dir", "test/dir"},
570
        {"test/link2/..", "test"},
571
        {"test/dir/link3", "."},
572
        {"test/link2/link3/test", "test"},
573
}
574
 
575
var EvalSymlinksAbsWindowsTests = []EvalSymlinksTest{
576
        {`c:\`, `c:\`},
577
}
578
 
579
// simpleJoin builds a file name from the directory and path.
580
// It does not use Join because we don't want ".." to be evaluated.
581
func simpleJoin(dir, path string) string {
582
        return dir + string(filepath.Separator) + path
583
}
584
 
585
func TestEvalSymlinks(t *testing.T) {
586
        tmpDir, err := ioutil.TempDir("", "evalsymlink")
587
        if err != nil {
588
                t.Fatal("creating temp dir:", err)
589
        }
590
        defer os.RemoveAll(tmpDir)
591
 
592
        // /tmp may itself be a symlink! Avoid the confusion, although
593
        // it means trusting the thing we're testing.
594
        tmpDir, err = filepath.EvalSymlinks(tmpDir)
595
        if err != nil {
596
                t.Fatal("eval symlink for tmp dir:", err)
597
        }
598
 
599
        // Create the symlink farm using relative paths.
600
        for _, d := range EvalSymlinksTestDirs {
601
                var err error
602
                path := simpleJoin(tmpDir, d.path)
603
                if d.dest == "" {
604
                        err = os.Mkdir(path, 0755)
605
                } else {
606
                        if runtime.GOOS != "windows" {
607
                                err = os.Symlink(d.dest, path)
608
                        }
609
                }
610
                if err != nil {
611
                        t.Fatal(err)
612
                }
613
        }
614
 
615
        var tests []EvalSymlinksTest
616
        if runtime.GOOS == "windows" {
617
                for _, d := range EvalSymlinksTests {
618
                        if d.path == d.dest {
619
                                // will test only real files and directories
620
                                tests = append(tests, d)
621
                        }
622
                }
623
        } else {
624
                tests = EvalSymlinksTests
625
        }
626
 
627
        // Evaluate the symlink farm.
628
        for _, d := range tests {
629
                path := simpleJoin(tmpDir, d.path)
630
                dest := simpleJoin(tmpDir, d.dest)
631
                if p, err := filepath.EvalSymlinks(path); err != nil {
632
                        t.Errorf("EvalSymlinks(%q) error: %v", d.path, err)
633
                } else if filepath.Clean(p) != filepath.Clean(dest) {
634
                        t.Errorf("Clean(%q)=%q, want %q", path, p, dest)
635
                }
636
        }
637
}
638
 
639
/* These tests do not work in the gccgo test environment.
640
 
641
// Test paths relative to $GOROOT/src
642
var abstests = []string{
643
        "../AUTHORS",
644
        "pkg/../../AUTHORS",
645
        "Make.inc",
646
        "pkg/math",
647
        ".",
648
        "$GOROOT/src/Make.inc",
649
        "$GOROOT/src/../src/Make.inc",
650
        "$GOROOT/misc/cgo",
651
        "$GOROOT",
652
}
653
 
654
func TestAbs(t *testing.T) {
655
        t.Logf("test needs to be rewritten; disabled")
656
        return
657
 
658
        oldwd, err := os.Getwd()
659
        if err != nil {
660
                t.Fatal("Getwd failed: " + err.Error())
661
        }
662
        defer os.Chdir(oldwd)
663
        goroot := os.Getenv("GOROOT")
664
        cwd := filepath.Join(goroot, "src")
665
        os.Chdir(cwd)
666
        for _, path := range abstests {
667
                path = strings.Replace(path, "$GOROOT", goroot, -1)
668
                info, err := os.Stat(path)
669
                if err != nil {
670
                        t.Errorf("%s: %s", path, err)
671
                        continue
672
                }
673
 
674
                abspath, err := filepath.Abs(path)
675
                if err != nil {
676
                        t.Errorf("Abs(%q) error: %v", path, err)
677
                        continue
678
                }
679
                absinfo, err := os.Stat(abspath)
680
                if err != nil || !os.SameFile(absinfo, info) {
681
                        t.Errorf("Abs(%q)=%q, not the same file", path, abspath)
682
                }
683
                if !filepath.IsAbs(abspath) {
684
                        t.Errorf("Abs(%q)=%q, not an absolute path", path, abspath)
685
                }
686
                if filepath.IsAbs(path) && abspath != filepath.Clean(path) {
687
                        t.Errorf("Abs(%q)=%q, isn't clean", path, abspath)
688
                }
689
        }
690
}
691
 
692
*/
693
 
694
type RelTests struct {
695
        root, path, want string
696
}
697
 
698
var reltests = []RelTests{
699
        {"a/b", "a/b", "."},
700
        {"a/b/.", "a/b", "."},
701
        {"a/b", "a/b/.", "."},
702
        {"./a/b", "a/b", "."},
703
        {"a/b", "./a/b", "."},
704
        {"ab/cd", "ab/cde", "../cde"},
705
        {"ab/cd", "ab/c", "../c"},
706
        {"a/b", "a/b/c/d", "c/d"},
707
        {"a/b", "a/b/../c", "../c"},
708
        {"a/b/../c", "a/b", "../b"},
709
        {"a/b/c", "a/c/d", "../../c/d"},
710
        {"a/b", "c/d", "../../c/d"},
711
        {"a/b/c/d", "a/b", "../.."},
712
        {"a/b/c/d", "a/b/", "../.."},
713
        {"a/b/c/d/", "a/b", "../.."},
714
        {"a/b/c/d/", "a/b/", "../.."},
715
        {"../../a/b", "../../a/b/c/d", "c/d"},
716
        {"/a/b", "/a/b", "."},
717
        {"/a/b/.", "/a/b", "."},
718
        {"/a/b", "/a/b/.", "."},
719
        {"/ab/cd", "/ab/cde", "../cde"},
720
        {"/ab/cd", "/ab/c", "../c"},
721
        {"/a/b", "/a/b/c/d", "c/d"},
722
        {"/a/b", "/a/b/../c", "../c"},
723
        {"/a/b/../c", "/a/b", "../b"},
724
        {"/a/b/c", "/a/c/d", "../../c/d"},
725
        {"/a/b", "/c/d", "../../c/d"},
726
        {"/a/b/c/d", "/a/b", "../.."},
727
        {"/a/b/c/d", "/a/b/", "../.."},
728
        {"/a/b/c/d/", "/a/b", "../.."},
729
        {"/a/b/c/d/", "/a/b/", "../.."},
730
        {"/../../a/b", "/../../a/b/c/d", "c/d"},
731
        {".", "a/b", "a/b"},
732
        {".", "..", ".."},
733
 
734
        // can't do purely lexically
735
        {"..", ".", "err"},
736
        {"..", "a", "err"},
737
        {"../..", "..", "err"},
738
        {"a", "/a", "err"},
739
        {"/a", "a", "err"},
740
}
741
 
742
var winreltests = []RelTests{
743
        {`C:a\b\c`, `C:a/b/d`, `..\d`},
744
        {`C:\`, `D:\`, `err`},
745
        {`C:`, `D:`, `err`},
746
}
747
 
748
func TestRel(t *testing.T) {
749
        tests := append([]RelTests{}, reltests...)
750
        if runtime.GOOS == "windows" {
751
                for i := range tests {
752
                        tests[i].want = filepath.FromSlash(tests[i].want)
753
                }
754
                tests = append(tests, winreltests...)
755
        }
756
        for _, test := range tests {
757
                got, err := filepath.Rel(test.root, test.path)
758
                if test.want == "err" {
759
                        if err == nil {
760
                                t.Errorf("Rel(%q, %q)=%q, want error", test.root, test.path, got)
761
                        }
762
                        continue
763
                }
764
                if err != nil {
765
                        t.Errorf("Rel(%q, %q): want %q, got error: %s", test.root, test.path, test.want, err)
766
                }
767
                if got != test.want {
768
                        t.Errorf("Rel(%q, %q)=%q, want %q", test.root, test.path, got, test.want)
769
                }
770
        }
771
}
772
 
773
type VolumeNameTest struct {
774
        path string
775
        vol  string
776
}
777
 
778
var volumenametests = []VolumeNameTest{
779
        {`c:/foo/bar`, `c:`},
780
        {`c:`, `c:`},
781
        {``, ``},
782
        {`\\\host`, ``},
783
        {`\\\host\`, ``},
784
        {`\\\host\share`, ``},
785
        {`\\\host\\share`, ``},
786
        {`\\host`, ``},
787
        {`//host`, ``},
788
        {`\\host\`, ``},
789
        {`//host/`, ``},
790
        {`\\host\share`, `\\host\share`},
791
        {`//host/share`, `//host/share`},
792
        {`\\host\share\`, `\\host\share`},
793
        {`//host/share/`, `//host/share`},
794
        {`\\host\share\foo`, `\\host\share`},
795
        {`//host/share/foo`, `//host/share`},
796
        {`\\host\share\\foo\\\bar\\\\baz`, `\\host\share`},
797
        {`//host/share//foo///bar////baz`, `//host/share`},
798
        {`\\host\share\foo\..\bar`, `\\host\share`},
799
        {`//host/share/foo/../bar`, `//host/share`},
800
}
801
 
802
func TestVolumeName(t *testing.T) {
803
        if runtime.GOOS != "windows" {
804
                return
805
        }
806
        for _, v := range volumenametests {
807
                if vol := filepath.VolumeName(v.path); vol != v.vol {
808
                        t.Errorf("VolumeName(%q)=%q, want %q", v.path, vol, v.vol)
809
                }
810
        }
811
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.