test-fixture
Advanced tools
Comparing version 1.0.2 to 2.0.0
99
index.js
@@ -1,82 +0,37 @@ | ||
'use strict'; | ||
const path = require('path') | ||
const tmp = require('tmp-promise') | ||
const fse = require('fs-extra') | ||
module.exports = fixtures; | ||
fixtures.Fixtures = Fixtures; | ||
class Fixtures { | ||
// @param {Array<string>} args | ||
constructor (...args) { | ||
this._root = this._root() | ||
this._path = path.resolve(this._root, ...args) | ||
var node_path = require('path'); | ||
var tmp = require('tmp'); | ||
var fse = require('fs-extra'); | ||
var _ = require('underscore'); | ||
this.resolve = this.resolve.bind(this) | ||
this.copy = this.copy.bind(this) | ||
} | ||
// @param {path...} | ||
function fixtures () { | ||
return new Fixtures(arguments); | ||
} | ||
// @param {Arguments|Array} args | ||
function Fixtures (args) { | ||
this.root = this._root(); | ||
this.path = fixtures._resolve(this.root, args); | ||
}; | ||
// Method for override | ||
// @returns the root of | ||
Fixtures.prototype._root = function() { | ||
return node_path.resolve('test', 'fixtures'); | ||
}; | ||
// @param {path...} arguments | ||
Fixtures.prototype.resolve = function() { | ||
return fixtures._resolve(this.path, arguments); | ||
}; | ||
// @param {path} root | ||
// @param {arguments} paths | ||
fixtures._resolve = function(root, paths) { | ||
if (!root) { | ||
return null; | ||
// Method for override | ||
// @returns the root of | ||
_root () { | ||
return path.resolve('test', 'fixtures') | ||
} | ||
// 'a' -> '/path/to/test/fixtures/a' | ||
// '/path/to/a' -> 'path/to/a' | ||
// undefined -> '/path/to/fixtures' | ||
// see README.md for details | ||
paths = [root].concat(_.toArray(paths)); | ||
return node_path.resolve.apply(node_path, paths); | ||
}; | ||
Fixtures.prototype.copy = function( /* [to], callback */ ) { | ||
var self = this; | ||
if (arguments.length == 2) { | ||
var dir = arguments[0]; | ||
var callback = arguments[1]; | ||
} else { | ||
var callback = arguments[0]; | ||
resolve (...args) { | ||
return args.length === 0 | ||
? this._path | ||
: path.resolve(this._path, ...args) | ||
} | ||
function copydir(dir) { | ||
fse.copy(self.path, dir, function(err) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
async copy (to) { | ||
const toDir = to | ||
? to | ||
: (await tmp.dir()).path | ||
self.path = dir; | ||
callback(err, dir); | ||
}); | ||
await fse.copy(this._path, toDir) | ||
return this._path = toDir | ||
} | ||
} | ||
if (dir) { | ||
copydir(dir); | ||
} else { | ||
tmp.dir(function(err, dir) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
copydir(dir); | ||
}); | ||
} | ||
}; | ||
module.exports = (...args) => new Fixtures(...args) |
{ | ||
"name": "test-fixture", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Copy test-fixtures to temp dir and get resolved file paths.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "make test" | ||
"test": "nyc ava --timeout=10s --verbose", | ||
"test:dev": "nyc ava --timeout=10s --verbose && npm run report:dev", | ||
"lint": "eslint .", | ||
"fix": "eslint . --fix", | ||
"posttest": "npm run report", | ||
"report": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"report:dev": "nyc report --reporter=html && npm run report:open", | ||
"report:open": "open coverage/index.html" | ||
}, | ||
"files": [ | ||
"src/" | ||
], | ||
"repository": { | ||
@@ -22,5 +32,11 @@ "type": "git", | ||
"engines": { | ||
"node": ">=0.11.0" | ||
"node": ">= 7" | ||
}, | ||
"author": "kael", | ||
"ava": { | ||
"babel": false, | ||
"files": [ | ||
"test/*.test.js" | ||
] | ||
}, | ||
"author": "kaelzhang", | ||
"license": "MIT", | ||
@@ -31,10 +47,13 @@ "bugs": { | ||
"devDependencies": { | ||
"mocha": "*", | ||
"chai": "*" | ||
"@ostai/eslint-config": "^3.1.1", | ||
"ava": "^1.4.1", | ||
"codecov": "^3.4.0", | ||
"eslint": "^5.16.0", | ||
"eslint-plugin-import": "^2.17.2", | ||
"nyc": "^14.1.1" | ||
}, | ||
"dependencies": { | ||
"underscore": "^1.6.0", | ||
"tmp": "^0.0.23", | ||
"fs-extra": "^0.10.0" | ||
"fs-extra": "^7.0.1", | ||
"tmp-promise": "^1.1.0" | ||
} | ||
} |
@@ -1,13 +0,16 @@ | ||
# test-fixture [![NPM version](https://badge.fury.io/js/test-fixture.svg)](http://badge.fury.io/js/test-fixture) [![Build Status](https://travis-ci.org/kaelzhang/node-test-fixture.svg?branch=master)](https://travis-ci.org/kaelzhang/node-test-fixture) [![Dependency Status](https://gemnasium.com/kaelzhang/node-test-fixture.svg)](https://gemnasium.com/kaelzhang/node-test-fixture) | ||
[![Build Status](https://travis-ci.org/kaelzhang/node-test-fixture.svg?branch=master)](https://travis-ci.org/kaelzhang/node-test-fixture) | ||
[![Coverage](https://codecov.io/gh/kaelzhang/node-test-fixture/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/node-test-fixture) | ||
Copy test-fixtures to temp dir and get resolved file paths. | ||
# test-fixture | ||
Copy test fixtures to a temp dir and get resolved file paths. | ||
### Why? | ||
I am tired of writing | ||
I am tired of writing these: | ||
- `path.resolve(__dirname, 'test', 'fixtures')`, | ||
- `tmp.dir(callback)`, | ||
- `path.resolve(__dirname, 'test', 'fixtures')`, | ||
- `tmp.dir(callback)`, | ||
- `fse.copy(fixtures, dir)` | ||
- `path.join(fixtures, 'file-a.js')` | ||
- `path.join(fixtures, 'some-file.js')` | ||
@@ -21,3 +24,3 @@ EVERY DAY! | ||
```bash | ||
$ npm install test-fixture --save | ||
$ npm i test-fixture -D | ||
``` | ||
@@ -28,13 +31,26 @@ | ||
```js | ||
var fixtures = require('test-fixture'); | ||
var f = fixtures(); // by default, it will use 'test/fixtures' dir. | ||
// By default, it will use 'test/fixtures' dir. | ||
const {copy, resolve} = require('test-fixture')() | ||
// copy 'test/fixtures' to the temp dir | ||
f.copy(function(err, dir){ | ||
f.resolve('a.js'); // '/<temp>/a.js' | ||
}); | ||
;(async () => { | ||
await copy() | ||
console.log(resolve('foo.js')) | ||
// '/<temp-dir>/foo.js' | ||
}) | ||
``` | ||
### fixtures([path...]) | ||
### fixtures(...paths): {resolve, copy} | ||
- **paths** `Array<path>` to define the root paths of the fixtures, which is similar as | ||
Defines the root of the fixture | ||
```js | ||
const path = require('path') | ||
const fixturesRoot = path.resolve(projectRoot, 'test', 'fixtures') | ||
path.resolve(fixturesRoot, ...args) | ||
``` | ||
`arguments` | `base`(dir of test fixtures) | ||
@@ -45,21 +61,18 @@ --------- | -------------------- | ||
`'a'`, `'b'` | `test/fixtures/a/b` | ||
`'/path/to'`(absolute) | `/path/to` | ||
`'/path/to'`(absolute), `'a'` | `/path/to/a` | ||
`'/path/to'` (absolute) | `/path/to` | ||
`'/path/to'` (absolute), `'a'` | `/path/to/a` | ||
Actually, the `base` is `path.resolve('text/fixtures', path...)` | ||
### .copy([to], callback) | ||
### await copy(to?) | ||
- to `path=` the destination folder where the test fixtures will be copied to. If not specified, `fixtures` will create a temporary dir. | ||
- callback `function(err, dir)` | ||
- err `Error` | ||
- dir `path` the destination directory for testing | ||
- **to?** `path=` the destination folder where the test fixtures will be copied to. If not specified, a temporary directory will be used. | ||
Copy the test fixtures into a temporary directory. | ||
Copy the test fixtures into another directory. | ||
### .resolve([path...]) | ||
### resolve(...paths) | ||
Resolves the paths to get the path of the test fixtures | ||
After `.copy()`ed, it will resolve paths based on the destination dir. | ||
After `.copy()`ed, it will resolve paths based on the destination dir. | ||
@@ -78,4 +91,4 @@ If not, it will use the base dir. But never use both of them simultaneously. | ||
```js | ||
var f = fixtures(base); | ||
f.resolve('a.js'); // -> /path/to/<base>/a.js | ||
const {resolve} = fixtures(base) | ||
resolve('a.js') // -> /path/to/<base>/a.js | ||
``` | ||
@@ -86,25 +99,11 @@ | ||
```js | ||
var f = fixtures(base); | ||
f.copy(to, function(err, dir){ | ||
if (err) { | ||
return; | ||
} | ||
f.resolve('a.js'); // -> /path/to/<to>/a.js | ||
}); | ||
``` | ||
const {copy, resolve} = fixtures(base) | ||
await copy('/path/to') | ||
## For Implementors | ||
resolve('a.js') // -> /path/to/a.js | ||
``` | ||
### fixtures.Fixtures(args) | ||
- args `Arguments|Array` paths to join | ||
### Override: ._root() | ||
Returns `path` the base root. By default, it will returns 'test/fixtures', but you can override this method to specify it by your own. | ||
## License | ||
MIT | ||
<!-- do not want to make nodeinit to complicated, you can edit this whenever you want. --> | ||
[MIT](LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
2
1
5492
6
4
30
1
+ Addedtmp-promise@^1.1.0
+ Addedbluebird@3.7.2(transitive)
+ Addedfs-extra@7.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedtmp@0.1.0(transitive)
+ Addedtmp-promise@1.1.0(transitive)
+ Addeduniversalify@0.1.2(transitive)
- Removedtmp@^0.0.23
- Removedunderscore@^1.6.0
- Removedfs-extra@0.10.0(transitive)
- Removedjsonfile@1.2.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedncp@0.5.1(transitive)
- Removedtmp@0.0.23(transitive)
- Removedunderscore@1.13.7(transitive)
Updatedfs-extra@^7.0.1