trpc.group/trpc-go/trpc-cmdline@v1.0.9/cmd/apidocs/apidocs_test.go (about) 1 // Tencent is pleased to support the open source community by making tRPC available. 2 // 3 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 4 // All rights reserved. 5 // 6 // If you have downloaded a copy of the tRPC source code from Tencent, 7 // please note that tRPC source code is licensed under the Apache 2.0 License, 8 // A copy of the Apache 2.0 License is included in this file. 9 10 package apidocs 11 12 import ( 13 "os" 14 "path/filepath" 15 "testing" 16 17 "trpc.group/trpc-go/trpc-cmdline/cmd/internal" 18 ) 19 20 func TestCmd_ApiDocs(t *testing.T) { 21 pwd, _ := os.Getwd() 22 defer func() { 23 os.Chdir(pwd) 24 }() 25 26 wd := filepath.Dir(filepath.Dir(pwd)) 27 pbdir := filepath.Join(wd, "testcase/apidocs") 28 29 if err := os.Chdir(pbdir); err != nil { 30 t.Fatal(err) 31 } 32 defer os.Chdir(wd) 33 34 type testCase struct { 35 pb string 36 generated string 37 flags map[string]string 38 wantErr bool 39 } 40 41 cases := []testCase{ 42 { 43 pb: "helloworld.proto", 44 generated: "helloworld.swagger.json", 45 flags: map[string]string{ 46 "protodir": ".", 47 "protofile": "helloworld.proto", 48 "swagger-out": "helloworld.swagger.json", 49 "check-update": "true", 50 }, 51 wantErr: false, 52 }, 53 { 54 pb: "helloworld.proto", 55 generated: "helloworld.openapi.json", 56 flags: map[string]string{ 57 "protodir": ".", 58 "protofile": "helloworld.proto", 59 "swagger": "false", 60 "openapi": "true", 61 "openapi-out": "helloworld.openapi.json", 62 "check-update": "true", 63 }, 64 wantErr: false, 65 }, 66 { 67 pb: "helloworld_restful.proto", 68 generated: "helloworld_restful.swagger.json", 69 flags: map[string]string{ 70 "swagger": "true", 71 "protofile": "helloworld_restful.proto", 72 "swagger-out": "helloworld_restful.swagger.json", 73 "check-update": "true", 74 }, 75 wantErr: false, 76 }, 77 } 78 apidocsCmd := CMD() 79 for _, arg := range cases { 80 generated := filepath.Join(pbdir, arg.generated) 81 defer os.Remove(generated) 82 if _, err := internal.RunAndWatch(apidocsCmd, arg.flags, nil); (err != nil) != arg.wantErr { 83 t.Errorf("apidocs cmd, wantErr = %v, got = %v", arg.wantErr, err) 84 } 85 } 86 }