github.com/yoheimuta/protolint@v0.49.8-0.20240515023657-4ecaebb7575d/internal/linter/file/protoSet_test.go (about)

     1  package file_test
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/yoheimuta/protolint/internal/linter/file"
     8  	"github.com/yoheimuta/protolint/internal/setting_test"
     9  )
    10  
    11  func TestNewProtoSet(t *testing.T) {
    12  	tests := []struct {
    13  		name             string
    14  		inputTargetPaths []string
    15  		wantProtoFiles   []file.ProtoFile
    16  		wantExistErr     bool
    17  	}{
    18  		{
    19  			name: "innerdir3 includes no files",
    20  			inputTargetPaths: []string{
    21  				setting_test.TestDataPath("testdir", "innerdir3"),
    22  			},
    23  			wantExistErr: true,
    24  		},
    25  		{
    26  			name: "innerdir2 includes no proto files",
    27  			inputTargetPaths: []string{
    28  				setting_test.TestDataPath("testdir", "innerdir2"),
    29  			},
    30  			wantExistErr: true,
    31  		},
    32  		{
    33  			name: "innerdir includes a proto file",
    34  			inputTargetPaths: []string{
    35  				setting_test.TestDataPath("testdir", "innerdir"),
    36  			},
    37  			wantProtoFiles: []file.ProtoFile{
    38  				file.NewProtoFile(
    39  					filepath.Join(setting_test.TestDataPath("testdir", "innerdir"), "/testinner.proto"),
    40  					"../../../_testdata/testdir/innerdir/testinner.proto",
    41  				),
    42  			},
    43  		},
    44  		{
    45  			name: "testdir includes proto files and inner dirs",
    46  			inputTargetPaths: []string{
    47  				setting_test.TestDataPath("testdir"),
    48  			},
    49  			wantProtoFiles: []file.ProtoFile{
    50  				file.NewProtoFile(
    51  					filepath.Join(setting_test.TestDataPath("testdir", "innerdir"), "/testinner.proto"),
    52  					"../../../_testdata/testdir/innerdir/testinner.proto",
    53  				),
    54  				file.NewProtoFile(
    55  					filepath.Join(setting_test.TestDataPath("testdir"), "/test.proto"),
    56  					"../../../_testdata/testdir/test.proto",
    57  				),
    58  				file.NewProtoFile(
    59  					filepath.Join(setting_test.TestDataPath("testdir"), "/test2.proto"),
    60  					"../../../_testdata/testdir/test2.proto",
    61  				),
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, test := range tests {
    67  		test := test
    68  		t.Run(test.name, func(t *testing.T) {
    69  			got, err := file.NewProtoSet(test.inputTargetPaths)
    70  			if test.wantExistErr {
    71  				if err == nil {
    72  					t.Errorf("got err nil, but want err")
    73  				}
    74  				return
    75  			}
    76  			if err != nil {
    77  				t.Errorf("got err %v, but want nil", err)
    78  				return
    79  			}
    80  
    81  			for i, gotf := range got.ProtoFiles() {
    82  				wantf := test.wantProtoFiles[i]
    83  				if gotf.Path() != wantf.Path() {
    84  					t.Errorf("got %v, but want %v", gotf.Path(), wantf.Path())
    85  				}
    86  				if gotf.DisplayPath() != wantf.DisplayPath() {
    87  					t.Errorf("got %v, but want %v", gotf.DisplayPath(), wantf.DisplayPath())
    88  				}
    89  			}
    90  		})
    91  	}
    92  }