find files
This commit is contained in:
61
test/glob.test.js
Normal file
61
test/glob.test.js
Normal file
@@ -0,0 +1,61 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Tests for the glob helpers in src/glob.js, shared by ripgrep and find_files.
|
||||
*
|
||||
* Run with: node --test
|
||||
* (or) node --test test/glob.test.js
|
||||
*/
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { escapeRegExp, globToRegExp, makeGlobMatcher } from '../src/glob.js';
|
||||
|
||||
test('escapeRegExp: escapes regex metacharacters', () => {
|
||||
assert.equal(escapeRegExp('a.b*c'), 'a\\.b\\*c');
|
||||
assert.ok(new RegExp(escapeRegExp('a.b')).test('a.b'));
|
||||
assert.ok(!new RegExp(escapeRegExp('a.b')).test('axb'));
|
||||
});
|
||||
|
||||
test('globToRegExp: * stays within a path segment', () => {
|
||||
const rx = globToRegExp('*.js');
|
||||
assert.ok(rx.test('a.js'));
|
||||
assert.ok(!rx.test('src/a.js'));
|
||||
});
|
||||
|
||||
test('globToRegExp: ** crosses path separators', () => {
|
||||
const rx = globToRegExp('src/**/*.js');
|
||||
assert.ok(rx.test('src/a/b/c.js'));
|
||||
// "**/" also matches zero directories.
|
||||
assert.ok(rx.test('src/a.js'));
|
||||
});
|
||||
|
||||
test('globToRegExp: ? matches a single non-slash char', () => {
|
||||
const rx = globToRegExp('a?c');
|
||||
assert.ok(rx.test('abc'));
|
||||
assert.ok(!rx.test('ac'));
|
||||
assert.ok(!rx.test('a/c'));
|
||||
});
|
||||
|
||||
test('makeGlobMatcher: slashless glob also matches the basename', () => {
|
||||
const m = makeGlobMatcher('*.js');
|
||||
assert.ok(m('src/a.js', 'a.js'));
|
||||
assert.ok(!m('src/a.ts', 'a.ts'));
|
||||
});
|
||||
|
||||
test('makeGlobMatcher: glob with a slash matches the whole path', () => {
|
||||
const m = makeGlobMatcher('src/**/*.js');
|
||||
assert.ok(m('src/tools/a.js', 'a.js'));
|
||||
assert.ok(!m('test/a.js', 'a.js'));
|
||||
});
|
||||
|
||||
test('makeGlobMatcher: ignores a trailing slash on directories', () => {
|
||||
const m = makeGlobMatcher('node_modules');
|
||||
assert.ok(m('node_modules/', 'node_modules'));
|
||||
});
|
||||
|
||||
test('makeGlobMatcher: ignoreCase makes matching case-insensitive', () => {
|
||||
assert.ok(makeGlobMatcher('*.JS', { ignoreCase: true })('a.js', 'a.js'));
|
||||
assert.ok(!makeGlobMatcher('*.JS')('a.js', 'a.js'));
|
||||
});
|
||||
Reference in New Issue
Block a user