github.com/x-motemen/ghq@v1.6.1/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 samePaths(lhs, rhs string) bool {
    15  	if runtime.GOOS != "windows" {
    16  		return lhs == rhs
    17  	}
    18  	lhss := strings.Split(lhs, "\n")
    19  	rhss := strings.Split(rhs, "\n")
    20  	return samePathSlice(lhss, rhss)
    21  }
    22  
    23  func TestDoRoot(t *testing.T) {
    24  	testCases := []struct {
    25  		name              string
    26  		setup             func(t *testing.T)
    27  		expect, allExpect string
    28  		skipOnWin         bool
    29  	}{{
    30  		name: "env",
    31  		setup: func(t *testing.T) {
    32  			setEnv(t, envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2")
    33  		},
    34  		expect:    "/path/to/ghqroot1\n",
    35  		allExpect: "/path/to/ghqroot1\n/path/to/ghqroot2\n",
    36  	}, {
    37  		name: "gitconfig",
    38  		setup: func(t *testing.T) {
    39  			setEnv(t, envGhqRoot, "")
    40  			t.Cleanup(gitconfig.WithConfig(t, `
    41  [ghq]
    42    root = /path/to/ghqroot12
    43    root = /path/to/ghqroot12
    44    root = /path/to/ghqroot11
    45  `))
    46  		},
    47  		expect:    "/path/to/ghqroot11\n",
    48  		allExpect: "/path/to/ghqroot11\n/path/to/ghqroot12\n",
    49  		/*
    50  			If your gitconfig contains a path to the start of slash, and you get it with `git config --type=path`,
    51  			the behavior on Windows is strange. Specifically, on Windows with GitHub Actions, a Git
    52  			installation path such as "C:/Program Files/Git/mingw64" is appended immediately before the path.
    53  			This has been addressed in the following issue, which seems to have been resolved in the v2.34.0
    54  			release.
    55  			    https://github.com/git-for-windows/git/pull/3472
    56  			However, Git on GitHub Actions is v2.39.2 at the time of this comment, and this problem continues
    57  			to occur. I'm not sure, so I'll skip the test for now.
    58  		*/
    59  		skipOnWin: true,
    60  	}, {
    61  		name: "default home",
    62  		setup: func(t *testing.T) {
    63  			tmpd := newTempDir(t)
    64  			fpath := filepath.Join(tmpd, "unknown-ghq-dummy")
    65  			f, err := os.Create(fpath)
    66  			if err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			f.Close()
    70  
    71  			setEnv(t, envGhqRoot, "")
    72  			setEnv(t, "GIT_CONFIG", fpath)
    73  			setEnv(t, "HOME", "/path/to/ghqhome")
    74  			setEnv(t, "USERPROFILE", "/path/to/ghqhome")
    75  		},
    76  		expect:    "/path/to/ghqhome/ghq\n",
    77  		allExpect: "/path/to/ghqhome/ghq\n",
    78  	}}
    79  
    80  	for _, tc := range testCases {
    81  		t.Run(tc.name, func(t *testing.T) {
    82  			if tc.skipOnWin && runtime.GOOS == "windows" {
    83  				t.SkipNow()
    84  			}
    85  			defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots)
    86  			_localRepositoryRoots = nil
    87  			localRepoOnce = &sync.Once{}
    88  			defer func(orig string) { _home = orig }(_home)
    89  			_home = ""
    90  			homeOnce = &sync.Once{}
    91  			tc.setup(t)
    92  			out, _, _ := capture(func() {
    93  				newApp().Run([]string{"", "root"})
    94  			})
    95  			if !samePaths(out, tc.expect) {
    96  				t.Errorf("got: %s, expect: %s", out, tc.expect)
    97  			}
    98  			out, _, _ = capture(func() {
    99  				newApp().Run([]string{"", "root", "--all"})
   100  			})
   101  			if !samePaths(out, tc.allExpect) {
   102  				t.Errorf("got: %s, expect: %s", out, tc.allExpect)
   103  			}
   104  		})
   105  	}
   106  }