Skip to content

Commit a953d7a

Browse files
committed
reorganize tests
1 parent d143cbc commit a953d7a

File tree

4 files changed

+93
-77
lines changed

4 files changed

+93
-77
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
7+
const samples = [
8+
['file*.txt', 'file[*].txt'],
9+
['file?.txt', 'file[?].txt'],
10+
['file[abc].txt', 'file[[]abc[]].txt'],
11+
['folder/**/*.txt', 'folder/[*][*]/[*].txt'],
12+
['file{1,2}.txt', 'file[{]1,2[}].txt'],
13+
['file[0-9]?.txt', 'file[[]0-9[]][?].txt'],
14+
['C:\\Users\\*.txt', 'C:\\Users\\[*].txt'],
15+
['?[]', '[?][[][]]'],
16+
];
17+
18+
for (const [pattern, expected] of samples) {
19+
const actual = path.escapeGlob(pattern);
20+
assert.strictEqual(actual, expected, `Expected ${pattern} to be escaped as ${expected}, got ${actual} instead`);
21+
}
22+
23+
// Test for non-string input
24+
assert.throws(() => path.escapeGlob(123), /.*must be of type string.*/);

test/parallel/test-path-glob.js

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
7+
const globs = {
8+
win32: [
9+
['foo\\bar\\baz', 'foo\\[bcr]ar\\baz', true], // Matches 'bar' or 'car' in 'foo\\bar'
10+
['foo\\bar\\baz', 'foo\\[!bcr]ar\\baz', false], // Matches anything except 'bar' or 'car' in 'foo\\bar'
11+
['foo\\bar\\baz', 'foo\\[bc-r]ar\\baz', true], // Matches 'bar' or 'car' using range in 'foo\\bar'
12+
['foo\\bar\\baz', 'foo\\*\\!bar\\*\\baz', false], // Matches anything with 'foo' and 'baz' but not 'bar'
13+
// in between
14+
['foo\\bar1\\baz', 'foo\\bar[0-9]\\baz', true], // Matches 'bar' followed by any digit in 'foo\\bar1'
15+
['foo\\bar5\\baz', 'foo\\bar[0-9]\\baz', true], // Matches 'bar' followed by any digit in 'foo\\bar5'
16+
['foo\\barx\\baz', 'foo\\bar[a-z]\\baz', true], // Matches 'bar' followed by any lowercase letter in 'foo\\barx'
17+
['foo\\bar\\baz\\boo', 'foo\\[bc-r]ar\\baz\\*', true], // Matches 'bar' or 'car' in 'foo\\bar'
18+
['foo\\bar\\baz', 'foo/**', true], // Matches anything in 'foo'
19+
['foo\\bar\\baz', '*', false], // No match
20+
],
21+
posix: [
22+
['foo/bar/baz', 'foo/[bcr]ar/baz', true], // Matches 'bar' or 'car' in 'foo/bar'
23+
['foo/bar/baz', 'foo/[!bcr]ar/baz', false], // Matches anything except 'bar' or 'car' in 'foo/bar'
24+
['foo/bar/baz', 'foo/[bc-r]ar/baz', true], // Matches 'bar' or 'car' using range in 'foo/bar'
25+
['foo/bar/baz', 'foo/*/!bar/*/baz', false], // Matches anything with 'foo' and 'baz' but not 'bar' in between
26+
['foo/bar1/baz', 'foo/bar[0-9]/baz', true], // Matches 'bar' followed by any digit in 'foo/bar1'
27+
['foo/bar5/baz', 'foo/bar[0-9]/baz', true], // Matches 'bar' followed by any digit in 'foo/bar5'
28+
['foo/barx/baz', 'foo/bar[a-z]/baz', true], // Matches 'bar' followed by any lowercase letter in 'foo/barx'
29+
['foo/bar/baz/boo', 'foo/[bc-r]ar/baz/*', true], // Matches 'bar' or 'car' in 'foo/bar'
30+
['foo/bar/baz', 'foo/**', true], // Matches anything in 'foo'
31+
['foo/bar/baz', '*', false], // No match
32+
],
33+
};
34+
35+
36+
for (const [platform, platformGlobs] of Object.entries(globs)) {
37+
for (const [pathStr, glob, expected] of platformGlobs) {
38+
const actual = path[platform].matchesGlob(pathStr, glob);
39+
assert.strictEqual(actual, expected, `Expected ${pathStr} to ` + (expected ? '' : 'not ') + `match ${glob} on ${platform}`);
40+
}
41+
}
42+
43+
// Test for non-string input
44+
assert.throws(() => path.matchesGlob(123, 'foo/bar/baz'), /.*must be of type string.*/);
45+
assert.throws(() => path.matchesGlob('foo/bar/baz', 123), /.*must be of type string.*/);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('assert');
5+
const path = require('path');
6+
7+
const samples = [
8+
['file*.txt', 'file[*].txt'],
9+
['file?.txt', 'file[?].txt'],
10+
['file[abc].txt', 'file[[]abc[]].txt'],
11+
['folder/**/*.txt', 'folder/[*][*]/[*].txt'],
12+
['file{1,2}.txt', 'file[{]1,2[}].txt'],
13+
['file[0-9]?.txt', 'file[[]0-9[]][?].txt'],
14+
['C:\\Users\\*.txt', 'C:\\Users\\[*].txt'],
15+
['?[]', '[?][[][]]'],
16+
];
17+
18+
for (const [expected, escapedPattern] of samples) {
19+
const actual = path.unescapeGlob(escapedPattern);
20+
assert.strictEqual(actual, expected, `Expected ${escapedPattern} to be unescaped as ${expected}, got ${actual} instead`);
21+
}
22+
23+
// Test for non-string input
24+
assert.throws(() => path.unescapeGlob(123), /.*must be of type string.*/);

0 commit comments

Comments
 (0)