pkgwalk_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "path"
  16. "reflect"
  17. "sort"
  18. "testing"
  19. )
  20. func Test_WalkPkg(t *testing.T) {
  21. testCases := []struct {
  22. pkg string
  23. fail bool
  24. expected []string
  25. }{
  26. {
  27. pkg: "./testdata/nonexistent-dir",
  28. fail: true,
  29. },
  30. {
  31. pkg: "./testdata/dir-without-gofiles",
  32. expected: []string{"./testdata/dir-without-gofiles"},
  33. },
  34. {
  35. pkg: "./testdata/dir-with-gofiles",
  36. expected: []string{"./testdata/dir-with-gofiles", "./testdata/dir-with-gofiles/subdir"},
  37. },
  38. }
  39. for i, tc := range testCases {
  40. visited := []string{}
  41. err := WalkPkg(tc.pkg, func(imp, abs string) error {
  42. if _, base := path.Split(imp); base == "skipme" {
  43. return ErrSkipPkg
  44. }
  45. visited = append(visited, imp)
  46. return nil
  47. })
  48. if err != nil && tc.fail {
  49. continue
  50. }
  51. if err != nil {
  52. t.Errorf("[%d] unexpected error: %v", i, err)
  53. continue
  54. }
  55. if tc.fail {
  56. t.Errorf("[%d] expected error", i)
  57. continue
  58. }
  59. if !reflect.DeepEqual(visited, tc.expected) {
  60. t.Errorf("[%d] unexpected results: %v", i, visited)
  61. }
  62. }
  63. }
  64. func Test_findPackage(t *testing.T) {
  65. testCases := []struct {
  66. pkg string
  67. fail bool
  68. }{
  69. {
  70. pkg: "./testdata/nonexistent-dir",
  71. fail: true,
  72. },
  73. {
  74. pkg: "./testdata/dir-without-gofiles",
  75. },
  76. {
  77. pkg: "./testdata/dir-with-gofiles",
  78. },
  79. }
  80. for i, tc := range testCases {
  81. _, err := findPackage(tc.pkg)
  82. if err != nil && tc.fail {
  83. continue
  84. }
  85. if err != nil {
  86. t.Errorf("[%d] unexpected error: %v", i, err)
  87. continue
  88. }
  89. if tc.fail {
  90. t.Errorf("[%d] expected error", i)
  91. continue
  92. }
  93. }
  94. }
  95. func Test_readDirInfos(t *testing.T) {
  96. testCases := []struct {
  97. dir string
  98. fail bool
  99. expected map[string]bool
  100. }{
  101. {
  102. dir: "./testdata/nonexistent-dir",
  103. fail: true,
  104. },
  105. {
  106. dir: "./testdata/dir-without-gofiles",
  107. expected: map[string]bool{"README": true},
  108. },
  109. {
  110. dir: "./testdata/dir-with-gofiles",
  111. expected: map[string]bool{
  112. "README": true,
  113. "foo.go": true,
  114. "bar.go": true,
  115. "subdir": true,
  116. "testdata": true,
  117. "_underscore": true,
  118. ".dot": true,
  119. "skipme": true,
  120. },
  121. },
  122. }
  123. for i, tc := range testCases {
  124. infos, err := readDirInfos(tc.dir)
  125. if err != nil && tc.fail {
  126. continue
  127. }
  128. if err != nil {
  129. t.Errorf("[%d] unexpected error: %v", i, err)
  130. continue
  131. }
  132. if tc.fail {
  133. t.Errorf("[%d] expected error", i)
  134. continue
  135. }
  136. result := make([]string, len(infos))
  137. sorted := make([]string, len(infos))
  138. for i, inf := range infos {
  139. result[i] = inf.Name()
  140. sorted[i] = inf.Name()
  141. }
  142. sort.Strings(sorted)
  143. if !reflect.DeepEqual(result, sorted) {
  144. t.Errorf("[%d] result was not sorted: %v", i, result)
  145. }
  146. for _, r := range result {
  147. if !tc.expected[r] {
  148. t.Errorf("[%d] got unexpected result: %s", i, r)
  149. } else {
  150. delete(tc.expected, r)
  151. }
  152. }
  153. for r := range tc.expected {
  154. t.Errorf("[%d] missing expected result: %s", i, r)
  155. }
  156. }
  157. }
  158. func Test_readDirNames(t *testing.T) {
  159. testCases := []struct {
  160. dir string
  161. fail bool
  162. expected map[string]bool
  163. }{
  164. {
  165. dir: "./testdata/nonexistent-dir",
  166. fail: true,
  167. },
  168. {
  169. dir: "./testdata/dir-without-gofiles",
  170. expected: map[string]bool{"README": true},
  171. },
  172. {
  173. dir: "./testdata/dir-with-gofiles",
  174. expected: map[string]bool{
  175. "README": true,
  176. "foo.go": true,
  177. "bar.go": true,
  178. "subdir": true,
  179. "testdata": true,
  180. "_underscore": true,
  181. ".dot": true,
  182. "skipme": true,
  183. },
  184. },
  185. }
  186. for i, tc := range testCases {
  187. result, err := readDirNames(tc.dir)
  188. if err != nil && tc.fail {
  189. continue
  190. }
  191. if err != nil {
  192. t.Errorf("[%d] unexpected error: %v", i, err)
  193. continue
  194. }
  195. if tc.fail {
  196. t.Errorf("[%d] expected error", i)
  197. continue
  198. }
  199. for _, r := range result {
  200. if !tc.expected[r] {
  201. t.Errorf("[%d] got unexpected result: %s", i, r)
  202. } else {
  203. delete(tc.expected, r)
  204. }
  205. }
  206. for r := range tc.expected {
  207. t.Errorf("[%d] missing expected result: %s", i, r)
  208. }
  209. }
  210. }