
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
chai-fs-latest
Advanced tools
Chai assertion plugin for the Node.js filesystem API. Uses path
and synchronous fs
to assert files and directories.
All assertions are available in expect
, should
and assert
style, and support the optional, message parameter.
Forked because of peerDependencies issue.
Install from npm:
$ npm install chai-fs
Have chai use the chai-fs module:
var chai = require('chai');
chai.use(require('chai-fs'));
No file system.
Assert the return value of path.basename(path)
expect(path).to.have.basename(name, ?msg);
expect(path).to.not.have.basename(name, ?msg);
path.should.have.basename(name, ?msg);
path.should.not.have.basename(name, ?msg);
assert.basename(path, name, ?msg);
assert.notBasename(path, name, ?msg);
Assert the return value of path.dirname(path)
expect(path).to.have.dirname(name, ?msg);
expect(path).to.not.have.dirname(name, ?msg);
path.should.have.dirname(name, ?msg);
path.should.not.have.dirname(name, ?msg);
assert.dirname(path, name, ?msg);
assert.notDirname(path, name, ?msg);
Assert the return value of path.extname(path)
expect(path).to.have.extname(name, ?msg);
expect(path).to.not.have.extname(name, ?msg);
path.should.have.extname(name, ?msg);
path.should.not.have.extname(name, ?msg);
assert.extname(path, name, ?msg);
assert.notExtname(path, name, ?msg);
Assert the path exists.
Uses fs.existsSync()
.
expect(path).to.be.a.path(?msg);
expect(path).to.not.be.a.path(?msg);
path.should.be.a.path(?msg);
path.should.not.be.a.path(?msg);
assert.pathExists(path, ?msg);
assert.notPathExists(path, ?msg);
Use of Chai's exist
-chain would've been nice but has issues with negations and the message parameter. So don't do that.
Assert the path exists and is a directory.
Uses fs.statSync().isDirectory()
expect(path).to.be.a.directory(?msg);
expect(path).to.not.be.a.directory(?msg);
path.should.be.a.directory(?msg);
path.should.not.be.a.directory(?msg);
assert.isDirectory(path, ?msg);
assert.notIsDirectory(path, ?msg);
Assert the path exists, is a directory and contains zero item.
expect(path).to.be.a.directory(?msg).and.empty;
expect(path).to.be.a.directory(?msg).and.not.empty;
path.should.be.a.directory(?msg).and.empty;
path.should.be.a.directory(?msg).and.not.empty;
assert.isEmptyDirectory(path, ?msg);
assert.notIsEmptyDirectory(path, ?msg);
directory()
fs.readdirSync().length === 0
.expect/should
you chain the .not
-negation after the regular directory()
.Assert the path exists and is a file.
Uses fs.statSync().isFile()
expect(path).to.be.a.file(?msg);
expect(path).to.not.be.a.file(?msg);
path.should.be.a.file(?msg);
path.should.not.be.a.file(?msg);
assert.isFile(path, ?msg);
assert.notIsFile(path, ?msg);
Assert the path exists, is a file and has zero size.
expect(path).to.be.a.file(?msg).and.empty;
expect(path).to.be.a.file(?msg).and.not.empty;
path.should.be.a.file(?msg).and.empty;
path.should.be.a.file(?msg).and.not.empty;
assert.isEmptyFile(path, ?msg);
assert.notIsEmptyFile(path, ?msg);
file()
fs.statSync().size === 0
.expect/should
you chain the .not
-negation after the regular file()
.Assert the path exists, is a file and contains json parsable text.
expect(path).to.be.a.file(?msg).with.json;
expect(path).to.be.a.file(?msg).with.not.json;
path.should.be.a.file(?msg).with.json;
path.should.be.a.file(?msg).with.not.json;
assert.jsonFile(path, ?msg);
assert.notJsonFile(path, ?msg);
file()
expect/should
you chain the .not
-negation after the regular file()
.with
chain is just syntax sugar.Assert the path exists, is a file, contains json parsable text conforming to given JSON-Schema.
expect(path).to.be.a.file(?msg).with.json.using.schema(obj);
expect(path).to.be.a.file(?msg).with.json.not.using.schema(obj);
path.should.be.a.file(?msg).with.json.using.schema(obj);
path.should.be.a.file(?msg).with.json.not.using.schema(obj);
assert.jsonSchemaFile(path, schema,?msg);
assert.notJsonSchemaFile(path, schema, ?msg);
file().with.json
chai.use()
.expect/should
you chain the .not
-negation after the regular json
.with
and using
chains are just syntax sugar.Assert the path exists, is a file and has specific content.
expect(path).to.have.content(data, ?msg);
expect(path).to.not.have.content(data, ?msg);
path.should.have.content(data, ?msg);
path.should.not.have.content(data, ?msg);
assert.fileContent(path, data, ?msg);
assert.notFileContent(path, data, ?msg);
Note: In a future version this might be supported as a chain behind file() and directory()
Assert the path exists, is a file and has content that match the regular expression.
expect(path).to.have.content.that.match(/xyz/, ?msg);
expect(path).to.not.have.content.that.match(/xyz/, ?msg);
path.should.have.content.that.match(/xyz/, ?msg);
path.should.not.have.content.that.match(/xyz/, ?msg);
assert.fileContentMatch(path, /xyz/, ?msg);
assert.notFileContentMatch(path, /xyz/, ?msg);
There are some ideas for future assertions saved in this document.
Contributions are welcome. Please follow the code, test and style patterns and keep JSHint happy. Please make sure things work on all platforms, or at least Widows/Mac/Linux.
Install development dependencies in your git checkout:
$ npm install
You need the global grunt command:
$ npm install grunt-cli -g
Build and run tests:
$ grunt
See the Gruntfile
for additional commands.
This plugin uses a prototype of an "assertion plugin test generator" to generates tests for all aspects of the assertions while keeping the specs DRY.
The pattern splits the test into a style declaration tree and a set of variation on 3 types of test scenarios. The generator then combines ('multiplies') every scenario variation with the style tree data to get good coverage of all cases.
The style tree defines ways to use an assertion: first level is the style: expect/should and assert. Then it defines both the normal use and the negation, then divides those into different invocations patterns for each style. So you can test with/without message, or as a chained method or property etc.
The tests are ways to specify assertions and the test expectations.
valid
- test expected to pass (but fail the negation)invalid
- test expected to fail (but pass the negation).error
- test expected to always fail (even when negated), because the data is invalid (eg: bad data type, missing parameters etc).The report field is used the verify the error message if the test fails. It supports a simple template format using the assertion data object.
This looks a bit complex and cumbersome but it does allow to quickly add large amount of detailed tests for all assertions. So far it seems to work empowering so I might extract this to a separate npm module later.
Note it will generate a large amount of case variations so a small error in the code or your test setup can explode the suite wit a many failing assertions. Look closely at which tests are failing to see what is causing what.
Copyright (c) 2013 Bart van der Schoor
Licensed under the MIT license.
FAQs
Chai assertions for Node.js filesystem
The npm package chai-fs-latest receives a total of 0 weekly downloads. As such, chai-fs-latest popularity was classified as not popular.
We found that chai-fs-latest demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.