trpc.group/trpc-go/trpc-cmdline@v1.0.9/util/pb/protoc_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 pb
    11  
    12  import (
    13  	"fmt"
    14  	"os"
    15  	"path/filepath"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/agiledragon/gomonkey"
    20  
    21  	"trpc.group/trpc-go/trpc-cmdline/config"
    22  )
    23  
    24  var wd string
    25  
    26  func TestMain(m *testing.M) {
    27  	if _, err := config.Init(); err != nil {
    28  		panic(err)
    29  	}
    30  	if err := setup(); err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	d, err := os.Getwd()
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	wd = filepath.Join(d, "testcase")
    39  
    40  	os.Exit(m.Run())
    41  }
    42  
    43  func TestProtoc(t *testing.T) {
    44  	languages := []string{"go"}
    45  	outputdir := filepath.Join(wd, "generated")
    46  	os.Mkdir(outputdir, os.ModePerm)
    47  	defer os.RemoveAll(outputdir)
    48  
    49  	type args struct {
    50  		protodirs []string
    51  		protofile string
    52  		pb2impt   map[string]string
    53  	}
    54  
    55  	tests := []struct {
    56  		name string
    57  		args args
    58  	}{
    59  		{
    60  			"case1",
    61  			args{
    62  				[]string{wd},
    63  				"helloworld.proto",
    64  				nil,
    65  			},
    66  		},
    67  		{
    68  			"case2",
    69  			args{
    70  				[]string{wd},
    71  				"helloworld.proto",
    72  				map[string]string{"helloworld.proto": "trpc.group/examples/helloworld"},
    73  			},
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			for _, lang := range languages {
    79  				err := Protoc(tt.args.protodirs, tt.args.protofile, lang, outputdir, WithPb2ImportPath(tt.args.pb2impt))
    80  				if err != nil {
    81  					t.Errorf("Protoc() error = %v", err)
    82  				}
    83  			}
    84  		})
    85  	}
    86  
    87  	// clean
    88  	os.RemoveAll(outputdir)
    89  }
    90  
    91  func Test_makeProtocOutByLanguage(t *testing.T) {
    92  	type args struct {
    93  		language  string
    94  		pbpkg     string
    95  		outputdir string
    96  	}
    97  	tests := []struct {
    98  		name string
    99  		args args
   100  		want string
   101  	}{
   102  		{
   103  			name: "case_other_language",
   104  			args: args{
   105  				language:  "other",
   106  				pbpkg:     "pbpkg",
   107  				outputdir: "outputdir",
   108  			},
   109  			want: "--other_out=pbpkg:outputdir",
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			p := gomonkey.ApplyFunc(os.MkdirAll, func(path string, perm os.FileMode) error {
   115  				return nil
   116  			})
   117  			defer p.Reset()
   118  
   119  			if got := makeProtocOutByLanguage(tt.args.language, tt.args.pbpkg, tt.args.outputdir); got != tt.want {
   120  				t.Errorf("makeProtocOutByLanguage() = %v, want %v", got, tt.want)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func Test_genRelPathFromWd(t *testing.T) {
   127  	type args struct {
   128  		wd        string
   129  		protofile string
   130  	}
   131  	tests := []struct {
   132  		name    string
   133  		args    args
   134  		want    string
   135  		wantErr bool
   136  		absRsp  map[string]string
   137  		absErr  map[string]error
   138  	}{
   139  		{
   140  			name: "get wd abs path error",
   141  			args: args{
   142  				wd:        "wd",
   143  				protofile: "protofile",
   144  			},
   145  			want:    "",
   146  			wantErr: true,
   147  			absRsp:  make(map[string]string),
   148  			absErr: map[string]error{
   149  				"wd": fmt.Errorf("error"),
   150  			},
   151  		},
   152  		{
   153  			name: "get protofile abs path error",
   154  			args: args{
   155  				wd:        "wd",
   156  				protofile: "protofile",
   157  			},
   158  			want:    "",
   159  			wantErr: true,
   160  			absRsp:  make(map[string]string),
   161  			absErr: map[string]error{
   162  				"protofile": fmt.Errorf("error"),
   163  			},
   164  		},
   165  		{
   166  			name: "gen ref path error",
   167  			args: args{
   168  				wd:        "wd",
   169  				protofile: "protofile",
   170  			},
   171  			want:    "",
   172  			wantErr: true,
   173  			absRsp: map[string]string{
   174  				"wd":        "/wd",
   175  				"protofile": "a/protofile",
   176  			},
   177  			absErr: make(map[string]error),
   178  		},
   179  		{
   180  			name: "gen ref path succ",
   181  			args: args{
   182  				wd:        "wd",
   183  				protofile: "protofile",
   184  			},
   185  			want:    "a/protofile",
   186  			wantErr: false,
   187  			absRsp: map[string]string{
   188  				"wd":        "/wd",
   189  				"protofile": "/wd/a/protofile",
   190  			},
   191  			absErr: make(map[string]error),
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			p := gomonkey.ApplyFunc(filepath.Abs, func(path string) (string, error) {
   197  				return tt.absRsp[path], tt.absErr[path]
   198  			})
   199  			defer p.Reset()
   200  
   201  			got, err := genRelPathFromWd(tt.args.protofile, tt.args.wd)
   202  			if (err != nil) != tt.wantErr {
   203  				t.Errorf("genRelPathFromWd() error = %v, wantErr %v", err, tt.wantErr)
   204  				return
   205  			}
   206  			if got != tt.want {
   207  				t.Errorf("genRelPathFromWd() got = %v, want %v", got, tt.want)
   208  			}
   209  		})
   210  	}
   211  }
   212  
   213  type mockFileInfo struct {
   214  	err   error
   215  	isDir bool
   216  }
   217  
   218  func (s *mockFileInfo) Name() string       { return "" }
   219  func (s *mockFileInfo) Size() int64        { return 0 }
   220  func (s *mockFileInfo) Mode() os.FileMode  { return 0 }
   221  func (s *mockFileInfo) ModTime() time.Time { return time.Time{} }
   222  func (s *mockFileInfo) IsDir() bool        { return s.isDir }
   223  func (s *mockFileInfo) Sys() interface{}   { return nil }
   224  
   225  func Test_genRelPathFromWdWithDirs(t *testing.T) {
   226  	type args struct {
   227  		protodirs []string
   228  		protofile string
   229  		wd        string
   230  	}
   231  	tests := []struct {
   232  		name                string
   233  		args                args
   234  		want                string
   235  		wantErr             bool
   236  		lstatErr            error
   237  		isDir               bool
   238  		genRelPathFromWdRsp string
   239  		genRelPathFromWdErr error
   240  	}{
   241  		{
   242  			name: "os.Lstat error",
   243  			args: args{
   244  				protodirs: []string{"dir1"},
   245  				protofile: "file",
   246  				wd:        "path",
   247  			},
   248  			want:     "",
   249  			wantErr:  true,
   250  			lstatErr: fmt.Errorf("error"),
   251  		},
   252  		{
   253  			name: "genRelPathFromWd error",
   254  			args: args{
   255  				protodirs: []string{"dir1"},
   256  				protofile: "file",
   257  				wd:        "path",
   258  			},
   259  			want:                "",
   260  			wantErr:             true,
   261  			isDir:               false,
   262  			genRelPathFromWdRsp: "path",
   263  			genRelPathFromWdErr: fmt.Errorf("error"),
   264  		},
   265  		{
   266  			name: "genRelPathFromWd succ",
   267  			args: args{
   268  				protodirs: []string{"dir1"},
   269  				protofile: "file",
   270  				wd:        "path",
   271  			},
   272  			want:                "path",
   273  			wantErr:             false,
   274  			isDir:               false,
   275  			genRelPathFromWdRsp: "path",
   276  		},
   277  	}
   278  	for _, tt := range tests {
   279  		t.Run(tt.name, func(t *testing.T) {
   280  			p := gomonkey.ApplyFunc(os.Lstat, func(name string) (os.FileInfo, error) {
   281  				return &mockFileInfo{isDir: tt.isDir}, tt.lstatErr
   282  			})
   283  			defer p.Reset()
   284  			p.ApplyFunc(genRelPathFromWd, func(protofile, wd string) (string, error) {
   285  				return tt.genRelPathFromWdRsp, tt.genRelPathFromWdErr
   286  			})
   287  
   288  			got, err := genRelPathFromWdWithDirs(tt.args.protodirs, tt.args.protofile, tt.args.wd)
   289  			if (err != nil) != tt.wantErr {
   290  				t.Errorf("genRelPathFromWdWithDirs() error = %v, wantErr %v", err, tt.wantErr)
   291  				return
   292  			}
   293  			if got != tt.want {
   294  				t.Errorf("genRelPathFromWdWithDirs() got = %v, want %v", got, tt.want)
   295  			}
   296  		})
   297  	}
   298  }
   299  
   300  func setup() error {
   301  	if _, err := config.Init(); err != nil {
   302  		return err
   303  	}
   304  	deps, err := config.LoadDependencies()
   305  	if err != nil {
   306  		return err
   307  	}
   308  	return config.SetupDependencies(deps)
   309  }