trpc.group/trpc-go/trpc-cmdline@v1.0.9/cmd/create/create_idl_fbs_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 create
    11  
    12  import (
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestCreateCmdByFlatbuffers(t *testing.T) {
    21  	require.Nil(t, setup(nil))
    22  
    23  	wd, _ := os.Getwd()
    24  	defer func() {
    25  		os.Chdir(wd)
    26  	}()
    27  
    28  	pd := filepath.Dir(wd)
    29  	pd = filepath.Dir(pd)
    30  	testdatadir := filepath.Join(pd, "testcase/flatbuffers")
    31  
    32  	testcases := []testcase{
    33  		{
    34  			name:   "1.1-without-import",
    35  			pbdir:  "1-without-import",
    36  			pbfile: "helloworld.fbs",
    37  			alias:  true,
    38  		}, {
    39  			name:    "1.2-without-import (rpconly)",
    40  			pbdir:   "1-without-import",
    41  			pbfile:  "helloworld.fbs",
    42  			rpconly: true,
    43  		}, {
    44  			name:          "1.3-without-import (split by method)",
    45  			pbdir:         "1-without-import",
    46  			pbfile:        "helloworld.fbs",
    47  			splitByMethod: true,
    48  		}, {
    49  			name:   "2-multi-fb-same-namespace",
    50  			pbdir:  "2-multi-fb-same-namespace",
    51  			pbfile: "hello.fbs",
    52  		}, {
    53  			name:   "3-multi-fb-diff-namespace",
    54  			pbdir:  "3-multi-fb-diff-namespace",
    55  			pbfile: "helloworld.fbs",
    56  		}, {
    57  			name:   "4.1-multi-fb-same-namespace-diff-dir",
    58  			pbdir:  "4.1-multi-fb-same-namespace-diff-dir",
    59  			pbfile: "helloworld.fbs",
    60  		}, {
    61  			name:   "4.2-multi-fb-same-namespace-diff-dir",
    62  			pbdir:  "4.2-multi-fb-same-namespace-diff-dir",
    63  			pbfile: "helloworld.fbs",
    64  		}, {
    65  			name:   "5-multi-fb-diff-gopkg fb",
    66  			pbdir:  "5-multi-fb-diff-gopkg",
    67  			pbfile: "fbsread.fbs",
    68  		}, {
    69  			name:          "5.1-multi-fb-diff-gopkg (split by method)",
    70  			pbdir:         "5-multi-fb-diff-gopkg",
    71  			pbfile:        "fbsread.fbs",
    72  			splitByMethod: true,
    73  		},
    74  	}
    75  
    76  	tmp := filepath.Join(os.TempDir(), "create/generated_fb")
    77  	os.RemoveAll(tmp)
    78  	defer os.RemoveAll(tmp)
    79  
    80  	for _, tt := range testcases {
    81  		tt := tt
    82  		t.Run("CreateCmd/fbs-"+tt.name, func(t *testing.T) {
    83  			any := filepath.Join(testdatadir, tt.pbdir)
    84  
    85  			if err := os.Chdir(any); err != nil {
    86  				panic(err)
    87  			}
    88  
    89  			dirs := []string{}
    90  			err := filepath.Walk(any, func(path string, info os.FileInfo, _ error) error {
    91  				if info.IsDir() {
    92  					dirs = append(dirs, path)
    93  				}
    94  				return nil
    95  			})
    96  			if err != nil {
    97  				panic("walk testcase error")
    98  			}
    99  
   100  			opts := []string{}
   101  			for _, d := range dirs {
   102  				opts = append(opts, "--fbsdir", d)
   103  			}
   104  			opts = append(opts, "--fbs", tt.pbfile)
   105  
   106  			out := filepath.Join(tmp, tt.name)
   107  			opts = append(opts, "-o", out)
   108  
   109  			if tt.rpconly {
   110  				opts = append(opts, "--rpconly")
   111  			}
   112  			if tt.splitByMethod {
   113  				opts = append(opts, "-s")
   114  			}
   115  			if tt.alias {
   116  				opts = append(opts, "--alias")
   117  			}
   118  			opts = append(opts, "--mock", "false")
   119  
   120  			resetFlags(createCmd)
   121  			runCreateCmd(t, "fbs "+tt.name, opts, out, tt.wantErr)
   122  		})
   123  	}
   124  }