1// Copyright 2021 Google LLC 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// https://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 15//go:build go1.16 16// +build go1.16 17 18package runfiles_test 19 20import ( 21 "io" 22 "io/fs" 23 "os" 24 "path/filepath" 25 "runtime" 26 "testing" 27 "testing/fstest" 28 29 "github.com/bazelbuild/rules_go/go/runfiles" 30) 31 32func TestFS(t *testing.T) { 33 fsys, err := runfiles.New() 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 // Ensure that the Runfiles object implements FS interfaces. 39 var _ fs.FS = fsys 40 var _ fs.StatFS = fsys 41 var _ fs.ReadFileFS = fsys 42 43 if runtime.GOOS == "windows" { 44 // Currently the result of 45 // 46 // fsys.Rlocation("io_bazel_rules_go/go/runfiles/test.txt") 47 // fsys.Rlocation("bazel_tools/tools/bash/runfiles/runfiles.bash") 48 // fsys.Rlocation("io_bazel_rules_go/go/runfiles/testprog/testprog") 49 // 50 // would be a full path like these 51 // 52 // C:\b\bk-windows-1z0z\bazel\rules-go-golang\go\tools\bazel\runfiles\test.txt 53 // C:\b\zslxztin\external\bazel_tools\tools\bash\runfiles\runfiles.bash 54 // C:\b\pm4ep4b2\execroot\io_bazel_rules_go\bazel-out\x64_windows-fastbuild\bin\go\tools\bazel\runfiles\testprog\testprog 55 // 56 // Which does not follow any particular patter / rules. 57 // This makes it very hard to define what we are looking for on Windows. 58 // So let's skip this for now. 59 return 60 } 61 62 expected1 := "io_bazel_rules_go/tests/runfiles/test.txt" 63 expected2 := "io_bazel_rules_go/tests/runfiles/testprog/testprog_/testprog" 64 expected3 := "bazel_tools/tools/bash/runfiles/runfiles.bash" 65 if err := fstest.TestFS(fsys, expected1, expected2, expected3); err != nil { 66 t.Error(err) 67 } 68} 69 70func TestFS_empty(t *testing.T) { 71 dir := t.TempDir() 72 manifest := filepath.Join(dir, "manifest") 73 if err := os.WriteFile(manifest, []byte("__init__.py \n"), 0o600); err != nil { 74 t.Fatal(err) 75 } 76 fsys, err := runfiles.New(runfiles.ManifestFile(manifest), runfiles.ProgramName("/invalid"), runfiles.Directory("/invalid")) 77 if err != nil { 78 t.Fatal(err) 79 } 80 t.Run("Open", func(t *testing.T) { 81 fd, err := fsys.Open("__init__.py") 82 if err != nil { 83 t.Fatal(err) 84 } 85 defer fd.Close() 86 got, err := io.ReadAll(fd) 87 if err != nil { 88 t.Error(err) 89 } 90 if len(got) != 0 { 91 t.Errorf("got nonempty contents: %q", got) 92 } 93 }) 94 t.Run("Stat", func(t *testing.T) { 95 got, err := fsys.Stat("__init__.py") 96 if err != nil { 97 t.Fatal(err) 98 } 99 if got.Name() != "__init__.py" { 100 t.Errorf("Name: got %q, want %q", got.Name(), "__init__.py") 101 } 102 if got.Size() != 0 { 103 t.Errorf("Size: got %d, want %d", got.Size(), 0) 104 } 105 if !got.Mode().IsRegular() { 106 t.Errorf("IsRegular: got %v, want %v", got.Mode().IsRegular(), true) 107 } 108 if got.IsDir() { 109 t.Errorf("IsDir: got %v, want %v", got.IsDir(), false) 110 } 111 }) 112 t.Run("ReadFile", func(t *testing.T) { 113 got, err := fsys.ReadFile("__init__.py") 114 if err != nil { 115 t.Error(err) 116 } 117 if len(got) != 0 { 118 t.Errorf("got nonempty contents: %q", got) 119 } 120 }) 121} 122