Socket
Socket
Sign inDemoInstall

fs-mock

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.1 to 1.1.0

lib/Helpers.js

85

lib/fs.js
// Generated by CoffeeScript 1.6.3
(function() {
var Errors, FSWatcher, Readable, Stats, Writable, escape, fs, isAppendable, isCreatable, isFunction, isReadable, isWritable, toDate, _path;
var Errors, FSWatcher, Helpers, Readable, Stats, Writable, escape, fs, isAppendable, isCreatable, isFunction, isReadable, isWritable, toDate;

@@ -11,6 +11,6 @@ Stats = require('./Stats');

Helpers = require('./Helpers');
escape = require('escape-regexp');
_path = require('path');
Readable = require('stream').Readable;

@@ -51,2 +51,14 @@

fs = (function() {
fs.DELIMITER = {
posix: '/',
windows: '\\'
};
fs.ROOT_DIRECTORY = {
posix: fs.DELIMITER.posix,
windows: 'c:'
};
fs.prototype._options = null;
fs.prototype._data = null;

@@ -58,13 +70,43 @@

function fs(tree, info) {
function fs(tree, options) {
var drive, _i, _len, _ref;
if (tree == null) {
tree = {};
}
if (info == null) {
info = {};
if (options == null) {
options = {};
}
this._data = {};
this._fileDescriptors = [];
this._addPath('/', {});
this._setTree(tree, info);
if (typeof options.windows === 'undefined') {
options.windows = false;
}
if (typeof options.root === 'undefined') {
options.root = (options.windows ? fs.ROOT_DIRECTORY.windows : fs.ROOT_DIRECTORY.posix);
}
if (typeof options.drives === 'undefined') {
options.drives = [];
}
if (options.root) {
options.root = Helpers.normalizePath(options.windows, options.root);
if (options.windows) {
options.root = Helpers.normalizeDriveWindows(options.root);
}
options._root = escape(options.root);
}
options.delimiter = (options.windows ? fs.DELIMITER.windows : fs.DELIMITER.posix);
options._delimiter = escape(options.delimiter);
if (!options.windows && options.drives.length > 0) {
throw new Error('Options drive can be used only with windows options.');
}
this._options = options;
if (options.root) {
this._addPath(options.root, null, null, true);
}
_ref = options.drives;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
drive = _ref[_i];
this._addPath(Helpers.normalizeDriveWindows(drive), null, null, true);
}
this._setTree(tree, {});
}

@@ -95,3 +137,3 @@

fs.prototype._addPath = function(path, data, info) {
fs.prototype._addPath = function(path, data, info, root) {
var item, stats, subData, subPath, type, _ref, _results;

@@ -104,2 +146,5 @@ if (data == null) {

}
if (root == null) {
root = false;
}
if (typeof info.stats === 'undefined') {

@@ -139,3 +184,5 @@ info.stats = {};

}
path = _path.join('/', path);
if (!root && this._options.root && path.match(new RegExp('^' + this._options._root)) === null) {
path = Helpers.joinPaths(this._options.windows, this._options.root, path);
}
stats = new Stats(path, info.stats);

@@ -154,3 +201,3 @@ stats.mode = info.mode;

subData = _ref[subPath];
_results.push(this._addPath(_path.join(path, subPath), subData));
_results.push(this._addPath(Helpers.joinPaths(this._options.windows, path, subPath), subData));
}

@@ -191,3 +238,3 @@ return _results;

var match, position, sub, _results;
match = path.match(/\//g);
match = path.match(new RegExp(this._options._delimiter, 'g'));
if (match !== null && match.length > 1) {

@@ -197,5 +244,5 @@ sub = path;

while (sub !== null) {
position = sub.lastIndexOf('/');
position = sub.lastIndexOf(this._options.delimiter);
if (position > 0) {
sub = sub.substring(0, sub.lastIndexOf('/'));
sub = sub.substring(0, sub.lastIndexOf(this._options.delimiter));
if (typeof this._data[sub] === 'undefined') {

@@ -232,5 +279,5 @@ _results.push(this._addPath(sub, {}));

if (path[0] === '.') {
path = _path.join('/', path);
path = Helpers.joinPaths(this._options.windows, this._options.delimiter, path);
}
return _path.normalize(path);
return Helpers.normalizePath(this._options.windows, path);
};

@@ -706,3 +753,3 @@

}
path = path === '/' ? '' : path;
path = path === this._options.delimiter ? '' : path;
path = escape(path);

@@ -713,4 +760,4 @@ files = [];

data = _ref[name];
if (name !== path && name !== '/' && (match = name.match(new RegExp('^' + path + '(.+)$'))) !== null) {
slashes = match[1].match(/\//g);
if (name !== path && name !== this._options.delimiter && (match = name.match(new RegExp('^' + path + '(.+)$'))) !== null) {
slashes = match[1].match(new RegExp(this._options._delimiter, 'g'));
slashes = slashes === null ? 0 : slashes.length;

@@ -717,0 +764,0 @@ if (slashes === 1) {

{
"name": "fs-mock",
"description": "Simple fs mock",
"version": "1.0.1",
"description": "Simple fs mock with posix and windows file system styles",
"version": "1.1.0",
"author": {

@@ -12,2 +12,5 @@ "name": "David Kudera",

"file system",
"posix",
"linux",
"windows",
"testing",

@@ -14,0 +17,0 @@ "mock"

@@ -5,5 +5,7 @@ [![NPM version](https://badge.fury.io/js/fs-mock.png)](http://badge.fury.io/js/fs-mock)

[![Donate](http://b.repl.ca/v1/donate-PayPal-brightgreen.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=89MDP3DHWXYPW)
# fs-mock
Mock for fs module
Simple fs mock with posix and windows file system styles.

@@ -43,2 +45,60 @@ ## Installation

## Windows
```
var fs = new FS({
'Users': {
'David': {
'password.txt': 'my super password'
}
}
}, {
windows: true
});
```
This will change delimiter from `/` to `\` and add root directory to `c:`.
### Other drives
```
new FS({ ... }, {
windows: true,
drives: ['c:', 'd:', 'z:']
});
```
### Another root directory
```
new FS({ ... }, {
windows: true,
drives: ['c:', 'd:', 'z:'],
root: 'z:'
});
```
Now every path will be saved into `z:` drive.
If you want to save paths to custom drives, you need to disable auto saving into `options.root`.
```
new FS({
'c:': {
'Users': {}
},
'x:': {
'xampp': {
'htdocs': {}
}
}
}, {
windows: true,
root: false
});
```
**I haven't got any machine with Windows so all methods (like chmod) works just like in Unix systems. Please let me know
if you want to improve this and how.**
## Supported functions

@@ -119,2 +179,6 @@

* 1.1.0
+ Added support for windows file systems
+ Added many tests
* 1.0.1

@@ -121,0 +185,0 @@ + Bug with root directories and readdir method

// Generated by CoffeeScript 1.6.3
(function() {
require('./fs');
require('./fs.posix');
require('./fs.windows');
}).call(this);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc