github.com/mymmsc/gox@v1.3.33/util/homedir/homedir_test.go (about)

     1  package homedir
     2  
     3  import (
     4  	"os"
     5  	"os/user"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func patchEnv(key, value string) func() {
    11  	bck := os.Getenv(key)
    12  	deferFunc := func() {
    13  		os.Setenv(key, bck)
    14  	}
    15  
    16  	if value != "" {
    17  		os.Setenv(key, value)
    18  	} else {
    19  		os.Unsetenv(key)
    20  	}
    21  
    22  	return deferFunc
    23  }
    24  
    25  func BenchmarkDir(b *testing.B) {
    26  	// We do this for any "warmups"
    27  	for i := 0; i < 10; i++ {
    28  		Dir()
    29  	}
    30  
    31  	b.ResetTimer()
    32  	for i := 0; i < b.N; i++ {
    33  		Dir()
    34  	}
    35  }
    36  
    37  func TestDir(t *testing.T) {
    38  	u, err := user.Current()
    39  	if err != nil {
    40  		t.Fatalf("err: %s", err)
    41  	}
    42  
    43  	dir, err := Dir()
    44  	if err != nil {
    45  		t.Fatalf("err: %s", err)
    46  	}
    47  
    48  	if u.HomeDir != dir {
    49  		t.Fatalf("%#v != %#v", u.HomeDir, dir)
    50  	}
    51  
    52  	DisableCache = true
    53  	defer func() { DisableCache = false }()
    54  	defer patchEnv("HOME", "")()
    55  	dir, err = Dir()
    56  	if err != nil {
    57  		t.Fatalf("err: %s", err)
    58  	}
    59  
    60  	if u.HomeDir != dir {
    61  		t.Fatalf("%#v != %#v", u.HomeDir, dir)
    62  	}
    63  }
    64  
    65  func TestExpand(t *testing.T) {
    66  	u, err := user.Current()
    67  	if err != nil {
    68  		t.Fatalf("err: %s", err)
    69  	}
    70  
    71  	cases := []struct {
    72  		Input  string
    73  		Output string
    74  		Err    bool
    75  	}{
    76  		{
    77  			"/foo",
    78  			"/foo",
    79  			false,
    80  		},
    81  
    82  		{
    83  			"~/foo",
    84  			filepath.Join(u.HomeDir, "foo"),
    85  			false,
    86  		},
    87  
    88  		{
    89  			"",
    90  			"",
    91  			false,
    92  		},
    93  
    94  		{
    95  			"~",
    96  			u.HomeDir,
    97  			false,
    98  		},
    99  
   100  		{
   101  			"~foo/foo",
   102  			"",
   103  			true,
   104  		},
   105  	}
   106  
   107  	for _, tc := range cases {
   108  		actual, err := Expand(tc.Input)
   109  		if (err != nil) != tc.Err {
   110  			t.Fatalf("Input: %#v\n\nErr: %s", tc.Input, err)
   111  		}
   112  
   113  		if actual != tc.Output {
   114  			t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual)
   115  		}
   116  	}
   117  
   118  	DisableCache = true
   119  	defer func() { DisableCache = false }()
   120  	defer patchEnv("HOME", "/custom/path/")()
   121  	expected := filepath.Join("/", "custom", "path", "foo/bar")
   122  	actual, err := Expand("~/foo/bar")
   123  
   124  	if err != nil {
   125  		t.Errorf("No error is expected, got: %v", err)
   126  	} else if actual != expected {
   127  		t.Errorf("Expected: %v; actual: %v", expected, actual)
   128  	}
   129  }