github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/basepathfs/basepathfs_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 basepathfs_test
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/avfs/avfs"
    25  	"github.com/avfs/avfs/test"
    26  	"github.com/avfs/avfs/vfs/basepathfs"
    27  	"github.com/avfs/avfs/vfs/memfs"
    28  )
    29  
    30  var (
    31  	// Tests that basepathfs.BasePathFS struct implements avfs.VFS interface.
    32  	_ avfs.VFS = &basepathfs.BasePathFS{}
    33  
    34  	// Tests that basepathfs.BasePathFile struct implements avfs.File interface.
    35  	_ avfs.File = &basepathfs.BasePathFile{}
    36  )
    37  
    38  func initFS(tb testing.TB) *basepathfs.BasePathFS {
    39  	baseFs := memfs.New()
    40  	basePath := avfs.FromUnixPath(baseFs, "/base/path")
    41  
    42  	err := baseFs.MkdirAll(basePath, avfs.DefaultDirPerm)
    43  	if err != nil {
    44  		tb.Fatalf("MkdirAll %s : want error to be nil, got %v", basePath, err)
    45  	}
    46  
    47  	vfs := basepathfs.New(baseFs, basePath)
    48  
    49  	return vfs
    50  }
    51  
    52  func initTest(t *testing.T) *test.Suite {
    53  	vfs := initFS(t)
    54  	ts := test.NewSuiteFS(t, vfs, vfs)
    55  
    56  	return ts
    57  }
    58  
    59  func TestBasePathFS(t *testing.T) {
    60  	ts := initTest(t)
    61  	ts.TestVFSAll(t)
    62  }
    63  
    64  // TestBasePathFsOptions tests BasePathFS configuration options.
    65  func TestBasePathFSOptions(t *testing.T) {
    66  	vfs := memfs.New()
    67  	nonExistingDir := avfs.FromUnixPath(vfs, "/non/existing/dir")
    68  
    69  	test.AssertPanic(t, "", func() {
    70  		_ = basepathfs.New(vfs, nonExistingDir)
    71  	})
    72  
    73  	existingFile := vfs.Join(vfs.TempDir(), "existing")
    74  
    75  	err := vfs.WriteFile(existingFile, []byte{}, avfs.DefaultFilePerm)
    76  	if err != nil {
    77  		t.Fatalf("WriteFile : want error to be nil, got %v", err)
    78  	}
    79  
    80  	test.AssertPanic(t, "", func() {
    81  		_ = basepathfs.New(vfs, existingFile)
    82  	})
    83  }
    84  
    85  func TestBasePathFSFeatures(t *testing.T) {
    86  	vfs := basepathfs.New(memfs.New(), "/")
    87  	if vfs.HasFeature(avfs.FeatSymlink) {
    88  		t.Errorf("Features : want FeatSymlink missing, got present")
    89  	}
    90  
    91  	if !vfs.HasFeature(avfs.FeatIdentityMgr) {
    92  		t.Errorf("Features : want FeatIdentityMgr present, got missing")
    93  	}
    94  
    95  	mfs := memfs.New()
    96  
    97  	vfs = basepathfs.New(mfs, "/")
    98  	if !vfs.HasFeature(avfs.FeatIdentityMgr) {
    99  		t.Errorf("Features : want FeatIdentityMgr present, got missing")
   100  	}
   101  }
   102  
   103  func TestBasepathFSOSType(t *testing.T) {
   104  	vfsBase := memfs.New()
   105  	vfs := basepathfs.New(vfsBase, vfsBase.TempDir())
   106  
   107  	osType := vfs.OSType()
   108  	if osType != vfsBase.OSType() {
   109  		t.Errorf("OSType : want os type to be %v, got %v", vfsBase.OSType(), osType)
   110  	}
   111  }