github.com/tinyhole/pblib@v0.0.0-20191001142211-98c63f19cf59/protoreflect/desc/protoparse/resolve_files_test.go (about)

     1  package protoparse
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/tinyhole/pblib/protoreflect/internal/testutil"
    10  )
    11  
    12  func TestResolveFilenames(t *testing.T) {
    13  	dir, err := ioutil.TempDir("", "resolve-filenames-test")
    14  	testutil.Ok(t, err)
    15  	defer func() {
    16  		_ = os.RemoveAll(dir)
    17  	}()
    18  
    19  	testCases := []struct {
    20  		name        string
    21  		importPaths []string
    22  		fileNames   []string
    23  		files       []string
    24  		expectedErr string
    25  		results     []string
    26  	}{
    27  		{
    28  			name:      "no import paths",
    29  			fileNames: []string{"test1.proto", "test2.proto", "test3.proto"},
    30  			results:   []string{"test1.proto", "test2.proto", "test3.proto"},
    31  		},
    32  		{
    33  			name:        "no import paths; absolute file name",
    34  			fileNames:   []string{filepath.Join(dir, "test.proto")},
    35  			expectedErr: errNoImportPathsForAbsoluteFilePath.Error(),
    36  		},
    37  		{
    38  			name:        "import and file paths relative to cwd",
    39  			importPaths: []string{"./test"},
    40  			fileNames:   []string{"./test/a.proto", "./test/b.proto", "./test/c.proto"},
    41  			results:     []string{"a.proto", "b.proto", "c.proto"},
    42  		},
    43  		{
    44  			name:        "absolute file paths, import paths relative to cwd",
    45  			importPaths: []string{"./test"},
    46  			fileNames:   []string{filepath.Join(dir, "test/a.proto"), filepath.Join(dir, "test/b.proto"), filepath.Join(dir, "test/c.proto")},
    47  			results:     []string{"a.proto", "b.proto", "c.proto"},
    48  		},
    49  		{
    50  			name:        "absolute import paths, file paths relative to cwd",
    51  			importPaths: []string{filepath.Join(dir, "test")},
    52  			fileNames:   []string{"./test/a.proto", "./test/b.proto", "./test/c.proto"},
    53  			results:     []string{"a.proto", "b.proto", "c.proto"},
    54  		},
    55  		{
    56  			name:        "file path relative to cwd not in import path",
    57  			importPaths: []string{filepath.Join(dir, "test")},
    58  			fileNames:   []string{"./test/a.proto", "./test/b.proto", "./foo/c.proto"},
    59  			expectedErr: "./foo/c.proto does not reside in any import path",
    60  		},
    61  		{
    62  			name:        "absolute file path not in import path",
    63  			importPaths: []string{filepath.Join(dir, "test")},
    64  			fileNames:   []string{"./test/a.proto", "./test/b.proto", filepath.Join(dir, "foo/c.proto")},
    65  			expectedErr: filepath.Join(dir, "foo/c.proto") + " does not reside in any import path",
    66  		},
    67  		{
    68  			name:        "relative paths, files relative to import path",
    69  			files:       []string{"test/a.proto", "test/b.proto", "test/c.proto"},
    70  			importPaths: []string{"test"},
    71  			fileNames:   []string{"a.proto", "b.proto", "c.proto"},
    72  			results:     []string{"a.proto", "b.proto", "c.proto"},
    73  		},
    74  		{
    75  			name:        "relative paths, files relative to mix",
    76  			files:       []string{"test/a.proto", "test/b.proto", "test/c.proto"},
    77  			importPaths: []string{"test"},
    78  			fileNames:   []string{"test/a.proto", "b.proto", "test/c.proto"},
    79  			results:     []string{"a.proto", "b.proto", "c.proto"},
    80  		},
    81  	}
    82  
    83  	origCwd, err := os.Getwd()
    84  	testutil.Ok(t, err)
    85  
    86  	err = os.Chdir(dir)
    87  	testutil.Ok(t, err)
    88  	defer func() {
    89  		_ = os.Chdir(origCwd)
    90  	}()
    91  
    92  	for _, tc := range testCases {
    93  		// setup any test files
    94  		for _, f := range tc.files {
    95  			subDir := filepath.Dir(f)
    96  			if subDir != "." {
    97  				err := os.MkdirAll(filepath.Join(dir, subDir), os.ModePerm)
    98  				testutil.Ok(t, err)
    99  			}
   100  			err := ioutil.WriteFile(filepath.Join(dir, f), nil, 0666)
   101  			testutil.Ok(t, err)
   102  		}
   103  
   104  		// run the function under test
   105  		res, err := ResolveFilenames(tc.importPaths, tc.fileNames...)
   106  		// assert outcome
   107  		if tc.expectedErr != "" {
   108  			testutil.Nok(t, err, "%s", tc.name)
   109  			testutil.Eq(t, tc.expectedErr, err.Error(), "%s", tc.name)
   110  		} else {
   111  			testutil.Ok(t, err, "%s", tc.name)
   112  			testutil.Eq(t, tc.results, res, "%s", tc.name)
   113  		}
   114  
   115  		// remove test files
   116  		for _, f := range tc.files {
   117  			err := os.Remove(filepath.Join(dir, f))
   118  			testutil.Ok(t, err)
   119  		}
   120  	}
   121  }