github.com/Songmu/gotesplit@v0.3.1/gotesplit_test.go (about) 1 package gotesplit 2 3 import ( 4 "os" 5 "reflect" 6 "testing" 7 ) 8 9 func TestGetTestList(t *testing.T) { 10 const sample = `TestDoCreate 11 TestCommandGet 12 TestLook 13 TestDoGet_bulk 14 TestCommandList 15 TestCommandListUnique 16 TestCommandListUnknown 17 TestDoList_query 18 TestDoList_unique 19 TestDoList_unknownRoot 20 TestDoList_notPermittedRoot 21 TestDoList_withSystemHiddenDir 22 TestDoRoot 23 TestDetectLocalRepoRoot 24 TestDetectVCSAndRepoURL 25 TestLocalRepositoryFromFullPath 26 TestNewLocalRepository 27 TestLocalRepositoryRoots 28 TestList_Symlink 29 TestList_Symlink_In_Same_Directory 30 TestFindVCSBackend 31 TestLocalRepository_VCS 32 TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots 33 TestNewRemoteRepository 34 TestNewRemoteRepository_vcs_error 35 TestNewRemoteRepository_error 36 TestNewURL 37 TestConvertGitURLHTTPToSSH 38 TestNewURL_err 39 TestFillUsernameToPath_err 40 TestVCSBackend 41 TestCvsDummyBackend 42 TestBranchOptionIgnoredErrors 43 ok github.com/x-motemen/ghq 0.114s 44 TestRunInDirSilently 45 TestRun 46 TestRunInDir 47 TestRunSilently 48 ok github.com/x-motemen/ghq/cmdutil 0.059s 49 ? github.com/x-motemen/ghq/hoge [no test files] 50 TestLog 51 ok github.com/x-motemen/ghq/logger 0.106s` 52 53 var expect []testList = []testList{{ 54 pkg: "github.com/x-motemen/ghq/logger", 55 list: []string{ 56 "TestLog", 57 }}, { 58 pkg: "github.com/x-motemen/ghq/cmdutil", 59 list: []string{ 60 "TestRun", 61 "TestRunInDir", 62 "TestRunInDirSilently", 63 "TestRunSilently", 64 }}, { 65 pkg: "github.com/x-motemen/ghq", 66 list: []string{ 67 "TestBranchOptionIgnoredErrors", 68 "TestCommandGet", 69 "TestCommandList", 70 "TestCommandListUnique", 71 "TestCommandListUnknown", 72 "TestConvertGitURLHTTPToSSH", 73 "TestCvsDummyBackend", 74 "TestDetectLocalRepoRoot", 75 "TestDetectVCSAndRepoURL", 76 "TestDoCreate", 77 "TestDoGet_bulk", 78 "TestDoList_notPermittedRoot", 79 "TestDoList_query", 80 "TestDoList_unique", 81 "TestDoList_unknownRoot", 82 "TestDoList_withSystemHiddenDir", 83 "TestDoRoot", 84 "TestFillUsernameToPath_err", 85 "TestFindVCSBackend", 86 "TestList_Symlink", 87 "TestList_Symlink_In_Same_Directory", 88 "TestLocalRepositoryFromFullPath", 89 "TestLocalRepositoryRoots", 90 "TestLocalRepositoryRoots_URLMatchLocalRepositoryRoots", 91 "TestLocalRepository_VCS", 92 "TestLook", 93 "TestNewLocalRepository", 94 "TestNewRemoteRepository", 95 "TestNewRemoteRepository_error", 96 "TestNewRemoteRepository_vcs_error", 97 "TestNewURL", 98 "TestNewURL_err", 99 "TestVCSBackend", 100 }}} 101 got := getTestLists(sample) 102 if !reflect.DeepEqual(expect, got) { 103 t.Errorf("expect: %#v\ngot: %#v", expect, got) 104 } 105 } 106 107 func TestDetectTags(t *testing.T) { 108 testCases := []struct { 109 input []string 110 expect string 111 }{ 112 {[]string{"aa", "bb"}, ""}, 113 {[]string{"aa", "-tags", "bb"}, "-tags=bb"}, 114 {[]string{"aa", "--tags=ccc", "bb"}, "--tags=ccc"}, 115 {[]string{"aa", "-tags"}, "-tags"}, 116 } 117 118 for _, tc := range testCases { 119 t.Run(tc.expect, func(t *testing.T) { 120 out := detectTags(tc.input) 121 if out != tc.expect { 122 t.Errorf("got: %s, expect: %s", out, tc.expect) 123 } 124 }) 125 } 126 } 127 128 func TestDetectRace(t *testing.T) { 129 testCases := []struct { 130 input []string 131 expect bool 132 desc string 133 }{ 134 {[]string{"-race"}, true, "-race only"}, 135 {[]string{"-tags", "aaa", "-race", "-bench"}, true, "-race with other flags"}, 136 {[]string{"--race", "-p", "1"}, true, "--race with other flags"}, 137 {[]string{}, false, "no flags"}, 138 {[]string{"-short", "-p", "1"}, false, "flags without -race"}, 139 } 140 141 for _, tc := range testCases { 142 t.Run(tc.desc, func(t *testing.T) { 143 out := detectRace(tc.input) 144 if out != tc.expect { 145 t.Errorf("got: %t, expect: %t", out, tc.expect) 146 } 147 }) 148 } 149 } 150 151 func TestGetTestListFromPkgs(t *testing.T) { 152 if err := os.Chdir("testdata/withtags"); err != nil { 153 wd, _ := os.Getwd() 154 t.Fatalf("unexpected error: %v, cwd: %s", err, wd) 155 } 156 157 expect := []testList{{ 158 pkg: "github.com/Songmu/gotesplit/testdata/withtags", 159 list: []string{ 160 "TestNoTag", 161 "TestTagA", 162 }, 163 }} 164 165 got, err := getTestListsFromPkgs([]string{"."}, "-tags=a", false) 166 if err != nil { 167 t.Fatalf("unexpected error: %v", err) 168 } 169 170 if !reflect.DeepEqual(expect, got) { 171 t.Errorf("expect: %#v\ngot: %#v", expect, got) 172 } 173 }