beforeEach(function() { mock({ src: { js: { 'a.js': mock.file({ mtime: new Date(100) }), 'b.js': mock.file({ mtime: new Date(200) }), 'c.js': mock.file({ mtime: new Date(300) }) }, less: { 'one.less': mock.file({mtime: new Date(100)}), 'two.less': mock.file({mtime: new Date(200)}) } }, dest: { js: { 'abc.min.js': mock.file({ mtime: new Date(200) }) }, css: { 'one.css': mock.file({mtime: new Date(100)}), 'two.css': mock.file({mtime: new Date(150)}) } } }); });
unixOnly && test('should keep x flag in mode and skip the rest', async t => { mockFs({ 'source-dir': { 'bash': mockFs.file({ mode: 0o764 }), 'broodwar.exe': mockFs.file({ mode: 0o677 }), 'wth.sh': mockFs.file({ mode: 0o711 }) } }); await packFiles(['bash', 'broodwar.exe', 'wth.sh']); const files = await parseFiles(); t.deepEqual(files.map(file => file.mode), [0o744, 0o655, 0o755]); });
/** * By default makePlatform.loadCache() will call cacheStorage.drop() if one of following: * 1. cached ENB version differs from actual * 2. actual make platfom mode differs from cached * 3. mtime of one of available makefiles differs from cached * Setup below configures cacheStorage in a way that makePlatform will not call cacheStorage.drop(). * In each test checking cacheStorage.drop() is being called one of this conditions is being switched and * make platform behavior checked. * @param {Object} cacheStorage * @param {Object} makePlatform * @param {Object} settings */ function setup(cacheStorage, makePlatform, settings) { const defaults = { currentENBVersion: 'defaultENBVersion', cachedENBVersion: 'defaultENBVersion', currentMakePlatformMode: 'defaultMakePlatformMode', cachedMakePlatformMode: 'defaultMakePlatformMode', currentMakeFileMtime: new Date(1), cachedMakeFileMtime: new Date(1) }; settings = Object.assign({}, defaults, settings); const makeFiles = {}; makeFiles[path.normalize('/path/to/project/.enb/make.js')] = settings.cachedMakeFileMtime.valueOf(); cacheStorage.get.withArgs(':make', 'version').returns(settings.cachedENBVersion); cacheStorage.get.withArgs(':make', 'mode').returns(settings.cachedMakePlatformMode); cacheStorage.get.withArgs(':make', 'makefiles').returns(makeFiles); mockFs({ '/path/to/project': { '.enb': { 'make.js': mockFs.file({ mtime: settings.currentMakeFileMtime }) } }, 'package.json': `{ "version": "${settings.currentENBVersion}" }` }); makePlatform.init('/path/to/project', settings.currentMakePlatformMode); }
it('must ignore outdated cache', function () { var time = new Date(1); mock({ blocks: { 'block.i18n.js': mock.file({ content: serialize({ scope: { key: 'val' } }), mtime: time }) }, bundle: {} }); var bundle = new MockNode('bundle'), cache = bundle.getNodeCache('bundle.keysets.lang.js'), dirname = path.resolve('blocks'), basename = 'block.i18n.js', relPath = path.join('blocks', basename), cacheKey = 'keysets-file-' + relPath, filename = path.resolve(relPath), fileList = new FileList(), dirList = new FileList(); fileList.addFiles(loadDirSync(dirname)); bundle.provideTechData('?.files', fileList); bundle.provideTechData('?.dirs', dirList); clearRequire(filename); require(filename); cache.cacheFileInfo(cacheKey, filename); mock({ blocks: { 'block.i18n.js': mock.file({ content: serialize({ scope: { key: 'val2' } }), mtime: new Date(2) }) }, bundle: {} }); return bundle.runTechAndRequire(Tech, { lang: 'lang' }) .spread(function (res) { res.must.eql({ scope: { key: 'val2' } }); }); });
beforeEach(function() { mock({ src: { js: { 'a.js': mock.file({ mtime: new Date(100) }), 'b.js': mock.file({ mtime: new Date(200) }), 'c.js': mock.file({ mtime: new Date(300) }) } } }); });
beforeEach(function() { mock({ 'q': mock.file({ mtime: new Date(100) }), dest: {} }); });
function defaultFS () { // this is so I can setup without affecting other tests return { '.env': fixtures.local_file, 'other.txt': fixtures.local_file, 'dnt': mock.file({mode: '000'}) } }
beforeEach(() => { mockFs({ '/data': { 'oldest.yml': mockFs.file({ content: 'foo', ctime: new Date(2018, 2, 1), }), 'newest.yml': mockFs.file({ content: 'bar', ctime: new Date(2018, 2, 3), }), 'middle.yml': mockFs.file({ content: 'baz', ctime: new Date(2018, 2, 2), }), }, }); });
it('should return empty array', () => { mockfs({ 'adapters.json': mockfs.file({ mode: 0x000 }) }); const getAdapters = proxyquire('../../../loaders/getAdapters', { yargs: { argv: {} } }); expect(getAdapters(defaultAdaptersFile, adaptersArg)).to.deep.equal([]); });
it('must get result from cache', function () { var time = new Date(1); mock({ bundle: { 'bundle.keysets.lang.js': mock.file({ content: serialize({ all: { '': core }, scope: { key: 'val' } }), mtime: time }) } }); var bundle = new MockNode('bundle'), cache = bundle.getNodeCache('bundle.lang.lang.js'), basename = 'bundle.keysets.lang.js', relPath = path.join('bundle', basename), cacheKey = 'keysets-file-' + relPath, filename = path.resolve(relPath); clearRequire(filename); require(filename); cache.cacheFileInfo(cacheKey, filename); mock({ bundle: { 'bundle.keysets.lang.js': mock.file({ content: serialize({ all: { '': core }, scope: { key: 'val2' } }), mtime: time }) } }); return bundle.runTechAndRequire(Tech, { lang: 'lang' }) .spread(function (i18n) { i18n('scope', 'key').must.be('val'); }); });
beforeEach(function() { mock ({ "/\u2764\u2764": { "\u2764": { "\u2764.md": mock.file({ content: "beep boop", mtime: _date }), "\u2764.png": mock.file({ content: "beep boop", mtime: _date }) }, "\u2764.md": mock.file({ content: "beep boop", mtime: _date }) } }); });
beforeEach(function() { mockFs({ '/reports/locked.log': mockFs.file({ mode: parseInt('444', 8) }) }); // Actually we have to replace gutil.log with a stub because its // current implementation uses "require" at run-time which will // fail after file system is mocked. logStub = sinon.stub(gutil, 'log'); });
.spread(function () { mock({ blocks: { 'block.i18n.js': mock.file({ content: serialize({ scope: { key: 'val2' } }), mtime: time }) }, bundle: {} }); return bundle.runTechAndRequire(Tech, { lang: 'lang' }); })
beforeEach(function() { mock({ file1: mock.file({ content: 'file1 content', mtime: new Date(100) }), file2: mock.file({ content: 'file2 content', mtime: new Date(100) }), file3: mock.file({ content: 'file3 content', mtime: new Date(100) }), dest: { file1: mock.file({ content: 'file1 content', mtime: new Date(150) }), file2: mock.file({ content: 'file2 content', mtime: new Date(50) }), file3: mock.file({ content: 'file3 content', mtime: new Date(150) }) } }); });
beforeEach(function() { mock({ 'file1.ext1': mock.file({ content: 'file1 content', mtime: new Date(100) }), 'file2.ext1': mock.file({ content: 'file2 content', mtime: new Date(100) }), dest: { 'file1.ext2': mock.file({ content: 'file1 content', mtime: new Date(100) }), 'file2.ext2': mock.file({ content: 'file2 content', mtime: new Date(50) }) } }); });
it('should return false if cached mtime equal to current mtime for required file', function () { mockFs({ '/path/to/test_file.js': mockFs.file({ mtime: new Date(1) }) }); var cache = createCache_(); sandbox.stub(cache, 'get'); cache.get.withArgs('cache_key').returns({ mtime: new Date(1).valueOf() }); expect(cache.needRebuildFile('cache_key', '/path/to/test_file.js')).to.be.false; });
grunt.registerTask('startMocking', function () { mock({ 'test/fake/': { 'file1.js': mock.file({ content: 'file content here', ctime: new Date(1411609054470), mtime: new Date(1411609054470) //Wed Sep 24 2014 18:37:34 GMT-0700 (PDT) }), 'file2.js': mock.file({ content: 'file content here', ctime: new Date(1369140245000), mtime: new Date(1369140245000) //Tue May 21 2013 05:44:05 GMT-0700 (PDT) }), 'file3.js': mock.file({ content: 'file content here', ctime: new Date(1328091453000), mtime: new Date(1328091453000) //Wed Feb 01 2012 02:17:33 GMT-0800 (PST) }), 'file4.js': mock.file({ content: 'file content here', ctime: new Date(1388563200000), mtime: new Date(1388563200000) //Wed Jan 01 2014 00:00:00 GMT-0800 (PST) }) } }); // grunt is using glob that is using graceful-fs. // It also needs to be mocked _fileGlobSync = grunt.file.glob.sync; grunt.file.glob.sync = function (pattern, options) { if (/^test\/fake\/.*/.test(pattern)) { return pattern; } else { return _fileGlobSync(pattern, options); } }; });
test('should ignore empty file', async t => { mockFs({ 'source-dir': { 'empty-file.txt': mockFs.file(), 'file-1.txt': 'Hi!' } }); await packFiles(['empty-file.txt', 'file-1.txt'], { emptyFiles: false }); const files = await parseFiles(); t.deepEqual(files.map(file => file.path), ['file-1.txt']); });
function prepare(templates, opts) { opts || (opts = {}); var scheme = { blocks: {}, bundle: {}, 'fake-bh.js': mock.file({ content: [ 'function BH () {};', 'BH.prototype.setOptions = function () {};', 'BH.prototype.apply = function () { return "^_^"; };', 'module.exports = BH;' ].join(EOL), mtime: new Date(opts.replaceBHCore ? 1 : 10) }) }, bundle, fileList; templates && templates.forEach(function (item, i) { scheme.blocks['block-' + i + '.bh.js'] = bhWrap(item); }); scheme[bhCoreFilename] = mock.file({ content: fs.readFileSync(bhCoreFilename, 'utf-8'), mtime: new Date(1) }); mock(scheme); bundle = new TestNode('bundle'); fileList = new FileList(); fileList.addFiles(loadDirSync('blocks')); bundle.provideTechData('?.files', fileList); return bundle; }
beforeEach(() => { fileList = new FileList(); dirName = path.resolve('./foo'); fullName = path.resolve('./foo/file1.txt'); mTime = new Date(); mockFs({ foo: { 'file1.txt': mockFs.file({ content: 'Hello World', ctime: mTime, mtime: mTime }) } }); });
beforeEach(function() { plugin = subject.createDeployPlugin({ name: 'deploySentry' }); indexFile = mockFs.file({ content: '<html><head><meta name="sentry:revision"></meta></head><body></body></html>' }); fileSystem = { '/path/to/fake/dir': { 'index.html': indexFile } }; mockFs(fileSystem); context.distDir = '/path/to/fake/dir'; context.revisionKey = 'abc123'; });
it('should cache info about file', function () { mockFs({ '/path/to/test_file.js': mockFs.file({ mtime: new Date(1) }) }); var cache = createCache_(); sandbox.stub(cache, 'set'); cache.cacheFileInfo('cache_key', '/path/to/test_file.js'); expect(cache.set).to.be.calledWith('cache_key', { name: 'test_file.js', fullname: '/path/to/test_file.js', suffix: 'js', mtime: 1 }); });
beforeEach(() => { this.instance = new FileBin('/some/directory', ['.md', '.markdown', '.txt']); mock({ '/some/directory': { 'first-file.md': 'first file content', 'second-file.markdown': 'second file content', 'third-file.txt': 'third file content', 'invalid.invalidext': 'invalid file content', 'mockfile.md': { mockfile: mock.file({ content: 'contents', atime: new Date(1), mtime: new Date(1), birthtime: new Date(1) }) }, 'subdirectory': {} } }); })
it('must get keysets from cache', function () { var time = new Date(1); mock({ blocks: { 'block.i18n.js': mock.file({ content: serialize({ scope: { key: 'val' } }), mtime: time }) }, bundle: {} }); var bundle = new MockNode('bundle'), dirname = path.resolve('blocks'), fileList = new FileList(), dirList = new FileList(); fileList.addFiles(loadDirSync(dirname)); bundle.provideTechData('?.files', fileList); bundle.provideTechData('?.dirs', dirList); return bundle.runTechAndRequire(Tech, { lang: 'lang' }) .spread(function () { mock({ blocks: { 'block.i18n.js': mock.file({ content: serialize({ scope: { key: 'val2' } }), mtime: time }) }, bundle: {} }); return bundle.runTechAndRequire(Tech, { lang: 'lang' }); }) .spread(function (res) { res.must.eql({ scope: { key: 'val' } }); }); });
'use strict'; var chai = chai || require('chai'); if ( typeof(cordova) == 'undefined' ) { var Storage = require('../lib/json-node-filesystem.js'); var path = require('path'); var testdir = path.resolve(module.filename, '../'); var mock = require('mock-fs'); var fakefs = {}; fakefs[testdir] = { '.bitpay': { 'locked.json': mock.file({ content: 'this file is not able to be written', mode: '0000' }), 'garbage.json': mock.file({ content: 'not a json object', }) } }; var ffs = mock.fs(fakefs); var storage = new Storage(testdir, ffs); } else { var Storage = cordova.require('com.bitpay.sdk.cordova.JSONLocalstorage'); var storage = new Storage(); localStorage.setItem('garbage', 'not a json object'); } describe('Storage Adapters', function(){
tap.test('removeOldAgentsShars', function (suite) { const agentsDir = '/usbkey/extra/agents/'; const progress = suite.comment; suite.afterEach(mockfs.restore); const fakeAgentsFiles = { 'agents-ebc7b8c2-8b3a-409b-b2d9-3c23f0e2b749.sh': mockfs.file({ content: '', mtime: new Date(2018, 6, 25, 20, 47)}), 'agent-fc6847e2-6f0b-4282-bf19-1d5448ef4a20.sh': mockfs.file({ content: '', mtime: new Date(2018, 6, 22, 18, 29)}), 'agents-release-20180510-20180510t044437z-g707200f.sh': mockfs.file({ content: '', mtime: new Date(2018, 5, 21, 18, 43)}) }; const moreFakeAgentFiles = { 'agent-8404446e-31bf-4b5f-ba31-2cebe725f61f.sh': mockfs.file({ content: '', mtime: new Date(2018, 6, 27, 18, 40)}), 'agents-e7264c70-21b4-4f27-a13e-450265954645.sh': mockfs.file({ content: '', mtime: new Date(2018, 7, 3, 20, 11)}) }; suite.test('empty', function (t) { mockfs({[agentsDir]: {}}); t.plan(2); usbkey.removeOldAgentsShars({progress: progress}, function (err) { t.false(err); t.ok(fs.existsSync(agentsDir)); t.done(); }); }); suite.test('too few to prune', function (t) { const fakeFs = {[agentsDir]: fakeAgentsFiles}; fakeFs[agentsDir]['latest'] = mockfs.symlink({ path: 'agent-ebc7b8c2-8b3a-409b-b2d9-3c23f0e2b749.sh'}); mockfs(fakeFs); t.plan(2); usbkey.removeOldAgentsShars({progress: progress}, function (err) { t.false(err); t.equal(fs.readdirSync(agentsDir).length, 4); t.done(); }); }); suite.test('prune', function (t) { const fakeFs = {[agentsDir]: Object.assign({}, fakeAgentsFiles, moreFakeAgentFiles)}; fakeFs[agentsDir]['latest'] = mockfs.symlink({ path: 'agents-e7264c70-21b4-4f27-a13e-450265954645'}); mockfs(fakeFs); t.plan(5); usbkey.removeOldAgentsShars({progress: progress}, function (err) { t.false(err); t.equal(fs.readdirSync(agentsDir).length, 4); t.ok(fs.existsSync( path.resolve( agentsDir, 'agents-e7264c70-21b4-4f27-a13e-450265954645.sh'))); t.ok(fs.existsSync( path.resolve(agentsDir, 'agent-8404446e-31bf-4b5f-ba31-2cebe725f61f.sh'))); t.ok(fs.existsSync( path.resolve( agentsDir, 'agents-ebc7b8c2-8b3a-409b-b2d9-3c23f0e2b749.sh'))); t.done(); }); }); suite.test('prune when eldest is latest', function (t) { const fakeFs = {[agentsDir]: Object.assign({}, fakeAgentsFiles, moreFakeAgentFiles)}; fakeFs[agentsDir]['latest'] = mockfs.symlink({ path: 'agents-release-20180510-20180510t044437z-g707200f.sh'}); mockfs(fakeFs); t.plan(6); usbkey.removeOldAgentsShars({progress: progress}, function (err) { t.false(err); // Here we end up keeping one more file": "latest, the 3 most recent // by mtime, and the file that "latest" points to. t.equal(fs.readdirSync(agentsDir).length, 5); t.ok(fs.existsSync( path.resolve( agentsDir, 'agents-release-20180510-20180510t044437z-g707200f.sh'))); t.ok(fs.existsSync( path.resolve( agentsDir, 'agents-e7264c70-21b4-4f27-a13e-450265954645.sh'))); t.ok(fs.existsSync( path.resolve(agentsDir, 'agent-8404446e-31bf-4b5f-ba31-2cebe725f61f.sh'))); t.ok(fs.existsSync( path.resolve( agentsDir, 'agents-ebc7b8c2-8b3a-409b-b2d9-3c23f0e2b749.sh'))); t.done(); }); }); suite.end(); });