main_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "reflect"
  6. "testing"
  7. )
  8. func TestParseReqParam(t *testing.T) {
  9. testcases := []struct {
  10. name string
  11. expected map[string]string
  12. request string
  13. expectedError error
  14. allowDeleteBodyV bool
  15. allowMergeV bool
  16. allowRepeatedFieldsInBodyV bool
  17. includePackageInTagsV bool
  18. fileV string
  19. importPathV string
  20. mergeFileNameV string
  21. useFQNForOpenAPINameV bool
  22. }{
  23. {
  24. // this one must be first - with no leading clearFlags call it
  25. // verifies our expectation of default values as we reset by
  26. // clearFlags
  27. name: "Test 0",
  28. expected: map[string]string{},
  29. request: "",
  30. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  31. fileV: "-", importPathV: "", mergeFileNameV: "apidocs",
  32. },
  33. {
  34. name: "Test 1",
  35. expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/googleapis/google/api"},
  36. request: "allow_delete_body,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/googleapis/googleapis/google/api",
  37. allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
  38. fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "apidocs",
  39. },
  40. {
  41. name: "Test 2",
  42. expected: map[string]string{"google/api/annotations.proto": "github.com/googleapis/googleapis/google/api"},
  43. request: "allow_delete_body=true,allow_merge=true,allow_repeated_fields_in_body=true,include_package_in_tags=true,merge_file_name=test_name,file=./foo.pb,import_prefix=/bar/baz,Mgoogle/api/annotations.proto=github.com/googleapis/googleapis/google/api",
  44. allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
  45. fileV: "./foo.pb", importPathV: "/bar/baz", mergeFileNameV: "test_name",
  46. },
  47. {
  48. name: "Test 3",
  49. expected: map[string]string{"a/b/c.proto": "github.com/x/y/z", "f/g/h.proto": "github.com/1/2/3/"},
  50. request: "allow_delete_body=false,allow_merge=false,Ma/b/c.proto=github.com/x/y/z,Mf/g/h.proto=github.com/1/2/3/",
  51. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  52. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  53. },
  54. {
  55. name: "Test 4",
  56. expected: map[string]string{},
  57. request: "",
  58. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  59. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  60. },
  61. {
  62. name: "Test 5",
  63. expected: map[string]string{},
  64. request: "unknown_param=17",
  65. expectedError: errors.New("cannot set flag unknown_param=17: no such flag -unknown_param"),
  66. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  67. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  68. },
  69. {
  70. name: "Test 6",
  71. expected: map[string]string{},
  72. request: "Mfoo",
  73. expectedError: errors.New("cannot set flag Mfoo: no such flag -Mfoo"),
  74. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  75. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  76. },
  77. {
  78. name: "Test 7",
  79. expected: map[string]string{},
  80. request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body,include_package_in_tags,merge_file_name",
  81. allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: true, includePackageInTagsV: true,
  82. fileV: "", importPathV: "", mergeFileNameV: "",
  83. },
  84. {
  85. name: "Test 8",
  86. expected: map[string]string{},
  87. request: "allow_delete_body,file,import_prefix,allow_merge,allow_repeated_fields_in_body=3,merge_file_name",
  88. expectedError: errors.New(`cannot set flag allow_repeated_fields_in_body=3: parse error`),
  89. allowDeleteBodyV: true, allowMergeV: true, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  90. fileV: "", importPathV: "", mergeFileNameV: "apidocs",
  91. },
  92. {
  93. name: "Test 9",
  94. expected: map[string]string{},
  95. request: "include_package_in_tags=3",
  96. expectedError: errors.New(`cannot set flag include_package_in_tags=3: parse error`),
  97. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false,
  98. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  99. },
  100. {
  101. name: "Test 10",
  102. expected: map[string]string{},
  103. request: "fqn_for_openapi_name=3",
  104. expectedError: errors.New(`cannot set flag fqn_for_openapi_name=3: parse error`),
  105. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForOpenAPINameV: false,
  106. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  107. },
  108. {
  109. name: "Test 11",
  110. expected: map[string]string{},
  111. request: "fqn_for_openapi_name=true",
  112. allowDeleteBodyV: false, allowMergeV: false, allowRepeatedFieldsInBodyV: false, includePackageInTagsV: false, useFQNForOpenAPINameV: true,
  113. fileV: "stdin", importPathV: "", mergeFileNameV: "apidocs",
  114. },
  115. }
  116. for i, tc := range testcases {
  117. t.Run(tc.name, func(tt *testing.T) {
  118. f := flag.CommandLine
  119. pkgMap := make(map[string]string)
  120. err := parseReqParam(tc.request, f, pkgMap)
  121. if tc.expectedError == nil {
  122. if err != nil {
  123. tt.Errorf("unexpected parse error '%v'", err)
  124. }
  125. if !reflect.DeepEqual(pkgMap, tc.expected) {
  126. tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
  127. }
  128. } else {
  129. if err == nil {
  130. tt.Error("expected parse error not returned")
  131. }
  132. if !reflect.DeepEqual(pkgMap, tc.expected) {
  133. tt.Errorf("pkgMap parse error, expected '%v', got '%v'", tc.expected, pkgMap)
  134. }
  135. if err.Error() != tc.expectedError.Error() {
  136. tt.Errorf("expected error malformed, expected %q, got %q", tc.expectedError.Error(), err.Error())
  137. }
  138. }
  139. checkFlags(tc.allowDeleteBodyV, tc.allowMergeV, tc.allowRepeatedFieldsInBodyV, tc.includePackageInTagsV, tc.useFQNForOpenAPINameV, tc.fileV, tc.importPathV, tc.mergeFileNameV, tt, i)
  140. clearFlags()
  141. })
  142. }
  143. }
  144. func checkFlags(allowDeleteV, allowMergeV, allowRepeatedFieldsInBodyV, includePackageInTagsV bool, useFQNForOpenAPINameV bool, fileV, importPathV, mergeFileNameV string, t *testing.T, tid int) {
  145. if *importPrefix != importPathV {
  146. t.Errorf("Test %v: import_prefix misparsed, expected '%v', got '%v'", tid, importPathV, *importPrefix)
  147. }
  148. if *file != fileV {
  149. t.Errorf("Test %v: file misparsed, expected '%v', got '%v'", tid, fileV, *file)
  150. }
  151. if *allowDeleteBody != allowDeleteV {
  152. t.Errorf("Test %v: allow_delete_body misparsed, expected '%v', got '%v'", tid, allowDeleteV, *allowDeleteBody)
  153. }
  154. if *allowMerge != allowMergeV {
  155. t.Errorf("Test %v: allow_merge misparsed, expected '%v', got '%v'", tid, allowMergeV, *allowMerge)
  156. }
  157. if *mergeFileName != mergeFileNameV {
  158. t.Errorf("Test %v: merge_file_name misparsed, expected '%v', got '%v'", tid, mergeFileNameV, *mergeFileName)
  159. }
  160. if *allowRepeatedFieldsInBody != allowRepeatedFieldsInBodyV {
  161. t.Errorf("Test %v: allow_repeated_fields_in_body misparsed, expected '%v', got '%v'", tid, allowRepeatedFieldsInBodyV, *allowRepeatedFieldsInBody)
  162. }
  163. if *includePackageInTags != includePackageInTagsV {
  164. t.Errorf("Test %v: include_package_in_tags misparsed, expected '%v', got '%v'", tid, includePackageInTagsV, *includePackageInTags)
  165. }
  166. if *useFQNForOpenAPIName != useFQNForOpenAPINameV {
  167. t.Errorf("Test %v: fqn_for_openapi_name misparsed, expected '%v', got '%v'", tid, useFQNForOpenAPINameV, *useFQNForOpenAPIName)
  168. }
  169. }
  170. func clearFlags() {
  171. *importPrefix = ""
  172. *file = "stdin"
  173. *allowDeleteBody = false
  174. *allowMerge = false
  175. *allowRepeatedFieldsInBody = false
  176. *includePackageInTags = false
  177. *mergeFileName = "apidocs"
  178. }