github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/cmd/gno/util_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestMatchPattern(t *testing.T) { 13 tests := []struct { 14 pattern string 15 names []string 16 expected []bool 17 }{ 18 { 19 pattern: "foo", 20 names: []string{"foo", "bar", "baz", "foo/bar"}, 21 expected: []bool{true, false, false, false}, 22 }, 23 { 24 pattern: "foo/...", 25 names: []string{"foo", "foo/bar", "foo/bar/baz", "bar", "baz"}, 26 expected: []bool{true, true, true, false, false}, 27 }, 28 { 29 pattern: "foo/bar/...", 30 names: []string{"foo/bar", "foo/bar/baz", "foo/baz/bar", "foo", "bar"}, 31 expected: []bool{true, true, false, false, false}, 32 }, 33 { 34 pattern: "foo/.../baz", 35 names: []string{"foo/bar", "foo/bar/baz", "foo/baz/bar", "foo", "bar"}, 36 expected: []bool{false, true, false, false, false}, 37 }, 38 { 39 pattern: "foo/.../baz/...", 40 names: []string{"foo/bar/baz", "foo/baz/bar", "foo/bar/baz/qux", "foo/baz/bar/qux"}, 41 expected: []bool{true, false, true, false}, 42 }, 43 { 44 pattern: "...", 45 names: []string{"foo", "bar", "baz", "foo/bar", "foo/bar/baz"}, 46 expected: []bool{true, true, true, true, true}, 47 }, 48 { 49 pattern: ".../bar", 50 names: []string{"foo", "bar", "baz", "foo/bar", "foo/bar/baz"}, 51 expected: []bool{false, false, false, true, false}, 52 }, 53 } 54 55 for _, test := range tests { 56 t.Run(test.pattern, func(t *testing.T) { 57 matchFunc := matchPattern(test.pattern) 58 for i, name := range test.names { 59 res := matchFunc(name) 60 assert.Equal(t, test.expected[i], res, "Expected: %v, Got: %v", test.expected[i], res) 61 } 62 }) 63 } 64 } 65 66 func TestTargetsFromPatterns(t *testing.T) { 67 tmpDir := t.TempDir() 68 createGnoPackages(t, tmpDir) 69 70 for _, tc := range []struct { 71 desc string 72 in, expected []string 73 errorShouldContain string 74 }{ 75 { 76 desc: "valid1", 77 in: []string{ 78 tmpDir, 79 }, 80 expected: []string{ 81 tmpDir, 82 }, 83 }, 84 { 85 desc: "valid2", 86 in: []string{ 87 tmpDir + "/foo", 88 }, 89 expected: []string{ 90 filepath.Join(tmpDir, "foo"), 91 }, 92 }, 93 { 94 desc: "valid_recursive1", 95 in: []string{ 96 tmpDir + "/...", 97 }, 98 expected: []string{ 99 filepath.Join(tmpDir, "foo"), 100 filepath.Join(tmpDir, "bar"), 101 filepath.Join(tmpDir, "baz"), 102 filepath.Join(tmpDir, "foo", "qux"), 103 filepath.Join(tmpDir, "bar", "quux"), 104 filepath.Join(tmpDir, "foo", "qux", "corge"), 105 }, 106 }, 107 { 108 desc: "valid_recursive2", 109 in: []string{ 110 tmpDir + "/foo/...", 111 }, 112 expected: []string{ 113 filepath.Join(tmpDir, "foo"), 114 filepath.Join(tmpDir, "foo", "qux"), 115 filepath.Join(tmpDir, "foo", "qux", "corge"), 116 }, 117 }, 118 { 119 desc: "valid_recursive2", 120 in: []string{ 121 tmpDir + "/.../qux", 122 }, 123 expected: []string{ 124 filepath.Join(tmpDir, "foo", "qux"), 125 }, 126 }, 127 { 128 desc: "valid_recursive3", 129 in: []string{ 130 tmpDir + "/.../qux/...", 131 }, 132 expected: []string{ 133 filepath.Join(tmpDir, "foo", "qux"), 134 filepath.Join(tmpDir, "foo", "qux", "corge"), 135 }, 136 }, 137 { 138 desc: "multiple_input", 139 in: []string{ 140 tmpDir + "/foo", 141 tmpDir + "/bar", 142 tmpDir + "/baz", 143 }, 144 expected: []string{ 145 filepath.Join(tmpDir, "foo"), 146 filepath.Join(tmpDir, "bar"), 147 filepath.Join(tmpDir, "baz"), 148 }, 149 }, 150 { 151 desc: "mixed_input1", 152 in: []string{ 153 tmpDir + "/foo", 154 tmpDir + "/bar/...", 155 }, 156 expected: []string{ 157 filepath.Join(tmpDir, "foo"), 158 filepath.Join(tmpDir, "bar"), 159 filepath.Join(tmpDir, "bar", "quux"), 160 }, 161 }, 162 { 163 desc: "mixed_input2", 164 in: []string{ 165 tmpDir + "/foo", 166 tmpDir + "/bar/...", 167 tmpDir + "/baz/baz.gno", 168 }, 169 expected: []string{ 170 filepath.Join(tmpDir, "foo"), 171 filepath.Join(tmpDir, "bar"), 172 filepath.Join(tmpDir, "bar", "quux"), 173 filepath.Join(tmpDir, "baz", "baz.gno"), 174 }, 175 }, 176 { 177 desc: "not_exists1", 178 in: []string{ 179 tmpDir + "/notexists", // dir path 180 }, 181 errorShouldContain: "no such file or directory", 182 }, 183 { 184 desc: "not_exists2", 185 in: []string{ 186 tmpDir + "/foo/bar.gno", // file path 187 }, 188 errorShouldContain: "no such file or directory", 189 }, 190 { 191 desc: "not_exists3", // mixed 192 in: []string{ 193 tmpDir + "/foo", // exists 194 tmpDir + "/notexists", // not exists 195 }, 196 errorShouldContain: "no such file or directory", 197 }, 198 } { 199 t.Run(tc.desc, func(t *testing.T) { 200 targets, err := targetsFromPatterns(tc.in) 201 if tc.errorShouldContain != "" { 202 assert.ErrorContains(t, err, tc.errorShouldContain) 203 return 204 } 205 assert.NoError(t, err) 206 require.Equal(t, len(tc.expected), len(targets)) 207 for _, tr := range targets { 208 assert.Contains(t, tc.expected, tr) 209 } 210 }) 211 } 212 } 213 214 func createGnoPackages(t *testing.T, tmpDir string) { 215 t.Helper() 216 217 type file struct { 218 name, data string 219 } 220 // Gno pkgs to create 221 pkgs := []struct { 222 dir string 223 files []file 224 }{ 225 // pkg 'foo', 'bar' and 'baz' 226 { 227 dir: filepath.Join(tmpDir, "foo"), 228 files: []file{ 229 { 230 name: "foo.gno", 231 data: `package foo`, 232 }, 233 }, 234 }, 235 { 236 dir: filepath.Join(tmpDir, "bar"), 237 files: []file{ 238 { 239 name: "bar.gno", 240 data: `package bar`, 241 }, 242 }, 243 }, 244 { 245 dir: filepath.Join(tmpDir, "baz"), 246 files: []file{ 247 { 248 name: "baz.gno", 249 data: `package baz`, 250 }, 251 }, 252 }, 253 254 // pkg inside 'foo' pkg 255 { 256 dir: filepath.Join(tmpDir, "foo", "qux"), 257 files: []file{ 258 { 259 name: "qux.gno", 260 data: `package qux`, 261 }, 262 }, 263 }, 264 265 // pkg inside 'bar' pkg 266 { 267 dir: filepath.Join(tmpDir, "bar", "quux"), 268 files: []file{ 269 { 270 name: "quux.gno", 271 data: `package quux`, 272 }, 273 }, 274 }, 275 276 // pkg inside 'foo/qux' pkg 277 { 278 dir: filepath.Join(tmpDir, "foo", "qux", "corge"), 279 files: []file{ 280 { 281 name: "corge.gno", 282 data: `package corge`, 283 }, 284 }, 285 }, 286 } 287 288 // Create pkgs 289 for _, p := range pkgs { 290 err := os.MkdirAll(p.dir, 0o700) 291 require.NoError(t, err) 292 for _, f := range p.files { 293 err = os.WriteFile(filepath.Join(p.dir, f.name), []byte(f.data), 0o644) 294 require.NoError(t, err) 295 } 296 } 297 }