Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
The mock-fs npm package allows you to mock the file system in Node.js, enabling you to test code that interacts with the file system without actually performing any real file system operations. This is particularly useful for unit testing and ensuring that your code behaves correctly in different file system scenarios.
Mocking a Simple File System
This feature allows you to create a simple mock file system with directories and files. The `mock` function takes an object that represents the file system structure. After setting up the mock file system, you can run your code that interacts with the file system. Finally, you call `mock.restore()` to clean up and restore the real file system.
const mock = require('mock-fs');
mock({
'path/to/fake/dir': {
'some-file.txt': 'file content here',
'empty-dir': {}
}
});
// Your code that interacts with the file system
mock.restore();
Mocking File System Errors
This feature allows you to simulate file system errors, such as permission errors. In this example, the `mock.file` function is used to create a file with no permissions. When attempting to read the file, an error is thrown, which you can catch and handle in your code.
const mock = require('mock-fs');
const fs = require('fs');
mock({
'path/to/fake/dir': {
'some-file.txt': mock.file({
content: 'file content here',
mode: 0o000 // no permissions
})
}
});
try {
fs.readFileSync('path/to/fake/dir/some-file.txt');
} catch (err) {
console.error('Error reading file:', err);
}
mock.restore();
Mocking a Large File System
This feature allows you to create a large mock file system with many files. The `mock.directory` function is used to create a directory with a large number of files. This is useful for testing how your code handles large file systems.
const mock = require('mock-fs');
mock({
'large-dir': mock.directory({
items: Array.from({ length: 1000 }, (_, i) => ({
[`file-${i}.txt`]: `content of file ${i}`
})).reduce((acc, item) => Object.assign(acc, item), {})
})
});
// Your code that interacts with the large file system
mock.restore();
The memfs package provides an in-memory file system that can be used for testing and other purposes. Unlike mock-fs, which mocks the Node.js file system module, memfs provides a complete in-memory file system implementation that can be used as a drop-in replacement for the real file system. This can be useful for more complex scenarios where you need a fully functional file system.
The fs-mock package is another library for mocking the file system in Node.js. It provides a simpler API compared to mock-fs and is designed for basic use cases. While it may not have all the features of mock-fs, it can be a good choice for simpler scenarios where you just need to mock a few files and directories.
A configurable mock file system. You know, for testing.
The code below makes it so the fs
module is temporarily backed by a mock file system with a few files and directories.
var mock = require('mock-fs');
var restore = 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 */}
});
Note that the mock
function returns a restore
function. When you are ready to restore the fs
module (so that it is backed by your real file system), call restore()
.
/// after a test runs
restore();
The table below shows what is covered by the mock fs
. Tests are run on Node 0.8, 0.10, and 0.11.
function | status | notes |
---|---|---|
fs.ReadStream | 100% | Complete |
fs.Stats | 100% | Incudes dev , ino , nlink , mode , size , rdev , blksize , blocks , atime , ctime , mtime , uid , gid , and is*() methods |
fs.WriteStream | 100% | Complete |
fs.appendFile | 100% | Complete |
fs.appendFileSync | 100% | Complete |
fs.chmod | 100% | Complete |
fs.chmodSync | 100% | Complete |
fs.chown | 100% | Complete |
fs.chownSync | 100% | Complete |
fs.close | 100% | Complete |
fs.closeSync | 100% | Complete |
fs.createReadStream | 100% | Complete |
fs.createWriteStream | 100% | Complete |
fs.exists | 100% | Complete |
fs.existsSync | 100% | Complete |
fs.fchmod | 100% | Complete |
fs.fchmodSync | 100% | Complete |
fs.fchown | 100% | Complete |
fs.fchownSync | 100% | Complete |
fs.fdatasync | 100% | Complete |
fs.fdatasyncSync | 100% | Complete |
fs.fstat | 100% | Provides a stats object |
fs.fstatSync | 100% | Provides a stats object |
fs.fsync | 100% | Complete |
fs.fsyncSync | 100% | Complete |
fs.ftruncate | 100% | Complete |
fs.ftruncateSync | 100% | Complete |
fs.futimes | 100% | Complete |
fs.futimesSync | 100% | Complete |
fs.lchmod | 100% | Complete |
fs.lchmodSync | 100% | Complete |
fs.lchown | 100% | Complete |
fs.lchownSync | 100% | Complete |
fs.link | 100% | Complete |
fs.linkSync | 100% | Complete |
fs.lstatSync | 100% | Complete |
fs.lstat | 100% | Complete |
fs.mkdir | 100% | Complete |
fs.mkdirSync | 100% | Complete |
fs.open | 100% | Complete |
fs.openSync | 100% | Complete |
fs.read | 100% | Complete |
fs.readSync | 100% | Complete |
fs.readFile | 100% | Complete |
fs.readFileSync | 100% | Complete |
fs.readdir | 100% | Complete |
fs.readdirSync | 100% | Complete |
fs.readlink | 100% | Complete |
fs.readlinkSync | 100% | Complete |
fs.realpath | 100% | Complete |
fs.realpathSync | 100% | Complete |
fs.rename | 100% | Complete |
fs.renameSync | 100% | Complete |
fs.rmdir | 100% | Complete |
fs.rmdirSync | 100% | Complete |
fs.stat | 100% | Provides a stats object |
fs.statSync | 100% | Provides a stats object |
fs.symlink | 100% | Complete |
fs.symlinkSync | 100% | Complete |
fs.truncate | 100% | Complete |
fs.truncateSync | 100% | Complete |
fs.unlink | 100% | Complete |
fs.unlinkSync | 100% | Complete |
fs.utimes | 100% | Complete |
fs.utimesSync | 100% | Complete |
fs.write | 100% | Complete |
fs.writeSync | 100% | Complete |
fs.writeFile | 100% | Complete |
fs.writeFileSync | 100% | Complete |
fs.FSWatcher | 0% | Implement fs.FSWatcher |
fs.unwatchFile | 0% | Implement binding.StatWatcher |
fs.watch | 0% | Implement fs.FSWatcher |
fs.watchFile | 0% | Implement binding.StatWatcher |
FAQs
A configurable mock file system. You know, for testing.
The npm package mock-fs receives a total of 347,621 weekly downloads. As such, mock-fs popularity was classified as popular.
We found that mock-fs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.