Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/cache/cache-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* ============
*/
var fs = require('fs');
var clearRequire = require('clear-require');
var vow = require('vow');
var inherit = require('inherit');

Expand All @@ -29,9 +28,8 @@ var CacheStorage = inherit(/** @lends CacheStorage.prototype */ {
*/
load: function () {
if (fs.existsSync(this._filename)) {
clearRequire(this._filename);
try {
this._data = require(this._filename);
this._data = JSON.parse(fs.readFileSync(this._filename, 'utf8'));
} catch (e) {
this._data = {};
}
Expand All @@ -45,7 +43,7 @@ var CacheStorage = inherit(/** @lends CacheStorage.prototype */ {
* Сохраняет кэш в файл.
*/
save: function () {
fs.writeFileSync(this._filename, 'module.exports = ' + JSON.stringify(this._data) + ';', 'utf8');
fs.writeFileSync(this._filename, JSON.stringify(this._data), 'utf8');
this._mtime = fs.statSync(this._filename).mtime.getTime();
},

Expand All @@ -64,7 +62,7 @@ var CacheStorage = inherit(/** @lends CacheStorage.prototype */ {

// Делаем stringify() на каждый объект в кеше чтобы уменьшить потребление памяти -
// stringify() на больших кешах может отъедать сотни МБ.
stream.write('module.exports = {');
stream.write('{');

prefixes.forEach(function (prefix, idx) {
stream.write('"' + prefix + '":');
Expand All @@ -74,7 +72,7 @@ var CacheStorage = inherit(/** @lends CacheStorage.prototype */ {
}
});

stream.end('};', function () {
stream.end('}', function () {
_this._mtime = fs.statSync(_this._filename).mtime.getTime();
return resolve();
});
Expand Down
2 changes: 1 addition & 1 deletion lib/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = inherit(/** @lends MakePlatform.prototype */ {
var tmpDir = path.join(configDir, 'tmp');

return vowFs.makeDir(tmpDir).then(function () {
_this._cacheStorage = new CacheStorage(path.join(tmpDir, 'cache.js'));
_this._cacheStorage = new CacheStorage(path.join(tmpDir, 'cache.json'));
_this._nodes = {};
});
},
Expand Down
59 changes: 21 additions & 38 deletions test/lib/cache/cache-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var vow = require('vow');
var fs = require('fs');
var path = require('path');
var mockFs = require('mock-fs');
var clearRequire = require('clear-require');
var CacheStorage = require('../../../lib/cache/cache-storage');

describe('cache/cache-storage', function () {
Expand All @@ -19,89 +18,73 @@ describe('cache/cache-storage', function () {
});

it('should save filename', function () {
var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.load();

expect(fs.existsSync).to.be.calledWith('/path/to/test_file.js');
expect(fs.existsSync).to.be.calledWith('/path/to/test_file.json');
});
});

describe('load', function () {
it('should set data as empty object if cache file does not exist', function () {
mockFs({
'/path/to': {
'test_file.js': ''
'test_file.json': ''
}
});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.load();
storage.save(); // the only way to check internal data contents is to save cache to file

assertStorageData('/path/to/test_file.js', {});
});

it('should clear require for cache file', function () {
mockFs({
'/path/to': {
'test_file.js': 'module.exports = {};'
}
});

var storage = createCacheStorage_('/path/to/test_file.js');

require.cache[path.resolve('/path/to/test_file.js')] = 'foo';
storage.load();

expect(require.cache[path.resolve('/path/to/test_file.js')])
.to.be.not.equal('foo');
assertStorageData('/path/to/test_file.json', {});
});

it('should load cached data', function () {
mockFs({
'/path/to': {
'test_file.js': 'module.exports = { foo: "bar" };'
'test_file.json': '{ "foo": "bar" }'
}
});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.load();
storage.save(); // the only way to check internal data contents is to save cache to file

assertStorageData('/path/to/test_file.js', { foo: 'bar' });
assertStorageData('/path/to/test_file.json', { foo: 'bar' });
});

it('should set data as empty object if exception occured during loading cache file', function () {
mockFs({
'/path/to': {
'test_file.js': 'throw new Error();'
'test_file.json': 'throw new Error();'
}
});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.load();
storage.save();

assertStorageData('/path/to/test_file.js', {});
assertStorageData('/path/to/test_file.json', {});
});
});

describe('save', function () {
it('should write data prepending it with module.exports = ', function () {
it('should write data in JSON format', function () {
mockFs({
'/path/to': {}
});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.set('testPrefix', 'testKey', 'test_value');
storage.save();

assertStorageData('/path/to/test_file.js', {
assertStorageData('/path/to/test_file.json', {
testPrefix: {
testKey: 'test_value'
}
Expand All @@ -122,12 +105,12 @@ describe('cache/cache-storage', function () {
'/path/to': {}
});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.set('testPrefix', 'testKey', 'test_value');

return storage.saveAsync().then(function () {
assertStorageData('/path/to/test_file.js', {
assertStorageData('/path/to/test_file.json', {
testPrefix: {
testKey: 'test_value'
}
Expand All @@ -137,7 +120,7 @@ describe('cache/cache-storage', function () {

it('should write data to stream split in chunks by prefix', function () {
var writeStream = sinon.createStubInstance(fs.WriteStream);
var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

sandbox.stub(fs, 'createWriteStream');
fs.createWriteStream.returns(writeStream);
Expand All @@ -155,7 +138,7 @@ describe('cache/cache-storage', function () {
it('should reject promise if error ocured during file write', function () {
mockFs({});

var storage = createCacheStorage_('/path/to/test_file.js');
var storage = createCacheStorage_('/path/to/test_file.json');

storage.set('testPrefix', 'testKey', 'test_value');

Expand Down Expand Up @@ -217,12 +200,12 @@ describe('cache/cache-storage', function () {
});

function createCacheStorage_(filename) {
return new CacheStorage(filename || '/path/to/default_file.js');
return new CacheStorage(filename || '/path/to/default_file.json');
}

function assertStorageData(dataPath, expected) {
clearRequire(path.resolve(dataPath)); // in test because it throws on non-existing file
var data = JSON.parse(fs.readFileSync(path.resolve(dataPath), 'utf8'));

expect(require(dataPath)).to.be.deep.equal(expected);
expect(data).to.be.deep.equal(expected);
}
});
2 changes: 1 addition & 1 deletion test/lib/make/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('make/init', function () {
it('should instantiate cache storage with path to cache file located in temp dir', function () {
return init_({ projectPath: '/path/to/project' }).then(function () {
expect(makePlatform.getCacheStorage())
.to.be.deep.equal(new CacheStorage(path.normalize('/path/to/project/.enb/tmp/cache.js')));
.to.be.deep.equal(new CacheStorage(path.normalize('/path/to/project/.enb/tmp/cache.json')));
});
});
});
Expand Down