-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs_test.go
95 lines (89 loc) · 2.37 KB
/
fs_test.go
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
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build go1.16
package runfiles_test
import (
"io"
"io/fs"
"os"
"path/filepath"
"testing"
"testing/fstest"
"github.com/phst/runfiles"
)
func TestFS(t *testing.T) {
fsys, err := runfiles.New()
if err != nil {
t.Fatal(err)
}
// Ensure that the Runfiles object implements FS interfaces.
var _ fs.FS = fsys
var _ fs.StatFS = fsys
var _ fs.ReadFileFS = fsys
if err := fstest.TestFS(fsys, "com_github_phst_runfiles/test.txt", "com_github_phst_runfiles/testprog/testprog"); err != nil {
t.Error(err)
}
}
func TestFS_empty(t *testing.T) {
dir := t.TempDir()
manifest := filepath.Join(dir, "manifest")
if err := os.WriteFile(manifest, []byte("__init__.py \n"), 0600); err != nil {
t.Fatal(err)
}
fsys, err := runfiles.New(runfiles.ManifestFile(manifest), runfiles.ProgramName("/invalid"), runfiles.Directory("/invalid"))
if err != nil {
t.Fatal(err)
}
t.Run("Open", func(t *testing.T) {
fd, err := fsys.Open("__init__.py")
if err != nil {
t.Fatal(err)
}
defer fd.Close()
got, err := io.ReadAll(fd)
if err != nil {
t.Error(err)
}
if len(got) != 0 {
t.Errorf("got nonempty contents: %q", got)
}
})
t.Run("Stat", func(t *testing.T) {
got, err := fsys.Stat("__init__.py")
if err != nil {
t.Fatal(err)
}
if got.Name() != "__init__.py" {
t.Errorf("Name: got %q, want %q", got.Name(), "__init__.py")
}
if got.Size() != 0 {
t.Errorf("Size: got %d, want %d", got.Size(), 0)
}
if !got.Mode().IsRegular() {
t.Errorf("IsRegular: got %v, want %v", got.Mode().IsRegular(), true)
}
if got.IsDir() {
t.Errorf("IsDir: got %v, want %v", got.IsDir(), false)
}
})
t.Run("ReadFile", func(t *testing.T) {
got, err := fsys.ReadFile("__init__.py")
if err != nil {
t.Error(err)
}
if len(got) != 0 {
t.Errorf("got nonempty contents: %q", got)
}
})
}