github.com/IBM/fsgo@v0.0.0-20220920202152-e16fd2119d49/lstater_test.go (about)

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