github.com/kujenga/gash@v0.0.0-20230321210531-bed9f4dc58b3/cmd/smart-pwd/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func setup(t testing.TB) (string, func()) {
    14  	// mock home directory
    15  	home, err := ioutil.TempDir("", "TestGetDir-home")
    16  	require.NoError(t, err)
    17  	home, err = filepath.EvalSymlinks(home)
    18  	require.NoError(t, err)
    19  	// mock home directory
    20  	os.Setenv("HOME", home)
    21  
    22  	// setup directories to test against.
    23  	for _, dir := range []string{
    24  		"aaa/.git",
    25  		"aaa/bbb/ccc",
    26  		"xxx/yyy",
    27  	} {
    28  		require.NoError(t, os.MkdirAll(filepath.Join(home, dir), 0777))
    29  	}
    30  	return home, func() { os.RemoveAll(home) }
    31  }
    32  
    33  var sink string
    34  
    35  func BenchmarkGetSmartPlain(b *testing.B) {
    36  	home, cleanup := setup(b)
    37  	defer cleanup()
    38  
    39  	var s string
    40  	b.ResetTimer()
    41  	for n := 0; n < b.N; n++ {
    42  		os.Chdir(filepath.Join(home, "aaa/bbb/ccc"))
    43  		s = getSmart()
    44  	}
    45  	b.StopTimer()
    46  	sink = s
    47  }
    48  
    49  func BenchmarkGetSmartGitRepo(b *testing.B) {
    50  	home, cleanup := setup(b)
    51  	defer cleanup()
    52  
    53  	var s string
    54  	b.ResetTimer()
    55  	for n := 0; n < b.N; n++ {
    56  		os.Chdir(filepath.Join(home, "xxx/yyy"))
    57  		s = getSmart()
    58  	}
    59  	b.StopTimer()
    60  	sink = s
    61  }
    62  
    63  func TestGetDir(t *testing.T) {
    64  	home, cleanup := setup(t)
    65  	defer cleanup()
    66  
    67  	tcs := []struct {
    68  		wd    string
    69  		raw   string
    70  		smart string
    71  	}{
    72  		{
    73  			wd:    filepath.Join(home, "aaa"),
    74  			raw:   "aaa",
    75  			smart: "aaa",
    76  		},
    77  		{
    78  			wd:    filepath.Join(home, "aaa/bbb/ccc"),
    79  			raw:   "aaa/bbb/ccc",
    80  			smart: "a/b/ccc",
    81  		},
    82  		{
    83  			wd:    filepath.Join(home, "xxx"),
    84  			raw:   filepath.Join(home, "xxx"),
    85  			smart: "~/xxx",
    86  		},
    87  		{
    88  			wd:    filepath.Join(home, "xxx/yyy"),
    89  			raw:   filepath.Join(home, "xxx/yyy"),
    90  			smart: "~/x/yyy",
    91  		},
    92  	}
    93  	for _, tc := range tcs {
    94  		// mock current working directory
    95  		os.Chdir(tc.wd)
    96  		// test output
    97  		out := getDir()
    98  		assert.Equal(t, tc.raw, out, "raw output should match expected")
    99  		smart := smartenUp(out)
   100  		assert.Equal(t, tc.smart, smart, "smart output should match expected")
   101  	}
   102  }
   103  
   104  func TestSmartenUp(t *testing.T) {
   105  	// mock home directory
   106  	os.Setenv("HOME", "/home")
   107  
   108  	tcs := []struct {
   109  		in     string
   110  		expect string
   111  	}{
   112  		{
   113  			in:     "aaa/bbb/ccc",
   114  			expect: "a/b/ccc",
   115  		},
   116  		{
   117  			in:     "/home/aaa/bbb/ccc",
   118  			expect: "~/a/b/ccc",
   119  		},
   120  	}
   121  	for _, tc := range tcs {
   122  		out := smartenUp(tc.in)
   123  		assert.Equal(t, tc.expect, out, "smart version should match expected")
   124  	}
   125  }