github.com/motemen/ghq@v1.0.3/cmd_root_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  	"sync"
     9  	"testing"
    10  
    11  	"github.com/Songmu/gitconfig"
    12  )
    13  
    14  func samePath(lhs, rhs string) bool {
    15  	if runtime.GOOS != "windows" {
    16  		return lhs == rhs
    17  	}
    18  
    19  	lhs, _ = filepath.Abs(filepath.Clean(lhs))
    20  	rhs, _ = filepath.Abs(filepath.Clean(rhs))
    21  	return strings.ToLower(lhs) == strings.ToLower(lhs)
    22  }
    23  
    24  func samePaths(lhs, rhs string) bool {
    25  	if runtime.GOOS != "windows" {
    26  		return lhs == rhs
    27  	}
    28  	lhss := strings.Split(lhs, "\n")
    29  	rhss := strings.Split(rhs, "\n")
    30  	for i := range lhss {
    31  		if !samePath(lhss[i], rhss[i]) {
    32  			return false
    33  		}
    34  	}
    35  	return true
    36  }
    37  
    38  func TestDoRoot(t *testing.T) {
    39  	testCases := []struct {
    40  		name              string
    41  		setup             func() func()
    42  		expect, allExpect string
    43  	}{{
    44  		name: "env",
    45  		setup: func() func() {
    46  			orig := os.Getenv(envGhqRoot)
    47  			os.Setenv(envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2")
    48  			return func() { os.Setenv(envGhqRoot, orig) }
    49  		},
    50  		expect:    "/path/to/ghqroot1\n",
    51  		allExpect: "/path/to/ghqroot1\n/path/to/ghqroot2\n",
    52  	}, {
    53  		name: "gitconfig",
    54  		setup: func() func() {
    55  			orig := os.Getenv(envGhqRoot)
    56  			os.Setenv(envGhqRoot, "")
    57  			teardown := gitconfig.WithConfig(t, `
    58  [ghq]
    59    root = /path/to/ghqroot12
    60    root = /path/to/ghqroot12
    61    root = /path/to/ghqroot11
    62  `)
    63  			return func() {
    64  				os.Setenv(envGhqRoot, orig)
    65  				teardown()
    66  			}
    67  		},
    68  		expect:    "/path/to/ghqroot11\n",
    69  		allExpect: "/path/to/ghqroot11\n/path/to/ghqroot12\n",
    70  	}, {
    71  		name: "default home",
    72  		setup: func() func() {
    73  			tmpd := newTempDir(t)
    74  			fpath := filepath.Join(tmpd, "unknown-ghq-dummy")
    75  			f, err := os.Create(fpath)
    76  			if err != nil {
    77  				t.Fatal(err)
    78  			}
    79  			f.Close()
    80  
    81  			restore1 := tmpEnv(envGhqRoot, "")
    82  			restore2 := tmpEnv("GIT_CONFIG", fpath)
    83  			restore3 := tmpEnv("HOME", "/path/to/ghqhome")
    84  
    85  			return func() {
    86  				os.RemoveAll(tmpd)
    87  				restore1()
    88  				restore2()
    89  				restore3()
    90  			}
    91  		},
    92  		expect:    "/path/to/ghqhome/ghq\n",
    93  		allExpect: "/path/to/ghqhome/ghq\n",
    94  	}}
    95  
    96  	for _, tc := range testCases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
    99  			_localRepositoryRoots = nil
   100  			localRepoOnce = &sync.Once{}
   101  			defer func(orig string) { _home = orig }(_home)
   102  			_home = ""
   103  			homeOnce = &sync.Once{}
   104  			defer tc.setup()()
   105  			out, _, _ := capture(func() {
   106  				newApp().Run([]string{"", "root"})
   107  			})
   108  			if !samePaths(out, tc.expect) {
   109  				t.Errorf("got: %s, expect: %s", out, tc.expect)
   110  			}
   111  			out, _, _ = capture(func() {
   112  				newApp().Run([]string{"", "root", "--all"})
   113  			})
   114  			if !samePaths(out, tc.allExpect) {
   115  				t.Errorf("got: %s, expect: %s", out, tc.allExpect)
   116  			}
   117  		})
   118  	}
   119  }