github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/memfs/memfs_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_race
    18  
    19  package memfs_test
    20  
    21  import (
    22  	"io/fs"
    23  	"testing"
    24  
    25  	"github.com/avfs/avfs"
    26  	"github.com/avfs/avfs/idm/memidm"
    27  	"github.com/avfs/avfs/test"
    28  	"github.com/avfs/avfs/vfs/memfs"
    29  )
    30  
    31  var (
    32  	// Tests that memfs.MemFS struct implements avfs.VFS interface.
    33  	_ avfs.VFS = &memfs.MemFS{}
    34  
    35  	// Tests that memfs.MemFile struct implements avfs.File interface.
    36  	_ avfs.File = &memfs.MemFile{}
    37  
    38  	// Tests that memfs.MemInfo struct implements fs.DirEntry interface.
    39  	_ fs.DirEntry = &memfs.MemInfo{}
    40  
    41  	// Tests that memfs.MemInfo struct implements fs.FileInfo interface.
    42  	_ fs.FileInfo = &memfs.MemInfo{}
    43  
    44  	// Tests that memfs.MemInfo struct implements avfs.SysStater interface.
    45  	_ avfs.SysStater = &memfs.MemInfo{}
    46  
    47  	// Tests that memfs.MemIOFS struct implements avfs.IOFS interface.
    48  	_ avfs.IOFS = &memfs.MemIOFS{}
    49  )
    50  
    51  func TestMemFS(t *testing.T) {
    52  	vfs := memfs.New()
    53  
    54  	ts := test.NewSuiteFS(t, vfs, vfs)
    55  	ts.TestVFSAll(t)
    56  }
    57  
    58  func TestMemFSWithNoIdm(t *testing.T) {
    59  	vfs := memfs.NewWithOptions(&memfs.Options{Idm: avfs.NotImplementedIdm})
    60  
    61  	ts := test.NewSuiteFS(t, vfs, vfs)
    62  	ts.TestVFSAll(t)
    63  }
    64  
    65  func TestMemFSOptionUser(t *testing.T) {
    66  	idm := memidm.New()
    67  
    68  	groupName := "aGroup"
    69  	_, err := idm.GroupAdd(groupName)
    70  	test.RequireNoError(t, err, "GroupAdd %s", groupName)
    71  
    72  	userName := "aUser"
    73  	u, err := idm.UserAdd(userName, groupName)
    74  	test.RequireNoError(t, err, "UserAdd %s", userName)
    75  
    76  	vfs := memfs.NewWithOptions(&memfs.Options{User: u})
    77  
    78  	dir := "test"
    79  	err = vfs.Mkdir(dir, avfs.DefaultDirPerm)
    80  	test.RequireNoError(t, err, "Mkdir %s", dir)
    81  
    82  	info, err := vfs.Stat(dir)
    83  	test.RequireNoError(t, err, "Stat %s", dir)
    84  
    85  	sst := vfs.ToSysStat(info)
    86  	if sst.Uid() != u.Uid() {
    87  		t.Errorf("want Uid to be %d, got %d", u.Uid(), sst.Uid())
    88  	}
    89  
    90  	if sst.Gid() != u.Gid() {
    91  		t.Errorf("want Uid to be %d, got %d", u.Gid(), sst.Gid())
    92  	}
    93  }
    94  
    95  // TestMemFsOptionName tests MemFS initialization with or without option name (WithName()).
    96  func TestMemFSOptionName(t *testing.T) {
    97  	const wantName = "whatever"
    98  
    99  	vfs := memfs.New()
   100  	if vfs.Name() != "" {
   101  		t.Errorf("New : want name to be '', got %s", vfs.Name())
   102  	}
   103  
   104  	vfs = memfs.NewWithOptions(&memfs.Options{Name: wantName})
   105  
   106  	name := vfs.Name()
   107  	if name != wantName {
   108  		t.Errorf("New : want name to be %s, got %s", wantName, vfs.Name())
   109  	}
   110  }
   111  
   112  func TestMemFSNilPtrFile(t *testing.T) {
   113  	f := (*memfs.MemFile)(nil)
   114  
   115  	test.FileNilPtr(t, f)
   116  }
   117  
   118  func TestMemFSConfig(t *testing.T) {
   119  	vfs := memfs.New()
   120  
   121  	wantFeatures := avfs.FeatHardlink | avfs.FeatSubFS | avfs.FeatSymlink | avfs.FeatIdentityMgr |
   122  		avfs.BuildFeatures()
   123  	if vfs.Features() != wantFeatures {
   124  		t.Errorf("Features : want Features to be %s, got %s", wantFeatures, vfs.Features())
   125  	}
   126  
   127  	name := vfs.Name()
   128  	if name != "" {
   129  		t.Errorf("Name : want name to be empty, got %v", name)
   130  	}
   131  
   132  	osType := vfs.OSType()
   133  	if osType != avfs.CurrentOSType() {
   134  		t.Errorf("OSType : want os type to be %v, got %v", avfs.CurrentOSType(), osType)
   135  	}
   136  }
   137  
   138  func BenchmarkMemFSAll(b *testing.B) {
   139  	vfs := memfs.New()
   140  
   141  	ts := test.NewSuiteFS(b, vfs, vfs)
   142  	ts.BenchAll(b)
   143  }