gen_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. package gogen
  2. import (
  3. goformat "go/format"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/tal-tech/go-zero/tools/goctl/api/parser"
  11. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  12. )
  13. const testApiTemplate = `
  14. info(
  15. title: doc title
  16. desc: ">
  17. doc description first part,
  18. doc description second part<"
  19. version: 1.0
  20. )
  21. // TODO: test
  22. // {
  23. type Request struct { // TODO: test
  24. // TODO
  25. Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // }
  26. } // TODO: test
  27. // TODO: test
  28. type Response struct {
  29. Message string ` + "`" + `json:"message"` + "`" + `
  30. }
  31. @server(
  32. // C0
  33. group: greet/s1
  34. )
  35. // C1
  36. service A-api {
  37. // C2
  38. @server( // C3
  39. handler: GreetHandler
  40. )
  41. get /greet/from/:name(Request) returns (Response) // hello
  42. // C4
  43. @handler NoResponseHandler // C5
  44. get /greet/get(Request)
  45. }
  46. `
  47. const testMultiServiceTemplate = `
  48. info(
  49. title: doc title
  50. desc: doc description first part
  51. version: 1.0
  52. )
  53. type Request struct {
  54. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  55. }
  56. type Response struct {
  57. Message string ` + "`" + `json:"message"` + "`" + `
  58. }
  59. service A-api {
  60. @server(
  61. handler: GreetHandler
  62. )
  63. get /greet/from/:name(Request) returns (Response)
  64. }
  65. service A-api {
  66. @server(
  67. handler: NoResponseHandler
  68. )
  69. get /greet/get(Request) returns
  70. }
  71. `
  72. const apiNoInfo = `
  73. type Request struct {
  74. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  75. }
  76. type Response struct {
  77. Message string ` + "`" + `json:"message"` + "`" + `
  78. }
  79. service A-api {
  80. @server(
  81. handler: GreetHandler
  82. )
  83. get /greet/from/:name(Request) returns (Response)
  84. }
  85. `
  86. const invalidApiFile = `
  87. type Request struct {
  88. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  89. }
  90. type Response struct {
  91. Message string ` + "`" + `json:"message"` + "`" + `
  92. }
  93. service A-api
  94. @server(
  95. handler: GreetHandler
  96. )
  97. get /greet/from/:name(Request) returns (Response)
  98. }
  99. `
  100. const anonymousAnnotation = `
  101. type Request struct {
  102. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  103. }
  104. type Response struct {
  105. Message string ` + "`" + `json:"message"` + "`" + `
  106. }
  107. service A-api {
  108. @handler GreetHandler
  109. get /greet/from/:name(Request) returns (Response)
  110. }
  111. `
  112. const apiHasMiddleware = `
  113. type Request struct {
  114. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  115. }
  116. type Response struct {
  117. Message string ` + "`" + `json:"message"` + "`" + `
  118. }
  119. @server(
  120. middleware: TokenValidate
  121. )
  122. service A-api {
  123. @handler GreetHandler
  124. get /greet/from/:name(Request) returns (Response)
  125. }
  126. `
  127. const apiJwt = `
  128. type Request struct {
  129. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  130. }
  131. type Response struct {
  132. Message string ` + "`" + `json:"message"` + "`" + `
  133. }
  134. @server(
  135. jwt: Auth
  136. )
  137. service A-api {
  138. @handler GreetHandler
  139. get /greet/from/:name(Request) returns (Response)
  140. }
  141. `
  142. const apiJwtWithMiddleware = `
  143. type Request struct {
  144. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  145. }
  146. type Response struct {
  147. Message string ` + "`" + `json:"message"` + "`" + `
  148. }
  149. @server(
  150. jwt: Auth
  151. middleware: TokenValidate
  152. )
  153. service A-api {
  154. @handler GreetHandler
  155. get /greet/from/:name(Request) returns (Response)
  156. }
  157. `
  158. const apiHasNoRequest = `
  159. service A-api {
  160. @handler GreetHandler
  161. post /greet/ping ()
  162. }
  163. `
  164. const apiRouteTest = `
  165. type Request struct {
  166. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  167. }
  168. type Response struct {
  169. Message string ` + "`" + `json:"message"` + "`" + `
  170. }
  171. service A-api {
  172. @handler NormalHandler
  173. get /greet/from/:name(Request) returns (Response)
  174. @handler NoResponseHandler
  175. get /greet/from/:sex(Request)
  176. @handler NoRequestHandler
  177. get /greet/from/request returns (Response)
  178. @handler NoRequestNoResponseHandler
  179. get /greet/from
  180. }
  181. `
  182. const hasCommentApiTest = `
  183. type Inline struct {
  184. }
  185. type Request struct {
  186. Inline
  187. Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // name in path
  188. }
  189. type Response struct {
  190. Message string ` + "`" + `json:"msg"` + "`" + ` // message
  191. }
  192. service A-api {
  193. @doc ("helloworld")
  194. @server(
  195. handler: GreetHandler
  196. )
  197. get /greet/from/:name(Request) returns (Response)
  198. }
  199. `
  200. const hasInlineNoExistTest = `
  201. type Request struct {
  202. Inline
  203. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  204. }
  205. type Response struct {
  206. Message string ` + "`" + `json:"message"` + "`" + ` // message
  207. }
  208. service A-api {
  209. @doc ("helloworld")
  210. @server(
  211. handler: GreetHandler
  212. )
  213. get /greet/from/:name(Request) returns (Response)
  214. }
  215. `
  216. const importApi = `
  217. type ImportData struct {
  218. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  219. }
  220. `
  221. const hasImportApi = `
  222. import "importApi.api"
  223. type Request struct {
  224. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  225. }
  226. type Response struct {
  227. Message string ` + "`" + `json:"message"` + "`" + ` // message
  228. }
  229. service A-api {
  230. @server(
  231. handler: GreetHandler
  232. )
  233. get /greet/from/:name(Request) returns (Response)
  234. }
  235. `
  236. const noStructTagApi = `
  237. type Request {
  238. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  239. }
  240. type XXX {}
  241. type (
  242. Response {
  243. Message string ` + "`" + `json:"message"` + "`" + `
  244. }
  245. A {}
  246. B struct {}
  247. )
  248. service A-api {
  249. @handler GreetHandler
  250. get /greet/from/:name(Request) returns (Response)
  251. }
  252. `
  253. const nestTypeApi = `
  254. type Request {
  255. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  256. XXX struct {
  257. }
  258. }
  259. service A-api {
  260. @handler GreetHandler
  261. get /greet/from/:name(Request)
  262. }
  263. `
  264. func TestParser(t *testing.T) {
  265. filename := "greet.api"
  266. err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
  267. assert.Nil(t, err)
  268. defer os.Remove(filename)
  269. api, err := parser.Parse(filename)
  270. assert.Nil(t, err)
  271. assert.Equal(t, len(api.Types), 2)
  272. assert.Equal(t, len(api.Service.Routes()), 2)
  273. assert.Equal(t, api.Service.Routes()[0].Path, "/greet/from/:name")
  274. assert.Equal(t, api.Service.Routes()[1].Path, "/greet/get")
  275. assert.Equal(t, api.Service.Routes()[1].RequestTypeName(), "Request")
  276. assert.Equal(t, api.Service.Routes()[1].ResponseType, nil)
  277. validate(t, filename)
  278. }
  279. func TestMultiService(t *testing.T) {
  280. filename := "greet.api"
  281. err := ioutil.WriteFile(filename, []byte(testMultiServiceTemplate), os.ModePerm)
  282. assert.Nil(t, err)
  283. defer os.Remove(filename)
  284. api, err := parser.Parse(filename)
  285. assert.Nil(t, err)
  286. assert.Equal(t, len(api.Service.Routes()), 2)
  287. assert.Equal(t, len(api.Service.Groups), 2)
  288. validate(t, filename)
  289. }
  290. func TestApiNoInfo(t *testing.T) {
  291. filename := "greet.api"
  292. err := ioutil.WriteFile(filename, []byte(apiNoInfo), os.ModePerm)
  293. assert.Nil(t, err)
  294. defer os.Remove(filename)
  295. _, err = parser.Parse(filename)
  296. assert.Nil(t, err)
  297. validate(t, filename)
  298. }
  299. func TestInvalidApiFile(t *testing.T) {
  300. filename := "greet.api"
  301. err := ioutil.WriteFile(filename, []byte(invalidApiFile), os.ModePerm)
  302. assert.Nil(t, err)
  303. defer os.Remove(filename)
  304. _, err = parser.Parse(filename)
  305. assert.NotNil(t, err)
  306. }
  307. func TestAnonymousAnnotation(t *testing.T) {
  308. filename := "greet.api"
  309. err := ioutil.WriteFile(filename, []byte(anonymousAnnotation), os.ModePerm)
  310. assert.Nil(t, err)
  311. defer os.Remove(filename)
  312. api, err := parser.Parse(filename)
  313. assert.Nil(t, err)
  314. assert.Equal(t, len(api.Service.Routes()), 1)
  315. assert.Equal(t, api.Service.Routes()[0].Handler, "GreetHandler")
  316. validate(t, filename)
  317. }
  318. func TestApiHasMiddleware(t *testing.T) {
  319. filename := "greet.api"
  320. err := ioutil.WriteFile(filename, []byte(apiHasMiddleware), os.ModePerm)
  321. assert.Nil(t, err)
  322. defer os.Remove(filename)
  323. _, err = parser.Parse(filename)
  324. assert.Nil(t, err)
  325. validate(t, filename)
  326. }
  327. func TestApiHasJwt(t *testing.T) {
  328. filename := "jwt.api"
  329. err := ioutil.WriteFile(filename, []byte(apiJwt), os.ModePerm)
  330. assert.Nil(t, err)
  331. defer os.Remove(filename)
  332. _, err = parser.Parse(filename)
  333. assert.Nil(t, err)
  334. validate(t, filename)
  335. }
  336. func TestApiHasJwtAndMiddleware(t *testing.T) {
  337. filename := "jwt.api"
  338. err := ioutil.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
  339. assert.Nil(t, err)
  340. defer os.Remove(filename)
  341. _, err = parser.Parse(filename)
  342. assert.Nil(t, err)
  343. validate(t, filename)
  344. }
  345. func TestApiHasNoRequestBody(t *testing.T) {
  346. filename := "greet.api"
  347. err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
  348. assert.Nil(t, err)
  349. defer os.Remove(filename)
  350. _, err = parser.Parse(filename)
  351. assert.Nil(t, err)
  352. }
  353. func TestApiRoutes(t *testing.T) {
  354. filename := "greet.api"
  355. err := ioutil.WriteFile(filename, []byte(apiRouteTest), os.ModePerm)
  356. assert.Nil(t, err)
  357. defer os.Remove(filename)
  358. _, err = parser.Parse(filename)
  359. assert.Nil(t, err)
  360. validate(t, filename)
  361. }
  362. func TestHasCommentRoutes(t *testing.T) {
  363. filename := "greet.api"
  364. err := ioutil.WriteFile(filename, []byte(hasCommentApiTest), os.ModePerm)
  365. assert.Nil(t, err)
  366. defer os.Remove(filename)
  367. _, err = parser.Parse(filename)
  368. assert.Nil(t, err)
  369. validate(t, filename)
  370. }
  371. func TestInlineTypeNotExist(t *testing.T) {
  372. filename := "greet.api"
  373. err := ioutil.WriteFile(filename, []byte(hasInlineNoExistTest), os.ModePerm)
  374. assert.Nil(t, err)
  375. defer os.Remove(filename)
  376. _, err = parser.Parse(filename)
  377. assert.NotNil(t, err)
  378. }
  379. func TestHasImportApi(t *testing.T) {
  380. filename := "greet.api"
  381. err := ioutil.WriteFile(filename, []byte(hasImportApi), os.ModePerm)
  382. assert.Nil(t, err)
  383. defer os.Remove(filename)
  384. importApiName := "importApi.api"
  385. err = ioutil.WriteFile(importApiName, []byte(importApi), os.ModePerm)
  386. assert.Nil(t, err)
  387. defer os.Remove(importApiName)
  388. api, err := parser.Parse(filename)
  389. assert.Nil(t, err)
  390. var hasInline bool
  391. for _, ty := range api.Types {
  392. if ty.Name() == "ImportData" {
  393. hasInline = true
  394. break
  395. }
  396. }
  397. assert.True(t, hasInline)
  398. validate(t, filename)
  399. }
  400. func TestNoStructApi(t *testing.T) {
  401. filename := "greet.api"
  402. err := ioutil.WriteFile(filename, []byte(noStructTagApi), os.ModePerm)
  403. assert.Nil(t, err)
  404. defer os.Remove(filename)
  405. spec, err := parser.Parse(filename)
  406. assert.Nil(t, err)
  407. assert.Equal(t, len(spec.Types), 5)
  408. validate(t, filename)
  409. }
  410. func TestNestTypeApi(t *testing.T) {
  411. filename := "greet.api"
  412. err := ioutil.WriteFile(filename, []byte(nestTypeApi), os.ModePerm)
  413. assert.Nil(t, err)
  414. defer os.Remove(filename)
  415. _, err = parser.Parse(filename)
  416. assert.NotNil(t, err)
  417. }
  418. func TestCamelStyle(t *testing.T) {
  419. filename := "greet.api"
  420. err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
  421. assert.Nil(t, err)
  422. defer os.Remove(filename)
  423. _, err = parser.Parse(filename)
  424. assert.Nil(t, err)
  425. validateWithCamel(t, filename, "GoZero")
  426. }
  427. func validate(t *testing.T, api string) {
  428. validateWithCamel(t, api, "gozero")
  429. }
  430. func validateWithCamel(t *testing.T, api, camel string) {
  431. dir := "_go"
  432. os.RemoveAll(dir)
  433. err := DoGenProject(api, dir, camel)
  434. defer os.RemoveAll(dir)
  435. assert.Nil(t, err)
  436. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  437. if strings.HasSuffix(path, ".go") {
  438. code, err := ioutil.ReadFile(path)
  439. assert.Nil(t, err)
  440. assert.Nil(t, validateCode(string(code)))
  441. }
  442. return nil
  443. })
  444. _, err = execx.Run("go test ./...", dir)
  445. assert.Nil(t, err)
  446. }
  447. func validateCode(code string) error {
  448. _, err := goformat.Source([]byte(code))
  449. return err
  450. }