trpc.group/trpc-go/trpc-cmdline@v1.0.9/plugin/gotag_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 plugin 11 12 import ( 13 "os" 14 "path/filepath" 15 "testing" 16 17 "github.com/stretchr/testify/require" 18 19 "trpc.group/trpc-go/trpc-cmdline/descriptor" 20 "trpc.group/trpc-go/trpc-cmdline/params" 21 "trpc.group/trpc-go/trpc-cmdline/parser" 22 "trpc.group/trpc-go/trpc-cmdline/util/paths" 23 "trpc.group/trpc-go/trpc-cmdline/util/pb" 24 ) 25 26 func TestPlugin_GoTag(t *testing.T) { 27 require.Nil(t, setup()) 28 u := &GoTag{} 29 require.Equal(t, "gotag", u.Name()) 30 31 opt := params.Option{ 32 Language: "go", 33 Gotag: true, 34 OutputDir: t.TempDir(), 35 } 36 os.MkdirAll(opt.OutputDir, os.ModePerm) 37 defer os.RemoveAll(opt.OutputDir) 38 39 // Don't run if not golang. 40 t.Run("lang !go", func(t *testing.T) { 41 opt := opt 42 opt.Language = "!go" 43 require.False(t, u.Check(nil, &opt)) 44 }) 45 46 // Golang, but no gotag. 47 t.Run("go && !gotag", func(t *testing.T) { 48 opt := opt 49 opt.Gotag = false 50 require.False(t, u.Check(nil, &opt)) 51 }) 52 53 // Run normally. 54 t.Run("go && gotag", func(t *testing.T) { 55 require.True(t, u.Check(nil, &opt)) 56 57 // setup 58 opt := opt 59 pbf, fd, err := parseGoTagSampleProtofile() 60 if err != nil { 61 panic(err) 62 } 63 64 // No corresponding pb.go file is generated for the pb file. 65 t.Run("lstat error", func(t *testing.T) { 66 require.NotNil(t, u.Run(fd, &opt)) 67 }) 68 69 // Parse tags area. 70 t.Run("tags area", func(t *testing.T) { 71 opt := opt 72 opt.RPCOnly = true 73 pbd := filepath.Dir(pbf) 74 75 wd, _ := os.Getwd() 76 defer os.Chdir(wd) 77 78 os.Chdir(pbd) 79 80 root := filepath.Clean(filepath.Join(wd, "..")) 81 protodirs := append([]string{ 82 root, 83 filepath.Join(root, "install"), 84 filepath.Join(root, "install/submodules"), 85 filepath.Join(root, "install/submodules/trpc"), 86 filepath.Join(root, "install/protos"), 87 filepath.Join(root, "testcase/plugins/gotag"), 88 }, paths.ExpandTRPCSearch(filepath.Join(root, "install"))...) 89 err = pb.Protoc(protodirs, pbf, "go", opt.OutputDir) 90 if err != nil { 91 panic(err) 92 } 93 94 require.Nil(t, u.Run(fd, &opt)) 95 }) 96 }) 97 } 98 99 func parseGoTagSampleProtofile() (string, *descriptor.FileDescriptor, error) { 100 wd, err := os.Getwd() 101 if err != nil { 102 return "", nil, err 103 } 104 // parse protofile 105 pbd := filepath.Clean(filepath.Join(wd, "../testcase/plugins/gotag")) 106 pbf := filepath.Join(pbd, "gotag.proto") 107 108 dir1, err := paths.Locate(pb.ProtoTRPC) 109 if err != nil { 110 panic(err) 111 } 112 113 fd, err := parser.ParseProtoFile( 114 "gotag.proto", 115 append(paths.ExpandSearch(dir1), pbd, dir1), 116 parser.WithLanguage("go"), 117 ) 118 if err != nil { 119 return "", nil, err 120 } 121 return pbf, fd, err 122 }