diff --git a/mock-fs/mock-fs-tests.ts b/mock-fs/mock-fs-tests.ts new file mode 100644 index 0000000000..3dd9f01210 --- /dev/null +++ b/mock-fs/mock-fs-tests.ts @@ -0,0 +1,79 @@ +/// + +var mock = require('mock-fs'); + +function a() { + mock({ + 'path/to/fake/dir': { + 'some-file.txt': 'file content here', + 'empty-dir': {/** empty directory */} + }, + 'path/to/some.png': new Buffer([8, 6, 7, 5, 3, 0, 9]), + 'some/other/path': {/** another empty directory */} + }); + + // after a test runs + mock.restore(); +} + +function b() { + mock({ + 'path/to/file.txt': 'file content here' + }); +} + +function c() { + mock({ + foo: mock.file({ + content: 'file content here', + ctime: new Date(1), + mtime: new Date(1) + }) + }); +} + +function d() { + // note that this could also be written as + // mock({'path/to/dir': { /** config */ }}) + mock({ + path: { + to: { + dir: { + file1: 'text content', + file2: new Buffer([1, 2, 3, 4]) + } + } + } + }); +} + +function e() { + mock({ + 'some/dir': mock.directory({ + mode: 0755, + items: { + file1: 'file one content', + file2: new Buffer([8, 6, 7, 5, 3, 0, 9]) + } + }) + }); +} + +function f() { + mock({ + 'some/dir': { + 'regular-file': 'file contents', + 'a-symlink': mock.symlink({ + path: 'regular-file' + }) + } + }); +} + +var mockedFS = mock.fs({ + '/file': 'blah' +}); + +if (mockedFS.readFileSync('/file', { encoding: 'utf8' }) === 'blah') { + console.log('woo'); +} diff --git a/mock-fs/mock-fs.d.ts b/mock-fs/mock-fs.d.ts new file mode 100644 index 0000000000..539e74e869 --- /dev/null +++ b/mock-fs/mock-fs.d.ts @@ -0,0 +1,51 @@ +// Type definitions for mock-fs 2.5.0 +// Project: https://github.com/tschaub/mock-fs +// Definitions by: Wim Looman +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "mock-fs" { + import fs = require("fs"); + + function mock(config?: mock.Config): void; + + module mock { + function file(config: FileConfig): File; + function directory(config: DirectoryConfig): Directory; + function symlink(config: SymlinkConfig): Symlink; + + function restore(): void; + + function fs(config?: Config): typeof fs; + + interface Config { + [path: string]: string | Buffer | File | Directory | Symlink | Config; + } + + interface CommonConfig { + mode?: number; + uid?: number; + git?: number; + atime?: Date; + ctime?: Date; + mtime?: Date; + } + + interface FileConfig extends CommonConfig { + content: string | Buffer; + } + interface DirectoryConfig extends CommonConfig { + items: Config; + } + interface SymlinkConfig extends CommonConfig { + path: string; + } + + class File { private _file: any; } + class Directory { private _directory: any; } + class Symlink { private _symlink: any; } + } + + export = mock; +}