github.com/mferrell/afero@v1.8.3-0.20220319163648-1d8d1d1d8040/lstater_test.go (about)

     1  // Copyright ©2018 Steve Francia <spf@spf13.com>
     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  //     http://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  package afero
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  )
    22  
    23  func TestLstatIfPossible(t *testing.T) {
    24  	wd, _ := os.Getwd()
    25  	defer func() {
    26  		os.Chdir(wd)
    27  	}()
    28  
    29  	osFs := &OsFs{}
    30  
    31  	workDir, err := TempDir(osFs, "", "afero-lstate")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	defer func() {
    37  		osFs.RemoveAll(workDir)
    38  	}()
    39  
    40  	memWorkDir := "/lstate"
    41  
    42  	memFs := NewMemMapFs()
    43  	overlayFs1 := &CopyOnWriteFs{base: osFs, layer: memFs}
    44  	overlayFs2 := &CopyOnWriteFs{base: memFs, layer: osFs}
    45  	overlayFsMemOnly := &CopyOnWriteFs{base: memFs, layer: NewMemMapFs()}
    46  	basePathFs := &BasePathFs{source: osFs, path: workDir}
    47  	basePathFsMem := &BasePathFs{source: memFs, path: memWorkDir}
    48  	roFs := &ReadOnlyFs{source: osFs}
    49  	roFsMem := &ReadOnlyFs{source: memFs}
    50  
    51  	pathFileMem := filepath.Join(memWorkDir, "aferom.txt")
    52  
    53  	WriteFile(osFs, filepath.Join(workDir, "afero.txt"), []byte("Hi, Afero!"), 0777)
    54  	WriteFile(memFs, filepath.Join(pathFileMem), []byte("Hi, Afero!"), 0777)
    55  
    56  	os.Chdir(workDir)
    57  	if err := os.Symlink("afero.txt", "symafero.txt"); err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	pathFile := filepath.Join(workDir, "afero.txt")
    62  	pathSymlink := filepath.Join(workDir, "symafero.txt")
    63  
    64  	checkLstat := func(l Lstater, name string, shouldLstat bool) os.FileInfo {
    65  		statFile, isLstat, err := l.LstatIfPossible(name)
    66  		if err != nil {
    67  			t.Fatalf("Lstat check failed: %s", err)
    68  		}
    69  		if isLstat != shouldLstat {
    70  			t.Fatalf("Lstat status was %t for %s", isLstat, name)
    71  		}
    72  		return statFile
    73  	}
    74  
    75  	testLstat := func(l Lstater, pathFile, pathSymlink string) {
    76  		shouldLstat := pathSymlink != ""
    77  		statRegular := checkLstat(l, pathFile, shouldLstat)
    78  		statSymlink := checkLstat(l, pathSymlink, shouldLstat)
    79  		if statRegular == nil || statSymlink == nil {
    80  			t.Fatal("got nil FileInfo")
    81  		}
    82  
    83  		symSym := statSymlink.Mode()&os.ModeSymlink == os.ModeSymlink
    84  		if symSym == (pathSymlink == "") {
    85  			t.Fatal("expected the FileInfo to describe the symlink")
    86  		}
    87  
    88  		_, _, err := l.LstatIfPossible("this-should-not-exist.txt")
    89  		if err == nil || !os.IsNotExist(err) {
    90  			t.Fatalf("expected file to not exist, got %s", err)
    91  		}
    92  	}
    93  
    94  	testLstat(osFs, pathFile, pathSymlink)
    95  	testLstat(overlayFs1, pathFile, pathSymlink)
    96  	testLstat(overlayFs2, pathFile, pathSymlink)
    97  	testLstat(basePathFs, "afero.txt", "symafero.txt")
    98  	testLstat(overlayFsMemOnly, pathFileMem, "")
    99  	testLstat(basePathFsMem, "aferom.txt", "")
   100  	testLstat(roFs, pathFile, pathSymlink)
   101  	testLstat(roFsMem, pathFileMem, "")
   102  }