Socket
Socket
Sign inDemoInstall

fs-jetpack

Package Overview
Dependencies
Maintainers
1
Versions
61
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-jetpack - npm Package Compare versions

Comparing version 0.10.0 to 0.10.1

.eslintignore

5

CHANGELOG.md

@@ -0,1 +1,6 @@

0.10.1 (2016-11-01)
-------------------
* Bugfixed case when `copyAsync()` was leaving open read stream if write stream errored.
* Tests ported from jasmine to mocha.
0.10.0 (2016-10-17)

@@ -2,0 +7,0 @@ -------------------

23

lib/copy.js

@@ -137,6 +137,15 @@ 'use strict';

var deferred = Q.defer();
fs.createReadStream(from)
.pipe(fs.createWriteStream(to, { mode: mode }))
.on('error', function (err) {
var readStream = fs.createReadStream(from);
var writeStream = fs.createWriteStream(to, { mode: mode });
readStream.on('error', deferred.reject);
writeStream.on('error', function (err) {
var toDirPath = pathUtil.dirname(to);
// Force read stream to close, since write stream errored
// read stream serves us no purpose.
readStream.resume();
if (err.code === 'ENOENT' && retriedAttempt === undefined) {

@@ -155,4 +164,8 @@ // Some parent directory doesn't exits. Create it and retry.

}
})
.on('finish', deferred.resolve);
});
writeStream.on('finish', deferred.resolve);
readStream.pipe(writeStream);
return deferred.promise;

@@ -159,0 +172,0 @@ };

@@ -6,2 +6,3 @@ /* eslint no-underscore-dangle:0 */

var Readable = require('stream').Readable;
var pathUtil = require('path');
var inspect = require('../inspect');

@@ -27,3 +28,3 @@ var list = require('../list');

list.sync(path).forEach(function (child) {
walkSync(path + '/' + child, options, callback, currentLevel + 1);
walkSync(path + pathUtil.sep + child, options, callback, currentLevel + 1);
});

@@ -89,3 +90,3 @@ }

name: name,
path: theNode.path + '/' + name,
path: theNode.path + pathUtil.sep + name,
parent: theNode,

@@ -92,0 +93,0 @@ level: theNode.level + 1

{
"name": "fs-jetpack",
"description": "Better file system API",
"version": "0.10.0",
"version": "0.10.1",
"author": "Jakub Szwacz <jakub@szwacz.com>",

@@ -13,2 +13,4 @@ "dependencies": {

"devDependencies": {
"chai": "^3.5.0",
"codecov": "^1.0.1",
"eslint": "^2.13.1",

@@ -18,7 +20,9 @@ "eslint-config-airbnb-base": "^3.0.1",

"fs-extra": "^0.16.3",
"jasmine": "^2.2.1",
"istanbul": "^0.4.5",
"mocha": "^3.1.2",
"pre-commit": "^1.1.2"
},
"scripts": {
"test": "jasmine",
"test": "mocha \"spec/**/*.spec.js\"",
"test-with-coverage": "istanbul cover node_modules/.bin/_mocha -- 'spec/**/*.spec.js'",
"lint": "eslint ."

@@ -25,0 +29,0 @@ },

@@ -1,2 +0,2 @@

fs-jetpack [![Build Status](https://travis-ci.org/szwacz/fs-jetpack.svg?branch=master)](https://travis-ci.org/szwacz/fs-jetpack) [![Build status](https://ci.appveyor.com/api/projects/status/er206e91fpuuqf58?svg=true)](https://ci.appveyor.com/project/szwacz/fs-jetpack)
fs-jetpack [![Build Status](https://travis-ci.org/szwacz/fs-jetpack.svg?branch=master)](https://travis-ci.org/szwacz/fs-jetpack) [![Build status](https://ci.appveyor.com/api/projects/status/er206e91fpuuqf58?svg=true)](https://ci.appveyor.com/project/szwacz/fs-jetpack) [![codecov](https://codecov.io/gh/szwacz/fs-jetpack/branch/master/graph/badge.svg)](https://codecov.io/gh/szwacz/fs-jetpack)
==========

@@ -3,0 +3,0 @@

@@ -1,155 +0,145 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('append |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('append', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('appends String to file', function (done) {
describe('appends String to file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.writeFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('abcxyz');
path('file.txt').shouldBeFileWithContent('abcxyz');
};
// SYNC
preparations();
jetpack.append('file.txt', 'xyz');
expectations();
// ASYNC
preparations();
jetpack.appendAsync('file.txt', 'xyz')
.then(function () {
it('sync', function () {
preparations();
jetpack.append('file.txt', 'xyz');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.appendAsync('file.txt', 'xyz')
.then(function () {
expectations();
done();
});
});
});
it('appends Buffer to file', function (done) {
describe('appends Buffer to file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.writeFileSync('file.txt', new Buffer([11]));
fse.writeFileSync('file.bin', new Buffer([11]));
};
var expectations = function () {
var buf = fse.readFileSync('file.txt');
expect(buf[0]).toBe(11);
expect(buf[1]).toBe(22);
expect(buf.length).toBe(2);
path('file.bin').shouldBeFileWithContent(new Buffer([11, 22]));
};
// SYNC
preparations();
jetpack.append('file.txt', new Buffer([22]));
expectations();
// ASYNC
preparations();
jetpack.appendAsync('file.txt', new Buffer([22]))
.then(function () {
it('sync', function () {
preparations();
jetpack.append('file.bin', new Buffer([22]));
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.appendAsync('file.bin', new Buffer([22]))
.then(function () {
expectations();
done();
});
});
});
it("if file doesn't exist creates it", function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe("if file doesn't exist creates it", function () {
var expectations = function () {
expect('file.txt').toBeFileWithContent('xyz');
path('file.txt').shouldBeFileWithContent('xyz');
};
// SYNC
preparations();
jetpack.append('file.txt', 'xyz');
expectations();
// ASYNC
preparations();
jetpack.appendAsync('file.txt', 'xyz')
.then(function () {
it('sync', function () {
jetpack.append('file.txt', 'xyz');
expectations();
done();
});
it('async', function (done) {
jetpack.appendAsync('file.txt', 'xyz')
.then(function () {
expectations();
done();
});
});
});
it("if parent directory doesn't exist creates it as well", function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe("if parent directory doesn't exist creates it", function () {
var expectations = function () {
expect('dir/dir/file.txt').toBeFileWithContent('xyz');
path('dir/dir/file.txt').shouldBeFileWithContent('xyz');
};
// SYNC
preparations();
jetpack.append('dir/dir/file.txt', 'xyz');
expectations();
// ASYNC
preparations();
jetpack.appendAsync('dir/dir/file.txt', 'xyz')
.then(function () {
it('sync', function () {
jetpack.append('dir/dir/file.txt', 'xyz');
expectations();
done();
});
it('async', function (done) {
jetpack.appendAsync('dir/dir/file.txt', 'xyz')
.then(function () {
expectations();
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function () {
expect('a/b.txt').toBeFileWithContent('abcxyz');
path('a/b.txt').shouldBeFileWithContent('abcxyz');
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.append('b.txt', 'xyz');
expectations();
// ASYNC
preparations();
jetContext.appendAsync('b.txt', 'xyz')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.append('b.txt', 'xyz');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.appendAsync('b.txt', 'xyz')
.then(function () {
expectations();
done();
});
});
});
describe('*nix specyfic |', function () {
if (process.platform === 'win32') {
return;
}
it('sets file mode on created file', function (done) {
if (process.platform !== 'win32') {
describe('sets file mode on created file (unix only)', function () {
var expectations = function () {
expect('file.txt').toHaveMode('711');
path('file.txt').shouldHaveMode('711');
};
// SYNC
jetpack.append('file.txt', 'abc', { mode: '711' });
expectations();
helper.clearWorkingDir();
// AYNC
jetpack.appendAsync('file.txt', 'abc', { mode: '711' })
.then(function () {
it('sync', function () {
jetpack.append('file.txt', 'abc', { mode: '711' });
expectations();
done();
});
it('async', function (done) {
jetpack.appendAsync('file.txt', 'abc', { mode: '711' })
.then(function () {
expectations();
done();
});
});
});
});
}
});

@@ -1,87 +0,90 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('copy |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('copy', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('copies a file', function (done) {
describe('copies a file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('abc');
expect('file_1.txt').toBeFileWithContent('abc');
path('file.txt').shouldBeFileWithContent('abc');
path('file_copied.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.copy('file.txt', 'file_1.txt');
expectations();
// ASYNC
preparations();
jetpack.copyAsync('file.txt', 'file_1.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('file.txt', 'file_copied.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('file.txt', 'file_copied.txt')
.then(function () {
expectations();
done();
});
});
});
it('can copy file to nonexistent directory (will create directory)', function (done) {
describe('can copy file to nonexistent directory (will create directory)', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('abc');
expect('dir/dir/file.txt').toBeFileWithContent('abc');
path('file.txt').shouldBeFileWithContent('abc');
path('dir/dir/file.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.copy('file.txt', 'dir/dir/file.txt');
expectations();
// ASYNC
preparations();
jetpack.copyAsync('file.txt', 'dir/dir/file.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('file.txt', 'dir/dir/file.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('file.txt', 'dir/dir/file.txt')
.then(function () {
expectations();
done();
});
});
});
it('copies empty directory', function (done) {
describe('copies empty directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('dir');
};
var expectations = function () {
expect('a/dir').toBeDirectory();
path('copied/dir').shouldBeDirectory();
};
// SYNC
preparations();
jetpack.copy('dir', 'a/dir');
expectations();
// ASYNC
preparations();
jetpack.copyAsync('dir', 'a/dir')
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('dir', 'copied/dir');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('dir', 'copied/dir')
.then(function () {
expectations();
done();
});
});
});
it('copies a tree of files', function (done) {
describe('copies a tree of files', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/f1.txt', 'abc');

@@ -91,130 +94,140 @@ fse.outputFileSync('a/b/f2.txt', '123');

};
var expectations = function () {
expect('dir/a/f1.txt').toBeFileWithContent('abc');
expect('dir/a/b/c').toBeDirectory();
expect('dir/a/b/f2.txt').toBeFileWithContent('123');
path('copied/a/f1.txt').shouldBeFileWithContent('abc');
path('copied/a/b/c').shouldBeDirectory();
path('copied/a/b/f2.txt').shouldBeFileWithContent('123');
};
// SYNC
preparations();
jetpack.copy('a', 'dir/a');
expectations();
// ASYNC
preparations();
jetpack.copyAsync('a', 'dir/a')
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('a', 'copied/a');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'copied/a')
.then(function () {
expectations();
done();
});
});
});
it("generates nice error if source path doesn't exist", function (done) {
describe("generates nice error if source path doesn't exist", function () {
var expectations = function (err) {
expect(err.code).toBe('ENOENT');
expect(err.message).toMatch(/^Path to copy doesn't exist/);
expect(err.code).to.equal('ENOENT');
expect(err.message).to.have.string("Path to copy doesn't exist");
};
// SYNC
try {
jetpack.copy('a', 'b');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
it('sync', function () {
try {
jetpack.copy('a', 'b');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// ASYNC
jetpack.copyAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
jetpack.copyAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function () {
expect('a/b.txt').toBeFileWithContent('abc');
expect('a/x.txt').toBeFileWithContent('abc');
path('a/b.txt').shouldBeFileWithContent('abc');
path('a/x.txt').shouldBeFileWithContent('abc');
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.copy('b.txt', 'x.txt');
expectations();
// ASYNC
preparations();
jetContext.copyAsync('b.txt', 'x.txt')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.copy('b.txt', 'x.txt');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.copyAsync('b.txt', 'x.txt')
.then(function () {
expectations();
done();
});
});
});
describe('overwriting behaviour', function () {
it('does not overwrite by default', function (done) {
describe('does not overwrite by default', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/file.txt', 'abc');
fse.mkdirsSync('b');
};
var expectations = function (err) {
expect(err.code).toBe('EEXIST');
expect(err.message).toMatch(/^Destination path already exists/);
expect(err.code).to.equal('EEXIST');
expect(err.message).to.have.string('Destination path already exists');
};
// SYNC
preparations();
try {
jetpack.copy('a', 'b');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
it('sync', function () {
preparations();
try {
jetpack.copy('a', 'b');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// ASYNC
preparations();
jetpack.copyAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('overwrites if it was specified', function (done) {
describe('overwrites if it was specified', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/file.txt', 'abc');
fse.outputFileSync('b/file.txt', 'xyz');
};
var expectations = function () {
expect('a/file.txt').toBeFileWithContent('abc');
expect('b/file.txt').toBeFileWithContent('abc');
path('a/file.txt').shouldBeFileWithContent('abc');
path('b/file.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.copy('a', 'b', { overwrite: true });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('a', 'b', { overwrite: true })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('a', 'b', { overwrite: true });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'b', { overwrite: true })
.then(function () {
expectations();
done();
});
});
});
});
describe('filter what to copy |', function () {
it('by simple pattern', function (done) {
describe('filter what to copy', function () {
describe('by simple pattern', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('dir/file.txt', '1');

@@ -224,31 +237,29 @@ fse.outputFileSync('dir/file.md', 'm1');

fse.outputFileSync('dir/a/file.md', 'm2');
fse.outputFileSync('dir/a/b/file.txt', '3');
fse.outputFileSync('dir/a/b/file.md', 'm3');
};
var expectations = function () {
expect('copy/file.txt').toBeFileWithContent('1');
expect('copy/file.md').not.toExist();
expect('copy/a/file.txt').toBeFileWithContent('2');
expect('copy/a/file.md').not.toExist();
expect('copy/a/b/file.txt').toBeFileWithContent('3');
expect('copy/a/b/file.md').not.toExist();
path('copy/file.txt').shouldBeFileWithContent('1');
path('copy/file.md').shouldNotExist();
path('copy/a/file.txt').shouldBeFileWithContent('2');
path('copy/a/file.md').shouldNotExist();
};
// SYNC
preparations();
jetpack.copy('dir', 'copy', { matching: '*.txt' });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('dir', 'copy', { matching: '*.txt' })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('dir', 'copy', { matching: '*.txt' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('dir', 'copy', { matching: '*.txt' })
.then(function () {
expectations();
done();
});
});
});
it('by pattern anchored to copied directory', function (done) {
describe('by pattern anchored to copied directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('x/y/dir/file.txt', '1');

@@ -258,79 +269,85 @@ fse.outputFileSync('x/y/dir/a/file.txt', '2');

};
var expectations = function () {
expect('copy/file.txt').not.toExist();
expect('copy/a/file.txt').toBeFileWithContent('2');
expect('copy/a/b/file.txt').not.toExist();
path('copy/file.txt').shouldNotExist();
path('copy/a/file.txt').shouldBeFileWithContent('2');
path('copy/a/b/file.txt').shouldNotExist();
};
// SYNC
preparations();
jetpack.copy('x/y/dir', 'copy', { matching: 'a/*.txt' });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('x/y/dir', 'copy', { matching: 'a/*.txt' })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('x/y/dir', 'copy', { matching: 'a/*.txt' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('x/y/dir', 'copy', { matching: 'a/*.txt' })
.then(function () {
expectations();
done();
});
});
});
it('can use ./ as indication of anchor directory', function (done) {
describe('can use ./ as indication of anchor directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('x/y/a.txt', '123');
fse.outputFileSync('x/y/b/a.txt', '456');
};
var expectations = function () {
expect('copy/a.txt').toExist();
expect('copy/b/a.txt').not.toExist();
path('copy/a.txt').shouldBeFileWithContent('123');
path('copy/b/a.txt').shouldNotExist();
};
// SYNC
preparations();
jetpack.copy('x/y', 'copy', { matching: './a.txt' });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('x/y', 'copy', { matching: './a.txt' })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('x/y', 'copy', { matching: './a.txt' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('x/y', 'copy', { matching: './a.txt' })
.then(function () {
expectations();
done();
});
});
});
it('matching works also if copying single file', function (done) {
describe('matching works also if copying single file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a', '123');
fse.outputFileSync('x', '456');
};
var expectations = function () {
expect('a-copy').not.toExist();
expect('x-copy').toExist();
path('a-copy').shouldNotExist();
path('x-copy').shouldBeFileWithContent('456');
};
// SYNC
preparations();
jetpack.copy('a', 'a-copy', { matching: 'x' });
jetpack.copy('x', 'x-copy', { matching: 'x' });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('a', 'a-copy', { matching: 'x' })
.then(function () {
return jetpack.copyAsync('x', 'x-copy', { matching: 'x' });
})
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('a', 'a-copy', { matching: 'x' });
jetpack.copy('x', 'x-copy', { matching: 'x' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'a-copy', { matching: 'x' })
.then(function () {
return jetpack.copyAsync('x', 'x-copy', { matching: 'x' });
})
.then(function () {
expectations();
done();
});
});
});
it('can use negation in patterns', function (done) {
describe('can use negation in patterns', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('x/y/dir/a/b');

@@ -341,42 +358,44 @@ fse.mkdirsSync('x/y/dir/a/x');

};
var expectations = function () {
expect('copy/dir/a/b').toBeDirectory();
expect('copy/dir/a/x').not.toExist();
expect('copy/dir/a/y').not.toExist();
expect('copy/dir/a/z').not.toExist();
path('copy/dir/a/b').shouldBeDirectory();
path('copy/dir/a/x').shouldNotExist();
path('copy/dir/a/y').shouldNotExist();
path('copy/dir/a/z').shouldNotExist();
};
// SYNC
preparations();
jetpack.copy('x/y', 'copy', {
matching: [
'**',
// Three different pattern types to test:
'!x',
'!dir/a/y',
'!./dir/a/z'
]
it('sync', function () {
preparations();
jetpack.copy('x/y', 'copy', {
matching: [
'**',
// Three different pattern types to test:
'!x',
'!dir/a/y',
'!./dir/a/z'
]
});
expectations();
});
expectations();
// ASYNC
preparations();
jetpack.copyAsync('x/y', 'copy', {
matching: [
'**',
// Three different pattern types to test:
'!x',
'!dir/a/y',
'!./dir/a/z'
]
})
.then(function () {
expectations();
done();
it('async', function (done) {
preparations();
jetpack.copyAsync('x/y', 'copy', {
matching: [
'**',
// Three different pattern types to test:
'!x',
'!dir/a/y',
'!./dir/a/z'
]
})
.then(function () {
expectations();
done();
});
});
});
it('wildcard copies everything', function (done) {
describe('wildcard copies everything', function () {
var preparations = function () {
helper.clearWorkingDir();
// Just a file

@@ -389,48 +408,45 @@ fse.outputFileSync('x/file.txt', '123');

};
var expectations = function () {
expect('copy/file.txt').toBeFileWithContent('123');
expect('copy/y/.dot').toBeFileWithContent('dot');
expect('copy/y/z').toBeDirectory();
path('copy/file.txt').shouldBeFileWithContent('123');
path('copy/y/.dot').shouldBeFileWithContent('dot');
path('copy/y/z').shouldBeDirectory();
};
// SYNC
preparations();
jetpack.copy('x', 'copy', { matching: '**' });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('x', 'copy', { matching: '**' })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('x', 'copy', { matching: '**' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('x', 'copy', { matching: '**' })
.then(function () {
expectations();
done();
});
});
});
});
describe('*nix specific |', function () {
if (process.platform === 'win32') {
return;
}
describe('can copy symlink', function () {
var preparations = function () {
fse.mkdirsSync('to_copy');
fse.symlinkSync('some/file', 'to_copy/symlink');
};
var expectations = function () {
expect(fse.lstatSync('copied/symlink').isSymbolicLink()).to.equal(true);
expect(fse.readlinkSync('copied/symlink')).to.equal(helper.osSep('some/file'));
};
it('copies also file permissions', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', 'abc');
fse.chmodSync('a/b', '700');
fse.chmodSync('a/b/c.txt', '711');
};
var expectations = function () {
expect('x/b').toHaveMode('700');
expect('x/b/c.txt').toHaveMode('711');
};
// SYNC
it('sync', function () {
preparations();
jetpack.copy('a', 'x');
jetpack.copy('to_copy', 'copied');
expectations();
});
// AYNC
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'x')
jetpack.copyAsync('to_copy', 'copied')
.then(function () {

@@ -441,22 +457,26 @@ expectations();

});
});
it('can copy symlink', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('to_copy');
fse.symlinkSync('some/file', 'to_copy/symlink');
};
var expectations = function () {
expect(fse.lstatSync('copied/symlink').isSymbolicLink()).toBe(true);
expect(fse.readlinkSync('copied/symlink')).toBe('some/file');
};
describe('can overwrite symlink', function () {
var preparations = function () {
fse.mkdirsSync('to_copy');
fse.symlinkSync('some/file', 'to_copy/symlink');
fse.mkdirsSync('copied');
fse.symlinkSync('some/other_file', 'copied/symlink');
};
// SYNC
var expectations = function () {
expect(fse.lstatSync('copied/symlink').isSymbolicLink()).to.equal(true);
expect(fse.readlinkSync('copied/symlink')).to.equal(helper.osSep('some/file'));
};
it('sync', function () {
preparations();
jetpack.copy('to_copy', 'copied');
jetpack.copy('to_copy', 'copied', { overwrite: true });
expectations();
});
// ASYNC
it('async', function (done) {
preparations();
jetpack.copyAsync('to_copy', 'copied')
jetpack.copyAsync('to_copy', 'copied', { overwrite: true })
.then(function () {

@@ -467,30 +487,35 @@ expectations();

});
});
it('can overwrite symlink', function (done) {
if (process.platform !== 'win32') {
describe('copies also file permissions (unix only)', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('to_copy');
fse.symlinkSync('some/file', 'to_copy/symlink');
fse.mkdirsSync('copied');
fse.symlinkSync('some/other_file', 'copied/symlink');
fse.outputFileSync('a/b/c.txt', 'abc');
fse.chmodSync('a/b', '700');
fse.chmodSync('a/b/c.txt', '711');
};
var expectations = function () {
expect(fse.lstatSync('copied/symlink').isSymbolicLink()).toBe(true);
expect(fse.readlinkSync('copied/symlink')).toBe('some/file');
path('x/b').shouldHaveMode('700');
path('x/b/c.txt').shouldHaveMode('711');
};
// SYNC
preparations();
jetpack.copy('to_copy', 'copied', { overwrite: true });
expectations();
// ASYNC
preparations();
jetpack.copyAsync('to_copy', 'copied', { overwrite: true })
.then(function () {
it('sync', function () {
preparations();
jetpack.copy('a', 'x');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.copyAsync('a', 'x')
.then(function () {
expectations();
done();
});
});
});
});
} else {
// TODO what with Windows?
}
});

@@ -1,6 +0,3 @@

/* eslint-env jasmine */
'use strict';
var pathUtil = require('path');
var expect = require('chai').expect;
var jetpack = require('..');

@@ -10,3 +7,3 @@

it('returns the same path as process.cwd for main instance of jetpack', function () {
expect(jetpack.cwd()).toBe(process.cwd());
expect(jetpack.cwd()).to.equal(process.cwd());
});

@@ -16,8 +13,8 @@

var jetCwd = jetpack.cwd('/'); // absolute path
expect(jetCwd.cwd()).toBe(pathUtil.resolve(process.cwd(), '/'));
expect(jetCwd.cwd()).to.equal(pathUtil.resolve(process.cwd(), '/'));
jetCwd = jetpack.cwd('../..'); // relative path
expect(jetCwd.cwd()).toBe(pathUtil.resolve(process.cwd(), '../..'));
expect(jetCwd.cwd()).to.equal(pathUtil.resolve(process.cwd(), '../..'));
expect(jetpack.cwd()).toBe(process.cwd()); // cwd of main lib should be intact
expect(jetpack.cwd()).to.equal(process.cwd()); // cwd of main lib should be intact
});

@@ -30,6 +27,6 @@

jetCwd1 = jetpack.cwd('..');
expect(jetCwd1.cwd()).toBe(pathUtil.resolve(process.cwd(), '..'));
expect(jetCwd1.cwd()).to.equal(pathUtil.resolve(process.cwd(), '..'));
jetCwd2 = jetCwd1.cwd('..');
expect(jetCwd2.cwd()).toBe(pathUtil.resolve(process.cwd(), '../..'));
expect(jetCwd2.cwd()).to.equal(pathUtil.resolve(process.cwd(), '../..'));
});

@@ -39,4 +36,4 @@

var jetCwd = jetpack.cwd('a', 'b', 'c');
expect(jetCwd.cwd()).toBe(pathUtil.resolve(process.cwd(), 'a', 'b', 'c'));
expect(jetCwd.cwd()).to.equal(pathUtil.resolve(process.cwd(), 'a', 'b', 'c'));
});
});

@@ -1,30 +0,23 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var pathUtil = require('path');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('dir |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('dir', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
describe('ensure dir exists |', function () {
it("creates dir if it doesn't exist", function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
expect('x').toBeDirectory();
};
describe("creates directory if it doesn't exist", function () {
var expectations = function () {
path('x').shouldBeDirectory();
};
// SYNC
preparations();
it('sync', function () {
jetpack.dir('x');
expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.dirAsync('x')

@@ -36,18 +29,20 @@ .then(function () {

});
});
it('does nothing if dir already exists', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('x');
};
var expectations = function () {
expect('x').toBeDirectory();
};
describe('does nothing if directory already exists', function () {
var preparations = function () {
fse.mkdirsSync('x');
};
// SYNC
var expectations = function () {
path('x').shouldBeDirectory();
};
it('sync', function () {
preparations();
jetpack.dir('x');
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -60,18 +55,15 @@ jetpack.dirAsync('x')

});
});
it('creates nested directories if necessary', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
expect('a/b/c').toBeDirectory();
};
describe('creates nested directories if necessary', function () {
var expectations = function () {
path('a/b/c').shouldBeDirectory();
};
// SYNC
preparations();
it('sync', function () {
jetpack.dir('a/b/c');
expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.dirAsync('a/b/c')

@@ -85,18 +77,20 @@ .then(function () {

describe('ensures dir empty |', function () {
it('not bothers about emptiness if not specified', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('a/b');
};
var expectations = function () {
expect('a/b').toExist();
};
describe("doesn't touch directory content by default", function () {
var preparations = function () {
fse.mkdirsSync('a/b');
fse.outputFileSync('a/c.txt', 'abc');
};
// SYNC
var expectations = function () {
path('a/b').shouldBeDirectory();
path('a/c.txt').shouldBeFileWithContent('abc');
};
it('sync', function () {
preparations();
jetpack.dir('a');
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -109,19 +103,21 @@ jetpack.dirAsync('a')

});
});
it('makes dir empty', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/file.txt', 'abc');
};
var expectations = function () {
expect('a/b/file.txt').not.toExist();
expect('a').toExist();
};
describe('makes directory empty if that option specified', function () {
var preparations = function () {
fse.outputFileSync('a/b/file.txt', 'abc');
};
// SYNC
var expectations = function () {
path('a/b/file.txt').shouldNotExist();
path('a').shouldBeDirectory();
};
it('sync', function () {
preparations();
jetpack.dir('a', { empty: true });
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -136,210 +132,192 @@ jetpack.dirAsync('a', { empty: true })

it('halts if given path is something other than directory', function (done) {
describe('throws if given path is something other than directory', function () {
var preparations = function () {
fse.outputFileSync('a', 'abc');
};
var expectations = function (err) {
expect(err.message).toContain('exists but is not a directory.');
expect(err.message).to.have.string('exists but is not a directory');
};
fse.outputFileSync('a', 'abc');
it('sync', function () {
preparations();
try {
jetpack.dir('a');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.dir('a');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.dirAsync('a')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.dirAsync('a')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('respects internal CWD of jetpack instance', function () {
var expectations = function () {
expect('a/b').toBeDirectory();
path('a/b').shouldBeDirectory();
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.dir('b');
expectations();
// ASYNC
preparations();
jetContext.dirAsync('b')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
jetContext.dir('b');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
jetContext.dirAsync('b')
.then(function () {
expectations();
done();
});
});
});
it('returns jetack instance pointing on this directory', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('returns jetack instance pointing on this directory', function () {
var expectations = function (jetpackContext) {
expect(jetpackContext.cwd()).toBe(pathUtil.resolve('a'));
expect(jetpackContext.cwd()).to.equal(pathUtil.resolve('a'));
};
// SYNC
preparations();
expectations(jetpack.dir('a'));
it('sync', function () {
expectations(jetpack.dir('a'));
});
// ASYNC
preparations();
jetpack.dirAsync('a')
.then(function (jetpackContext) {
expectations(jetpackContext);
done();
it('async', function (done) {
jetpack.dirAsync('a')
.then(function (jetpackContext) {
expectations(jetpackContext);
done();
});
});
});
describe('windows specyfic |', function () {
if (process.platform !== 'win32') {
return;
}
it('specyfying mode have no effect, and throws no error', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
if (process.platform !== 'win32') {
describe('sets mode to newly created directory (unix only)', function () {
var expectations = function () {
expect('x').toBeDirectory();
path('a').shouldHaveMode('511');
};
// SYNC
preparations();
jetpack.dir('x', { mode: '511' });
expectations();
it('sync, mode passed as string', function () {
jetpack.dir('a', { mode: '511' });
expectations();
});
// ASYNC
preparations();
jetpack.dirAsync('x', { mode: '511' })
.then(function () {
it('sync, mode passed as number', function () {
jetpack.dir('a', { mode: parseInt('511', 8) });
expectations();
done();
});
});
});
describe('*nix specyfic |', function () {
if (process.platform === 'win32') {
return;
}
it('async, mode passed as string', function (done) {
jetpack.dirAsync('a', { mode: '511' })
.then(function () {
expectations();
done();
});
});
// Tests assume umask is not greater than 022
it('async, mode passed as number', function (done) {
jetpack.dirAsync('a', { mode: parseInt('511', 8) })
.then(function () {
expectations();
done();
});
});
});
it('sets mode to newly created directory', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('sets desired mode to every created directory (unix only)', function () {
var expectations = function () {
expect('a').toHaveMode('511');
path('a').shouldHaveMode('711');
path('a/b').shouldHaveMode('711');
};
// SYNC
// mode as a string
preparations();
jetpack.dir('a', { mode: '511' });
expectations();
// mode as a number
preparations();
jetpack.dir('a', { mode: parseInt('511', 8) });
expectations();
// ASYNC
// mode as a string
preparations();
jetpack.dirAsync('a', { mode: '511' })
.then(function () {
it('sync', function () {
jetpack.dir('a/b', { mode: '711' });
expectations();
});
// mode as a number
preparations();
return jetpack.dirAsync('a', { mode: parseInt('511', 8) });
})
.then(function () {
expectations();
done();
it('async', function (done) {
jetpack.dirAsync('a/b', { mode: '711' })
.then(function () {
expectations();
done();
});
});
});
it('sets that mode to every created directory', function (done) {
describe('changes mode of existing directory to desired (unix only)', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirSync('a', '777');
};
var expectations = function () {
expect('a').toHaveMode('711');
expect('a/b').toHaveMode('711');
path('a').shouldHaveMode('511');
};
// SYNC
preparations();
jetpack.dir('a/b', { mode: '711' });
expectations();
// ASYNC
preparations();
jetpack.dirAsync('a/b', { mode: '711' })
.then(function () {
it('sync', function () {
preparations();
jetpack.dir('a', { mode: '511' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.dirAsync('a', { mode: '511' })
.then(function () {
expectations();
done();
});
});
});
it('changes mode of existing directory to desired', function (done) {
describe('leaves mode of directory intact by default (unix only)', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirSync('a', '777');
fse.mkdirSync('a', '700');
};
var expectations = function () {
expect('a').toHaveMode('511');
path('a').shouldHaveMode('700');
};
// SYNC
preparations();
jetpack.dir('a', { mode: '511' });
expectations();
// ASYNC
preparations();
jetpack.dirAsync('a', { mode: '511' })
.then(function () {
it('sync', function () {
preparations();
jetpack.dir('a');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.dirAsync('a')
.then(function () {
expectations();
done();
});
});
});
it('leaves mode of directory intact if this option was not specified', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirSync('a', '700');
};
} else {
describe('specyfying mode have no effect and throws no error (windows only)', function () {
var expectations = function () {
expect('a').toHaveMode('700');
path('x').shouldBeDirectory();
};
// SYNC
preparations();
jetpack.dir('a');
expectations();
// ASYNC
preparations();
jetpack.dirAsync('a')
.then(function () {
it('sync', function () {
jetpack.dir('x', { mode: '511' });
expectations();
done();
});
it('async', function (done) {
jetpack.dirAsync('x', { mode: '511' })
.then(function () {
expectations();
done();
});
});
});
});
}
});

@@ -1,85 +0,127 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('exists', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it("returns false if file doesn't exist", function (done) {
// SYNC
expect(jetpack.exists('file.txt')).toBe(false);
describe("returns false if file doesn't exist", function () {
var expectations = function (exists) {
expect(exists).to.equal(false);
};
// ASYNC
jetpack.existsAsync('file.txt')
.then(function (exists) {
expect(exists).toBe(false);
done();
it('sync', function () {
expectations(jetpack.exists('file.txt'));
});
it('async', function (done) {
jetpack.existsAsync('file.txt')
.then(function (exists) {
expectations(exists);
done();
});
});
});
it("returns 'dir' if directory exists on given path", function (done) {
fse.mkdirsSync('a');
describe("returns 'dir' if directory exists on given path", function () {
var preparations = function () {
fse.mkdirsSync('a');
};
// SYNC
expect(jetpack.exists('a')).toBe('dir');
var expectations = function (exists) {
expect(exists).to.equal('dir');
};
// ASYNC
jetpack.existsAsync('a')
.then(function (exists) {
expect(exists).toBe('dir');
done();
it('sync', function () {
preparations();
expectations(jetpack.exists('a'));
});
it('async', function (done) {
preparations();
jetpack.existsAsync('a')
.then(function (exists) {
expectations(exists);
done();
});
});
});
it("returns 'file' if file exists on given path", function (done) {
fse.outputFileSync('text.txt', 'abc');
describe("returns 'file' if file exists on given path", function () {
var preparations = function () {
fse.outputFileSync('text.txt', 'abc');
};
// SYNC
expect(jetpack.exists('text.txt')).toBe('file');
var expectations = function (exists) {
expect(exists).to.equal('file');
};
// ASYNC
jetpack.existsAsync('text.txt')
.then(function (exists) {
expect(exists).toBe('file');
done();
it('sync', function () {
preparations();
expectations(jetpack.exists('text.txt'));
});
it('async', function (done) {
preparations();
jetpack.existsAsync('text.txt')
.then(function (exists) {
expectations(exists);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
var jetContext = jetpack.cwd('a');
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/text.txt', 'abc');
};
fse.outputFileSync('a/text.txt', 'abc');
var expectations = function (exists) {
expect(exists).to.equal('file');
};
// SYNC
expect(jetContext.exists('text.txt')).toBe('file');
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.exists('text.txt'));
});
// ASYNC
jetContext.existsAsync('text.txt')
.then(function (exists) {
expect(exists).toBe('file');
done();
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.existsAsync('text.txt')
.then(function (exists) {
expectations(exists);
done();
});
});
});
describe('edge cases', function () {
it("ENOTDIR error changes into 'false'", function (done) {
// We have here malformed path: /some/dir/file.txt/some_dir
// (so file is in the middle of path, not at the end).
// This leads to ENOTDIR error, but technically speaking this
// path doesn't exist so let's just return false.
describe("(edge case) ENOTDIR error changed into 'false'", function () {
// We have here malformed path: /some/dir/file.txt/some_dir
// (so file is in the middle of path, not at the end).
// This leads to ENOTDIR error, but technically speaking this
// path doesn't exist so let's just return false.
// TODO Not fully sure this is sensible behaviour. It just turns one misleading
// state into another. The fact is this path is malformed. Can we do better?
var preparations = function () {
fse.outputFileSync('text.txt', 'abc');
};
// SYNC
expect(jetpack.exists('text.txt/something')).toBe(false);
var expectations = function (exists) {
expect(exists).to.equal(false);
};
// ASYNC
it('sync', function () {
preparations();
expectations(jetpack.exists('text.txt/something'));
});
it('async', function (done) {
preparations();
jetpack.existsAsync('text.txt/something')
.then(function (exists) {
expect(exists).toBe(false);
expectations(exists);
done();

@@ -86,0 +128,0 @@ });

@@ -1,29 +0,22 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('file |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('file', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
describe('ensure file exists |', function () {
it("file doesn't exist before call", function (done) {
var prepartions = function () {
helper.clearWorkingDir();
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('');
};
describe("creates file if it doesn't exist", function () {
var expectations = function () {
path('file.txt').shouldBeFileWithContent('');
};
// SYNC
prepartions();
it('sync', function () {
jetpack.file('file.txt');
expectations();
});
// ASYNC
prepartions();
it('async', function (done) {
jetpack.fileAsync('file.txt')

@@ -33,20 +26,23 @@ .then(function () {

done();
});
})
.catch(done);
});
});
it('file already exists', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('abc');
};
describe('leaves file intact if it already exists', function () {
var preparations = function () {
fse.outputFileSync('file.txt', 'abc');
};
// SYNC
var expectations = function () {
path('file.txt').shouldBeFileWithContent('abc');
};
it('sync', function () {
preparations();
jetpack.file('file.txt');
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -57,22 +53,18 @@ jetpack.fileAsync('file.txt')

done();
});
})
.catch(done);
});
});
describe('ensures file content |', function () {
it('from string', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('ąbć');
};
describe('can save file content given as string', function () {
var expectations = function () {
path('file.txt').shouldBeFileWithContent('ąbć');
};
// SYNC
preparations();
it('sync', function () {
jetpack.file('file.txt', { content: 'ąbć' });
expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.fileAsync('file.txt', { content: 'ąbć' })

@@ -82,23 +74,18 @@ .then(function () {

done();
});
})
.catch(done);
});
});
it('from buffer', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
var buf = fse.readFileSync('file');
expect(buf[0]).toBe(11);
expect(buf[1]).toBe(22);
expect(buf.length).toBe(2);
};
describe('can save file content given as buffer', function () {
var expectations = function () {
path('file').shouldBeFileWithContent(new Buffer([11, 22]));
};
// SYNC
preparations();
it('sync', function () {
jetpack.file('file', { content: new Buffer([11, 22]) });
expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.fileAsync('file', { content: new Buffer([11, 22]) })

@@ -108,26 +95,24 @@ .then(function () {

done();
});
})
.catch(done);
});
});
it('from object (json)', function (done) {
var obj = {
a: 'abc',
b: 123
};
describe('can save file content given as plain JS object (will be saved as JSON)', function () {
var obj = {
a: 'abc',
b: 123
};
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
var data = JSON.parse(fse.readFileSync('file.txt', 'utf8'));
expect(data).toEqual(obj);
};
var expectations = function () {
var data = JSON.parse(fse.readFileSync('file.txt', 'utf8'));
expect(data).to.eql(obj);
};
// SYNC
preparations();
it('sync', function () {
jetpack.file('file.txt', { content: obj });
expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.fileAsync('file.txt', { content: obj })

@@ -137,24 +122,22 @@ .then(function () {

done();
});
})
.catch(done);
});
});
it('written JSON data can be indented', function (done) {
var obj = {
a: 'abc',
b: 123
};
describe('written JSON data can be indented', function () {
var obj = {
a: 'abc',
b: 123
};
var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
var sizeA = fse.statSync('a.json').size;
var sizeB = fse.statSync('b.json').size;
var sizeC = fse.statSync('c.json').size;
expect(sizeB).toBeGreaterThan(sizeA);
expect(sizeC).toBeGreaterThan(sizeB);
};
var expectations = function () {
var sizeA = fse.statSync('a.json').size;
var sizeB = fse.statSync('b.json').size;
var sizeC = fse.statSync('c.json').size;
expect(sizeB).to.be.above(sizeA);
expect(sizeC).to.be.above(sizeB);
};
// SYNC
preparations();
it('sync', function () {
jetpack.file('a.json', { content: obj, jsonIndent: 0 });

@@ -164,5 +147,5 @@ jetpack.file('b.json', { content: obj }); // Default indent = 2

expectations();
});
// ASYNC
preparations();
it('async', function (done) {
jetpack.fileAsync('a.json', { content: obj, jsonIndent: 0 })

@@ -178,20 +161,23 @@ .then(function () {

done();
});
})
.catch(done);
});
});
it('replaces content of already existing file', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.writeFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').toBeFileWithContent('123');
};
describe('replaces content of already existing file', function () {
var preparations = function () {
fse.writeFileSync('file.txt', 'abc');
};
// SYNC
var expectations = function () {
path('file.txt').shouldBeFileWithContent('123');
};
it('sync', function () {
preparations();
jetpack.file('file.txt', { content: '123' });
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -202,215 +188,215 @@ jetpack.fileAsync('file.txt', { content: '123' })

done();
});
})
.catch(done);
});
});
it('halts if given path is not a file', function (done) {
describe('throws if given path is not a file', function () {
var preparations = function () {
fse.mkdirsSync('a');
};
var expectations = function (err) {
expect(err.message).toContain('exists but is not a file.');
expect(err.message).to.have.string('exists but is not a file.');
};
fse.mkdirsSync('a');
it('sync', function () {
preparations();
try {
jetpack.file('a');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.file('a');
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.fileAsync('a')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.fileAsync('a')
.catch(function (err) {
expectations(err);
done();
})
.catch(done);
});
});
it("if directory for file doesn't exist creates it too", function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe("if directory for file doesn't exist creates it as well", function () {
var expectations = function () {
expect('a/b/c.txt').toBeFileWithContent('');
path('a/b/c.txt').shouldBeFileWithContent('');
};
// SYNC
preparations();
jetpack.file('a/b/c.txt');
expectations();
// ASYNC
preparations();
jetpack.fileAsync('a/b/c.txt')
.then(function () {
it('sync', function () {
jetpack.file('a/b/c.txt');
expectations();
done();
});
it('async', function (done) {
jetpack.fileAsync('a/b/c.txt')
.then(function () {
expectations();
done();
})
.catch(done);
});
});
it('returns currently used jetpack instance', function (done) {
// SYNC
expect(jetpack.file('file.txt')).toBe(jetpack);
describe('returns currently used jetpack instance', function () {
var expectations = function (jetpackContext) {
expect(jetpackContext).to.equal(jetpack);
};
// ASYNC
jetpack.fileAsync('file.txt')
.then(function (jetpackContext) {
expect(jetpackContext).toBe(jetpack);
done();
it('sync', function () {
expectations(jetpack.file('file.txt'));
});
it('async', function (done) {
jetpack.fileAsync('file.txt')
.then(function (jetpackContext) {
expectations(jetpackContext);
done();
})
.catch(done);
});
});
it('respects internal CWD of jetpack instance', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('respects internal CWD of jetpack instance', function () {
var expectations = function () {
expect('a/b.txt').toBeFileWithContent('');
path('a/b.txt').shouldBeFileWithContent('');
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.file('b.txt');
expectations();
// ASYNC
preparations();
jetContext.fileAsync('b.txt')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
jetContext.file('b.txt');
expectations();
done();
});
});
describe('windows specyfic |', function () {
if (process.platform !== 'win32') {
return;
}
it('specyfying mode should have no effect, and throw no error', function (done) {
// SYNC
jetpack.file('file.txt', { mode: '511' });
helper.clearWorkingDir();
// ASYNC
jetpack.fileAsync('file.txt', { mode: '511' })
it('async', function (done) {
var jetContext = jetpack.cwd('a');
jetContext.fileAsync('b.txt')
.then(function () {
expectations();
done();
});
})
.catch(done);
});
});
describe('*nix specyfic |', function () {
if (process.platform === 'win32') {
return;
}
// Tests assume umask is not greater than 022
it('sets mode of newly created file', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
if (process.platform !== 'win32') {
describe('sets mode of newly created file (unix only)', function () {
var expectations = function () {
expect('file.txt').toHaveMode('511');
path('file.txt').shouldHaveMode('711');
};
// SYNC
// mode as string
preparations();
jetpack.file('file.txt', { mode: '511' });
expectations();
// mode as number
preparations();
jetpack.file('file.txt', { mode: parseInt('511', 8) });
expectations();
// AYNC
// mode as string
preparations();
jetpack.fileAsync('file.txt', { mode: '511' })
.then(function () {
it('sync, mode passed as string', function () {
jetpack.file('file.txt', { mode: '711' });
expectations();
});
// mode as number
preparations();
return jetpack.fileAsync('file.txt', { mode: parseInt('511', 8) });
})
.then(function () {
it('sync, mode passed as number', function () {
jetpack.file('file.txt', { mode: parseInt('711', 8) });
expectations();
done();
});
it('async, mode passed as string', function (done) {
jetpack.fileAsync('file.txt', { mode: '711' })
.then(function () {
expectations();
done();
})
.catch(done);
});
it('async, mode passed as number', function (done) {
jetpack.fileAsync('file.txt', { mode: parseInt('711', 8) })
.then(function () {
expectations();
done();
})
.catch(done);
});
});
it("changes mode of existing file if doesn't match", function (done) {
describe("changes mode of existing file if it doesn't match (unix only)", function () {
var preparations = function () {
helper.clearWorkingDir();
fse.writeFileSync('file.txt', 'abc', { mode: '700' });
};
var expectations = function () {
expect('file.txt').toHaveMode('511');
path('file.txt').shouldHaveMode('511');
};
// SYNC
preparations();
jetpack.file('file.txt', { mode: '511' });
expectations();
// ASYNC
preparations();
jetpack.fileAsync('file.txt', { mode: '511' })
.then(function () {
it('sync', function () {
preparations();
jetpack.file('file.txt', { mode: '511' });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.fileAsync('file.txt', { mode: '511' })
.then(function () {
expectations();
done();
})
.catch(done);
});
});
it('leaves mode of file intact if not explicitly specified', function (done) {
describe('leaves mode of file intact if not explicitly specified (unix only)', function () {
var preparations = function () {
fse.writeFileSync('file.txt', 'abc', { mode: '700' });
};
var expectations = function () {
expect('file.txt').toHaveMode('700');
path('file.txt').shouldHaveMode('700');
};
preparations();
// SYNC
// ensure exists
jetpack.file('file.txt');
expectations();
// make file empty
jetpack.file('file.txt', { empty: true });
expectations();
// set file content
jetpack.file('file.txt', { content: '123' });
expectations();
// AYNC
// ensure exists
jetpack.fileAsync('file.txt')
.then(function () {
it('sync, ensure exists', function () {
preparations();
jetpack.file('file.txt');
expectations();
});
// make file empty
return jetpack.fileAsync('file.txt', { empty: true });
})
.then(function () {
it('sync, ensure content', function () {
preparations();
jetpack.file('file.txt', { content: 'abc' });
expectations();
});
// set file content
return jetpack.fileAsync('file.txt', { content: '123' });
})
.then(function () {
expectations();
done();
it('async, ensure exists', function (done) {
preparations();
jetpack.fileAsync('file.txt')
.then(function () {
expectations();
done();
})
.catch(done);
});
it('async, ensure content', function (done) {
preparations();
jetpack.fileAsync('file.txt', { content: 'abc' })
.then(function () {
expectations();
done();
})
.catch(done);
});
});
});
} else {
describe('specyfying mode have no effect and throws no error (windows only)', function () {
it('sync', function () {
jetpack.file('file.txt', { mode: '711' });
});
it('async', function (done) {
jetpack.fileAsync('file.txt', { mode: '711' })
.then(function () {
done();
})
.catch(done);
});
});
}
});

@@ -1,95 +0,121 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('find |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('find', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('returns list of relative paths anchored to CWD', function (done) {
describe('returns list of relative paths anchored to CWD', function () {
var preparations = function () {
fse.outputFileSync('a/b/file.txt', 'abc');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['a/b/file.txt']);
var normalizedPaths = helper.osSep(['a/b/file.txt']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('a/b/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.find('a', { matching: '*.txt' }));
});
// SYNC
expectations(jetpack.find('a', { matching: '*.txt' }));
// ASYNC
jetpack.findAsync('a', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
it('async', function (done) {
preparations();
jetpack.findAsync('a', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
it('if recursive option set will exclude subfolders from search', function (done) {
describe('if recursive=false will exclude subfolders from search', function () {
var preparations = function () {
fse.outputFileSync('x/file.txt', 'abc');
fse.outputFileSync('x/y/file.txt', '123');
fse.outputFileSync('x/y/b/file.txt', '456');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['x/file.txt']);
var normalizedPaths = helper.osSep(['x/file.txt']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('x/file.txt', 'abc');
fse.outputFileSync('x/y/file.txt', '123');
fse.outputFileSync('x/y/b/file.txt', '456');
it('sync', function () {
preparations();
expectations(jetpack.find('x', { matching: '*.txt', recursive: false }));
});
// SYNC
expectations(jetpack.find('x', { matching: '*.txt', recursive: false }));
// ASYNC
jetpack.findAsync('x', { matching: '*.txt', recursive: false })
.then(function (found) {
expectations(found);
done();
it('async', function (done) {
preparations();
jetpack.findAsync('x', { matching: '*.txt', recursive: false })
.then(function (found) {
expectations(found);
done();
});
});
});
it('defaults to CWD if no path provided', function (done) {
describe('defaults to CWD if no path provided', function () {
var preparations = function () {
fse.outputFileSync('a/b/file.txt', 'abc');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['a/b/file.txt']);
var normalizedPaths = helper.osSep(['a/b/file.txt']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('a/b/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.find({ matching: '*.txt' }));
});
// SYNC
expectations(jetpack.find({ matching: '*.txt' }));
// ASYNC
jetpack.findAsync({ matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
it('async', function (done) {
preparations();
jetpack.findAsync({ matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
it('returns empty list if nothing found', function (done) {
describe('returns empty list if nothing found', function () {
var preparations = function () {
fse.outputFileSync('a/b/c.md', 'abc');
};
var expectations = function (found) {
expect(found).toEqual([]);
expect(found).to.eql([]);
};
fse.outputFileSync('a/b/c.md', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.find('a', { matching: '*.txt' }));
});
// SYNC
expectations(jetpack.find('a', { matching: '*.txt' }));
// ASYNC
jetpack.findAsync('a', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
it('async', function (done) {
preparations();
jetpack.findAsync('a', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
it('finds all paths which match globs', function (done) {
describe('finds all paths which match globs', function () {
var preparations = function () {
fse.outputFileSync('a/b/file.txt', '1');
fse.outputFileSync('a/b/c/file.txt', '2');
fse.outputFileSync('a/b/c/file.md', '3');
fse.outputFileSync('a/x/y/z', 'Zzzzz...');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
normalizedFound.sort();
expect(normalizedFound).toEqual([
var normalizedPaths = helper.osSep([
'a/b/c/file.txt',

@@ -99,163 +125,324 @@ 'a/b/file.txt',

]);
found.sort();
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('a/b/file.txt', '1');
fse.outputFileSync('a/b/c/file.txt', '2');
fse.outputFileSync('a/b/c/file.md', '3');
fse.outputFileSync('a/x/y/z', 'Zzzzz...');
it('sync', function () {
preparations();
expectations(jetpack.find('a', { matching: ['*.txt', 'z'] }));
});
// SYNC
expectations(jetpack.find('a', { matching: ['*.txt', 'z'] }));
it('async', function (done) {
preparations();
jetpack.findAsync('a', { matching: ['*.txt', 'z'] })
.then(function (found) {
expectations(found);
done();
});
});
});
// ASYNC
jetpack.findAsync('a', { matching: ['*.txt', 'z'] })
.then(function (found) {
expectations(found);
done();
describe("anchors globs to directory you're finding in", function () {
var preparations = function () {
fse.outputFileSync('x/y/a/b/file.txt', '123');
fse.outputFileSync('x/y/a/b/c/file.txt', '456');
};
var expectations = function (found) {
var normalizedPaths = helper.osSep(['x/y/a/b/file.txt']);
expect(found).to.eql(normalizedPaths);
};
it('sync', function () {
preparations();
expectations(jetpack.find('x/y/a', { matching: 'b/*.txt' }));
});
it('async', function (done) {
preparations();
jetpack.findAsync('x/y/a', { matching: 'b/*.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
it("anchors globs to directory you're finding in", function (done) {
describe('can use ./ as indication of anchor directory', function () {
var preparations = function () {
fse.outputFileSync('x/y/file.txt', '123');
fse.outputFileSync('x/y/b/file.txt', '456');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['x/y/a/b/file.txt']);
var normalizedPaths = helper.osSep(['x/y/file.txt']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('x/y/a/b/file.txt', '123');
fse.outputFileSync('x/y/a/b/c/file.txt', '456');
it('sync', function () {
preparations();
expectations(jetpack.find('x/y', { matching: './file.txt' }));
});
// SYNC
expectations(jetpack.find('x/y/a', { matching: 'b/*.txt' }));
it('async', function (done) {
preparations();
jetpack.findAsync('x/y', { matching: './file.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
// ASYNC
jetpack.findAsync('x/y/a', { matching: 'b/*.txt' })
.then(function (found) {
expectations(found);
done();
describe('deals with negation globs', function () {
var preparations = function () {
fse.outputFileSync('x/y/a/b', 'bbb');
fse.outputFileSync('x/y/a/x', 'xxx');
fse.outputFileSync('x/y/a/y', 'yyy');
fse.outputFileSync('x/y/a/z', 'zzz');
};
var expectations = function (found) {
var normalizedPaths = helper.osSep(['x/y/a/b']);
expect(found).to.eql(normalizedPaths);
};
it('sync', function () {
preparations();
expectations(jetpack.find('x/y', {
matching: [
'a/*',
// Three different pattern types to test:
'!x',
'!a/y',
'!./a/z'
]
}));
});
it('async', function (done) {
preparations();
jetpack.findAsync('x/y', {
matching: [
'a/*',
// Three different pattern types to test:
'!x',
'!a/y',
'!./a/z'
]
})
.then(function (found) {
expectations(found);
done();
});
});
});
it('can use ./ as indication of anchor directory', function (done) {
describe("doesn't look for directories by default", function () {
var preparations = function () {
fse.outputFileSync('a/b/foo1', 'abc');
fse.mkdirsSync('a/b/foo2');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['x/y/file.txt']);
var normalizedPaths = helper.osSep(['a/b/foo1']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('x/y/file.txt', '123');
fse.outputFileSync('x/y/b/file.txt', '456');
it('sync', function () {
preparations();
expectations(jetpack.find('a', { matching: 'foo*' }));
});
// SYNC
expectations(jetpack.find('x/y', { matching: './file.txt' }));
it('async', function (done) {
preparations();
jetpack.findAsync('a', { matching: 'foo*' })
.then(function (found) {
expectations(found);
done();
});
});
});
// ASYNC
jetpack.findAsync('x/y', { matching: './file.txt' })
.then(function (found) {
expectations(found);
done();
describe('can look for files and directories', function () {
var preparations = function () {
fse.outputFileSync('a/b/foo1', 'abc');
fse.mkdirsSync('a/b/foo2');
};
var expectations = function (found) {
var normalizedPaths = helper.osSep(['a/b/foo1', 'a/b/foo2']);
expect(found).to.eql(normalizedPaths);
};
it('sync', function () {
preparations();
expectations(jetpack.find('a', {
matching: 'foo*',
directories: true
}));
});
it('async', function (done) {
preparations();
jetpack.findAsync('a', {
matching: 'foo*',
directories: true
})
.then(function (found) {
expectations(found);
done();
})
.catch(done);
});
});
it('deals with negation globs', function (done) {
describe('can look for only directories', function () {
var preparations = function () {
fse.outputFileSync('a/b/foo1', 'abc');
fse.mkdirsSync('a/b/foo2');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['x/y/a/b']);
var normalizedPaths = helper.osSep(['a/b/foo2']);
expect(found).to.eql(normalizedPaths);
};
fse.outputFileSync('x/y/a/b', 'bbb');
fse.outputFileSync('x/y/a/x', 'xxx');
fse.outputFileSync('x/y/a/y', 'yyy');
fse.outputFileSync('x/y/a/z', 'zzz');
it('sync', function () {
preparations();
expectations(jetpack.find('a', {
matching: 'foo*',
files: false,
directories: true
}));
});
// SYNC
expectations(jetpack.find('x/y', {
matching: [
'a/*',
// Three different pattern types to test:
'!x',
'!a/y',
'!./a/z'
]
}));
it('async', function (done) {
preparations();
jetpack.findAsync('a', {
matching: 'foo*',
files: false,
directories: true
})
.then(function (found) {
expectations(found);
done();
})
.catch(done);
});
});
// ASYNC
jetpack.findAsync('x/y', {
matching: [
'a/*',
// Three different pattern types to test:
'!x',
'!a/y',
'!./a/z'
]
})
.then(function (found) {
expectations(found);
done();
describe('when you turn off files and directoies returns empty list', function () {
var preparations = function () {
fse.outputFileSync('a/b/foo1', 'abc');
fse.mkdirsSync('a/b/foo2');
};
var expectations = function (found) {
expect(found).to.eql([]);
};
it('sync', function () {
preparations();
expectations(jetpack.find('a', {
matching: 'foo*',
files: false,
directories: false
}));
});
it('async', function (done) {
preparations();
jetpack.findAsync('a', {
matching: 'foo*',
files: false,
directories: false
})
.then(function (found) {
expectations(found);
done();
});
});
});
it("throws if path doesn't exist", function (done) {
describe("throws if path doesn't exist", function () {
var expectations = function (err) {
expect(err.code).toBe('ENOENT');
expect(err.message).toContain("Path you want to find stuff in doesn't exist");
expect(err.code).to.equal('ENOENT');
expect(err.message).to.have.string("Path you want to find stuff in doesn't exist");
};
// SYNC
try {
jetpack.find('a', { matching: '*.txt' });
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
it('sync', function () {
try {
jetpack.find('a', { matching: '*.txt' });
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// ASYNC
jetpack.findAsync('a', { matching: '*.txt' })
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
jetpack.findAsync('a', { matching: '*.txt' })
.catch(function (err) {
expectations(err);
done();
});
});
});
it('throws if path is a file, not a directory', function (done) {
describe('throws if path is a file, not a directory', function () {
var preparations = function () {
fse.outputFileSync('a/b', 'abc');
};
var expectations = function (err) {
expect(err.code).toBe('ENOTDIR');
expect(err.message).toContain('Path you want to find stuff in must be a directory');
expect(err.code).to.equal('ENOTDIR');
expect(err.message).to.have.string('Path you want to find stuff in must be a directory');
};
fse.outputFileSync('a/b', 'abc');
it('sync', function () {
preparations();
try {
jetpack.find('a/b', { matching: '*.txt' });
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.find('a/b', { matching: '*.txt' });
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.findAsync('a/b', { matching: '*.txt' })
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.findAsync('a/b', { matching: '*.txt' })
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/b/c/d.txt', 'abc');
};
var expectations = function (found) {
var normalizedFound = helper.convertToUnixPathSeparators(found);
expect(normalizedFound).toEqual(['b/c/d.txt']); // NOT a/b/c/d.txt
var normalizedPaths = helper.osSep(['b/c/d.txt']); // NOT a/b/c/d.txt
expect(found).to.eql(normalizedPaths);
};
var jetContext = jetpack.cwd('a');
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.find('b', { matching: '*.txt' }));
});
fse.outputFileSync('a/b/c/d.txt', 'abc');
// SYNC
expectations(jetContext.find('b', { matching: '*.txt' }));
// ASYNC
jetContext.findAsync('b', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.findAsync('b', { matching: '*.txt' })
.then(function (found) {
expectations(found);
done();
});
});
});
});

@@ -1,16 +0,18 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('inspectTree |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('inspectTree', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('inspects whole tree of files', function (done) {
describe('inspects whole tree of files', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
};
var expectations = function (data) {
expect(data).toEqual({
expect(data).to.eql({
name: 'dir',

@@ -40,49 +42,60 @@ type: 'dir',

fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir'));
});
// SYNC
expectations(jetpack.inspectTree('dir'));
// ASYNC
jetpack.inspectTreeAsync('dir')
.then(function (tree) {
expectations(tree);
done();
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir')
.then(function (tree) {
expectations(tree);
done();
});
});
});
it('can calculate size of a whole tree', function (done) {
describe('can calculate size of a whole tree', function () {
var preparations = function () {
fse.mkdirsSync('dir/empty');
fse.outputFileSync('dir/empty.txt', '');
fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
};
var expectations = function (data) {
// dir
expect(data.size).toBe(7);
expect(data.size).to.equal(7);
// dir/empty
expect(data.children[0].size).toBe(0);
expect(data.children[0].size).to.equal(0);
// dir/empty.txt
expect(data.children[1].size).toBe(0);
expect(data.children[1].size).to.equal(0);
// dir/file.txt
expect(data.children[2].size).toBe(3);
expect(data.children[2].size).to.equal(3);
// dir/subdir
expect(data.children[3].size).toBe(4);
expect(data.children[3].size).to.equal(4);
// dir/subdir/file.txt
expect(data.children[3].children[0].size).toBe(4);
expect(data.children[3].children[0].size).to.equal(4);
};
fse.mkdirsSync('dir/empty');
fse.outputFileSync('dir/empty.txt', '');
fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir'));
});
// SYNC
expectations(jetpack.inspectTree('dir'));
// ASYNC
jetpack.inspectTreeAsync('dir')
.then(function (tree) {
expectations(tree);
done();
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir')
.then(function (tree) {
expectations(tree);
done();
});
});
});
it('can output relative path for every tree node', function (done) {
describe('can output relative path for every tree node', function () {
var preparations = function () {
fse.outputFileSync('dir/subdir/file.txt', 'defg');
};
var expectations = function (data) {

@@ -106,23 +119,29 @@ // data will look like...

// }
expect(data.relativePath).toBe('.');
expect(data.children[0].relativePath).toBe('./subdir');
expect(data.children[0].children[0].relativePath).toBe('./subdir/file.txt');
expect(data.relativePath).to.equal('.');
expect(data.children[0].relativePath).to.equal('./subdir');
expect(data.children[0].children[0].relativePath).to.equal('./subdir/file.txt');
};
fse.outputFileSync('dir/subdir/file.txt', 'defg');
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir', { relativePath: true }));
});
// SYNC
expectations(jetpack.inspectTree('dir', { relativePath: true }));
// ASYNC
jetpack.inspectTreeAsync('dir', { relativePath: true })
.then(function (tree) {
expectations(tree);
done();
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir', { relativePath: true })
.then(function (tree) {
expectations(tree);
done();
});
});
});
it('if given path is a file still works OK', function (done) {
describe('if given path is a file just inspects that file', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
};
var expectations = function (data) {
expect(data).toEqual({
expect(data).to.eql({
name: 'file.txt',

@@ -134,18 +153,24 @@ type: 'file',

fse.outputFileSync('dir/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir/file.txt'));
});
// SYNC
expectations(jetpack.inspectTree('dir/file.txt'));
// ASYNC
jetpack.inspectTreeAsync('dir/file.txt')
.then(function (tree) {
expectations(tree);
done();
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir/file.txt')
.then(function (tree) {
expectations(tree);
done();
});
});
});
it('deals ok with empty directory', function (done) {
describe('behaves ok with empty directory', function () {
var preparations = function () {
fse.mkdirsSync('empty');
};
var expectations = function (data) {
expect(data).toEqual({
expect(data).to.eql({
name: 'empty',

@@ -158,81 +183,91 @@ type: 'dir',

fse.mkdirsSync('empty');
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('empty'));
});
// SYNC
expectations(jetpack.inspectTree('empty'));
// ASYNC
jetpack.inspectTreeAsync('empty')
.then(function (tree) {
expectations(tree);
done();
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('empty')
.then(function (tree) {
expectations(tree);
done();
});
});
});
it("returns undefined if path doesn't exist", function (done) {
describe("returns undefined if path doesn't exist", function () {
var expectations = function (data) {
expect(data).toBe(undefined);
expect(data).to.equal(undefined);
};
// SYNC
expectations(jetpack.inspectTree('nonexistent'));
it('sync', function () {
expectations(jetpack.inspectTree('nonexistent'));
});
// ASYNC
jetpack.inspectTreeAsync('nonexistent')
.then(function (dataAsync) {
expectations(dataAsync);
done();
it('async', function (done) {
jetpack.inspectTreeAsync('nonexistent')
.then(function (dataAsync) {
expectations(dataAsync);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function (data) {
expect(data.name).toBe('b.txt');
expect(data.name).to.equal('b.txt');
};
var jetContext = jetpack.cwd('a');
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.inspectTree('b.txt'));
});
fse.outputFileSync('a/b.txt', 'abc');
// SYNC
expectations(jetContext.inspectTree('b.txt'));
// ASYNC
jetContext.inspectTreeAsync('b.txt')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.inspectTreeAsync('b.txt')
.then(function (data) {
expectations(data);
done();
});
});
});
describe('*nix specific', function () {
if (process.platform === 'win32') {
return;
}
it('can deal with symlink', function (done) {
var expectations = function (tree) {
expect(tree).toEqual({
name: 'dir',
type: 'dir',
size: 3,
children: [{
name: 'file.txt',
type: 'file',
size: 3
}, {
name: 'symlinked_file.txt',
type: 'symlink',
pointsAt: 'dir/file.txt'
}]
});
};
describe('can inspect symlink', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
fse.symlinkSync('dir/file.txt', 'dir/symlinked_file.txt');
};
// SYNC
var expectations = function (tree) {
expect(tree).to.eql({
name: 'dir',
type: 'dir',
size: 3,
children: [{
name: 'file.txt',
type: 'file',
size: 3
}, {
name: 'symlinked_file.txt',
type: 'symlink',
pointsAt: helper.osSep('dir/file.txt')
}]
});
};
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir'));
});
// ASYNC
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir')

@@ -245,2 +280,60 @@ .then(function (tree) {

});
describe('can compute checksum of a whole tree', function () {
var preparations = function () {
fse.outputFileSync('dir/a.txt', 'abc');
fse.outputFileSync('dir/b.txt', 'defg');
};
var expectations = function (data) {
// md5 of
// 'a.txt' + '900150983cd24fb0d6963f7d28e17f72' +
// 'b.txt' + '025e4da7edac35ede583f5e8d51aa7ec'
expect(data.md5).to.equal('b0ff9df854172efe752cb36b96c8bccd');
// md5 of 'abc'
expect(data.children[0].md5).to.equal('900150983cd24fb0d6963f7d28e17f72');
// md5 of 'defg'
expect(data.children[1].md5).to.equal('025e4da7edac35ede583f5e8d51aa7ec');
};
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('dir', { checksum: 'md5' }));
});
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('dir', { checksum: 'md5' })
.then(function (tree) {
expectations(tree);
done();
});
});
});
describe('can count checksum of empty directory', function () {
var preparations = function () {
fse.mkdirsSync('empty_dir');
};
var expectations = function (data) {
// md5 of empty string
expect(data.md5).to.equal('d41d8cd98f00b204e9800998ecf8427e');
};
// SYNC
it('sync', function () {
preparations();
expectations(jetpack.inspectTree('empty_dir', { checksum: 'md5' }));
});
it('async', function (done) {
preparations();
jetpack.inspectTreeAsync('empty_dir', { checksum: 'md5' })
.then(function (tree) {
expectations(tree);
done();
});
});
});
});

@@ -1,16 +0,17 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('inspect |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('inspect', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('can inspect a file', function (done) {
describe('can inspect a file', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
};
var expectations = function (data) {
expect(data).toEqual({
expect(data).to.eql({
name: 'file.txt',

@@ -22,18 +23,24 @@ type: 'file',

fse.outputFileSync('dir/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.inspect('dir/file.txt'));
});
// SYNC
expectations(jetpack.inspect('dir/file.txt'));
// ASYNC
jetpack.inspectAsync('dir/file.txt')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
preparations();
jetpack.inspectAsync('dir/file.txt')
.then(function (data) {
expectations(data);
done();
});
});
});
it('can inspect a directory', function (done) {
describe('can inspect a directory', function () {
var preparations = function () {
fse.mkdirsSync('empty');
};
var expectations = function (data) {
expect(data).toEqual({
expect(data).to.eql({
name: 'empty',

@@ -44,106 +51,104 @@ type: 'dir'

fse.mkdirsSync('dir/empty');
it('sync', function () {
preparations();
expectations(jetpack.inspect('empty'));
});
// SYNC
expectations(jetpack.inspect('dir/empty'));
// ASYNC
jetpack.inspectAsync('dir/empty')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
preparations();
jetpack.inspectAsync('empty')
.then(function (data) {
expectations(data);
done();
});
});
});
it("returns undefined if path doesn't exist", function (done) {
describe("returns undefined if path doesn't exist", function () {
var expectations = function (data) {
expect(data).toBe(undefined);
expect(data).to.equal(undefined);
};
// SYNC
expectations(jetpack.inspect('nonexistent'));
it('sync', function () {
expectations(jetpack.inspect('nonexistent'));
});
// ASYNC
jetpack.inspectAsync('nonexistent')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
jetpack.inspectAsync('nonexistent')
.then(function (data) {
expectations(data);
done();
});
});
});
it('can output file times (ctime, mtime, atime)', function (done) {
describe('can output file times (ctime, mtime, atime)', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
};
var expectations = function (data) {
expect(typeof data.accessTime.getTime).toBe('function');
expect(typeof data.modifyTime.getTime).toBe('function');
expect(typeof data.changeTime.getTime).toBe('function');
expect(typeof data.accessTime.getTime).to.equal('function');
expect(typeof data.modifyTime.getTime).to.equal('function');
expect(typeof data.changeTime.getTime).to.equal('function');
};
fse.outputFileSync('dir/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.inspect('dir/file.txt', { times: true }));
});
// SYNC
expectations(jetpack.inspect('dir/file.txt', { times: true }));
// ASYNC
jetpack.inspectAsync('dir/file.txt', { times: true })
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
preparations();
jetpack.inspectAsync('dir/file.txt', { times: true })
.then(function (data) {
expectations(data);
done();
});
});
});
it('can output absolute path', function (done) {
describe('can output absolute path', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
};
var expectations = function (data) {
expect(data.absolutePath).toBe(jetpack.path('dir/file.txt'));
expect(data.absolutePath).to.equal(jetpack.path('dir/file.txt'));
};
fse.outputFileSync('dir/file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.inspect('dir/file.txt', { absolutePath: true }));
});
// SYNC
expectations(jetpack.inspect('dir/file.txt', { absolutePath: true }));
// ASYNC
jetpack.inspectAsync('dir/file.txt', { absolutePath: true })
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
preparations();
jetpack.inspectAsync('dir/file.txt', { absolutePath: true })
.then(function (data) {
expectations(data);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function (data) {
expect(data.name).toBe('b.txt');
expect(data.name).to.equal('b.txt');
};
var jetContext = jetpack.cwd('a');
fse.outputFileSync('a/b.txt', 'abc');
// SYNC
expectations(jetContext.inspect('b.txt'));
// ASYNC
jetContext.inspectAsync('b.txt')
.then(function (data) {
expectations(data);
done();
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.inspect('b.txt'));
});
});
describe('*nix specific', function () {
if (process.platform === 'win32') {
return;
}
it('can output file mode', function (done) {
var expectations = function (data) {
expect(typeof data.mode).toBe('number');
};
fse.outputFileSync('dir/file.txt', 'abc');
// SYNC
expectations(jetpack.inspect('dir/file.txt', { mode: true }));
// ASYNC
jetpack.inspectAsync('dir/file.txt', { mode: true })
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.inspectAsync('b.txt')
.then(function (data) {

@@ -154,19 +159,25 @@ expectations(data);

});
});
it('follows symlink by default', function (done) {
var expectations = function (data) {
expect(data).toEqual({
name: 'symlinked_file.txt',
type: 'file',
size: 3
});
};
describe('follows symlink by default', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
fse.symlinkSync('dir/file.txt', 'symlinked_file.txt');
};
// SYNC
var expectations = function (data) {
expect(data).to.eql({
name: 'symlinked_file.txt',
type: 'file',
size: 3
});
};
it('sync', function () {
preparations();
expectations(jetpack.inspect('symlinked_file.txt'));
});
// ASYNC
it('async', function (done) {
preparations();
jetpack.inspectAsync('symlinked_file.txt')

@@ -178,19 +189,25 @@ .then(function (data) {

});
});
it('stats symlink if option specified', function (done) {
var expectations = function (data) {
expect(data).toEqual({
name: 'symlinked_file.txt',
type: 'symlink',
pointsAt: 'dir/file.txt'
});
};
describe('stats symlink if option specified', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc');
fse.symlinkSync('dir/file.txt', 'symlinked_file.txt');
};
// SYNC
var expectations = function (data) {
expect(data).to.eql({
name: 'symlinked_file.txt',
type: 'symlink',
pointsAt: helper.osSep('dir/file.txt')
});
};
it('sync', function () {
preparations();
expectations(jetpack.inspect('symlinked_file.txt', { symlinks: true }));
});
// ASYNC
it('async', function (done) {
preparations();
jetpack.inspectAsync('symlinked_file.txt', { symlinks: true })

@@ -203,2 +220,87 @@ .then(function (data) {

});
if (process.platform !== 'win32') {
describe('can output file mode (unix only)', function () {
var preparations = function () {
fse.outputFileSync('dir/file.txt', 'abc', {
mode: '511'
});
};
var expectations = function (data) {
expect(helper.parseMode(data.mode)).to.equal('511');
};
it('sync', function () {
preparations();
expectations(jetpack.inspect('dir/file.txt', { mode: true }));
});
it('async', function (done) {
preparations();
jetpack.inspectAsync('dir/file.txt', { mode: true })
.then(function (data) {
expectations(data);
done();
});
});
});
} else {
// TODO what with Windows?
}
describe('checksums', function () {
var testsData = [
{
name: 'md5',
type: 'md5',
content: 'abc',
expected: '900150983cd24fb0d6963f7d28e17f72'
},
{
name: 'sha1',
type: 'sha1',
content: 'abc',
expected: 'a9993e364706816aba3e25717850c26c9cd0d89d'
},
{
name: 'sha256',
type: 'sha256',
content: 'abc',
expected: 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
},
{
name: 'calculates correctly checksum of an empty file',
type: 'md5',
content: '',
expected: 'd41d8cd98f00b204e9800998ecf8427e'
}
];
testsData.forEach(function (test) {
describe(test.name, function () {
var preparations = function () {
fse.outputFileSync('file.txt', test.content);
};
var expectations = function (data) {
expect(data[test.type]).to.eql(test.expected);
};
it('sync', function () {
preparations();
expectations(jetpack.inspect('file.txt', { checksum: test.type }));
});
it('async', function (done) {
preparations();
jetpack.inspectAsync('file.txt', { checksum: test.type })
.then(function (data) {
expectations(data);
done();
});
});
});
});
});
});

@@ -1,113 +0,136 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('list |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('list', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('lists file names in given path', function (done) {
describe('lists file names in given path', function () {
var preparations = function () {
fse.mkdirsSync('dir/empty');
fse.outputFileSync('dir/empty.txt', '');
fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
};
var expectations = function (data) {
expect(data).toEqual(['empty', 'empty.txt', 'file.txt', 'subdir']);
expect(data).to.eql(['empty', 'empty.txt', 'file.txt', 'subdir']);
};
fse.mkdirsSync('dir/empty');
fse.outputFileSync('dir/empty.txt', '');
fse.outputFileSync('dir/file.txt', 'abc');
fse.outputFileSync('dir/subdir/file.txt', 'defg');
it('sync', function () {
preparations();
expectations(jetpack.list('dir'));
});
// SYNC
expectations(jetpack.list('dir'));
// ASYNC
jetpack.listAsync('dir')
.then(function (listAsync) {
expectations(listAsync);
done();
it('async', function (done) {
preparations();
jetpack.listAsync('dir')
.then(function (listAsync) {
expectations(listAsync);
done();
});
});
});
it('lists CWD if no path parameter passed', function (done) {
describe('lists CWD if no path parameter passed', function () {
var preparations = function () {
fse.mkdirsSync('dir/a');
fse.outputFileSync('dir/b');
};
var expectations = function (data) {
expect(data).toEqual(['a', 'b']);
expect(data).to.eql(['a', 'b']);
};
var jetContext = jetpack.cwd('dir');
it('sync', function () {
var jetContext = jetpack.cwd('dir');
preparations();
expectations(jetContext.list());
});
fse.mkdirsSync('dir/a');
fse.outputFileSync('dir/b');
// SYNC
expectations(jetContext.list());
// ASYNC
jetContext.listAsync()
.then(function (list) {
expectations(list);
done();
it('async', function (done) {
var jetContext = jetpack.cwd('dir');
preparations();
jetContext.listAsync()
.then(function (list) {
expectations(list);
done();
});
});
});
it("returns undefined if path doesn't exist", function (done) {
describe("returns undefined if path doesn't exist", function () {
var expectations = function (data) {
expect(data).toBe(undefined);
expect(data).to.equal(undefined);
};
// SYNC
expectations(jetpack.list('nonexistent'));
it('sync', function () {
expectations(jetpack.list('nonexistent'));
});
// ASYNC
jetpack.listAsync('nonexistent')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
jetpack.listAsync('nonexistent')
.then(function (data) {
expectations(data);
done();
});
});
});
it('throws if given path is not a directory', function (done) {
describe('throws if given path is not a directory', function () {
var preparations = function () {
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function (err) {
expect(err.code).toBe('ENOTDIR');
expect(err.code).to.equal('ENOTDIR');
};
fse.outputFileSync('file.txt', 'abc');
it('sync', function () {
preparations();
try {
jetpack.list('file.txt');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.list('file.txt');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.listAsync('file.txt')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.listAsync('file.txt')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/b/c.txt', 'abc');
};
var expectations = function (data) {
expect(data).toEqual(['c.txt']);
expect(data).to.eql(['c.txt']);
};
var jetContext = jetpack.cwd('a');
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.list('b'));
});
fse.outputFileSync('a/b/c.txt', 'abc');
// SYNC
expectations(jetContext.list('b'));
// ASYNC
jetContext.listAsync('b')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.listAsync('b')
.then(function (data) {
expectations(data);
done();
});
});
});
});

@@ -1,133 +0,141 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('move |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('move', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('moves file', function (done) {
describe('moves file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function () {
expect('a/b.txt').not.toExist();
expect('c.txt').toBeFileWithContent('abc');
path('a/b.txt').shouldNotExist();
path('c.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.move('a/b.txt', 'c.txt');
expectations();
// ASYNC
preparations();
jetpack.moveAsync('a/b.txt', 'c.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.move('a/b.txt', 'c.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.moveAsync('a/b.txt', 'c.txt')
.then(function () {
expectations();
done();
});
});
});
it('moves directory', function (done) {
describe('moves directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', 'abc');
fse.mkdirsSync('x');
};
var expectations = function () {
expect('a').not.toExist();
expect('x/y/b/c.txt').toBeFileWithContent('abc');
path('a').shouldNotExist();
path('x/y/b/c.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.move('a', 'x/y');
expectations();
// ASYNC
preparations();
jetpack.moveAsync('a', 'x/y')
.then(function () {
it('sync', function () {
preparations();
jetpack.move('a', 'x/y');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.moveAsync('a', 'x/y')
.then(function () {
expectations();
done();
});
});
});
it('creates nonexistent directories for destination path', function (done) {
describe('creates nonexistent directories for destination path', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a.txt', 'abc');
};
var expectations = function () {
expect('a.txt').not.toExist();
expect('a/b/z.txt').toBeFileWithContent('abc');
path('a.txt').shouldNotExist();
path('a/b/z.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.move('a.txt', 'a/b/z.txt');
expectations();
// ASYNC
preparations();
jetpack.moveAsync('a.txt', 'a/b/z.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.move('a.txt', 'a/b/z.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.moveAsync('a.txt', 'a/b/z.txt')
.then(function () {
expectations();
done();
});
});
});
it("generates nice error when source path doesn't exist", function (done) {
describe("generates nice error when source path doesn't exist", function () {
var expectations = function (err) {
expect(err.code).toBe('ENOENT');
expect(err.message).toMatch(/^Path to move doesn't exist/);
expect(err.code).to.equal('ENOENT');
expect(err.message).to.have.string("Path to move doesn't exist");
};
// SYNC
try {
jetpack.move('a', 'b');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
it('sync', function () {
try {
jetpack.move('a', 'b');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// ASYNC
jetpack.moveAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
jetpack.moveAsync('a', 'b')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function () {
expect('a/b.txt').not.toExist();
expect('a/x.txt').toBeFileWithContent('abc');
path('a/b.txt').shouldNotExist();
path('a/x.txt').shouldBeFileWithContent('abc');
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.move('b.txt', 'x.txt');
expectations();
// ASYNC
preparations();
jetContext.moveAsync('b.txt', 'x.txt')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.move('b.txt', 'x.txt');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.moveAsync('b.txt', 'x.txt')
.then(function () {
expectations();
done();
});
});
});
});

@@ -1,17 +0,14 @@

/* eslint-env jasmine */
'use strict';
var pathUtil = require('path');
var expect = require('chai').expect;
var jetpack = require('..');
describe('path', function () {
it('if empty returns same path as cwd()', function () {
expect(jetpack.path()).toBe(jetpack.cwd());
expect(jetpack.path('')).toBe(jetpack.cwd());
expect(jetpack.path('.')).toBe(jetpack.cwd());
it('if no parameters passed returns same path as cwd()', function () {
expect(jetpack.path()).to.equal(jetpack.cwd());
expect(jetpack.path('')).to.equal(jetpack.cwd());
expect(jetpack.path('.')).to.equal(jetpack.cwd());
});
it('is absolute if prepending slash present', function () {
expect(jetpack.path('/blah')).toBe(pathUtil.resolve('/blah'));
expect(jetpack.path('/blah')).to.equal(pathUtil.resolve('/blah'));
});

@@ -24,4 +21,4 @@

var b = pathUtil.join(jetpack.cwd(), 'subdir', 'b');
expect(jetpack.path('a')).toBe(a);
expect(jetpackSubdir.path('b')).toBe(b);
expect(jetpack.path('a')).to.equal(a);
expect(jetpackSubdir.path('b')).to.equal(b);
});

@@ -31,4 +28,4 @@

var abc = pathUtil.join(jetpack.cwd(), 'a', 'b', 'c');
expect(jetpack.path('a', 'b', 'c')).toBe(abc);
expect(jetpack.path('a', 'b', 'c')).to.equal(abc);
});
});

@@ -1,123 +0,152 @@

/* eslint-env jasmine */
'use strict';
var Q = require('q');
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('read |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('read', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('reads file as a string', function (done) {
describe('reads file as a string', function () {
var preparations = function () {
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function (content) {
expect(content).toBe('abc');
expect(content).to.equal('abc');
};
fse.outputFileSync('file.txt', 'abc');
it('sync', function () {
preparations();
expectations(jetpack.read('file.txt')); // defaults to 'utf8'
expectations(jetpack.read('file.txt', 'utf8')); // explicitly specified
});
// SYNC
expectations(jetpack.read('file.txt')); // defaults to 'utf8'
expectations(jetpack.read('file.txt', 'utf8')); // explicitly specified
// ASYNC
jetpack.readAsync('file.txt') // defaults to 'utf8'
.then(function (content) {
expectations(content);
return jetpack.readAsync('file.txt', 'utf8'); // explicitly said
})
.then(function (content) {
expectations(content);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.txt') // defaults to 'utf8'
.then(function (content) {
expectations(content);
return jetpack.readAsync('file.txt', 'utf8'); // explicitly said
})
.then(function (content) {
expectations(content);
done();
});
});
});
it('reads file as a Buffer', function (done) {
describe('reads file as a Buffer', function () {
var preparations = function () {
fse.outputFileSync('file.txt', new Buffer([11, 22]));
};
var expectations = function (content) {
expect(Buffer.isBuffer(content)).toBe(true);
expect(content.length).toBe(2);
expect(content[0]).toBe(11);
expect(content[1]).toBe(22);
expect(Buffer.isBuffer(content)).to.equal(true);
expect(content.length).to.equal(2);
expect(content[0]).to.equal(11);
expect(content[1]).to.equal(22);
};
fse.outputFileSync('file.txt', new Buffer([11, 22]));
it('sync', function () {
preparations();
expectations(jetpack.read('file.txt', 'buffer'));
});
// SYNC
expectations(jetpack.read('file.txt', 'buffer'));
// ASYNC
jetpack.readAsync('file.txt', 'buffer')
.then(function (content) {
expectations(content);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.txt', 'buffer')
.then(function (content) {
expectations(content);
done();
});
});
});
it('reads file as a Buffer (deprecated)', function (done) {
describe('reads file as a Buffer (deprecated)', function () {
var preparations = function () {
fse.outputFileSync('file.txt', new Buffer([11, 22]));
};
var expectations = function (content) {
expect(Buffer.isBuffer(content)).toBe(true);
expect(content.length).toBe(2);
expect(content[0]).toBe(11);
expect(content[1]).toBe(22);
expect(Buffer.isBuffer(content)).to.equal(true);
expect(content.length).to.equal(2);
expect(content[0]).to.equal(11);
expect(content[1]).to.equal(22);
};
fse.outputFileSync('file.txt', new Buffer([11, 22]));
it('sync', function () {
preparations();
expectations(jetpack.read('file.txt', 'buf'));
});
// SYNC
expectations(jetpack.read('file.txt', 'buf'));
// ASYNC
jetpack.readAsync('file.txt', 'buf')
.then(function (content) {
expectations(content);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.txt', 'buf')
.then(function (content) {
expectations(content);
done();
});
});
});
it('reads file as JSON', function (done) {
describe('reads file as JSON', function () {
var obj = {
utf8: 'ąćłźż'
};
var preparations = function () {
fse.outputFileSync('file.json', JSON.stringify(obj));
};
var expectations = function (content) {
expect(content).toEqual(obj);
expect(content).to.eql(obj);
};
fse.outputFileSync('file.json', JSON.stringify(obj));
it('sync', function () {
preparations();
expectations(jetpack.read('file.json', 'json'));
});
// SYNC
expectations(jetpack.read('file.json', 'json'));
// ASYNC
jetpack.readAsync('file.json', 'json')
.then(function (content) {
expectations(content);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.json', 'json')
.then(function (content) {
expectations(content);
done();
});
});
});
it('gives nice error message when JSON parsing failed', function (done) {
describe('gives nice error message when JSON parsing failed', function () {
var preparations = function () {
fse.outputFileSync('file.json', '{ "abc: 123 }'); // Malformed JSON
};
var expectations = function (err) {
expect(err.message).toContain('JSON parsing failed while reading');
expect(err.message).to.have.string('JSON parsing failed while reading');
};
fse.outputFileSync('file.json', '{ "abc: 123 }'); // Malformed JSON
it('sync', function () {
preparations();
try {
jetpack.read('file.json', 'json');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.read('file.json', 'json');
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.readAsync('file.json', 'json')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.json', 'json')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('reads file as JSON with Date parsing', function (done) {
describe('reads file as JSON with Date parsing', function () {
var obj = {

@@ -127,83 +156,105 @@ utf8: 'ąćłźż',

};
var preparations = function () {
fse.outputFileSync('file.json', JSON.stringify(obj));
};
var expectations = function (content) {
expect(content).toEqual(obj);
expect(content).to.eql(obj);
};
fse.outputFileSync('file.json', JSON.stringify(obj));
it('sync', function () {
preparations();
expectations(jetpack.read('file.json', 'jsonWithDates'));
});
// SYNC
expectations(jetpack.read('file.json', 'jsonWithDates'));
// ASYNC
jetpack.readAsync('file.json', 'jsonWithDates')
.then(function (content) {
expectations(content);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('file.json', 'jsonWithDates')
.then(function (content) {
expectations(content);
done();
});
});
});
it("returns undefined if file doesn't exist", function (done) {
describe("returns undefined if file doesn't exist", function () {
var expectations = function (content) {
expect(content).toBe(undefined);
expect(content).to.equal(undefined);
};
// SYNC
expectations(jetpack.read('nonexistent.txt'));
expectations(jetpack.read('nonexistent.txt', 'json'));
expectations(jetpack.read('nonexistent.txt', 'buffer'));
it('sync', function () {
expectations(jetpack.read('nonexistent.txt'));
expectations(jetpack.read('nonexistent.txt', 'json'));
expectations(jetpack.read('nonexistent.txt', 'buffer'));
});
// ASYNC
Q.spread([
jetpack.readAsync('nonexistent.txt'),
jetpack.readAsync('nonexistent.txt', 'json'),
jetpack.readAsync('nonexistent.txt', 'buffer')
], function (content1, content2, content3) {
expectations(content1);
expectations(content2);
expectations(content3);
done();
it('async', function (done) {
Q.spread([
jetpack.readAsync('nonexistent.txt'),
jetpack.readAsync('nonexistent.txt', 'json'),
jetpack.readAsync('nonexistent.txt', 'buffer')
], function (content1, content2, content3) {
expectations(content1);
expectations(content2);
expectations(content3);
done();
});
});
});
it('throws if given path is a directory', function (done) {
describe('throws if given path is a directory', function () {
var preparations = function () {
fse.mkdirsSync('dir');
};
var expectations = function (err) {
expect(err.code).toBe('EISDIR');
expect(err.code).to.equal('EISDIR');
};
fse.mkdirsSync('dir');
it('sync', function () {
preparations();
try {
jetpack.read('dir');
throw new Error('Expected error to be thrown');
} catch (err) {
expectations(err);
}
});
// SYNC
try {
jetpack.read('dir');
throw new Error('to make sure this code throws');
} catch (err) {
expectations(err);
}
// ASYNC
jetpack.readAsync('dir')
.catch(function (err) {
expectations(err);
done();
it('async', function (done) {
preparations();
jetpack.readAsync('dir')
.catch(function (err) {
expectations(err);
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
fse.outputFileSync('a/file.txt', 'abc');
};
var expectations = function (data) {
expect(data).toBe('abc');
expect(data).to.equal('abc');
};
var jetContext = jetpack.cwd('a');
fse.outputFileSync('a/file.txt', 'abc');
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
expectations(jetContext.read('file.txt'));
});
// SYNC
expectations(jetContext.read('file.txt'));
// ASYNC
jetContext.readAsync('file.txt')
.then(function (data) {
expectations(data);
done();
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.readAsync('file.txt')
.then(function (data) {
expectations(data);
done();
});
});
});
});

@@ -1,50 +0,51 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('remove', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it("doesn't throw if path already doesn't exist", function (done) {
// SYNC
jetpack.remove('dir');
describe("doesn't throw if path already doesn't exist", function () {
it('sync', function () {
jetpack.remove('dir');
});
// ASYNC
jetpack.removeAsync('dir')
.then(function () {
done();
it('async', function (done) {
jetpack.removeAsync('dir')
.then(function () {
done();
});
});
});
it('should delete file', function (done) {
describe('should delete file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('file.txt', 'abc');
};
var expectations = function () {
expect('file.txt').not.toExist();
path('file.txt').shouldNotExist();
};
// SYNC
preparations();
jetpack.remove('file.txt');
expectations();
// ASYNC
preparations();
jetpack.removeAsync('file.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.remove('file.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.removeAsync('file.txt')
.then(function () {
expectations();
done();
});
});
});
it('removes directory with stuff inside', function (done) {
describe('removes directory with stuff inside', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('a/b/c');

@@ -54,96 +55,99 @@ fse.outputFileSync('a/f.txt', 'abc');

};
var expectations = function () {
expect('a').not.toExist();
path('a').shouldNotExist();
};
// SYNC
preparations();
jetpack.remove('a');
expectations();
// ASYNC
preparations();
jetpack.removeAsync('a')
.then(function () {
it('sync', function () {
preparations();
jetpack.remove('a');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.removeAsync('a')
.then(function () {
expectations();
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', '123');
};
var expectations = function () {
expect('a').toExist();
expect('a/b').not.toExist();
path('a').shouldBeDirectory();
path('a/b').shouldNotExist();
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.remove('b');
expectations();
// ASYNC
preparations();
jetContext.removeAsync('b')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.remove('b');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.removeAsync('b')
.then(function () {
expectations();
done();
});
});
});
it('can be called witn no parameters, what will remove CWD directory', function (done) {
describe('can be called witn no parameters, what will remove CWD directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', 'abc');
};
var expectations = function () {
expect('a').not.toExist();
path('a').shouldNotExist();
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.remove();
expectations();
// ASYNC
preparations();
jetContext.removeAsync()
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.remove();
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.removeAsync()
.then(function () {
expectations();
done();
});
});
});
describe('*nix specific', function () {
if (process.platform === 'win32') {
return;
}
describe('removes only symlinks, never real content where symlinks point', function () {
var preparations = function () {
fse.outputFileSync('have_to_stay_file', 'abc');
fse.mkdirsSync('to_remove');
fse.symlinkSync('../have_to_stay_file', 'to_remove/symlink');
// Make sure we symlinked it properly.
expect(fse.readFileSync('to_remove/symlink', 'utf8')).to.equal('abc');
};
it('removes only symlinks, never real content where symlinks point', function (done) {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('have_to_stay_file', 'abc');
fse.mkdirsSync('to_remove');
fse.symlinkSync('../have_to_stay_file', 'to_remove/symlink');
// Make sure we symlinked it properly.
expect(fse.readFileSync('to_remove/symlink', 'utf8')).toBe('abc');
};
var expectations = function () {
expect('have_to_stay_file').toBeFileWithContent('abc');
expect('to_remove').not.toExist();
};
var expectations = function () {
path('have_to_stay_file').shouldBeFileWithContent('abc');
path('to_remove').shouldNotExist();
};
// SYNC
it('sync', function () {
preparations();
jetpack.remove('to_remove');
expectations();
});
// ASYNC
it('async', function (done) {
preparations();

@@ -150,0 +154,0 @@ jetpack.removeAsync('to_remove')

@@ -1,86 +0,89 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('rename |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('rename', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('renames file', function (done) {
describe('renames file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b.txt', 'abc');
};
var expectations = function () {
expect('a/b.txt').not.toExist();
expect('a/x.txt').toBeFileWithContent('abc');
path('a/b.txt').shouldNotExist();
path('a/x.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.rename('a/b.txt', 'x.txt');
expectations();
// ASYNC
preparations();
jetpack.renameAsync('a/b.txt', 'x.txt')
.then(function () {
it('sync', function () {
preparations();
jetpack.rename('a/b.txt', 'x.txt');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.renameAsync('a/b.txt', 'x.txt')
.then(function () {
expectations();
done();
});
});
});
it('renames directory', function (done) {
describe('renames directory', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', 'abc');
};
var expectations = function () {
expect('a/b').not.toExist();
expect('a/x').toBeDirectory();
path('a/b').shouldNotExist();
path('a/x').shouldBeDirectory();
};
// SYNC
preparations();
jetpack.rename('a/b', 'x');
expectations();
// ASYNC
preparations();
jetpack.renameAsync('a/b', 'x')
.then(function () {
it('sync', function () {
preparations();
jetpack.rename('a/b', 'x');
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.renameAsync('a/b', 'x')
.then(function () {
expectations();
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync('a/b/c.txt', 'abc');
};
var expectations = function () {
expect('a/b').not.toExist();
expect('a/x').toBeDirectory();
path('a/b').shouldNotExist();
path('a/x').shouldBeDirectory();
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.rename('b', 'x');
expectations();
// ASYNC
preparations();
jetContext.renameAsync('b', 'x')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.rename('b', 'x');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
preparations();
jetContext.renameAsync('b', 'x')
.then(function () {
expectations();
done();
});
});
});
});

@@ -1,12 +0,9 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('streams |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('streams', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);

@@ -22,3 +19,3 @@ it('exposes vanilla stream methods', function (done) {

output.on('finish', function () {
expect('b.txt').toBeFileWithContent('abc');
path('b.txt').shouldBeFileWithContent('abc');
done();

@@ -40,3 +37,3 @@ });

output.on('finish', function () {
expect('dir/b.txt').toBeFileWithContent('abc');
path('dir/b.txt').shouldBeFileWithContent('abc');
done();

@@ -43,0 +40,0 @@ });

@@ -1,82 +0,75 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var helper = require('./helper');
var jetpack = require('..');
describe('symlink |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('symlink', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('can create a symlink', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('can create a symlink', function () {
var expectations = function () {
expect(fse.lstatSync('symlink').isSymbolicLink()).toBe(true);
expect(fse.readlinkSync('symlink')).toBe('some_path');
expect(fse.lstatSync('symlink').isSymbolicLink()).to.equal(true);
expect(fse.readlinkSync('symlink')).to.equal('some_path');
};
// SYNC
preparations();
jetpack.symlink('some_path', 'symlink');
expectations();
// ASYNC
preparations();
jetpack.symlinkAsync('some_path', 'symlink')
.then(function () {
it('sync', function () {
jetpack.symlink('some_path', 'symlink');
expectations();
done();
});
it('async', function (done) {
jetpack.symlinkAsync('some_path', 'symlink')
.then(function () {
expectations();
done();
});
});
});
it('can create nonexistent parent directories', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('can create nonexistent parent directories', function () {
var expectations = function () {
expect(fse.lstatSync('a/b/symlink').isSymbolicLink()).toBe(true);
expect(fse.lstatSync('a/b/symlink').isSymbolicLink()).to.equal(true);
};
// SYNC
preparations();
jetpack.symlink('whatever', 'a/b/symlink');
expectations();
// ASYNC
preparations();
jetpack.symlinkAsync('whatever', 'a/b/symlink')
.then(function () {
it('sync', function () {
jetpack.symlink('whatever', 'a/b/symlink');
expectations();
done();
});
it('async', function (done) {
jetpack.symlinkAsync('whatever', 'a/b/symlink')
.then(function () {
expectations();
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
describe('respects internal CWD of jetpack instance', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.mkdirsSync('a/b');
};
var expectations = function () {
expect(fse.lstatSync('a/b/symlink').isSymbolicLink()).toBe(true);
expect(fse.lstatSync('a/b/symlink').isSymbolicLink()).to.equal(true);
};
var jetContext = jetpack.cwd('a/b');
// SYNC
preparations();
jetContext.symlink('whatever', 'symlink');
expectations();
// ASYNC
preparations();
jetContext.symlinkAsync('whatever', 'symlink')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a/b');
preparations();
jetContext.symlink('whatever', 'symlink');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a/b');
preparations();
jetContext.symlinkAsync('whatever', 'symlink')
.then(function () {
expectations();
done();
});
});
});
});

@@ -1,12 +0,9 @@

/* eslint-env jasmine */
'use strict';
var expect = require('chai').expect;
var matcher = require('../../lib/utils/matcher');
describe('matcher |', function () {
describe('matcher', function () {
it('can test against one pattern passed as a string', function () {
var test = matcher.create('a');
expect(test('/a')).toBe(true);
expect(test('/b')).toBe(false);
expect(test('/a')).to.equal(true);
expect(test('/b')).to.equal(false);
});

@@ -16,17 +13,17 @@

var test = matcher.create(['a', 'b']);
expect(test('/a')).toBe(true);
expect(test('/b')).toBe(true);
expect(test('/c')).toBe(false);
expect(test('/a')).to.equal(true);
expect(test('/b')).to.equal(true);
expect(test('/c')).to.equal(false);
});
describe('possible mask tokens |', function () {
describe('possible mask tokens', function () {
it('*', function () {
var test = matcher.create(['*']);
expect(test('/a')).toBe(true);
expect(test('/a/b.txt')).toBe(true);
expect(test('/a')).to.equal(true);
expect(test('/a/b.txt')).to.equal(true);
test = matcher.create(['a*b']);
expect(test('/ab')).toBe(true);
expect(test('/a_b')).toBe(true);
expect(test('/a__b')).toBe(true);
expect(test('/ab')).to.equal(true);
expect(test('/a_b')).to.equal(true);
expect(test('/a__b')).to.equal(true);
});

@@ -36,4 +33,4 @@

var test = matcher.create(['/*']);
expect(test('/a')).toBe(true);
expect(test('/a/b')).toBe(false);
expect(test('/a')).to.equal(true);
expect(test('/a/b')).to.equal(false);
});

@@ -43,11 +40,11 @@

var test = matcher.create(['**']);
expect(test('/a')).toBe(true);
expect(test('/a/b')).toBe(true);
expect(test('/a')).to.equal(true);
expect(test('/a/b')).to.equal(true);
test = matcher.create(['/a/**/d']);
expect(test('/a/d')).toBe(true);
expect(test('/a/b/d')).toBe(true);
expect(test('/a/b/c/d')).toBe(true);
expect(test('/a')).toBe(false);
expect(test('/d')).toBe(false);
expect(test('/a/d')).to.equal(true);
expect(test('/a/b/d')).to.equal(true);
expect(test('/a/b/c/d')).to.equal(true);
expect(test('/a')).to.equal(false);
expect(test('/d')).to.equal(false);
});

@@ -57,6 +54,6 @@

var test = matcher.create(['/**/a']);
expect(test('/a')).toBe(true);
expect(test('/x/a')).toBe(true);
expect(test('/x/y/a')).toBe(true);
expect(test('/a/b')).toBe(false);
expect(test('/a')).to.equal(true);
expect(test('/x/a')).to.equal(true);
expect(test('/x/y/a')).to.equal(true);
expect(test('/a/b')).to.equal(false);
});

@@ -66,5 +63,5 @@

var test = matcher.create(['*.+(txt|md)']);
expect(test('/a.txt')).toBe(true);
expect(test('/b.md')).toBe(true);
expect(test('/c.rtf')).toBe(false);
expect(test('/a.txt')).to.equal(true);
expect(test('/b.md')).to.equal(true);
expect(test('/c.rtf')).to.equal(false);
});

@@ -74,5 +71,5 @@

var test = matcher.create(['*.{jpg,png}']);
expect(test('a.jpg')).toBe(true);
expect(test('b.png')).toBe(true);
expect(test('c.txt')).toBe(false);
expect(test('a.jpg')).to.equal(true);
expect(test('b.png')).to.equal(true);
expect(test('c.txt')).to.equal(false);
});

@@ -82,5 +79,5 @@

var test = matcher.create(['a?c']);
expect(test('/abc')).toBe(true);
expect(test('/ac')).toBe(false);
expect(test('/abbc')).toBe(false);
expect(test('/abc')).to.equal(true);
expect(test('/ac')).to.equal(false);
expect(test('/abbc')).to.equal(false);
});

@@ -90,3 +87,3 @@

var test = matcher.create(['#a']);
expect(test('/#a')).toBe(true);
expect(test('/#a')).to.equal(true);
});

@@ -98,4 +95,4 @@ });

var test = matcher.create('!abc');
expect(test('/abc')).toBe(false);
expect(test('/xyz')).toBe(true);
expect(test('/abc')).to.equal(false);
expect(test('/xyz')).to.equal(true);
});

@@ -105,5 +102,5 @@

var test = matcher.create(['!abc', '!xyz']);
expect(test('/abc')).toBe(false);
expect(test('/xyz')).toBe(false);
expect(test('/whatever')).toBe(true);
expect(test('/abc')).to.equal(false);
expect(test('/xyz')).to.equal(false);
expect(test('/whatever')).to.equal(true);
});

@@ -113,9 +110,9 @@

var test = matcher.create(['abc', '123', '!/xyz/**', '!/789/**']);
expect(test('/abc')).toBe(true);
expect(test('/456/123')).toBe(true);
expect(test('/xyz/abc')).toBe(false);
expect(test('/789/123')).toBe(false);
expect(test('/whatever')).toBe(false);
expect(test('/abc')).to.equal(true);
expect(test('/456/123')).to.equal(true);
expect(test('/xyz/abc')).to.equal(false);
expect(test('/789/123')).to.equal(false);
expect(test('/whatever')).to.equal(false);
});
});
});

@@ -1,101 +0,102 @@

/* eslint-env jasmine */
/* eslint no-console:0 */
'use strict';
var fse = require('fs-extra');
var pathUtil = require('path');
var fse = require('fs-extra');
var helper = require('../support/spec_helper');
var expect = require('chai').expect;
var helper = require('../helper');
var walker = require('../../lib/utils/tree_walker');
describe('tree walker |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('tree walker', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('inspects all files and folders recursively and returns them one by one', function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('a');
describe('inspects all files and folders recursively and returns them one by one', function () {
var preparations = function () {
fse.outputFileSync('a/a.txt', 'a');
fse.outputFileSync('a/b/z1.txt', 'z1');
fse.outputFileSync('a/b/z2.txt', 'z2');
fse.mkdirsSync('a/b/c');
};
var expectations = function (data) {
expect(data[0]).toEqual({
path: pathUtil.resolve('a'),
item: {
type: 'dir',
name: 'a'
expect(data).to.eql([
{
path: pathUtil.resolve('a'),
item: {
type: 'dir',
name: 'a'
}
},
{
path: pathUtil.resolve('a', 'a.txt'),
item: {
type: 'file',
name: 'a.txt',
size: 1
}
},
{
path: pathUtil.resolve('a', 'b'),
item: {
type: 'dir',
name: 'b'
}
},
{
path: pathUtil.resolve('a', 'b', 'c'),
item: {
type: 'dir',
name: 'c'
}
},
{
path: pathUtil.resolve('a', 'b', 'z1.txt'),
item: {
type: 'file',
name: 'z1.txt',
size: 2
}
},
{
path: pathUtil.resolve('a', 'b', 'z2.txt'),
item: {
type: 'file',
name: 'z2.txt',
size: 2
}
}
});
expect(data[1]).toEqual({
path: pathUtil.resolve('a/a.txt'),
item: {
type: 'file',
name: 'a.txt',
size: 1
}
});
expect(data[2]).toEqual({
path: pathUtil.resolve('a/b'),
item: {
type: 'dir',
name: 'b'
}
});
expect(data[3]).toEqual({
path: pathUtil.resolve('a/b/c'),
item: {
type: 'dir',
name: 'c'
}
});
expect(data[4]).toEqual({
path: pathUtil.resolve('a/b/z1.txt'),
item: {
type: 'file',
name: 'z1.txt',
size: 2
}
});
expect(data[5]).toEqual({
path: pathUtil.resolve('a/b/z2.txt'),
item: {
type: 'file',
name: 'z2.txt',
size: 2
}
});
]);
};
// preparations
fse.outputFileSync('a/a.txt', 'a');
fse.outputFileSync('a/b/z1.txt', 'z1');
fse.outputFileSync('a/b/z2.txt', 'z2');
fse.mkdirsSync('a/b/c');
// SYNC
walker.sync(absoluteStartingPath, {}, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('a');
var data = [];
preparations();
walker.sync(absoluteStartingPath, {}, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('a');
var data = [];
var st;
preparations();
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
it("won't penetrate folder tree deeper than maxLevelsDeep option tells", function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('a');
describe("won't penetrate folder tree deeper than maxLevelsDeep option tells", function () {
var options = {

@@ -105,61 +106,71 @@ maxLevelsDeep: 1

var preparations = function () {
fse.outputFileSync('a/a.txt', 'a');
fse.outputFileSync('a/b/z1.txt', 'z1');
};
var expectations = function (data) {
expect(data[0]).toEqual({
path: pathUtil.resolve('a'),
item: {
type: 'dir',
name: 'a'
expect(data).to.eql([
{
path: pathUtil.resolve('a'),
item: {
type: 'dir',
name: 'a'
}
},
{
path: pathUtil.resolve('a', 'a.txt'),
item: {
type: 'file',
name: 'a.txt',
size: 1
}
},
{
path: pathUtil.resolve('a', 'b'),
item: {
type: 'dir',
name: 'b'
}
}
});
expect(data[1]).toEqual({
path: pathUtil.resolve('a/a.txt'),
item: {
type: 'file',
name: 'a.txt',
size: 1
}
});
expect(data[2]).toEqual({
path: pathUtil.resolve('a/b'),
item: {
type: 'dir',
name: 'b'
}
});
expect(data[3]).toEqual(undefined); // Shouldn't report file a/b/z1.txt
]);
};
// preparations
fse.outputFileSync('a/a.txt', 'a');
fse.outputFileSync('a/b/z1.txt', 'z1');
// SYNC
walker.sync(absoluteStartingPath, options, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('a');
var data = [];
preparations();
walker.sync(absoluteStartingPath, options, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, options)
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('a');
var data = [];
var st;
preparations();
st = walker.stream(absoluteStartingPath, options)
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
it('will do fine with empty directory as entry point', function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('abc');
describe('will do fine with empty directory as entry point', function () {
var preparations = function () {
fse.mkdirsSync('abc');
};
var expectations = function (data) {
expect(data).toEqual([
expect(data).to.eql([
{

@@ -175,34 +186,39 @@ path: pathUtil.resolve('abc'),

// preparations
fse.mkdirsSync('abc');
// SYNC
walker.sync(absoluteStartingPath, {}, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('abc');
var data = [];
preparations();
walker.sync(absoluteStartingPath, {}, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('abc');
var data = [];
var st;
preparations();
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
it('will do fine with file as entry point', function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('abc.txt');
describe('will do fine with file as entry point', function () {
var preparations = function () {
fse.outputFileSync('abc.txt', 'abc');
};
var expectations = function (data) {
expect(data).toEqual([
expect(data).to.eql([
{

@@ -219,34 +235,35 @@ path: pathUtil.resolve('abc.txt'),

// preparations
fse.outputFileSync('abc.txt', 'abc');
// SYNC
walker.sync(absoluteStartingPath, {}, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('abc.txt');
var data = [];
preparations();
walker.sync(absoluteStartingPath, {}, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('abc.txt');
var data = [];
var st;
preparations();
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
it('will do fine with nonexistent entry point', function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('abc.txt');
describe('will do fine with nonexistent entry point', function () {
var expectations = function (data) {
expect(data).toEqual([
expect(data).to.eql([
{

@@ -259,28 +276,31 @@ path: pathUtil.resolve('abc.txt'),

// SYNC
walker.sync(absoluteStartingPath, {}, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('abc.txt');
var data = [];
walker.sync(absoluteStartingPath, {}, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('abc.txt');
var data = [];
var st;
st = walker.stream(absoluteStartingPath, {})
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
it('supports inspect options', function (done) {
var syncData = [];
var streamData = [];
var st;
var absoluteStartingPath = pathUtil.resolve('abc');
describe('supports inspect options', function () {
var options = {

@@ -292,4 +312,8 @@ inspectOptions: {

var preparations = function () {
fse.outputFileSync('abc/a.txt', 'a');
};
var expectations = function (data) {
expect(data).toEqual([
expect(data).to.eql([
{

@@ -303,3 +327,3 @@ path: pathUtil.resolve('abc'),

{
path: pathUtil.resolve('abc/a.txt'),
path: pathUtil.resolve('abc', 'a.txt'),
item: {

@@ -315,25 +339,31 @@ type: 'file',

// preparations
fse.outputFileSync('abc/a.txt', 'a');
// SYNC
walker.sync(absoluteStartingPath, options, function (path, item) {
syncData.push({ path: path, item: item });
it('sync', function () {
var absoluteStartingPath = pathUtil.resolve('abc');
var data = [];
preparations();
walker.sync(absoluteStartingPath, options, function (path, item) {
data.push({ path: path, item: item });
});
expectations(data);
});
expectations(syncData);
// ASYNC
st = walker.stream(absoluteStartingPath, options)
.on('readable', function () {
var a = st.read();
if (a) {
streamData.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(streamData);
done();
it('async', function (done) {
var absoluteStartingPath = pathUtil.resolve('abc');
var data = [];
var st;
preparations();
st = walker.stream(absoluteStartingPath, options)
.on('readable', function () {
var a = st.read();
if (a) {
data.push(a);
}
})
.on('error', console.error)
.on('end', function () {
expectations(data);
done();
});
});
});
});

@@ -1,88 +0,86 @@

/* eslint-env jasmine */
'use strict';
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('atomic write |', function () {
var path = 'file.txt';
var newPath = path + '.__new__';
describe('atomic write', function () {
var filePath = 'file.txt';
var tempPath = filePath + '.__new__';
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it("fresh write (file doesn't exist yet)", function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe("fresh write (file doesn't exist yet)", function () {
var expectations = function () {
expect(path).toBeFileWithContent('abc');
expect(newPath).not.toExist();
path(filePath).shouldBeFileWithContent('abc');
path(tempPath).shouldNotExist();
};
// SYNC
preparations();
jetpack.write(path, 'abc', { atomic: true });
expectations();
// ASYNC
preparations();
jetpack.writeAsync(path, 'abc', { atomic: true })
.then(function () {
it('sync', function () {
jetpack.write(filePath, 'abc', { atomic: true });
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync(filePath, 'abc', { atomic: true })
.then(function () {
expectations();
done();
});
});
});
it('overwrite existing file', function (done) {
describe('overwrite existing file', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync(path, 'xyz');
fse.outputFileSync(filePath, 'xyz');
};
var expectations = function () {
expect(path).toBeFileWithContent('abc');
expect(newPath).not.toExist();
path(filePath).shouldBeFileWithContent('abc');
path(tempPath).shouldNotExist();
};
// SYNC
preparations();
jetpack.write(path, 'abc', { atomic: true });
expectations();
// ASYNC
preparations();
jetpack.writeAsync(path, 'abc', { atomic: true })
.then(function () {
it('sync', function () {
preparations();
jetpack.write(filePath, 'abc', { atomic: true });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.writeAsync(filePath, 'abc', { atomic: true })
.then(function () {
expectations();
done();
});
});
});
it('if previous operation failed', function (done) {
describe('if previous operation failed', function () {
var preparations = function () {
helper.clearWorkingDir();
fse.outputFileSync(path, 'xyz');
// File from interrupted previous operation remained.
fse.outputFileSync(newPath, '123');
fse.outputFileSync(filePath, 'xyz');
// Simulating remained file from interrupted previous write attempt.
fse.outputFileSync(tempPath, '123');
};
var expectations = function () {
expect(path).toBeFileWithContent('abc');
expect(newPath).not.toExist();
path(filePath).shouldBeFileWithContent('abc');
path(tempPath).shouldNotExist();
};
// SYNC
preparations();
jetpack.write(path, 'abc', { atomic: true });
expectations();
// ASYNC
preparations();
jetpack.writeAsync(path, 'abc', { atomic: true })
.then(function () {
it('sync', function () {
preparations();
jetpack.write(filePath, 'abc', { atomic: true });
expectations();
done();
});
it('async', function (done) {
preparations();
jetpack.writeAsync(filePath, 'abc', { atomic: true })
.then(function () {
expectations();
done();
});
});
});
});

@@ -1,63 +0,51 @@

/* eslint-env jasmine */
'use strict';
var Q = require('q');
var fse = require('fs-extra');
var helper = require('./support/spec_helper');
var expect = require('chai').expect;
var path = require('./path_assertions');
var helper = require('./helper');
var jetpack = require('..');
describe('write |', function () {
beforeEach(helper.beforeEach);
afterEach(helper.afterEach);
describe('write', function () {
beforeEach(helper.setCleanTestCwd);
afterEach(helper.switchBackToCorrectCwd);
it('writes data from string', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('writes data from string', function () {
var expectations = function () {
expect('file.txt').toBeFileWithContent('abc');
path('file.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.write('file.txt', 'abc');
expectations();
// ASYNC
preparations();
jetpack.writeAsync('file.txt', 'abc')
.then(function () {
it('sync', function () {
jetpack.write('file.txt', 'abc');
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync('file.txt', 'abc')
.then(function () {
expectations();
done();
});
});
});
it('writes data from Buffer', function (done) {
var buf = new Buffer([11, 22]);
var preparations = function () {
helper.clearWorkingDir();
};
describe('writes data from Buffer', function () {
var expectations = function () {
var content = fse.readFileSync('file.txt');
expect(content.length).toBe(2);
expect(content[0]).toBe(11);
expect(content[1]).toBe(22);
path('file.txt').shouldBeFileWithContent(new Buffer([11, 22]));
};
// SYNC
preparations();
jetpack.write('file.txt', buf);
expectations();
// ASYNC
preparations();
jetpack.writeAsync('file.txt', buf)
.then(function () {
it('sync', function () {
jetpack.write('file.txt', new Buffer([11, 22]));
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync('file.txt', new Buffer([11, 22]))
.then(function () {
expectations();
done();
});
});
});
it('writes data as JSON', function (done) {
describe('writes data as JSON', function () {
var obj = {

@@ -67,25 +55,22 @@ utf8: 'ąćłźż'

var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
var content = JSON.parse(fse.readFileSync('file.json', 'utf8'));
expect(content).toEqual(obj);
expect(content).to.eql(obj);
};
// SYNC
preparations();
jetpack.write('file.json', obj);
expectations();
// ASYNC
preparations();
jetpack.writeAsync('file.json', obj)
.then(function () {
it('sync', function () {
jetpack.write('file.json', obj);
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync('file.json', obj)
.then(function () {
expectations();
done();
});
});
});
it('written JSON data can be indented', function (done) {
describe('written JSON data can be indented', function () {
var obj = {

@@ -95,5 +80,2 @@ utf8: 'ąćłźż'

var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {

@@ -103,29 +85,27 @@ var sizeA = fse.statSync('a.json').size;

var sizeC = fse.statSync('c.json').size;
expect(sizeB).toBeGreaterThan(sizeA);
expect(sizeC).toBeGreaterThan(sizeB);
expect(sizeB).to.be.above(sizeA);
expect(sizeC).to.be.above(sizeB);
};
// SYNC
preparations();
jetpack.write('a.json', obj, { jsonIndent: 0 });
jetpack.write('b.json', obj); // Default indent = 2
jetpack.write('c.json', obj, { jsonIndent: 4 });
expectations();
// ASYNC
preparations();
jetpack.writeAsync('a.json', obj, { jsonIndent: 0 })
.then(function () {
return jetpack.writeAsync('b.json', obj); // Default indent = 2
})
.then(function () {
return jetpack.writeAsync('c.json', obj, { jsonIndent: 4 });
})
.then(function () {
it('sync', function () {
jetpack.write('a.json', obj, { jsonIndent: 0 });
jetpack.write('b.json', obj); // Default indent = 2
jetpack.write('c.json', obj, { jsonIndent: 4 });
expectations();
done();
});
it('async', function (done) {
Q.all([
jetpack.writeAsync('a.json', obj, { jsonIndent: 0 }),
jetpack.writeAsync('b.json', obj), // Default indent = 2
jetpack.writeAsync('c.json', obj, { jsonIndent: 4 })
])
.then(function () {
expectations();
done();
});
});
});
it('writes and reads file as JSON with Date parsing', function (done) {
describe('writes and reads file as JSON with Date parsing', function () {
var obj = {

@@ -135,69 +115,60 @@ date: new Date()

var preparations = function () {
helper.clearWorkingDir();
};
var expectations = function () {
var content = JSON.parse(fse.readFileSync('file.json', 'utf8'));
expect(content.date).toBe(obj.date.toISOString());
expect(content.date).to.equal(obj.date.toISOString());
};
// SYNC
preparations();
jetpack.write('file.json', obj);
expectations();
// ASYNC
preparations();
jetpack.writeAsync('file.json', obj)
.then(function () {
it('sync', function () {
jetpack.write('file.json', obj);
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync('file.json', obj)
.then(function () {
expectations();
done();
});
});
});
it('write can create nonexistent parent directories', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('can create nonexistent parent directories', function () {
var expectations = function () {
expect('a/b/c.txt').toBeFileWithContent('abc');
path('a/b/c.txt').shouldBeFileWithContent('abc');
};
// SYNC
preparations();
jetpack.write('a/b/c.txt', 'abc');
expectations();
// ASYNC
preparations();
jetpack.writeAsync('a/b/c.txt', 'abc')
.then(function () {
it('sync', function () {
jetpack.write('a/b/c.txt', 'abc');
expectations();
done();
});
it('async', function (done) {
jetpack.writeAsync('a/b/c.txt', 'abc')
.then(function () {
expectations();
done();
});
});
});
it('respects internal CWD of jetpack instance', function (done) {
var preparations = function () {
helper.clearWorkingDir();
};
describe('respects internal CWD of jetpack instance', function () {
var expectations = function () {
expect('a/b/c.txt').toBeFileWithContent('abc');
path('a/b/c.txt').shouldBeFileWithContent('abc');
};
var jetContext = jetpack.cwd('a');
// SYNC
preparations();
jetContext.write('b/c.txt', 'abc');
expectations();
// ASYNC
preparations();
jetContext.writeAsync('b/c.txt', 'abc')
.then(function () {
it('sync', function () {
var jetContext = jetpack.cwd('a');
jetContext.write('b/c.txt', 'abc');
expectations();
done();
});
it('async', function (done) {
var jetContext = jetpack.cwd('a');
jetContext.writeAsync('b/c.txt', 'abc')
.then(function () {
expectations();
done();
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc