github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/memfs/memfs_examples_test.go (about) 1 // 2 // Copyright 2020 The AVFS authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 //go:build avfs_setostype 18 19 package memfs_test 20 21 import ( 22 "errors" 23 "fmt" 24 "io/fs" 25 "log" 26 27 "github.com/avfs/avfs" 28 "github.com/avfs/avfs/idm/memidm" 29 "github.com/avfs/avfs/test" 30 "github.com/avfs/avfs/vfs/memfs" 31 ) 32 33 // ExampleNew should produce the same results independently of the host OS. 34 func ExampleNew() { 35 idm := memidm.NewWithOptions(&memidm.Options{OSType: avfs.OsLinux}) 36 vfs := memfs.NewWithOptions(&memfs.Options{Idm: idm, OSType: avfs.OsLinux}) 37 38 fmt.Println(vfs.Features()) 39 fmt.Println(vfs.User().Name()) 40 fmt.Println(vfs.TempDir()) 41 42 homeDir := avfs.HomeDirUser(vfs, vfs.User()) 43 fmt.Println(homeDir) 44 45 _, err := vfs.Stat(homeDir) 46 if err == nil { 47 fmt.Printf("%s exists", homeDir) 48 } 49 50 // Features(Hardlink|IdentityMgr|SetOSType|SubFS|Symlink|SystemDirs) 51 // root 52 // /tmp 53 // /root 54 // /root exists 55 } 56 57 func ExampleNewWithOptions_noSystemDirs() { 58 vfs := memfs.NewWithOptions(&memfs.Options{OSType: avfs.OsLinux}) 59 tmpDir := vfs.TempDir() 60 61 _, err := vfs.Stat(tmpDir) 62 if errors.Is(err, fs.ErrNotExist) { 63 fmt.Printf("%s does not exist", tmpDir) 64 } 65 66 // Output: /tmp does not exist 67 } 68 69 func ExampleNewWithOptions_noIdm() { 70 vfs := memfs.NewWithOptions(&memfs.Options{Idm: avfs.NotImplementedIdm, OSType: avfs.OsLinux}) 71 fmt.Println(vfs.User().Name()) 72 73 // Output: Default 74 } 75 76 func ExampleMemFS_Sub() { 77 idm := memidm.NewWithOptions(&memidm.Options{OSType: avfs.OsLinux}) 78 vfsSrc := memfs.NewWithOptions(&memfs.Options{Idm: idm, OSType: avfs.OsLinux}) 79 80 _, err := vfsSrc.Idm().UserAdd(test.UsrTest, "root") 81 if err != nil { 82 log.Fatalf("UserAdd : want error to be nil, got %v", err) 83 } 84 85 vfsSub, err := vfsSrc.Sub("/") 86 if err != nil { 87 log.Fatalf("Sub : want error to be nil, got %v", err) 88 } 89 90 err = vfsSub.SetUserByName(test.UsrTest) 91 if err != nil { 92 log.Fatalf("SetUser : want error to be nil, got %v", err) 93 } 94 95 fmt.Println(vfsSrc.User().Name()) 96 fmt.Println(vfsSub.User().Name()) 97 98 // Output: 99 // root 100 // UsrTest 101 } 102 103 func ExampleMemFS_SetUserByName() { 104 idm := memidm.NewWithOptions(&memidm.Options{OSType: avfs.OsLinux}) 105 vfs := memfs.NewWithOptions(&memfs.Options{Idm: idm, OSType: avfs.OsLinux}) 106 107 _, err := vfs.Idm().UserAdd(test.UsrTest, idm.AdminGroup().Name()) 108 if err != nil { 109 log.Fatalf("UserAdd : want error to be nil, got %v", err) 110 } 111 112 fmt.Println(vfs.User().Name()) 113 114 err = vfs.SetUserByName(test.UsrTest) 115 if err != nil { 116 log.Fatalf("SetUser : want error to be nil, got %v", err) 117 } 118 119 fmt.Println(vfs.User().Name()) 120 121 // Output: 122 // root 123 // UsrTest 124 }