|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { globDirname } from '../src/glob-dirname' |
| 3 | + |
| 4 | +const dir = (str: string) => globDirname([str])[0] |
| 5 | + |
| 6 | +describe('globDirname', () => { |
| 7 | + test('should handle various glob patterns correctly', () => { |
| 8 | + expect(dir('.')).toMatchInlineSnapshot(`"."`) |
| 9 | + expect(dir('.*')).toMatchInlineSnapshot(`"."`) |
| 10 | + expect(dir('/.*')).toMatchInlineSnapshot(`"."`) |
| 11 | + expect(dir('/.*/')).toMatchInlineSnapshot(`"."`) |
| 12 | + expect(dir('a/.*/b')).toMatchInlineSnapshot(`"a"`) |
| 13 | + expect(dir('a*/.*/b')).toMatchInlineSnapshot(`"."`) |
| 14 | + expect(dir('*/a/b/c')).toMatchInlineSnapshot(`"."`) |
| 15 | + expect(dir('*')).toMatchInlineSnapshot(`"."`) |
| 16 | + expect(dir('*/')).toMatchInlineSnapshot(`"."`) |
| 17 | + expect(dir('*/*')).toMatchInlineSnapshot(`"."`) |
| 18 | + expect(dir('*/*/')).toMatchInlineSnapshot(`"."`) |
| 19 | + expect(dir('**')).toMatchInlineSnapshot(`"."`) |
| 20 | + expect(dir('**/')).toMatchInlineSnapshot(`"."`) |
| 21 | + expect(dir('**/*')).toMatchInlineSnapshot(`"."`) |
| 22 | + expect(dir('**/*/')).toMatchInlineSnapshot(`"."`) |
| 23 | + expect(dir('/*.js')).toMatchInlineSnapshot(`"."`) |
| 24 | + expect(dir('*.js')).toMatchInlineSnapshot(`"."`) |
| 25 | + expect(dir('**/*.js')).toMatchInlineSnapshot(`"."`) |
| 26 | + expect(dir('{a,b}')).toMatchInlineSnapshot(`"."`) |
| 27 | + expect(dir('/{a,b}')).toMatchInlineSnapshot(`"."`) |
| 28 | + expect(dir('/{a,b}/')).toMatchInlineSnapshot(`"."`) |
| 29 | + expect(dir('(a|b)')).toMatchInlineSnapshot(`"."`) |
| 30 | + expect(dir('/(a|b)')).toMatchInlineSnapshot(`"."`) |
| 31 | + expect(dir('./(a|b)')).toMatchInlineSnapshot(`"."`) |
| 32 | + expect(dir('a/(b c)')).toMatchInlineSnapshot(`"a"`) |
| 33 | + expect(dir('a/(b c)/')).toMatchInlineSnapshot(`"a"`) |
| 34 | + expect(dir('a/(b c)/d')).toMatchInlineSnapshot(`"a"`) |
| 35 | + expect(dir('path/to/*.js')).toMatchInlineSnapshot(`"path/to"`) |
| 36 | + expect(dir('/root/path/to/*.js')).toMatchInlineSnapshot(`"/root/path/to"`) |
| 37 | + expect(dir('chapter/foo [bar]/')).toMatchInlineSnapshot(`"chapter"`) |
| 38 | + expect(dir('path/[a-z]')).toMatchInlineSnapshot(`"path"`) |
| 39 | + expect(dir('[a-z]')).toMatchInlineSnapshot(`"."`) |
| 40 | + expect(dir('path/{to,from}')).toMatchInlineSnapshot(`"path"`) |
| 41 | + expect(dir('path/(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 42 | + expect(dir('path/(foo bar)/subdir/foo.*')).toMatchInlineSnapshot(`"path"`) |
| 43 | + expect(dir('path/!(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 44 | + expect(dir('path/?(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 45 | + expect(dir('path/+(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 46 | + expect(dir('path/*(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 47 | + expect(dir('path/@(to|from)')).toMatchInlineSnapshot(`"path"`) |
| 48 | + expect(dir('path/!/foo')).toMatchInlineSnapshot(`"path/!/foo"`) |
| 49 | + expect(dir('path/?/foo')).toMatchInlineSnapshot(`"path"`) |
| 50 | + expect(dir('path/+/foo')).toMatchInlineSnapshot(`"path/+/foo"`) |
| 51 | + expect(dir('path/*/foo')).toMatchInlineSnapshot(`"path"`) |
| 52 | + expect(dir('path/@/foo')).toMatchInlineSnapshot(`"path/@/foo"`) |
| 53 | + expect(dir('path/!/foo/')).toMatchInlineSnapshot(`"path/!/foo/"`) |
| 54 | + expect(dir('path/?/foo/')).toMatchInlineSnapshot(`"path"`) |
| 55 | + expect(dir('path/+/foo/')).toMatchInlineSnapshot(`"path/+/foo/"`) |
| 56 | + expect(dir('path/*/foo/')).toMatchInlineSnapshot(`"path"`) |
| 57 | + expect(dir('path/@/foo/')).toMatchInlineSnapshot(`"path/@/foo/"`) |
| 58 | + expect(dir('path/**/*')).toMatchInlineSnapshot(`"path"`) |
| 59 | + expect(dir('path/**/subdir/foo.*')).toMatchInlineSnapshot(`"path"`) |
| 60 | + expect(dir('path/subdir/**/foo.js')).toMatchInlineSnapshot(`"path/subdir"`) |
| 61 | + expect(dir('path/!subdir/foo.js')).toMatchInlineSnapshot(`"path/!subdir/foo.js"`) |
| 62 | + expect(dir('path/{foo,bar}/')).toMatchInlineSnapshot(`"path"`) |
| 63 | + expect(dir('path')).toMatchInlineSnapshot(`"path"`) |
| 64 | + expect(dir('path/foo')).toMatchInlineSnapshot(`"path/foo"`) |
| 65 | + expect(dir('path/foo/')).toMatchInlineSnapshot(`"path/foo/"`) |
| 66 | + expect(dir('path/foo/bar.js')).toMatchInlineSnapshot(`"path/foo/bar.js"`) |
| 67 | + }) |
| 68 | + |
| 69 | + test('handle escaped characters', () => { |
| 70 | + expect(dir('path/\\*\\*/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*\\*/subdir"`) |
| 71 | + expect(dir('path/\\[\\*\\]/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\[\\*\\]/subdir"`) |
| 72 | + expect(dir('path/\\*(a|b)/subdir/foo.*')).toMatchInlineSnapshot(`"path"`) |
| 73 | + expect(dir('path/\\*/(a|b)/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*"`) |
| 74 | + expect(dir('path/\\*\\(a\\|b\\)/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\*\\(a\\|b\\)/subdir"`) |
| 75 | + expect(dir('path/\\[foo bar\\]/subdir/foo.*')).toMatchInlineSnapshot(`"path/\\[foo bar\\]/subdir"`) |
| 76 | + expect(dir('path/\\[bar]/')).toMatchInlineSnapshot(`"path/\\[bar]/"`) |
| 77 | + expect(dir('path/\\[bar]')).toMatchInlineSnapshot(`"path/\\[bar]"`) |
| 78 | + expect(dir('[bar]')).toMatchInlineSnapshot(`"."`) |
| 79 | + expect(dir('[bar]/')).toMatchInlineSnapshot(`"."`) |
| 80 | + expect(dir('./\\[bar]')).toMatchInlineSnapshot(`"./\\[bar]"`) |
| 81 | + expect(dir('\\[bar]/')).toMatchInlineSnapshot(`"\\[bar]/"`) |
| 82 | + expect(dir('\\!dir/*')).toMatchInlineSnapshot(`"\\!dir"`) |
| 83 | + expect(dir('[bar\\]/')).toMatchInlineSnapshot(`"[bar\\]/"`) |
| 84 | + expect(dir('path/foo \\[bar]/')).toMatchInlineSnapshot(`"path/foo \\[bar]/"`) |
| 85 | + expect(dir('path/\\{foo,bar}/')).toMatchInlineSnapshot(`"path/\\{foo,bar}/"`) |
| 86 | + expect(dir('\\{foo,bar}/')).toMatchInlineSnapshot(`"\\{foo,bar}/"`) |
| 87 | + expect(dir('\\{foo,bar\\}/')).toMatchInlineSnapshot(`"\\{foo,bar\\}/"`) |
| 88 | + expect(dir('{foo,bar\\}/')).toMatchInlineSnapshot(`"."`) |
| 89 | + }) |
| 90 | + |
| 91 | + test('should handle glob patterns with braces', () => { |
| 92 | + expect(dir('js/t(wo|est)/*.js')).toMatchInlineSnapshot(`"js"`) |
| 93 | + expect(dir('js/t/(wo|est)/*.js')).toMatchInlineSnapshot(`"js/t"`) |
| 94 | + expect(dir('lib/{components,pages}/**/{test,another}/*.txt')).toMatchInlineSnapshot(`"lib"`) |
| 95 | + expect(dir('js/test{0..9}/*.js')).toMatchInlineSnapshot(`"js"`) |
| 96 | + expect(dir('js/t[a-z]st}/*.js')).toMatchInlineSnapshot(`"js"`) |
| 97 | + expect(dir('path/{../,./,{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot(`"path"`) |
| 98 | + expect(dir('path/{../,./,\\{bar,/baz},qux}/')).toMatchInlineSnapshot(`"path"`) |
| 99 | + expect(dir('path/\\{../,./,\\{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot( |
| 100 | + `"path/\\{../,./,\\{bar,/baz\\},qux\\}/"`, |
| 101 | + ) |
| 102 | + expect(dir('{../,./,{bar,/baz\\},qux\\}/')).toMatchInlineSnapshot(`"."`) |
| 103 | + expect(dir('{../,./,{bar,/baz\\},qux\\}')).toMatchInlineSnapshot(`"."`) |
| 104 | + expect(dir('path/{,/,bar/{baz,qux\\}}/')).toMatchInlineSnapshot(`"path"`) |
| 105 | + expect(dir('path/{,/,bar/{baz,qux}\\}/')).toMatchInlineSnapshot(`"path"`) |
| 106 | + }) |
| 107 | +}) |
0 commit comments