gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/elvish/util/tempdir_test.go (about)

     1  package util
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  )
     8  
     9  func TestWithTempDirs_PassesDirs(t *testing.T) {
    10  	WithTempDirs(10, func(dirs []string) {
    11  		for _, dir := range dirs {
    12  			stat, err := os.Stat(dir)
    13  			if err != nil {
    14  				t.Errorf("WithTempDir passes %q, but it cannot be stat'ed", dir)
    15  			}
    16  			if !stat.IsDir() {
    17  				t.Errorf("WithTempDir passes %q, but it is not dir", dir)
    18  			}
    19  		}
    20  	})
    21  }
    22  
    23  func TestWithTempDir_RemovesDirs(t *testing.T) {
    24  	var tempDirs []string
    25  	WithTempDirs(10, func(dirs []string) { tempDirs = dirs })
    26  	for _, dir := range tempDirs {
    27  		_, err := os.Stat(dir)
    28  		if err == nil {
    29  			t.Errorf("After WithTempDir returns, %q still exists", dir)
    30  		}
    31  	}
    32  }
    33  
    34  func TestInTempDir_CDIn(t *testing.T) {
    35  	InTempDir(func(tmpDir string) {
    36  		pwd := getPwd()
    37  		evaledTmpDir, err := filepath.EvalSymlinks(tmpDir)
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  		if pwd != evaledTmpDir {
    42  			t.Errorf("In InTempDir, working dir (%q) != EvalSymlinks(argument) (%q)", pwd, evaledTmpDir)
    43  		}
    44  	})
    45  }
    46  
    47  func TestInTempDir_CDOut(t *testing.T) {
    48  	before := getPwd()
    49  	InTempDir(func(tmpDir string) {})
    50  	after := getPwd()
    51  	if before != after {
    52  		t.Errorf("With InTempDir, working dir before %q != after %q", before, after)
    53  	}
    54  }
    55  
    56  func getPwd() string {
    57  	dir, err := os.Getwd()
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	dir, err = filepath.EvalSymlinks(dir)
    62  	if err != nil {
    63  		panic(err)
    64  	}
    65  	return dir
    66  }