Socket
Socket
Sign inDemoInstall

chai-files

Package Overview
Dependencies
1
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.0 to 1.2.0

src/dir-helper.js

17

CHANGELOG.md

@@ -5,7 +5,14 @@

## v1.2.0
- `expect(file(...)).to.be.empty`
- `expect(dir(...)).to.exist`
- `expect(dir(...)).to.be.empty`
## v1.1.0
- `expect(file(...).to.equal(...)`
- `expect(file(...).to.equal(file(...))`
- `expect(file(...).to.match(...)`
- `expect(file(...)).to.equal(...)`
- `expect(file(...)).to.equal(file(...))`
- `expect(file(...)).to.match(...)`

@@ -17,3 +24,3 @@

- `expect(file(...).to.exist`
- `expect(file(...).to.contain(...)`
- `expect(file(...)).to.exist`
- `expect(file(...)).to.contain(...)`
var chaiFiles = require('./src/chai-files');
var file = require('./src/file-helper').file;
var dir = require('./src/dir-helper').dir;
module.exports = chaiFiles;
module.exports.file = file;
module.exports.dir = dir;
{
"name": "chai-files",
"version": "1.1.0",
"version": "1.2.0",
"description": "file system assertions for chai",

@@ -30,3 +30,6 @@ "main": "index.js",

"mocha": "^2.4.5"
},
"dependencies": {
"assertion-error": "^1.0.1"
}
}

@@ -31,2 +31,3 @@

var file = chaiFiles.file;
var dir = chaiFiles.dir;
```

@@ -37,3 +38,3 @@

Check if a file exist:
Check if a file or directory exist:

@@ -43,2 +44,5 @@ ```js

expect(file('index.coffee')).to.not.exist;
expect(dir('foo')).to.exist;
expect(dir('missing')).to.not.exist;
```

@@ -67,2 +71,15 @@

### .to.be.empty
Check if a file or directory is empty:
```js
expect(file('empty.txt')).to.be.empty;
expect(file('foo.txt')).to.not.be.empty;
expect(dir('empty')).to.be.empty;
expect(dir('foo')).to.not.be.empty;
```
### .to.contain(...)

@@ -69,0 +86,0 @@

var FileHelper = require('./file-helper').FileHelper;
var DirectoryHelper = require('./dir-helper').DirectoryHelper;

@@ -9,3 +10,3 @@ module.exports = function(chai, utils) {

*
* Asserts that a file exists.
* Asserts that a file or directory exists.
*

@@ -15,2 +16,5 @@ * expect(file('index.js')).to.exist;

*
* expect(dir('foo')).to.exist;
* expect(dir('missing')).to.not.exist;
*
* @name exist

@@ -24,7 +28,9 @@ * @namespace BDD

var obj = this._obj;
if (obj instanceof FileHelper) {
if (obj instanceof FileHelper || obj instanceof DirectoryHelper) {
var ssf = utils.flag(this, 'ssfi');
if (utils.flag(this, 'negate')) {
obj.assertDoesNotExist();
obj.assertDoesNotExist(ssf);
} else {
obj.assertExists();
obj.assertExists(ssf);
}

@@ -64,7 +70,10 @@

if (obj instanceof FileHelper) {
var ssf = utils.flag(this, 'ssfi');
if (utils.flag(this, 'negate')) {
obj.assertDoesNotEqual(value);
obj.assertDoesNotEqual(value, ssf);
} else {
obj.assertEquals(value);
obj.assertEquals(value, ssf);
}
} else {

@@ -80,3 +89,41 @@ _super.apply(this, arguments);

/**
* ### .empty
*
* Asserts that a file or directory is empty.
*
* expect(file('empty.txt')).to.be.empty;
* expect(file('foo.txt')).to.not.be.empty;
*
* expect(dir('empty')).to.be.empty;
* expect(dir('foo')).to.not.be.empty;
*
* @name empty
* @namespace BDD
* @api public
*/
function empty(_super) {
return function() {
var obj = this._obj;
if (obj instanceof FileHelper || obj instanceof DirectoryHelper) {
var ssf = utils.flag(this, 'ssfi');
if (utils.flag(this, 'negate')) {
obj.assertIsNotEmpty(ssf);
} else {
obj.assertIsEmpty(ssf);
}
} else {
_super.call(this);
}
};
}
Assertion.overwriteProperty('empty', empty);
/**
* ### .include(value)

@@ -108,7 +155,10 @@ *

if (obj instanceof FileHelper) {
var ssf = utils.flag(this, 'ssfi');
if (utils.flag(this, 'negate')) {
obj.assertDoesNotContain(value);
obj.assertDoesNotContain(value, ssf);
} else {
obj.assertContains(value);
obj.assertContains(value, ssf);
}
} else {

@@ -144,7 +194,10 @@ _super.apply(this, arguments);

if (obj instanceof FileHelper) {
var ssf = utils.flag(this, 'ssfi');
if (utils.flag(this, 'negate')) {
obj.assertDoesNotMatch(regex);
obj.assertDoesNotMatch(regex, ssf);
} else {
obj.assertMatches(regex);
obj.assertMatches(regex, ssf);
}
} else {

@@ -151,0 +204,0 @@ _super.apply(this, arguments);

var fs = require('fs');
var path = require('path');
var AssertionError = require('assertion-error');

@@ -10,40 +11,55 @@ function FileHelper(path) {

FileHelper.prototype._absPath = function() {
return path.resolve(process.cwd(), this.path);
};
Object.defineProperty(FileHelper.prototype, 'absolutePath', {
get: function() {
return path.resolve(process.cwd(), this.path);
}
});
FileHelper.prototype.exists = function() {
if (this._exists === null) {
var path = this._absPath();
if (fs.existsSync) {
this._exists = fs.existsSync(path);
} else {
Object.defineProperty(FileHelper.prototype, 'stats', {
get: function() {
if (this._stats === undefined) {
try {
fs.accessSync(path, fs.F_OK);
this._exists = true;
this._stats = fs.statSync(this.absolutePath);
} catch (e) {
this._exists = false
this._stats = null;
}
}
return this._stats;
}
});
return this._exists;
};
Object.defineProperty(FileHelper.prototype, 'exists', {
get: function() {
return Boolean(this.stats);
}
});
FileHelper.prototype.assertExists = function() {
if (!this.exists()) {
throw new Error('expected "' + this.path + '" to exist');
Object.defineProperty(FileHelper.prototype, 'content', {
get: function() {
if (this._content === null) {
this._content = fs.readFileSync(this.absolutePath, {encoding: 'utf-8'});
}
return this._content;
}
};
});
FileHelper.prototype.assertDoesNotExist = function() {
if (this.exists()) {
throw new Error('expected "' + this.path + '" to not exist');
Object.defineProperty(FileHelper.prototype, 'isEmpty', {
get: function() {
return this.content === '';
}
});
FileHelper.prototype.assertExists = function(ssf) {
if (!this.exists) {
throw new AssertionError('expected "' + this.path + '" to exist', {}, ssf);
} else if (!this.stats.isFile()) {
throw new AssertionError('expected "' + this.path + '" to be a file', {}, ssf);
}
};
FileHelper.prototype._loadContent = function() {
if (this._content === null) {
this._content = fs.readFileSync(this._absPath(), {encoding: 'utf-8'});
FileHelper.prototype.assertDoesNotExist = function(ssf) {
if (this.exists) {
throw new AssertionError('expected "' + this.path + '" to not exist', {}, ssf);
}

@@ -53,58 +69,76 @@ };

FileHelper.prototype.equals = function(str) {
this._loadContent();
return this._content === str;
return this.content === str;
};
FileHelper.prototype.assertEquals = function(value) {
FileHelper.prototype.assertEquals = function(value, ssf) {
var valueIsFile = (value instanceof FileHelper);
this.assertExists();
this.assertExists(ssf);
if (valueIsFile) {
value.assertExists();
value._loadContent();
value.assertExists(ssf);
}
var str = valueIsFile ? value._content : value;
var str = valueIsFile ? value.content : value;
if (!this.equals(str)) {
var error = new Error('expected "' + this.path + '" to equal "' + (valueIsFile ? value.path : value) + '"');
error.actual = this._content;
error.expected = str;
throw error;
throw new AssertionError('expected "' + this.path + '" to equal "' + (valueIsFile ? value.path : value) + '"', {
showDiff: true,
actual: this.content,
expected: str,
}, ssf);
}
};
FileHelper.prototype.assertDoesNotEqual = function(value) {
FileHelper.prototype.assertDoesNotEqual = function(value, ssf) {
var valueIsFile = (value instanceof FileHelper);
this.assertExists();
this.assertExists(ssf);
if (valueIsFile) {
value.assertExists();
value._loadContent();
value.assertExists(ssf);
}
var str = valueIsFile ? value._content : value;
var str = valueIsFile ? value.content : value;
if (this.equals(str)) {
throw new Error('expected "' + this.path + '" to not equal "' + (valueIsFile ? value.path : value) + '"');
throw new AssertionError('expected "' + this.path + '" to not equal "' + (valueIsFile ? value.path : value) + '"', {}, ssf);
}
};
FileHelper.prototype.assertIsEmpty = function(ssf) {
this.assertExists(ssf);
if (!this.isEmpty) {
throw new AssertionError('expected "' + this.path + '" to be empty', {
showDiff: true,
actual: this.content,
expected: '',
}, ssf);
}
};
FileHelper.prototype.assertIsNotEmpty = function(ssf) {
this.assertExists(ssf);
if (this.isEmpty) {
throw new AssertionError('expected "' + this.path + '" to not be empty', {}, ssf);
}
};
FileHelper.prototype.contains = function(str) {
this._loadContent();
return this._content.indexOf(str) !== -1;
return this.content.indexOf(str) !== -1;
};
FileHelper.prototype.assertContains = function(str) {
this.assertExists();
FileHelper.prototype.assertContains = function(str, ssf) {
this.assertExists(ssf);
if (!this.contains(str)) {
var error = new Error('expected "' + this.path + '" to contain "' + str + '"');
error.actual = this._content;
error.expected = str;
throw error;
throw new AssertionError('expected "' + this.path + '" to contain "' + str + '"', {
showDiff: true,
actual: this.content,
expected: str,
}, ssf);
}
};
FileHelper.prototype.assertDoesNotContain = function(str) {
this.assertExists();
FileHelper.prototype.assertDoesNotContain = function(str, ssf) {
this.assertExists(ssf);
if (this.contains(str)) {
throw new Error('expected "' + this.path + '" to not contain "' + str + '"');
throw new AssertionError('expected "' + this.path + '" to not contain "' + str + '"', {}, ssf);
}

@@ -114,23 +148,27 @@ };

FileHelper.prototype.matches = function(regex) {
this._loadContent();
return regex.test(this._content);
return regex.test(this.content);
};
FileHelper.prototype.assertMatches = function(regex) {
this.assertExists();
FileHelper.prototype.assertMatches = function(regex, ssf) {
this.assertExists(ssf);
if (!this.matches(regex)) {
var error = new Error('expected "' + this.path + '" to match ' + regex);
error.actual = this._content;
error.expected = regex;
throw error;
throw new AssertionError('expected "' + this.path + '" to match ' + regex, {
showDiff: true,
actual: this.content,
expected: regex,
}, ssf);
}
};
FileHelper.prototype.assertDoesNotMatch = function(regex) {
this.assertExists();
FileHelper.prototype.assertDoesNotMatch = function(regex, ssf) {
this.assertExists(ssf);
if (this.matches(regex)) {
throw new Error('expected "' + this.path + '" to not match ' + regex);
throw new AssertionError('expected "' + this.path + '" to not match ' + regex, {}, ssf);
}
};
FileHelper.prototype.inspect = function() {
return 'file(\'' + this.path + '\')';
};
function file(path) {

@@ -137,0 +175,0 @@ return new FileHelper(path);

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