cuelang.org/go@v0.13.0/cue/interpreter/embed/embed_test.go (about)

     1  // Copyright 2024 CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package embed
    16  
    17  import (
    18  	"testing"
    19  	"testing/fstest"
    20  
    21  	"github.com/go-quicktest/qt"
    22  	"github.com/google/go-cmp/cmp/cmpopts"
    23  )
    24  
    25  func TestFSGlob(t *testing.T) {
    26  	// These test cases are the same for both Unix and Windows.
    27  	testCases := []struct {
    28  		testName  string
    29  		paths     []string
    30  		pattern   string
    31  		want      []string
    32  		wantError string
    33  	}{{
    34  		testName: "EmptyDirectory",
    35  		paths:    []string{},
    36  		pattern:  "*/*.txt",
    37  		want:     nil,
    38  	}, {
    39  		testName: "DirectoryWithSingleDotFile",
    40  		paths: []string{
    41  			"foo/bar",
    42  			"foo/bar.txt",
    43  			"foo/.hello.txt",
    44  		},
    45  		pattern: "*/*.txt",
    46  		want: []string{
    47  			"foo/bar.txt",
    48  		},
    49  	}, {
    50  		testName: "DotPrefixedDirectory",
    51  		paths: []string{
    52  			"foo/bar.txt",
    53  			"foo/.hello.txt",
    54  			"foo/baz.txt",
    55  			".git/something.txt",
    56  		},
    57  		pattern: "*/*.txt",
    58  		want: []string{
    59  			"foo/bar.txt",
    60  			"foo/baz.txt",
    61  		},
    62  	}, {
    63  		testName: "DotPrefixedDirectoryWithExplicitDot",
    64  		paths: []string{
    65  			"foo/bar.txt",
    66  			"foo/.hello.txt",
    67  			"foo/baz.txt",
    68  			".git/something.txt",
    69  			".git/.otherthing.txt",
    70  		},
    71  		pattern: ".*/*.txt",
    72  		want: []string{
    73  			".git/something.txt",
    74  		},
    75  	}, {
    76  		testName: "DotInCharacterClass",
    77  		paths: []string{
    78  			".foo",
    79  		},
    80  		pattern: "[.x]foo",
    81  		want:    nil,
    82  	}, {
    83  		testName: "SlashInCharacterClass",
    84  		paths: []string{
    85  			"fooxbar",
    86  			"foo/bar",
    87  		},
    88  		pattern:   "foo[/x]bar",
    89  		wantError: `syntax error in pattern`,
    90  	}}
    91  	for _, tc := range testCases {
    92  		t.Run(tc.testName, func(t *testing.T) {
    93  			m := make(fstest.MapFS)
    94  			for _, p := range tc.paths {
    95  				m[p] = &fstest.MapFile{
    96  					Mode: 0o666,
    97  				}
    98  			}
    99  			got, err := fsGlob(m, tc.pattern)
   100  			if tc.wantError != "" {
   101  				qt.Assert(t, qt.ErrorMatches(err, tc.wantError))
   102  				return
   103  			}
   104  			qt.Assert(t, qt.CmpEquals(got, tc.want, cmpopts.EquateEmpty()))
   105  		})
   106  	}
   107  }