.end(function(err, res) { expect(fs.createWriteStream.calledWithMatch( new RegExp(res.body.login+'\\.png') )).to.be.equal(true); done(); });
test('non existing host should fail with no dangling temp file', function (t) { t.plan(3) var opts = getOpts() opts.pkg.binary = { host: 'https://foo.bar.baz' } var downloadUrl = util.getDownloadUrl(opts) var cachedPrebuild = util.cachedPrebuild(downloadUrl) var _createWriteStream = fs.createWriteStream.bind(fs) fs.createWriteStream = function (path) { t.ok(false, 'no temporary file should be written') return _createWriteStream(path) } t.equal(fs.existsSync(cachedPrebuild), false, 'nothing cached') download(opts, function (err) { t.ok(err, 'should error') t.equal(fs.existsSync(cachedPrebuild), false, 'nothing cached') fs.createWriteStream = _createWriteStream }) })
afterEach(function() { if (fs.createWriteStream.restore) { fs.createWriteStream.restore(); } if (fs.createReadStream.restore) { fs.createReadStream.restore(); } });
'saves a file': function (test) { const todo = JSON.parse('{"queue_time":1507509981169,"domain":"redacteed.com","rcpt_to":[{"original":"<*****@*****.**>","original_host":"redacteed.com","host":"redacteed.com","user":"******"}],"mail_from":{"original":"<*****@*****.**>","original_host":"tnpi.net","host":"tnpi.net","user":"******"},"notes":{"authentication_results":["spf=pass smtp.mailfrom=tnpi.net"],"spf_mail_result":"Pass","spf_mail_record":"v=spf1 a mx include:mx.theartfarm.com ?include:forwards._spf.tnpi.net include:lists._spf.tnpi.net -all","attachment_count":0,"attachments":[{"ctype":"application/pdf","filename":"FileWithoutAccent Chars.pdf","extension":".pdf","md5":"6c1d5f5c047cff3f6320b1210970bdf6"}],"attachment_ctypes":["application/pdf","multipart/mixed","text/plain","application/pdf"],"attachment_files":["FileWithoutaccent Chars.pdf"],"attachment_archive_files":[]},"uuid":"1D5483B0-3E00-4280-A961-3AFD2017B4FC.1"}'); const fd = fs.openSync('tests/queue/plain', 'w'); const ws = new fs.createWriteStream('tests/queue/plain', { fd, flags: constants.WRITE_EXCL }); ws.on('close', () => { // console.log(arguments); test.ok(1); test.done(); }) ws.on('error', (e) => { console.error(e); test.done(); }) this.outbound.build_todo(todo, ws, () => { ws.write(Buffer.from('This is the message body')); fs.fsync(fd, () => { ws.close(); }) }) },
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 }) })
it('can output to a writable stream from a string argument', function () { var fakeStream = {}; sinon.stub(fs, 'createWriteStream').returns(fakeStream); var stream = this.stream = mocha({output: 'result.log'}); stream.end(); fs.createWriteStream.should.be.calledWith('result.log'); fs.createWriteStream.restore(); this.childOut.pipe.should.be.calledWith(fakeStream); this.childErr.pipe.should.be.calledWith(fakeStream); });
test('error during download should fail with no dangling temp file', function (t) { t.plan(7) var downloadError = new Error('something went wrong during download') var opts = getOpts() opts.pkg.binary = { host: 'http://localhost:8889', remote_path: 'prebuilds', package_name: 'woohooo-{abi}' } var downloadUrl = util.getDownloadUrl(opts) var cachedPrebuild = util.cachedPrebuild(downloadUrl) var tempFile var _createWriteStream = fs.createWriteStream.bind(fs) fs.createWriteStream = function (path) { tempFile = path t.ok(/\.tmp$/i.test(path), 'this is the temporary file') return _createWriteStream(path) } var _request = http.request http.request = function (opts) { http.request = _request t.equal('http://' + opts.hostname + ':' + opts.port + opts.path, downloadUrl, 'correct url') var wrapped = arguments[1] arguments[1] = function (res) { t.equal(res.statusCode, 200, 'correct statusCode') // simulates error during download setTimeout(function () { res.emit('error', downloadError) }, 10) wrapped(res) } return _request.apply(http, arguments) } var server = http.createServer(function (req, res) { t.equal(req.url, '/prebuilds/woohooo-' + process.versions.modules, 'correct url') res.statusCode = 200 res.write('yep') // simulates hanging request }).listen(8889, function () { download(opts, function (err) { t.equal(err.message, downloadError.message, 'correct error') t.equal(fs.existsSync(tempFile), false, 'no dangling temp file') t.equal(fs.existsSync(cachedPrebuild), false, 'nothing cached') t.end() fs.createWriteStream = _createWriteStream server.unref() }) }) })
"should create write stream and pipe to it": function () { this.stub(fs, "existsSync").returns(true); fs.createWriteStream.returns("MY_FD"); this.attachFn(this.req, this.res); assert.called(fs.createWriteStream); assert.calledWith(fs.createWriteStream, "/path/test/test.tgz", { flags : "w", encoding : null, mode : "0660" }); assert.called(this.req.pipe); assert.calledWith(this.req.pipe, "MY_FD"); }
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.json'); sandbox.stub(fs, 'createWriteStream'); fs.createWriteStream.returns(writeStream); writeStream.on.returns(writeStream); storage.set('testPrefix', 'testKey', 'test_value'); storage.saveAsync(); expect(writeStream.write).to.be.calledWith('"testPrefix":'); expect(writeStream.write).to.be.calledWith(JSON.stringify({ testKey: 'test_value' })); });
afterEach(function() { http.get.restore(); fs.createWriteStream.restore(); });
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 }) })
it('initialises the file output stream', () => { const expectedPath = './feed/corleone_family/tom_hagen_2016-04-20T13:58:51.084Z.json'; assert.isDefined(stream.fileStream); assert.ok(fs.createWriteStream.calledWith(expectedPath)); });
.end(function() { expect(fs.createWriteStream.calledWithMatch(/public\/avatars\/.*\.png/)).to.be.equal(true); done(); });
fs.fsync(fd, () => { ws.close(); })
this.outbound.build_todo(todo, ws, () => { ws.write(Buffer.from('This is the message body')); fs.fsync(fd, () => { ws.close(); }) })
const nameToWriteStream = filePath => tryFunction(fs.createWriteStream.bind(fs), e => console.warn(e))(filePath);