golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/gcsfs/gcsfs_test.go (about) 1 // Copyright 2022 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 gcsfs 6 7 import ( 8 "context" 9 "flag" 10 "io/fs" 11 "os" 12 "path/filepath" 13 "testing" 14 "testing/fstest" 15 "time" 16 17 "cloud.google.com/go/storage" 18 "google.golang.org/api/option" 19 ) 20 21 var slowTest = flag.Bool("slow", false, "run slow tests that access GCS") 22 23 func TestGCSFS(t *testing.T) { 24 if !*slowTest { 25 t.Skip("reads a largeish GCS bucket") 26 } 27 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) 28 defer cancel() 29 client, err := storage.NewClient(context.Background(), option.WithScopes(storage.ScopeReadOnly)) 30 if err != nil { 31 t.Fatal(err) 32 } 33 fsys := NewFS(ctx, client, "vcs-test") 34 expected := []string{ 35 "auth/or401.zip", 36 "bzr/hello.zip", 37 } 38 if err := fstest.TestFS(fsys, expected...); err != nil { 39 t.Error(err) 40 } 41 42 sub, err := fs.Sub(fsys, "auth") 43 if err != nil { 44 t.Fatal(err) 45 } 46 if err := fstest.TestFS(sub, "or401.zip"); err != nil { 47 t.Error(err) 48 } 49 } 50 51 func TestDirFS(t *testing.T) { 52 if err := fstest.TestFS(DirFS("./testdata/dirfs"), "a", "b", "dir/x"); err != nil { 53 t.Fatal(err) 54 } 55 } 56 57 func TestDirFSDotFiles(t *testing.T) { 58 temp := t.TempDir() 59 if err := os.WriteFile(temp+"/.foo", nil, 0777); err != nil { 60 t.Fatal(err) 61 } 62 files, err := fs.ReadDir(DirFS(temp), ".") 63 if err != nil { 64 t.Fatal(err) 65 } 66 if len(files) != 0 { 67 t.Errorf("ReadDir didn't hide . files: %v", files) 68 } 69 } 70 71 func TestDirFSWrite(t *testing.T) { 72 temp := t.TempDir() 73 fsys := DirFS(temp) 74 f, err := Create(fsys, "fsystest.txt") 75 if err != nil { 76 t.Fatal(err) 77 } 78 if _, err := f.Write([]byte("hey\n")); err != nil { 79 t.Fatal(err) 80 } 81 if err := f.Close(); err != nil { 82 t.Fatal(err) 83 } 84 b, err := os.ReadFile(filepath.Join(temp, "fsystest.txt")) 85 if err != nil { 86 t.Fatal(err) 87 } 88 if string(b) != "hey\n" { 89 t.Fatalf("unexpected file contents %q, want %q", string(b), "hey\n") 90 } 91 }