chai-files
Advanced tools
Comparing version 1.0.0 to 1.1.0
117
index.js
@@ -1,114 +0,5 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var chaiFiles = require('./src/chai-files'); | ||
var file = require('./src/file-helper').file; | ||
function FileHelper(path) { | ||
this.path = path; | ||
this._exists = null; | ||
this._content = null; | ||
} | ||
FileHelper.prototype._absPath = 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 { | ||
try { | ||
fs.accessSync(path, fs.F_OK); | ||
this._exists = true; | ||
} catch (e) { | ||
this._exists = false | ||
} | ||
} | ||
} | ||
return this._exists; | ||
}; | ||
FileHelper.prototype.assertExists = function() { | ||
if (!this.exists()) { | ||
throw new Error('expected "' + this.path + '" to exist'); | ||
} | ||
}; | ||
FileHelper.prototype.assertDoesNotExist = function() { | ||
if (this.exists()) { | ||
throw new Error('expected "' + this.path + '" to not exist'); | ||
} | ||
}; | ||
FileHelper.prototype._loadContent = function() { | ||
if (this._content === null) { | ||
this._content = fs.readFileSync(this._absPath(), {encoding: 'utf-8'}); | ||
} | ||
}; | ||
FileHelper.prototype.contains = function(str) { | ||
this._loadContent(); | ||
return this._content.indexOf(str) !== -1; | ||
}; | ||
FileHelper.prototype.assertContains = function(str) { | ||
this.assertExists(); | ||
if (!this.contains(str)) { | ||
var error = new Error('expected "' + this.path + '" to contain "' + str + '"'); | ||
error.actual = this._content; | ||
error.expected = str; | ||
throw error; | ||
} | ||
}; | ||
FileHelper.prototype.assertDoesNotContain = function(str) { | ||
this.assertExists(); | ||
if (this.contains(str)) { | ||
throw new Error('expected "' + this.path + '" to not contain "' + str + '"'); | ||
} | ||
}; | ||
module.exports = function(chai, utils) { | ||
var Assertion = chai.Assertion; | ||
Assertion.overwriteProperty('exist', function(_super) { | ||
return function() { | ||
var obj = this._obj; | ||
if (obj instanceof FileHelper) { | ||
if (utils.flag(this, 'negate')) { | ||
obj.assertDoesNotExist(); | ||
} else { | ||
obj.assertExists(); | ||
} | ||
} else { | ||
_super.call(this); | ||
} | ||
}; | ||
}); | ||
Assertion.overwriteChainableMethod('contain', function(_super) { | ||
return function(str) { | ||
var obj = this._obj; | ||
if (obj instanceof FileHelper) { | ||
if (utils.flag(this, 'negate')) { | ||
obj.assertDoesNotContain(str); | ||
} else { | ||
obj.assertContains(str); | ||
} | ||
} else { | ||
_super.apply(this, arguments); | ||
} | ||
}; | ||
}, function(_super) { | ||
return function() { | ||
return _super.apply(this, arguments); | ||
} | ||
}); | ||
}; | ||
module.exports.file = function(path) { | ||
return new FileHelper(path); | ||
}; | ||
module.exports = chaiFiles; | ||
module.exports.file = file; |
{ | ||
"name": "chai-files", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "file system assertions for chai", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,2 +6,3 @@ | ||
[![Build Status](https://travis-ci.org/Turbo87/chai-files.svg?branch=master)](https://travis-ci.org/Turbo87/chai-files) | ||
[![Build status](https://ci.appveyor.com/api/projects/status/github/Turbo87/chai-files?svg=true)](https://ci.appveyor.com/project/Turbo87/chai-files/branch/master) | ||
@@ -25,3 +26,3 @@ file system assertions for chai | ||
var chai = require('chai'); | ||
var chaiFiles = require('../index'); | ||
var chaiFiles = require('chai-files'); | ||
@@ -35,3 +36,3 @@ chai.use(chaiFiles); | ||
### expect(file(...)).to.exist | ||
### .to.exist | ||
@@ -46,4 +47,24 @@ Check if a file exist: | ||
### expect(file(...)).to.contain(...) | ||
### .to.equal(...) | ||
Check if the file content equals a string: | ||
```js | ||
expect(file('foo.txt')).to.equal('foo'); | ||
expect(file('foo.txt')).to.not.equal('bar'); | ||
``` | ||
### .to.equal(file(...)) | ||
Check if the file equals another file: | ||
```js | ||
expect(file('foo.txt')).to.equal(file('foo-copy.txt')); | ||
expect(file('foo.txt')).to.not.equal(file('bar.txt')); | ||
``` | ||
### .to.contain(...) | ||
Check if a file contains a string: | ||
@@ -57,4 +78,14 @@ | ||
### .to.match(/.../) | ||
Check if a file matches a regular expression: | ||
```js | ||
expect(file('foo.txt')).to.match(/fo+/); | ||
expect(file('foo.txt')).to.not.match(/bar?/); | ||
``` | ||
License | ||
------------------------------------------------------------------------------ | ||
chai-files is licensed under the [MIT License](LICENSE). |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11409
9
256
87
1