github.com/safing/portbase@v0.19.5/utils/renameio/tempfile_linux_test.go (about)

     1  //go:build linux
     2  
     3  package renameio
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"syscall"
     9  	"testing"
    10  )
    11  
    12  func TestTempDir(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	if tmpdir, ok := os.LookupEnv("TMPDIR"); ok {
    16  		t.Cleanup(func() {
    17  			_ = os.Setenv("TMPDIR", tmpdir) // restore
    18  		})
    19  	} else {
    20  		t.Cleanup(func() {
    21  			_ = os.Unsetenv("TMPDIR") // restore
    22  		})
    23  	}
    24  
    25  	mount1, err := os.MkdirTemp("", "test-renameio-testtempdir1")
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	t.Cleanup(func() {
    30  		_ = os.RemoveAll(mount1)
    31  	})
    32  
    33  	mount2, err := os.MkdirTemp("", "test-renameio-testtempdir2")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	t.Cleanup(func() {
    38  		_ = os.RemoveAll(mount2)
    39  	})
    40  
    41  	if err := syscall.Mount("tmpfs", mount1, "tmpfs", 0, ""); err != nil {
    42  		t.Skipf("cannot mount tmpfs on %s: %v", mount1, err)
    43  	}
    44  	t.Cleanup(func() {
    45  		_ = syscall.Unmount(mount1, 0)
    46  	})
    47  
    48  	if err := syscall.Mount("tmpfs", mount2, "tmpfs", 0, ""); err != nil {
    49  		t.Skipf("cannot mount tmpfs on %s: %v", mount2, err)
    50  	}
    51  	t.Cleanup(func() {
    52  		_ = syscall.Unmount(mount2, 0)
    53  	})
    54  
    55  	tests := []struct {
    56  		name   string
    57  		dir    string
    58  		path   string
    59  		TMPDIR string
    60  		want   string
    61  	}{
    62  		{
    63  			name: "implicit TMPDIR",
    64  			path: filepath.Join(os.TempDir(), "foo.txt"),
    65  			want: os.TempDir(),
    66  		},
    67  
    68  		{
    69  			name:   "explicit TMPDIR",
    70  			path:   filepath.Join(mount1, "foo.txt"),
    71  			TMPDIR: mount1,
    72  			want:   mount1,
    73  		},
    74  
    75  		{
    76  			name:   "explicit unsuitable TMPDIR",
    77  			path:   filepath.Join(mount1, "foo.txt"),
    78  			TMPDIR: mount2,
    79  			want:   mount1,
    80  		},
    81  
    82  		{
    83  			name:   "nonexistant TMPDIR",
    84  			path:   filepath.Join(mount1, "foo.txt"),
    85  			TMPDIR: "/nonexistant",
    86  			want:   mount1,
    87  		},
    88  
    89  		{
    90  			name:   "caller-specified",
    91  			dir:    "/overridden",
    92  			path:   filepath.Join(mount1, "foo.txt"),
    93  			TMPDIR: "/nonexistant",
    94  			want:   "/overridden",
    95  		},
    96  	}
    97  
    98  	for _, tt := range tests {
    99  		testCase := tt
   100  
   101  		t.Run(testCase.name, func(t *testing.T) {
   102  			t.Parallel()
   103  
   104  			if testCase.TMPDIR == "" {
   105  				_ = os.Unsetenv("TMPDIR")
   106  			} else {
   107  				_ = os.Setenv("TMPDIR", testCase.TMPDIR)
   108  			}
   109  
   110  			if got := tempDir(testCase.dir, testCase.path); got != testCase.want {
   111  				t.Fatalf("tempDir(%q, %q): got %q, want %q", testCase.dir, testCase.path, got, testCase.want)
   112  			}
   113  		})
   114  	}
   115  }