Skip to content

Commit c77b9b0

Browse files
committed
pkg/path/testdata: stage filepath files from Go
As we are going to support functions for all OSes, we cannot rely on the current Go API. We copy the implementation and modify. This is a simple copy from tip in 1.15. Modifications are done in followup CLs and files will eventually be moved to pkg/path after modifications. Change-Id: I8f0feffab6c711ae78a1c6b63e370229a9e7cbe1 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7843 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent d5ced74 commit c77b9b0

16 files changed

+4326
-0
lines changed

pkg/path/testdata/example_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 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+
"fmt"
9+
"path/filepath"
10+
)
11+
12+
func ExampleExt() {
13+
fmt.Printf("No dots: %q\n", filepath.Ext("index"))
14+
fmt.Printf("One dot: %q\n", filepath.Ext("index.js"))
15+
fmt.Printf("Two dots: %q\n", filepath.Ext("main.test.js"))
16+
// Output:
17+
// No dots: ""
18+
// One dot: ".js"
19+
// Two dots: ".js"
20+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// Copyright 2013 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+
// +build !windows,!plan9
6+
7+
package filepath_test
8+
9+
import (
10+
"fmt"
11+
"path/filepath"
12+
)
13+
14+
func ExampleSplitList() {
15+
fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin"))
16+
// Output:
17+
// On Unix: [/a/b/c /usr/bin]
18+
}
19+
20+
func ExampleRel() {
21+
paths := []string{
22+
"/a/b/c",
23+
"/b/c",
24+
"./b/c",
25+
}
26+
base := "/a"
27+
28+
fmt.Println("On Unix:")
29+
for _, p := range paths {
30+
rel, err := filepath.Rel(base, p)
31+
fmt.Printf("%q: %q %v\n", p, rel, err)
32+
}
33+
34+
// Output:
35+
// On Unix:
36+
// "/a/b/c": "b/c" <nil>
37+
// "/b/c": "../b/c" <nil>
38+
// "./b/c": "" Rel: can't make ./b/c relative to /a
39+
}
40+
41+
func ExampleSplit() {
42+
paths := []string{
43+
"/home/arnie/amelia.jpg",
44+
"/mnt/photos/",
45+
"rabbit.jpg",
46+
"/usr/local//go",
47+
}
48+
fmt.Println("On Unix:")
49+
for _, p := range paths {
50+
dir, file := filepath.Split(p)
51+
fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, dir, file)
52+
}
53+
// Output:
54+
// On Unix:
55+
// input: "/home/arnie/amelia.jpg"
56+
// dir: "/home/arnie/"
57+
// file: "amelia.jpg"
58+
// input: "/mnt/photos/"
59+
// dir: "/mnt/photos/"
60+
// file: ""
61+
// input: "rabbit.jpg"
62+
// dir: ""
63+
// file: "rabbit.jpg"
64+
// input: "/usr/local//go"
65+
// dir: "/usr/local//"
66+
// file: "go"
67+
}
68+
69+
func ExampleJoin() {
70+
fmt.Println("On Unix:")
71+
fmt.Println(filepath.Join("a", "b", "c"))
72+
fmt.Println(filepath.Join("a", "b/c"))
73+
fmt.Println(filepath.Join("a/b", "c"))
74+
fmt.Println(filepath.Join("a/b", "/c"))
75+
76+
fmt.Println(filepath.Join("a/b", "../../../xyz"))
77+
78+
// Output:
79+
// On Unix:
80+
// a/b/c
81+
// a/b/c
82+
// a/b/c
83+
// a/b/c
84+
// ../xyz
85+
}
86+
87+
func ExampleMatch() {
88+
fmt.Println("On Unix:")
89+
fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo"))
90+
fmt.Println(filepath.Match("/home/catch/*", "/home/catch/foo/bar"))
91+
fmt.Println(filepath.Match("/home/?opher", "/home/gopher"))
92+
fmt.Println(filepath.Match("/home/\\*", "/home/*"))
93+
94+
// Output:
95+
// On Unix:
96+
// true <nil>
97+
// false <nil>
98+
// true <nil>
99+
// true <nil>
100+
}
101+
102+
func ExampleBase() {
103+
fmt.Println("On Unix:")
104+
fmt.Println(filepath.Base("/foo/bar/baz.js"))
105+
fmt.Println(filepath.Base("/foo/bar/baz"))
106+
fmt.Println(filepath.Base("/foo/bar/baz/"))
107+
fmt.Println(filepath.Base("dev.txt"))
108+
fmt.Println(filepath.Base("../todo.txt"))
109+
fmt.Println(filepath.Base(".."))
110+
fmt.Println(filepath.Base("."))
111+
fmt.Println(filepath.Base("/"))
112+
fmt.Println(filepath.Base(""))
113+
114+
// Output:
115+
// On Unix:
116+
// baz.js
117+
// baz
118+
// baz
119+
// dev.txt
120+
// todo.txt
121+
// ..
122+
// .
123+
// /
124+
// .
125+
}
126+
127+
func ExampleDir() {
128+
fmt.Println("On Unix:")
129+
fmt.Println(filepath.Dir("/foo/bar/baz.js"))
130+
fmt.Println(filepath.Dir("/foo/bar/baz"))
131+
fmt.Println(filepath.Dir("/foo/bar/baz/"))
132+
fmt.Println(filepath.Dir("/dirty//path///"))
133+
fmt.Println(filepath.Dir("dev.txt"))
134+
fmt.Println(filepath.Dir("../todo.txt"))
135+
fmt.Println(filepath.Dir(".."))
136+
fmt.Println(filepath.Dir("."))
137+
fmt.Println(filepath.Dir("/"))
138+
fmt.Println(filepath.Dir(""))
139+
140+
// Output:
141+
// On Unix:
142+
// /foo/bar
143+
// /foo/bar
144+
// /foo/bar/baz
145+
// /dirty/path
146+
// .
147+
// ..
148+
// .
149+
// .
150+
// /
151+
// .
152+
}
153+
154+
func ExampleIsAbs() {
155+
fmt.Println("On Unix:")
156+
fmt.Println(filepath.IsAbs("/home/gopher"))
157+
fmt.Println(filepath.IsAbs(".bashrc"))
158+
fmt.Println(filepath.IsAbs(".."))
159+
fmt.Println(filepath.IsAbs("."))
160+
fmt.Println(filepath.IsAbs("/"))
161+
fmt.Println(filepath.IsAbs(""))
162+
163+
// Output:
164+
// On Unix:
165+
// true
166+
// false
167+
// false
168+
// false
169+
// true
170+
// false
171+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2018 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+
// +build !windows,!plan9
6+
7+
package filepath_test
8+
9+
import (
10+
"fmt"
11+
"io/fs"
12+
"io/ioutil"
13+
"os"
14+
"path/filepath"
15+
)
16+
17+
func prepareTestDirTree(tree string) (string, error) {
18+
tmpDir, err := ioutil.TempDir("", "")
19+
if err != nil {
20+
return "", fmt.Errorf("error creating temp directory: %v\n", err)
21+
}
22+
23+
err = os.MkdirAll(filepath.Join(tmpDir, tree), 0755)
24+
if err != nil {
25+
os.RemoveAll(tmpDir)
26+
return "", err
27+
}
28+
29+
return tmpDir, nil
30+
}
31+
32+
func ExampleWalk() {
33+
tmpDir, err := prepareTestDirTree("dir/to/walk/skip")
34+
if err != nil {
35+
fmt.Printf("unable to create test dir tree: %v\n", err)
36+
return
37+
}
38+
defer os.RemoveAll(tmpDir)
39+
os.Chdir(tmpDir)
40+
41+
subDirToSkip := "skip"
42+
43+
fmt.Println("On Unix:")
44+
err = filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
45+
if err != nil {
46+
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
47+
return err
48+
}
49+
if info.IsDir() && info.Name() == subDirToSkip {
50+
fmt.Printf("skipping a dir without errors: %+v \n", info.Name())
51+
return filepath.SkipDir
52+
}
53+
fmt.Printf("visited file or dir: %q\n", path)
54+
return nil
55+
})
56+
if err != nil {
57+
fmt.Printf("error walking the path %q: %v\n", tmpDir, err)
58+
return
59+
}
60+
// Output:
61+
// On Unix:
62+
// visited file or dir: "."
63+
// visited file or dir: "dir"
64+
// visited file or dir: "dir/to"
65+
// visited file or dir: "dir/to/walk"
66+
// skipping a dir without errors: skip
67+
}

pkg/path/testdata/export_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright 2013 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
6+
7+
var LstatP = &lstat
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2016 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
6+
7+
var (
8+
ToNorm = toNorm
9+
NormBase = normBase
10+
)

0 commit comments

Comments
 (0)