Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional "caseInsensitivity" for volume - to reproduce windows behaviour #973

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"demo:webfs": "webpack serve --config ./src/webfs/webpack.config.js",
"prettier": "prettier --ignore-path .gitignore --write \"src/**/*.{ts,js}\"",
"prettier:diff": "prettier -l \"src/**/*.{ts,js}\"",
"test": "jest --maxWorkers 2",
"test": "jest",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch",
"tslint": "tslint \"src/**/*.ts\" -t verbose",
Expand Down
60 changes: 44 additions & 16 deletions src/__tests__/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ describe('volume', () => {
});
});
describe('.symlink(target, path[, type], callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.realpathSync(path[, options])', () => {
const vol = new Volume();
Expand Down Expand Up @@ -899,7 +899,7 @@ describe('volume', () => {
});
});
describe('.lstat(path, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.statSync(path)', () => {
const vol = new Volume();
Expand Down Expand Up @@ -944,7 +944,7 @@ describe('volume', () => {
});
});
describe('.stat(path, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.fstatSync(fd)', () => {
const vol = new Volume();
Expand Down Expand Up @@ -992,7 +992,7 @@ describe('volume', () => {
});
});
describe('.fstat(fd, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.linkSync(existingPath, newPath)', () => {
const vol = new Volume();
Expand All @@ -1012,7 +1012,7 @@ describe('volume', () => {
});
});
describe('.link(existingPath, newPath, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.readdirSync(path)', () => {
it('Returns simple list', () => {
Expand All @@ -1039,7 +1039,7 @@ describe('volume', () => {
});
});
describe('.readdir(path, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.readlinkSync(path[, options])', () => {
it('Simple symbolic link to one file', () => {
Expand Down Expand Up @@ -1087,7 +1087,7 @@ describe('volume', () => {
});
});
describe('.ftruncate(fd[, len], callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.truncateSync(path[, len])', () => {
const vol = new Volume();
Expand All @@ -1114,7 +1114,7 @@ describe('volume', () => {
});
});
describe('.truncate(path[, len], callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.utimesSync(path, atime, mtime)', () => {
const vol = new Volume();
Expand All @@ -1134,7 +1134,7 @@ describe('volume', () => {
});
});
describe('.utimes(path, atime, mtime, callback)', () => {
xit('...', () => {});
xit('...', () => { });
});
describe('.mkdirSync(path[, options])', () => {
it('Create dir at root', () => {
Expand Down Expand Up @@ -1180,8 +1180,8 @@ describe('volume', () => {
});
});
describe('.mkdir(path[, mode], callback)', () => {
xit('...', () => {});
xit('Create /dir1/dir2/dir3', () => {});
xit('...', () => { });
xit('Create /dir1/dir2/dir3', () => { });
});
describe('.mkdtempSync(prefix[, options])', () => {
it('Create temp dir at root', () => {
Expand All @@ -1200,14 +1200,14 @@ describe('volume', () => {
});
});
describe('.mkdtemp(prefix[, options], callback)', () => {
xit('Create temp dir at root', () => {});
xit('Create temp dir at root', () => { });
it('throws when prefix is not a string', () => {
const vol = new Volume();
expect(() => vol.mkdtemp({} as string, () => {})).toThrow(TypeError);
expect(() => vol.mkdtemp({} as string, () => { })).toThrow(TypeError);
});
it('throws when prefix contains null bytes', () => {
const vol = new Volume();
expect(() => vol.mkdtemp('/tmp-\u0000', () => {})).toThrow(/path.+string.+null bytes/i);
expect(() => vol.mkdtemp('/tmp-\u0000', () => { })).toThrow(/path.+string.+null bytes/i);
});
});
describe('.rmdirSync(path)', () => {
Expand All @@ -1226,7 +1226,7 @@ describe('volume', () => {
});
});
describe('.rmdir(path, callback)', () => {
xit('Remove single dir', () => {});
xit('Remove single dir', () => { });
it('Async remove dir /dir1/dir2/dir3 recursively', done => {
const vol = new Volume();
vol.mkdirSync('/dir1/dir2/dir3', { recursive: true });
Expand Down Expand Up @@ -1370,7 +1370,7 @@ describe('volume', () => {
vol.writeFileSync('/lol.txt', '2');
}, 1);
});
xit('Multiple listeners for one file', () => {});
xit('Multiple listeners for one file', () => { });
});
describe('.unwatchFile(path[, listener])', () => {
it('Stops watching before .writeFile', done => {
Expand Down Expand Up @@ -1421,4 +1421,32 @@ describe('volume', () => {
expect(new StatWatcher(vol).vol).toBe(vol);
});
});
describe('caseSensitive: false', () => {
it('is case insensitive', () => {
const vol = new Volume(undefined, false);
vol.mkdirSync('/dir');
vol.writeFileSync('/Dir/Foo.txt', 'foo');
expect(vol.readFileSync('/dir/foo.TXT', { encoding: 'utf8' })).toBe('foo');
vol.unlinkSync('/dir/foo.txt');
expect(vol.existsSync('/Dir/Foo.txt')).toBe(false)
});
it('preserve original filename casing', () => {
const vol = new Volume(undefined, false);
vol.mkdirSync('/dir/lchild', { recursive: true });
vol.mkdirSync('/Dir/UCHILD', { recursive: true });

expect(vol.readdirSync('/', { encoding: 'utf8' })).toEqual(['dir']);
expect(vol.readdirSync('/dIr/lChIlD', { encoding: 'utf8' })).toEqual([]);
expect(vol.readdirSync('/DiR/uChIld', { encoding: 'utf8' })).toEqual([]);

vol.writeFileSync('/DiR/lChIlD/foo.txt', 'lfoo');
vol.writeFileSync('/dIr/uChIlD/FOO.txt', 'UFOO');

expect(vol.readdirSync('/dIr/lChIlD', { encoding: 'utf8' })).toEqual(["foo.txt"]);
expect(vol.readdirSync('/DiR/uChIld', { encoding: 'utf8' })).toEqual(["FOO.txt"]);

expect(vol.readFileSync('/DiR/lChIlD/FoO.TXT', { encoding: 'utf8' })).toBe('lfoo');
expect(vol.readFileSync('/dIr/UcHiLd/fOo.TXT', { encoding: 'utf8' })).toBe('UFOO');
});
});
});
9 changes: 6 additions & 3 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class Link extends EventEmitter {
}

setChild(name: string, link: Link = new Link(this.vol, this, name)): Link {
this.children.set(name, link);
this.children.set(this.vol.caseSensitive ? name : name.toLowerCase(), link)
link.parent = this;
this.length++;

Expand All @@ -393,16 +393,19 @@ export class Link extends EventEmitter {
link.children.delete('..');
this.getNode().nlink--;
}
this.children.delete(link.getName());
let name = link.getName();

this.children.delete(this.vol.caseSensitive ? name : name.toLowerCase());
this.length--;

this.getNode().mtime = new Date();
this.emit('child:delete', link, this);
}

getChild(name: string): Link | undefined {
const tmp = !this.vol.caseSensitive ? name = name.toLowerCase() : name;
this.getNode().mtime = new Date();
return this.children.get(name);
return this.children.get(tmp);
}

getPath(): string {
Expand Down
16 changes: 10 additions & 6 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export type TFlagsCopy =
// ---------------------------------------- Options

// Options for `fs.appendFile` and `fs.appendFileSync`
export interface IAppendFileOptions extends opts.IFileOptions {}
export interface IAppendFileOptions extends opts.IFileOptions { }

// Options for `fs.watchFile`
export interface IWatchFileOptions {
Expand Down Expand Up @@ -284,6 +284,8 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
// Current number of open files.
openFiles = 0;

caseSensitive: boolean;

StatWatcher: new () => StatWatcher;
ReadStream: new (...args) => misc.IReadStream;
WriteStream: new (...args) => IWriteStream;
Expand All @@ -302,9 +304,10 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
return this.promisesApi;
}

constructor(props = {}) {
constructor(props = {}, caseSensitive = true) {
this.props = Object.assign({ Node, Link, File }, props);

this.caseSensitive = caseSensitive;
const root = this.createLink();
root.setNode(this.createNode(true));

Expand Down Expand Up @@ -1483,11 +1486,12 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
}

const list: TDataOut[] = [];
for (const name of link.children.keys()) {
if (name === '.' || name === '..') {
for (const [childName, childLink] of link.children.entries()) {
if (childLink === undefined || childName === '.' || childName === '..') {
continue;
}
list.push(strToEncoding(name, options.encoding));

list.push(strToEncoding(this.caseSensitive ? childName : childLink.getName(), options.encoding));
}

if (!isWin && options.encoding !== 'buffer') list.sort();
Expand Down Expand Up @@ -2243,7 +2247,7 @@ export interface IWriteStream extends Writable {
bytesWritten: number;
path: string;
pending: boolean;
new (path: PathLike, options: opts.IWriteStreamOptions);
new(path: PathLike, options: opts.IWriteStreamOptions);
open();
close();
}
Expand Down
Loading