...

Source file src/github.com/sagikazarmark/locafero/finder_test.go

Documentation: github.com/sagikazarmark/locafero

     1  package locafero
     2  
     3  import (
     4  	"fmt"
     5  	"path"
     6  	"testing"
     7  
     8  	"github.com/spf13/afero"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func Example() {
    14  	fsys := afero.NewBasePathFs(afero.NewOsFs(), "testdata")
    15  
    16  	finder := Finder{
    17  		Paths: []string{
    18  			"home/user",
    19  			"etc",
    20  		},
    21  		Names: []string{"config.*"},
    22  		Type:  FileTypeFile,
    23  	}
    24  
    25  	results, err := finder.Find(fsys)
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	fmt.Print(results)
    31  
    32  	// Output: [home/user/config.yaml etc/config.yaml]
    33  }
    34  
    35  func TestFinder_Find(t *testing.T) {
    36  	fsys := afero.NewMemMapFs()
    37  
    38  	files := []string{
    39  		"home/user/.config/app/config.yaml",
    40  		"home/user/app/config.yaml",
    41  		"home/user/config.json",
    42  		"home/user/config.yaml",
    43  		"home/user/config/app.yaml",
    44  		"home/user/config/config.yaml",
    45  		"etc/app/config.yaml",
    46  		"etc/config.json",
    47  		"etc/config.yaml",
    48  		"etc/config/app.yaml",
    49  		"etc/config/config.yaml",
    50  	}
    51  
    52  	for _, file := range files {
    53  		dir := path.Dir(file)
    54  
    55  		err := fsys.MkdirAll(dir, 0o777)
    56  		require.NoError(t, err)
    57  
    58  		_, err = fsys.Create(file)
    59  		require.NoError(t, err)
    60  	}
    61  
    62  	tests := []struct {
    63  		name    string
    64  		finder  Finder
    65  		results []string
    66  	}{
    67  		{
    68  			name:    "nothing to find",
    69  			finder:  Finder{},
    70  			results: nil,
    71  		},
    72  		{
    73  			name: "no names to find",
    74  			finder: Finder{
    75  				Paths: []string{"home/user"},
    76  			},
    77  			results: nil,
    78  		},
    79  		{
    80  			name: "no paths to find in",
    81  			finder: Finder{
    82  				Names: []string{"config.yaml"},
    83  			},
    84  			results: nil,
    85  		},
    86  		{
    87  			name: "find in path",
    88  			finder: Finder{
    89  				Paths: []string{"home/user"},
    90  				Names: []string{"config.yaml"},
    91  			},
    92  			results: []string{
    93  				"home/user/config.yaml",
    94  			},
    95  		},
    96  		{
    97  			name: "find in multiple paths",
    98  			finder: Finder{
    99  				Paths: []string{"home/user", "etc"},
   100  				Names: []string{"config.yaml"},
   101  			},
   102  			results: []string{
   103  				"home/user/config.yaml",
   104  				"etc/config.yaml",
   105  			},
   106  		},
   107  		{
   108  			name: "find multiple names in multiple paths",
   109  			finder: Finder{
   110  				Paths: []string{"home/user", "etc"},
   111  				Names: []string{"config", "config.yaml"},
   112  			},
   113  			results: []string{
   114  				"home/user/config",
   115  				"home/user/config.yaml",
   116  				"etc/config",
   117  				"etc/config.yaml",
   118  			},
   119  		},
   120  		{
   121  			name: "find in subdirs of each other",
   122  			finder: Finder{
   123  				Paths: []string{"home/user", "home/user/app"},
   124  				Names: []string{"config.yaml"},
   125  			},
   126  			results: []string{
   127  				"home/user/config.yaml",
   128  				"home/user/app/config.yaml",
   129  			},
   130  		},
   131  		{
   132  			name: "find files only",
   133  			finder: Finder{
   134  				Paths: []string{"home/user", "etc"},
   135  				Names: []string{"config", "config.yaml"},
   136  				Type:  FileTypeFile,
   137  			},
   138  			results: []string{
   139  				"home/user/config.yaml",
   140  				"etc/config.yaml",
   141  			},
   142  		},
   143  		{
   144  			name: "find dirs only",
   145  			finder: Finder{
   146  				Paths: []string{"home/user", "etc"},
   147  				Names: []string{"config", "config.yaml"},
   148  				Type:  FileTypeDir,
   149  			},
   150  			results: []string{
   151  				"home/user/config",
   152  				"etc/config",
   153  			},
   154  		},
   155  		{
   156  			name: "glob match",
   157  			finder: Finder{
   158  				Paths: []string{"home/user", "etc"},
   159  				Names: []string{"config*"},
   160  			},
   161  			results: []string{
   162  				"home/user/config",
   163  				"home/user/config.json",
   164  				"home/user/config.yaml",
   165  				"etc/config",
   166  				"etc/config.json",
   167  				"etc/config.yaml",
   168  			},
   169  		},
   170  		{
   171  			name: "glob match",
   172  			finder: Finder{
   173  				Paths: []string{"home/user", "etc"},
   174  				Names: []string{"config.*"},
   175  			},
   176  			results: []string{
   177  				"home/user/config.json",
   178  				"home/user/config.yaml",
   179  				"etc/config.json",
   180  				"etc/config.yaml",
   181  			},
   182  		},
   183  		{
   184  			name: "glob match files",
   185  			finder: Finder{
   186  				Paths: []string{"home/user", "etc"},
   187  				Names: []string{"config*"},
   188  				Type:  FileTypeFile,
   189  			},
   190  			results: []string{
   191  				"home/user/config.json",
   192  				"home/user/config.yaml",
   193  				"etc/config.json",
   194  				"etc/config.yaml",
   195  			},
   196  		},
   197  		{
   198  			name: "glob match dirs",
   199  			finder: Finder{
   200  				Paths: []string{"home/user", "etc"},
   201  				Names: []string{"config*"},
   202  				Type:  FileTypeDir,
   203  			},
   204  			results: []string{
   205  				"home/user/config",
   206  				"etc/config",
   207  			},
   208  		},
   209  		{
   210  			name: "glob match in subdirs of each other",
   211  			finder: Finder{
   212  				Paths: []string{"home/user", "home/user/config", "etc", "etc/config"},
   213  				Names: []string{"config*"},
   214  			},
   215  			results: []string{
   216  				"home/user/config",
   217  				"home/user/config.json",
   218  				"home/user/config.yaml",
   219  				"home/user/config/config.yaml",
   220  				"etc/config",
   221  				"etc/config.json",
   222  				"etc/config.yaml",
   223  				"etc/config/config.yaml",
   224  			},
   225  		},
   226  		{
   227  			name: "glob match files in subdirs of each other",
   228  			finder: Finder{
   229  				Paths: []string{"home/user", "home/user/config", "etc", "etc/config"},
   230  				Names: []string{"config*"},
   231  				Type:  FileTypeFile,
   232  			},
   233  			results: []string{
   234  				"home/user/config.json",
   235  				"home/user/config.yaml",
   236  				"home/user/config/config.yaml",
   237  				"etc/config.json",
   238  				"etc/config.yaml",
   239  				"etc/config/config.yaml",
   240  			},
   241  		},
   242  		{
   243  			name: "glob match dirs in subdirs of each other",
   244  			finder: Finder{
   245  				Paths: []string{"home/user", "home/user/config", "etc", "etc/config"},
   246  				Names: []string{"config*"},
   247  				Type:  FileTypeDir,
   248  			},
   249  			results: []string{
   250  				"home/user/config",
   251  				"etc/config",
   252  			},
   253  		},
   254  	}
   255  
   256  	for _, tt := range tests {
   257  		tt := tt
   258  
   259  		t.Run(tt.name, func(t *testing.T) {
   260  			results, err := tt.finder.Find(fsys)
   261  			require.NoError(t, err)
   262  
   263  			assert.Equal(t, tt.results, results)
   264  		})
   265  	}
   266  }
   267  

View as plain text