|
| 1 | +/** Tests for the glob matcher used by Goal Run readonly constraints. */ |
| 2 | +import { describe, it, expect } from 'vitest' |
| 3 | +import { globToRegExp, matchesAnyGlob } from './glob-match.js' |
| 4 | + |
| 5 | +describe('globToRegExp', () => { |
| 6 | + it('matches a directory tree with **', () => { |
| 7 | + const re = globToRegExp('.github/workflows/**') |
| 8 | + expect(re.test('.github/workflows/ci.yml')).toBe(true) |
| 9 | + expect(re.test('.github/workflows/nested/deploy.yml')).toBe(true) |
| 10 | + expect(re.test('.github/other/ci.yml')).toBe(false) |
| 11 | + }) |
| 12 | + |
| 13 | + it('* does not cross path separators', () => { |
| 14 | + const re = globToRegExp('src/*.ts') |
| 15 | + expect(re.test('src/index.ts')).toBe(true) |
| 16 | + expect(re.test('src/sub/index.ts')).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('? matches a single non-slash character', () => { |
| 20 | + const re = globToRegExp('v?.txt') |
| 21 | + expect(re.test('v1.txt')).toBe(true) |
| 22 | + expect(re.test('v12.txt')).toBe(false) |
| 23 | + expect(re.test('v/.txt')).toBe(false) |
| 24 | + }) |
| 25 | + |
| 26 | + it('matches an extension glob', () => { |
| 27 | + const re = globToRegExp('*.lock') |
| 28 | + expect(re.test('yarn.lock')).toBe(true) |
| 29 | + expect(re.test('package-lock.json')).toBe(false) |
| 30 | + }) |
| 31 | + |
| 32 | + it('escapes regex metacharacters in literals', () => { |
| 33 | + const re = globToRegExp('a.b+c') |
| 34 | + expect(re.test('a.b+c')).toBe(true) |
| 35 | + expect(re.test('axbxc')).toBe(false) |
| 36 | + }) |
| 37 | +}) |
| 38 | + |
| 39 | +describe('matchesAnyGlob', () => { |
| 40 | + it('returns true when any pattern matches', () => { |
| 41 | + const globs = ['.github/workflows/**', 'tests/security/**'] |
| 42 | + expect(matchesAnyGlob('tests/security/auth.test.ts', globs)).toBe(true) |
| 43 | + expect(matchesAnyGlob('.github/workflows/ci.yml', globs)).toBe(true) |
| 44 | + expect(matchesAnyGlob('src/index.ts', globs)).toBe(false) |
| 45 | + }) |
| 46 | + |
| 47 | + it('returns false for an empty pattern list', () => { |
| 48 | + expect(matchesAnyGlob('anything', [])).toBe(false) |
| 49 | + }) |
| 50 | +}) |
0 commit comments