test('cached prebuild', function (t) { t.plan(8) rm.sync(build) var opts = getOpts() var downloadUrl = util.getDownloadUrl(opts) var cachedPrebuild = util.cachedPrebuild(downloadUrl) var npmCache = util.npmCache() var existsCallNum = 0 var _access = fs.access ? fs.access.bind(fs) : fs.access var _exists = fs.exists.bind(fs) if (_access) { fs.access = function (path, a, cb) { if (existsCallNum++ === 0) { t.equal(path, npmCache, 'fs.exists called for npm cache') _access(path, cb) } } } fs.exists = function (path, cb) { if (existsCallNum++ === 0) { t.equal(path, npmCache, 'fs.exists called for npm cache') _exists(path, cb) } else { t.equal(path, cachedPrebuild, 'fs.exists called for prebuild') _exists(path, function (exists) { t.equal(exists, true, 'prebuild should be cached') cb(exists) }) } } var _createWriteStream = fs.createWriteStream.bind(fs) fs.createWriteStream = function (path) { t.ok(/\.node$/i.test(path), 'this is the unpacked file') return _createWriteStream(path) } var _createReadStream = fs.createReadStream.bind(fs) fs.createReadStream = function (path) { t.equal(path, cachedPrebuild, 'createReadStream called for cachedPrebuild') return _createReadStream(path) } t.equal(fs.existsSync(build), false, 'no build folder') download(opts, function (err) { t.error(err, 'no error') t.equal(fs.existsSync(unpacked), true, unpacked + ' should exist') fs.createReadStream = _createReadStream fs.createWriteStream = _createWriteStream fs.exists = _exists fs.access = _access }) })
test('downloading from GitHub, not cached', function (t) { t.plan(14) rm.sync(build) rm.sync(util.prebuildCache()) var opts = getOpts() var downloadUrl = util.getDownloadUrl(opts) var cachedPrebuild = util.cachedPrebuild(downloadUrl) var npmCache = util.npmCache() var tempFile var existsCallNum = 0 var _access = fs.access ? fs.access.bind(fs) : fs.access var _exists = fs.exists.bind(fs) if (_access) { fs.access = function (path, a, cb) { if (existsCallNum++ === 0) { t.equal(path, npmCache, 'fs.exists called for npm cache') _access(path, cb) } } } fs.exists = function (path, cb) { if (existsCallNum++ === 0) { t.equal(path, npmCache, 'fs.exists called for npm cache') _exists(path, cb) } else { t.equal(path, cachedPrebuild, 'fs.exists called for prebuild') _exists(path, function (exists) { t.equal(exists, false, 'prebuild should be cached') cb(exists) }) } } var mkdirCount = 0 var _mkdir = fs.mkdir.bind(fs) fs.mkdir = function () { var args = [].slice.call(arguments) if (mkdirCount++ === 0) { t.equal(args[0], util.prebuildCache(), 'fs.mkdir called for prebuildCache') } _mkdir.apply(fs, arguments) } var writeStreamCount = 0 var _createWriteStream = fs.createWriteStream.bind(fs) fs.createWriteStream = function (path) { if (writeStreamCount++ === 0) { tempFile = path t.ok(/\.tmp$/i.test(path), 'this is the temporary file') } else { t.ok(/\.node$/i.test(path), 'this is the unpacked file') } return _createWriteStream(path) } var _createReadStream = fs.createReadStream.bind(fs) fs.createReadStream = function (path) { t.equal(path, cachedPrebuild, 'createReadStream called for cachedPrebuild') return _createReadStream(path) } var _request = https.request https.request = function (opts) { https.request = _request t.equal('https://' + opts.hostname + opts.path, downloadUrl, 'correct url') return _request.apply(https, arguments) } t.equal(fs.existsSync(build), false, 'no build folder') download(opts, function (err) { t.error(err, 'no error') t.equal(fs.existsSync(util.prebuildCache()), true, 'prebuildCache created') t.equal(fs.existsSync(cachedPrebuild), true, 'prebuild was cached') t.equal(fs.existsSync(unpacked), true, unpacked + ' should exist') t.equal(fs.existsSync(tempFile), false, 'temp file should be gone') fs.exists = _exists fs.access = _access fs.mkdir = _mkdir fs.createWriteStream = _createWriteStream fs.createReadStream = _createReadStream }) })
afterEach(function () { clock.restore(); fs.access.restore(); fs.rename.restore(); FileRotateTransport.prototype._dateRotateUpdateDay.restore(); });
it('calls callback if log file already exists', function () { fs.access.yields(); transport._dateRotateLog(cb); fs.rename.should.not.have.been.called; cb.should.have.been.calledWithExactly({message: 'Archive already exists'}); });
after(() => { fs.access.restore(); });