Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mockfs

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mockfs

Mocking FS module implementation for testing purpouses

  • 0.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-99.73%
Maintainers
1
Weekly downloads
 
Created
Source

mockfs

Build Status Coverage Status NPM version Dependency Status

MockFS - Mocking FS module implementation for testing purpouses.

Basic idea is to declare file system contents via JSON spec, mount it, and use through real fs.* functions like an ordinary one.

/**
 * /
 *  file-buffer
 *  file-base64
 *  file-string
 *  file-alt
 *  dir/
 *      file-in-dir
 */
var fs = require('fs'),
    MockFS = require('mockfs'),
    spec, mfs, fd;

spec = {
  time: 'Tue May 07 2013 17:09:57 GMT+0400' // global default time for any FS item, optional
  ctime: new Date(),                        // creation time default, optional
/*atime: ...,                               // atime and ctime is not set
  mtime: ...,*/                             // value is taken from FS defaults (time)
  items: {
    'file-buffer': new Buffer('qwerty'),    // specify content as Buffer
    'file-base64': new Buffer('cXdlcnR5', 'base64'), // buffer with encoding
    'file-string': 'qwerty',                // or as string
    'file-alt': {                           // alternative syntax
      uid: 'johndoe',                       // owner user, as login name or id
      gid: 300,                             // owner group
      mode: 0766,                           // access mode
      atime: new Date(),                    // Date instance
      mtime: 1000255364,                    // timestamp
      ctime: "-500"                         // number with a sign (+/-) - delta from fs default value
      content: 'asobject'                   // file content
    },
    'dir': {                                // directory - always an object with items property (which is object too)
      atime: 'Tue May 07 2013 17:09:57 GMT+0400' // Date as string
      mtime: "+500",                        // stats, uid, gid, mode - on directories too
    /*ctime*/                               // ctime is not set, value taken from FS defaults  
      items: {                              // directory contents
        'file-in-dir': 'inside directory'             
      }
    }
};

mfs = new MockFS(spec);
mfs.mount('/mnt/mock');

fs.existsSync('/mnt/mock/file-buffer'); // true
fs.readFileSync('/mnt/mock/file-string').toString(); // "qwerty"
fs.readFile('/mnt/mock/dir/file-in-dir', function(e, r){
    Buffer.isBuffer(r); // true
    r.toString(); // "inside directory"
});

// file descriptors is also works
fs.open('/mnt/mock/file-base64', 'r', function(e, fd){
    if(fd) {
        var buf = new Buffer(100);
        fs.read(fd, buf, 0, 100, null, function(e, bytesRead){
            console.log(bytesRead); // 6
            console.log(buf.toString('utf8', 0, bytesRead)); // qwerty
            fs.closeSync(fd);
        });    
    }
});

mfs.umount('/mnt/mock');

fs.existsSync('/mnt/mock/file-buffer'); // false

Implemented by wrapping bundled fs module's basic functions (file descriptors handling, stat, rename/delete files/directories). So, functions as createReadStream, appendFileSync and so are supported "out of the box" without any wrapping.

Currently, NodeJS v0.8+ is supported.

TODO

  • Support legacy interfaces
  • Access rights check (read/write/search permissions)
  • *chown(Sync), *chmod(Sync) functions
  • Links support
  • (un)watch(File) support
  • mirror utility to create MockFS specs from real file systems
  • Integrate with some date parsing library for convinient atime/ctime/mtime specification

Roadmap

v0.2

  • Access rights check (read/write/search permissions)
  • *chown(Sync), *chmod(Sync) functions

v0.3

  • Links support
  • (un)watch(File) support

v1.0

  • Support legacy interfaces
  • Pass NodeJS test suite

Similar libraries

Keywords

FAQs

Package last updated on 15 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc