github.com/moby/docker@v26.1.3+incompatible/pkg/system/lstat_unix_test.go (about)

     1  //go:build linux || freebsd
     2  
     3  package system // import "github.com/docker/docker/pkg/system"
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  // TestLstat tests Lstat for existing and non existing files
    12  func TestLstat(t *testing.T) {
    13  	tmpDir := t.TempDir()
    14  	file := filepath.Join(tmpDir, "exist")
    15  	if err := os.WriteFile(file, []byte("hello"), 0o644); err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	statFile, err := Lstat(file)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if statFile == nil {
    24  		t.Fatal("returned empty stat for existing file")
    25  	}
    26  
    27  	statInvalid, err := Lstat(filepath.Join(tmpDir, "nosuchfile"))
    28  	if err == nil {
    29  		t.Fatal("did not return error for non-existing file")
    30  	}
    31  	if statInvalid != nil {
    32  		t.Fatal("returned non-nil stat for non-existing file")
    33  	}
    34  }