Upstream mrmlnc/fast-glob state of the touched files before 0754373f47
This commit is contained in:
commit
ed3aca1ad6
3 files changed
+528
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import * as assert from 'node:assert';
|
||||
import * as path from 'node:path';
|
||||
import * as process from 'node:process';
|
||||
import { Stats, StatsMode } from '@nodelib/fs.macchiato';
|
||||
import { describe, it } from 'mocha';
|
||||
import Settings, { type Options } from '../settings.js';
|
||||
import * as tests from '../tests/index.js';
|
||||
import type {
|
||||
Entry,
|
||||
ErrnoException,
|
||||
FsStats,
|
||||
Pattern,
|
||||
} from '../types/index.js';
|
||||
import { Reader } from './reader.js';
|
||||
|
||||
class TestReader extends Reader<never[]> {
|
||||
constructor(options?: Options) {
|
||||
super(new Settings(options));
|
||||
}
|
||||
|
||||
public dynamic(): never[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
public static(): never[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
public getFullEntryPath(filepath: string): string {
|
||||
return this._getFullEntryPath(filepath);
|
||||
}
|
||||
|
||||
public makeEntry(stats: FsStats, pattern: Pattern): Entry {
|
||||
return this._makeEntry(stats, pattern);
|
||||
}
|
||||
|
||||
public isFatalError(error: ErrnoException): boolean {
|
||||
return this._isFatalError(error);
|
||||
}
|
||||
}
|
||||
|
||||
function getReader(options?: Options): TestReader {
|
||||
return new TestReader(options);
|
||||
}
|
||||
|
||||
describe('Readers → Reader', () => {
|
||||
describe('Constructor', () => {
|
||||
it('should create instance of class', () => {
|
||||
const reader = getReader();
|
||||
|
||||
assert.ok(reader instanceof TestReader);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.getFullEntryPath', () => {
|
||||
it('should return path to entry', () => {
|
||||
const reader = getReader();
|
||||
|
||||
const expected = path.join(process.cwd(), 'config.json');
|
||||
|
||||
const actual = reader.getFullEntryPath('config.json');
|
||||
|
||||
assert.strictEqual(actual, expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.makeEntry', () => {
|
||||
it('should return created entry', () => {
|
||||
const reader = getReader();
|
||||
const pattern = 'config.json';
|
||||
const stats = new Stats({ mode: StatsMode.File });
|
||||
|
||||
const actual = reader.makeEntry(stats, pattern);
|
||||
|
||||
assert.strictEqual(actual.name, pattern);
|
||||
assert.strictEqual(actual.path, pattern);
|
||||
assert.ok(actual.dirent.isFile());
|
||||
});
|
||||
|
||||
it('should return created entry with fs.Stats', () => {
|
||||
const reader = getReader({ stats: true });
|
||||
const pattern = 'config.json';
|
||||
|
||||
const actual = reader.makeEntry(new Stats(), pattern);
|
||||
|
||||
assert.ok(actual.stats);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.isFatalError', () => {
|
||||
it('should return false for ENOENT error', () => {
|
||||
const reader = getReader();
|
||||
|
||||
assert.ok(!reader.isFatalError(tests.errno.getEnoent()));
|
||||
});
|
||||
|
||||
it('should return true for EPERM error', () => {
|
||||
const reader = getReader();
|
||||
|
||||
assert.ok(reader.isFatalError(tests.errno.getEperm()));
|
||||
});
|
||||
|
||||
it('should return true for ENOENT error when the `errorFilter` option returns false', () => {
|
||||
const reader = getReader({ errorFilter: () => false });
|
||||
|
||||
assert.ok(reader.isFatalError(tests.errno.getEnoent()));
|
||||
});
|
||||
|
||||
it('should return false for EPERM error when the `errorFilter` option returns true', () => {
|
||||
const reader = getReader({ errorFilter: () => true });
|
||||
|
||||
assert.ok(!reader.isFatalError(tests.errno.getEperm()));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import * as path from 'node:path';
|
||||
import * as fsStat from '@nodelib/fs.stat';
|
||||
import * as utils from '../utils/index.js';
|
||||
import type Settings from '../settings.js';
|
||||
import type {
|
||||
Entry,
|
||||
ErrnoException,
|
||||
FsStats,
|
||||
Pattern,
|
||||
ReaderOptions,
|
||||
} from '../types/index.js';
|
||||
|
||||
export abstract class Reader<T> {
|
||||
readonly #settings: Settings;
|
||||
|
||||
protected readonly _fsStatSettings: fsStat.Settings;
|
||||
|
||||
constructor(settings: Settings) {
|
||||
this.#settings = settings;
|
||||
|
||||
this._fsStatSettings = new fsStat.Settings({
|
||||
followSymbolicLink: settings.followSymbolicLinks,
|
||||
fs: settings.fs,
|
||||
throwErrorOnBrokenSymbolicLink: settings.throwErrorOnBrokenSymbolicLink,
|
||||
});
|
||||
}
|
||||
|
||||
public abstract dynamic(root: string, options: ReaderOptions): T;
|
||||
public abstract static(patterns: Pattern[], options: ReaderOptions): T;
|
||||
|
||||
protected _getFullEntryPath(filepath: string): string {
|
||||
return path.resolve(this.#settings.cwd, filepath);
|
||||
}
|
||||
|
||||
protected _makeEntry(stats: FsStats, pattern: Pattern): Entry {
|
||||
const entry: Entry = {
|
||||
name: pattern,
|
||||
path: pattern,
|
||||
dirent: utils.fs.createDirentFromStats(pattern, stats),
|
||||
};
|
||||
|
||||
if (this.#settings.stats) {
|
||||
entry.stats = stats;
|
||||
}
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
protected _isFatalError(error: ErrnoException): boolean {
|
||||
if (this.#settings.errorFilter !== undefined) {
|
||||
return !this.#settings.errorFilter(error);
|
||||
}
|
||||
|
||||
return !utils.errno.isEnoentCodeError(error) && !utils.errno.isEnotdirCodeError(error);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user