gen_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package gogen
  2. import (
  3. goformat "go/format"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. "git.i2edu.net/i2/go-zero/tools/goctl/api/parser"
  10. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/execx"
  11. "github.com/stretchr/testify/assert"
  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. signature: true
  137. )
  138. service A-api {
  139. @handler GreetHandler
  140. get /greet/from/:name(Request) returns (Response)
  141. }
  142. `
  143. const apiJwtWithMiddleware = `
  144. type Request struct {
  145. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  146. }
  147. type Response struct {
  148. Message string ` + "`" + `json:"message"` + "`" + `
  149. }
  150. @server(
  151. jwt: Auth
  152. middleware: TokenValidate
  153. )
  154. service A-api {
  155. @handler GreetHandler
  156. get /greet/from/:name(Request) returns (Response)
  157. }
  158. `
  159. const apiHasNoRequest = `
  160. service A-api {
  161. @handler GreetHandler
  162. post /greet/ping ()
  163. }
  164. `
  165. const apiRouteTest = `
  166. type Request struct {
  167. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  168. }
  169. type Response struct {
  170. Message string ` + "`" + `json:"message"` + "`" + `
  171. }
  172. service A-api {
  173. @handler NormalHandler
  174. get /greet/from/:name(Request) returns (Response)
  175. @handler NoResponseHandler
  176. get /greet/from/:sex(Request)
  177. @handler NoRequestHandler
  178. get /greet/from/request returns (Response)
  179. @handler NoRequestNoResponseHandler
  180. get /greet/from
  181. }
  182. `
  183. const hasCommentApiTest = `
  184. type Inline struct {
  185. }
  186. type Request struct {
  187. Inline
  188. Name string ` + "`" + `path:"name,options=you|me"` + "`" + ` // name in path
  189. }
  190. type Response struct {
  191. Message string ` + "`" + `json:"msg"` + "`" + ` // message
  192. }
  193. service A-api {
  194. @doc ("helloworld")
  195. @server(
  196. handler: GreetHandler
  197. )
  198. get /greet/from/:name(Request) returns (Response)
  199. }
  200. `
  201. const hasInlineNoExistTest = `
  202. type Request struct {
  203. Inline
  204. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  205. }
  206. type Response struct {
  207. Message string ` + "`" + `json:"message"` + "`" + ` // message
  208. }
  209. service A-api {
  210. @doc ("helloworld")
  211. @server(
  212. handler: GreetHandler
  213. )
  214. get /greet/from/:name(Request) returns (Response)
  215. }
  216. `
  217. const importApi = `
  218. type ImportData struct {
  219. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  220. }
  221. `
  222. const hasImportApi = `
  223. import "importApi.api"
  224. type Request struct {
  225. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  226. }
  227. type Response struct {
  228. Message string ` + "`" + `json:"message"` + "`" + ` // message
  229. }
  230. service A-api {
  231. @server(
  232. handler: GreetHandler
  233. )
  234. get /greet/from/:name(Request) returns (Response)
  235. }
  236. `
  237. const noStructTagApi = `
  238. type Request {
  239. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  240. }
  241. type XXX {}
  242. type (
  243. Response {
  244. Message string ` + "`" + `json:"message"` + "`" + `
  245. }
  246. A {}
  247. B struct {}
  248. )
  249. service A-api {
  250. @handler GreetHandler
  251. get /greet/from/:name(Request) returns (Response)
  252. }
  253. `
  254. const nestTypeApi = `
  255. type Request {
  256. Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
  257. XXX struct {
  258. }
  259. }
  260. service A-api {
  261. @handler GreetHandler
  262. get /greet/from/:name(Request)
  263. }
  264. `
  265. func TestParser(t *testing.T) {
  266. filename := "greet.api"
  267. err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
  268. assert.Nil(t, err)
  269. defer os.Remove(filename)
  270. api, err := parser.Parse(filename)
  271. assert.Nil(t, err)
  272. assert.Equal(t, len(api.Types), 2)
  273. assert.Equal(t, len(api.Service.Routes()), 2)
  274. assert.Equal(t, api.Service.Routes()[0].Path, "/greet/from/:name")
  275. assert.Equal(t, api.Service.Routes()[1].Path, "/greet/get")
  276. assert.Equal(t, api.Service.Routes()[1].RequestTypeName(), "Request")
  277. assert.Equal(t, api.Service.Routes()[1].ResponseType, nil)
  278. validate(t, filename)
  279. }
  280. func TestMultiService(t *testing.T) {
  281. filename := "greet.api"
  282. err := ioutil.WriteFile(filename, []byte(testMultiServiceTemplate), os.ModePerm)
  283. assert.Nil(t, err)
  284. defer os.Remove(filename)
  285. api, err := parser.Parse(filename)
  286. assert.Nil(t, err)
  287. assert.Equal(t, len(api.Service.Routes()), 2)
  288. assert.Equal(t, len(api.Service.Groups), 2)
  289. validate(t, filename)
  290. }
  291. func TestApiNoInfo(t *testing.T) {
  292. filename := "greet.api"
  293. err := ioutil.WriteFile(filename, []byte(apiNoInfo), os.ModePerm)
  294. assert.Nil(t, err)
  295. defer os.Remove(filename)
  296. _, err = parser.Parse(filename)
  297. assert.Nil(t, err)
  298. validate(t, filename)
  299. }
  300. func TestInvalidApiFile(t *testing.T) {
  301. filename := "greet.api"
  302. err := ioutil.WriteFile(filename, []byte(invalidApiFile), os.ModePerm)
  303. assert.Nil(t, err)
  304. defer os.Remove(filename)
  305. _, err = parser.Parse(filename)
  306. assert.NotNil(t, err)
  307. }
  308. func TestAnonymousAnnotation(t *testing.T) {
  309. filename := "greet.api"
  310. err := ioutil.WriteFile(filename, []byte(anonymousAnnotation), os.ModePerm)
  311. assert.Nil(t, err)
  312. defer os.Remove(filename)
  313. api, err := parser.Parse(filename)
  314. assert.Nil(t, err)
  315. assert.Equal(t, len(api.Service.Routes()), 1)
  316. assert.Equal(t, api.Service.Routes()[0].Handler, "GreetHandler")
  317. validate(t, filename)
  318. }
  319. func TestApiHasMiddleware(t *testing.T) {
  320. filename := "greet.api"
  321. err := ioutil.WriteFile(filename, []byte(apiHasMiddleware), os.ModePerm)
  322. assert.Nil(t, err)
  323. defer os.Remove(filename)
  324. _, err = parser.Parse(filename)
  325. assert.Nil(t, err)
  326. validate(t, filename)
  327. }
  328. func TestApiHasJwt(t *testing.T) {
  329. filename := "jwt.api"
  330. err := ioutil.WriteFile(filename, []byte(apiJwt), os.ModePerm)
  331. assert.Nil(t, err)
  332. defer os.Remove(filename)
  333. _, err = parser.Parse(filename)
  334. assert.Nil(t, err)
  335. validate(t, filename)
  336. }
  337. func TestApiHasJwtAndMiddleware(t *testing.T) {
  338. filename := "jwt.api"
  339. err := ioutil.WriteFile(filename, []byte(apiJwtWithMiddleware), os.ModePerm)
  340. assert.Nil(t, err)
  341. defer os.Remove(filename)
  342. _, err = parser.Parse(filename)
  343. assert.Nil(t, err)
  344. validate(t, filename)
  345. }
  346. func TestApiHasNoRequestBody(t *testing.T) {
  347. filename := "greet.api"
  348. err := ioutil.WriteFile(filename, []byte(apiHasNoRequest), os.ModePerm)
  349. assert.Nil(t, err)
  350. defer os.Remove(filename)
  351. _, err = parser.Parse(filename)
  352. assert.Nil(t, err)
  353. }
  354. func TestApiRoutes(t *testing.T) {
  355. filename := "greet.api"
  356. err := ioutil.WriteFile(filename, []byte(apiRouteTest), os.ModePerm)
  357. assert.Nil(t, err)
  358. defer os.Remove(filename)
  359. _, err = parser.Parse(filename)
  360. assert.Nil(t, err)
  361. validate(t, filename)
  362. }
  363. func TestHasCommentRoutes(t *testing.T) {
  364. filename := "greet.api"
  365. err := ioutil.WriteFile(filename, []byte(hasCommentApiTest), os.ModePerm)
  366. assert.Nil(t, err)
  367. defer os.Remove(filename)
  368. _, err = parser.Parse(filename)
  369. assert.Nil(t, err)
  370. validate(t, filename)
  371. }
  372. func TestInlineTypeNotExist(t *testing.T) {
  373. filename := "greet.api"
  374. err := ioutil.WriteFile(filename, []byte(hasInlineNoExistTest), os.ModePerm)
  375. assert.Nil(t, err)
  376. defer os.Remove(filename)
  377. _, err = parser.Parse(filename)
  378. assert.NotNil(t, err)
  379. }
  380. func TestHasImportApi(t *testing.T) {
  381. filename := "greet.api"
  382. err := ioutil.WriteFile(filename, []byte(hasImportApi), os.ModePerm)
  383. assert.Nil(t, err)
  384. defer os.Remove(filename)
  385. importApiName := "importApi.api"
  386. err = ioutil.WriteFile(importApiName, []byte(importApi), os.ModePerm)
  387. assert.Nil(t, err)
  388. defer os.Remove(importApiName)
  389. api, err := parser.Parse(filename)
  390. assert.Nil(t, err)
  391. var hasInline bool
  392. for _, ty := range api.Types {
  393. if ty.Name() == "ImportData" {
  394. hasInline = true
  395. break
  396. }
  397. }
  398. assert.True(t, hasInline)
  399. validate(t, filename)
  400. }
  401. func TestNoStructApi(t *testing.T) {
  402. filename := "greet.api"
  403. err := ioutil.WriteFile(filename, []byte(noStructTagApi), os.ModePerm)
  404. assert.Nil(t, err)
  405. defer os.Remove(filename)
  406. spec, err := parser.Parse(filename)
  407. assert.Nil(t, err)
  408. assert.Equal(t, len(spec.Types), 5)
  409. validate(t, filename)
  410. }
  411. func TestNestTypeApi(t *testing.T) {
  412. filename := "greet.api"
  413. err := ioutil.WriteFile(filename, []byte(nestTypeApi), os.ModePerm)
  414. assert.Nil(t, err)
  415. defer os.Remove(filename)
  416. _, err = parser.Parse(filename)
  417. assert.NotNil(t, err)
  418. }
  419. func TestCamelStyle(t *testing.T) {
  420. filename := "greet.api"
  421. err := ioutil.WriteFile(filename, []byte(testApiTemplate), os.ModePerm)
  422. assert.Nil(t, err)
  423. defer os.Remove(filename)
  424. _, err = parser.Parse(filename)
  425. assert.Nil(t, err)
  426. validateWithCamel(t, filename, "GoZero")
  427. }
  428. func validate(t *testing.T, api string) {
  429. validateWithCamel(t, api, "gozero")
  430. }
  431. func validateWithCamel(t *testing.T, api, camel string) {
  432. dir := "_go"
  433. os.RemoveAll(dir)
  434. err := DoGenProject(api, dir, camel)
  435. defer os.RemoveAll(dir)
  436. assert.Nil(t, err)
  437. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  438. if strings.HasSuffix(path, ".go") {
  439. code, err := ioutil.ReadFile(path)
  440. assert.Nil(t, err)
  441. assert.Nil(t, validateCode(string(code)))
  442. }
  443. return nil
  444. })
  445. _, err = execx.Run("go test ./...", dir)
  446. assert.Nil(t, err)
  447. }
  448. func validateCode(code string) error {
  449. _, err := goformat.Source([]byte(code))
  450. return err
  451. }