request_type_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package cquery
  2. import (
  3. "encoding/json"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. )
  8. func TestGetOutputFilesParseResults(t *testing.T) {
  9. t.Parallel()
  10. testCases := []struct {
  11. description string
  12. input string
  13. expectedOutput []string
  14. }{
  15. {
  16. description: "no result",
  17. input: "",
  18. expectedOutput: []string{},
  19. },
  20. {
  21. description: "one result",
  22. input: "test",
  23. expectedOutput: []string{"test"},
  24. },
  25. {
  26. description: "splits on comma with space",
  27. input: "foo, bar",
  28. expectedOutput: []string{"foo", "bar"},
  29. },
  30. }
  31. for _, tc := range testCases {
  32. t.Run(tc.description, func(t *testing.T) {
  33. actualOutput := GetOutputFiles.ParseResult(tc.input)
  34. if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
  35. t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
  36. }
  37. })
  38. }
  39. }
  40. func TestGetCcInfoParseResults(t *testing.T) {
  41. t.Parallel()
  42. testCases := []struct {
  43. description string
  44. inputCcInfo CcInfo
  45. expectedOutput CcInfo
  46. }{
  47. {
  48. description: "no result",
  49. inputCcInfo: CcInfo{},
  50. expectedOutput: CcInfo{},
  51. },
  52. {
  53. description: "all items set",
  54. inputCcInfo: CcInfo{
  55. OutputFiles: []string{"out1", "out2"},
  56. CcObjectFiles: []string{"object1", "object2"},
  57. CcSharedLibraryFiles: []string{"shared_lib1", "shared_lib2"},
  58. CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
  59. Includes: []string{".", "dir/subdir"},
  60. SystemIncludes: []string{"system/dir", "system/other/dir"},
  61. Headers: []string{"dir/subdir/hdr.h"},
  62. RootStaticArchives: []string{"rootstaticarchive1"},
  63. RootDynamicLibraries: []string{"rootdynamiclibrary1"},
  64. TocFile: "lib.so.toc",
  65. },
  66. expectedOutput: CcInfo{
  67. OutputFiles: []string{"out1", "out2"},
  68. CcObjectFiles: []string{"object1", "object2"},
  69. CcSharedLibraryFiles: []string{"shared_lib1", "shared_lib2"},
  70. CcStaticLibraryFiles: []string{"static_lib1", "static_lib2"},
  71. Includes: []string{".", "dir/subdir"},
  72. SystemIncludes: []string{"system/dir", "system/other/dir"},
  73. Headers: []string{"dir/subdir/hdr.h"},
  74. RootStaticArchives: []string{"rootstaticarchive1"},
  75. RootDynamicLibraries: []string{"rootdynamiclibrary1"},
  76. TocFile: "lib.so.toc",
  77. },
  78. },
  79. }
  80. for _, tc := range testCases {
  81. t.Run(tc.description, func(t *testing.T) {
  82. jsonInput, _ := json.Marshal(tc.inputCcInfo)
  83. actualOutput, err := GetCcInfo.ParseResult(string(jsonInput))
  84. if err != nil {
  85. t.Errorf("error parsing result: %q", err)
  86. } else if err == nil && !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
  87. t.Errorf("expected %#v\n!= actual %#v", tc.expectedOutput, actualOutput)
  88. }
  89. })
  90. }
  91. }
  92. func TestGetCcInfoParseResultsError(t *testing.T) {
  93. t.Parallel()
  94. testCases := []struct {
  95. description string
  96. input string
  97. expectedError string
  98. }{
  99. {
  100. description: "not json",
  101. input: ``,
  102. expectedError: `cannot parse cquery result '': EOF`,
  103. },
  104. {
  105. description: "invalid field",
  106. input: `{
  107. "toc_file": "dir/file.so.toc"
  108. }`,
  109. expectedError: `json: unknown field "toc_file"`,
  110. },
  111. }
  112. for _, tc := range testCases {
  113. t.Run(tc.description, func(t *testing.T) {
  114. _, err := GetCcInfo.ParseResult(tc.input)
  115. if !strings.Contains(err.Error(), tc.expectedError) {
  116. t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
  117. }
  118. })
  119. }
  120. }
  121. func TestGetApexInfoParseResults(t *testing.T) {
  122. t.Parallel()
  123. testCases := []struct {
  124. description string
  125. input string
  126. expectedOutput ApexInfo
  127. }{
  128. {
  129. description: "no result",
  130. input: "{}",
  131. expectedOutput: ApexInfo{},
  132. },
  133. {
  134. description: "one result",
  135. input: `{
  136. "signed_output":"my.apex",
  137. "unsigned_output":"my.apex.unsigned",
  138. "requires_native_libs":["//bionic/libc:libc","//bionic/libdl:libdl"],
  139. "bundle_key_info":["foo.pem", "foo.privkey"],
  140. "container_key_info":["foo.x509.pem", "foo.pk8", "foo"],
  141. "package_name":"package.name",
  142. "symbols_used_by_apex": "path/to/my.apex_using.txt",
  143. "backing_libs":"path/to/backing.txt",
  144. "bundle_file": "dir/bundlefile.zip",
  145. "installed_files":"path/to/installed-files.txt",
  146. "provides_native_libs":[],
  147. "make_modules_to_install": ["foo","bar"]
  148. }`,
  149. expectedOutput: ApexInfo{
  150. // ApexInfo
  151. SignedOutput: "my.apex",
  152. UnsignedOutput: "my.apex.unsigned",
  153. RequiresLibs: []string{"//bionic/libc:libc", "//bionic/libdl:libdl"},
  154. ProvidesLibs: []string{},
  155. BundleKeyInfo: []string{"foo.pem", "foo.privkey"},
  156. ContainerKeyInfo: []string{"foo.x509.pem", "foo.pk8", "foo"},
  157. PackageName: "package.name",
  158. SymbolsUsedByApex: "path/to/my.apex_using.txt",
  159. BackingLibs: "path/to/backing.txt",
  160. BundleFile: "dir/bundlefile.zip",
  161. InstalledFiles: "path/to/installed-files.txt",
  162. // ApexMkInfo
  163. MakeModulesToInstall: []string{"foo", "bar"},
  164. },
  165. },
  166. }
  167. for _, tc := range testCases {
  168. t.Run(tc.description, func(t *testing.T) {
  169. actualOutput, err := GetApexInfo.ParseResult(tc.input)
  170. if err != nil {
  171. t.Errorf("Unexpected error %q", err)
  172. }
  173. if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
  174. t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
  175. }
  176. })
  177. }
  178. }
  179. func TestGetApexInfoParseResultsError(t *testing.T) {
  180. t.Parallel()
  181. testCases := []struct {
  182. description string
  183. input string
  184. expectedError string
  185. }{
  186. {
  187. description: "not json",
  188. input: ``,
  189. expectedError: `cannot parse cquery result '': EOF`,
  190. },
  191. {
  192. description: "invalid field",
  193. input: `{
  194. "fake_field": "path/to/file"
  195. }`,
  196. expectedError: `json: unknown field "fake_field"`,
  197. },
  198. }
  199. for _, tc := range testCases {
  200. t.Run(tc.description, func(t *testing.T) {
  201. _, err := GetApexInfo.ParseResult(tc.input)
  202. if !strings.Contains(err.Error(), tc.expectedError) {
  203. t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
  204. }
  205. })
  206. }
  207. }
  208. func TestGetCcUnstrippedParseResults(t *testing.T) {
  209. t.Parallel()
  210. testCases := []struct {
  211. description string
  212. input string
  213. expectedOutput CcUnstrippedInfo
  214. }{
  215. {
  216. description: "no result",
  217. input: "{}",
  218. expectedOutput: CcUnstrippedInfo{},
  219. },
  220. {
  221. description: "one result",
  222. input: `{"OutputFile":"myapp", "UnstrippedOutput":"myapp_unstripped"}`,
  223. expectedOutput: CcUnstrippedInfo{
  224. OutputFile: "myapp",
  225. UnstrippedOutput: "myapp_unstripped",
  226. },
  227. },
  228. }
  229. for _, tc := range testCases {
  230. t.Run(tc.description, func(t *testing.T) {
  231. actualOutput, err := GetCcUnstrippedInfo.ParseResult(tc.input)
  232. if err != nil {
  233. t.Errorf("Unexpected error %q", err)
  234. }
  235. if !reflect.DeepEqual(tc.expectedOutput, actualOutput) {
  236. t.Errorf("expected %#v != actual %#v", tc.expectedOutput, actualOutput)
  237. }
  238. })
  239. }
  240. }
  241. func TestGetCcUnstrippedParseResultsErrors(t *testing.T) {
  242. t.Parallel()
  243. testCases := []struct {
  244. description string
  245. input string
  246. expectedError string
  247. }{
  248. {
  249. description: "not json",
  250. input: ``,
  251. expectedError: `cannot parse cquery result '': EOF`,
  252. },
  253. {
  254. description: "invalid field",
  255. input: `{
  256. "fake_field": "path/to/file"
  257. }`,
  258. expectedError: `json: unknown field "fake_field"`,
  259. },
  260. }
  261. for _, tc := range testCases {
  262. t.Run(tc.description, func(t *testing.T) {
  263. _, err := GetCcUnstrippedInfo.ParseResult(tc.input)
  264. if !strings.Contains(err.Error(), tc.expectedError) {
  265. t.Errorf("expected string %q in error message, got %q", tc.expectedError, err)
  266. }
  267. })
  268. }
  269. }