Skip to content

Commit a50960c

Browse files
committed
pkg/path/testdata: adapt Go implementation
remove build tags and allow support for multiple OSes Notes: - evaluation is still fully hermetic - many functions may default to Unix by default - VolumeName defaults to Windows - Resolve was added instead of Abs. The latter requires a Getwd(), which wouldn't be hermetic. We could also overload Join, and change its semantics, but this seems a bit error-prone. - In windows both \ and / are interpreted as slashes in some contexts. It is therefore important to distinguish between using Separator and IsSeparator. - Note the super-cool tests. :) Questions: - Should VolumeName even take an OS argument? Change-Id: I080bee599700a98e6019ca0666f72b4a59ba8da1 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7845 Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent a2692ef commit a50960c

18 files changed

+673
-2712
lines changed

pkg/path/testdata/example_nix_test.go

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
// Copyright 2013 The Go Authors. All rights reserved.
216
// Use of this source code is governed by a BSD-style
317
// license that can be found in the LICENSE file.
418

5-
// +build !windows,!plan9
6-
7-
package filepath_test
19+
package path_test
820

921
import (
1022
"fmt"
11-
"path/filepath"
23+
24+
"cuelang.org/go/pkg/path"
1225
)
1326

1427
func ExampleSplitList() {
15-
fmt.Println("On Unix:", filepath.SplitList("/a/b/c:/usr/bin"))
28+
fmt.Println("On Unix:", path.SplitList("/a/b/c:/usr/bin", path.Unix))
1629
// Output:
1730
// On Unix: [/a/b/c /usr/bin]
1831
}
@@ -27,7 +40,7 @@ func ExampleRel() {
2740

2841
fmt.Println("On Unix:")
2942
for _, p := range paths {
30-
rel, err := filepath.Rel(base, p)
43+
rel, err := path.Rel(base, p, path.Unix)
3144
fmt.Printf("%q: %q %v\n", p, rel, err)
3245
}
3346

@@ -47,8 +60,8 @@ func ExampleSplit() {
4760
}
4861
fmt.Println("On Unix:")
4962
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)
63+
pair := path.Split(p, path.Unix)
64+
fmt.Printf("input: %q\n\tdir: %q\n\tfile: %q\n", p, pair[0], pair[1])
5265
}
5366
// Output:
5467
// On Unix:
@@ -68,12 +81,12 @@ func ExampleSplit() {
6881

6982
func ExampleJoin() {
7083
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"))
84+
fmt.Println(path.Join([]string{"a", "b", "c"}, path.Unix))
85+
fmt.Println(path.Join([]string{"a", "b/c"}, path.Unix))
86+
fmt.Println(path.Join([]string{"a/b", "c"}, path.Unix))
87+
fmt.Println(path.Join([]string{"a/b", "/c"}, path.Unix))
7588

76-
fmt.Println(filepath.Join("a/b", "../../../xyz"))
89+
fmt.Println(path.Join([]string{"a/b", "../../../xyz"}, path.Unix))
7790

7891
// Output:
7992
// On Unix:
@@ -86,10 +99,10 @@ func ExampleJoin() {
8699

87100
func ExampleMatch() {
88101
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/*"))
102+
fmt.Println(path.Match("/home/catch/*", "/home/catch/foo", path.Unix))
103+
fmt.Println(path.Match("/home/catch/*", "/home/catch/foo/bar", path.Unix))
104+
fmt.Println(path.Match("/home/?opher", "/home/gopher", path.Unix))
105+
fmt.Println(path.Match("/home/\\*", "/home/*", path.Unix))
93106

94107
// Output:
95108
// On Unix:
@@ -101,15 +114,15 @@ func ExampleMatch() {
101114

102115
func ExampleBase() {
103116
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(""))
117+
fmt.Println(path.Base("/foo/bar/baz.js", path.Unix))
118+
fmt.Println(path.Base("/foo/bar/baz", path.Unix))
119+
fmt.Println(path.Base("/foo/bar/baz/", path.Unix))
120+
fmt.Println(path.Base("dev.txt", path.Unix))
121+
fmt.Println(path.Base("../todo.txt", path.Unix))
122+
fmt.Println(path.Base("..", path.Unix))
123+
fmt.Println(path.Base(".", path.Unix))
124+
fmt.Println(path.Base("/", path.Unix))
125+
fmt.Println(path.Base("", path.Unix))
113126

114127
// Output:
115128
// On Unix:
@@ -126,16 +139,16 @@ func ExampleBase() {
126139

127140
func ExampleDir() {
128141
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(""))
142+
fmt.Println(path.Dir("/foo/bar/baz.js", path.Unix))
143+
fmt.Println(path.Dir("/foo/bar/baz", path.Unix))
144+
fmt.Println(path.Dir("/foo/bar/baz/", path.Unix))
145+
fmt.Println(path.Dir("/dirty//path///", path.Unix))
146+
fmt.Println(path.Dir("dev.txt", path.Unix))
147+
fmt.Println(path.Dir("../todo.txt", path.Unix))
148+
fmt.Println(path.Dir("..", path.Unix))
149+
fmt.Println(path.Dir(".", path.Unix))
150+
fmt.Println(path.Dir("/", path.Unix))
151+
fmt.Println(path.Dir("", path.Unix))
139152

140153
// Output:
141154
// On Unix:
@@ -153,12 +166,12 @@ func ExampleDir() {
153166

154167
func ExampleIsAbs() {
155168
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(""))
169+
fmt.Println(path.IsAbs("/home/gopher", path.Unix))
170+
fmt.Println(path.IsAbs(".bashrc", path.Unix))
171+
fmt.Println(path.IsAbs("..", path.Unix))
172+
fmt.Println(path.IsAbs(".", path.Unix))
173+
fmt.Println(path.IsAbs("/", path.Unix))
174+
fmt.Println(path.IsAbs("", path.Unix))
162175

163176
// Output:
164177
// On Unix:

pkg/path/testdata/example_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1+
// Copyright 2020 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
// Copyright 2017 The Go Authors. All rights reserved.
216
// Use of this source code is governed by a BSD-style
317
// license that can be found in the LICENSE file.
418

5-
package filepath_test
19+
package path_test
620

721
import (
822
"fmt"
9-
"path/filepath"
23+
24+
"cuelang.org/go/pkg/path"
1025
)
1126

1227
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"))
28+
fmt.Printf("No dots: %q\n", path.Ext("index", "unix"))
29+
fmt.Printf("One dot: %q\n", path.Ext("index.js", "unix"))
30+
fmt.Printf("Two dots: %q\n", path.Ext("main.test.js", "unix"))
1631
// Output:
1732
// No dots: ""
1833
// One dot: ".js"

pkg/path/testdata/example_unix_walk_test.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

pkg/path/testdata/export_test.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

pkg/path/testdata/export_windows_test.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)