Add tests for uploadToServer option.

This commit is contained in:
Thiago de Arruda 2017-03-30 11:11:49 -03:00
parent f0d447cd68
commit 460fb9cdb9
2 changed files with 97 additions and 4 deletions

View file

@ -1,5 +1,6 @@
const assert = require('assert')
const childProcess = require('child_process')
const fs = require('fs')
const http = require('http')
const multiparty = require('multiparty')
const path = require('path')
@ -73,6 +74,93 @@ describe('crashReporter module', function () {
})
})
it('should not send minidump if uploadToServer is false', function (done) {
this.timeout(120000)
if (process.platform === 'darwin') {
crashReporter.setUploadToServer(false)
}
let server
let dumpFile
let crashesDir
const testDone = (uploaded) => {
if (uploaded) {
return done(new Error('fail'))
}
server.close()
if (process.platform === 'darwin') {
crashReporter.setUploadToServer(true)
}
assert(fs.existsSync(dumpFile))
fs.unlinkSync(dumpFile)
done()
}
let pollInterval
const pollDumpFile = () => {
fs.readdir(crashesDir, (err, files) => {
if (err) {
return
}
let dumps = files.filter((f) => /\.dmp$/.test(f))
if (!dumps.length) {
return
}
assert.equal(1, dumps.length)
dumpFile = path.join(crashesDir, dumps[0])
clearInterval(pollInterval)
// dump file should not be deleted when not uploading, so we wait
// 500 ms and assert it still exists in `testDone`
setTimeout(testDone, 500)
})
}
remote.ipcMain.once('set-crash-directory', (event, dir) => {
if (process.platform === 'linux') {
crashesDir = dir
} else {
crashesDir = crashReporter.getCrashesDirectory()
if (process.platform === 'darwin') {
// crashpad uses an extra subdirectory
crashesDir = path.join(crashesDir, 'completed')
}
}
// Before starting, remove all dump files in the crash directory.
// This is required because:
// - mac crashpad not seem to allow changing the crash directory after
// the first "start" call.
// - Other tests in this suite may leave dumps there.
// - We want to verify in `testDone` that a dump file is created and
// not deleted.
fs.readdir(crashesDir, (err, files) => {
if (!err) {
for (let file of files) {
if (/\.dmp$/.test(file)) {
fs.unlinkSync(path.join(crashesDir, file))
}
}
}
event.returnValue = null // allow the renderer to crash
pollInterval = setInterval(pollDumpFile, 100)
})
})
server = startServer({
callback (port) {
const crashUrl = url.format({
protocol: 'file',
pathname: path.join(fixtures, 'api', 'crash.html'),
search: `?port=${port}&skipUpload=1`
})
w.loadURL(crashUrl)
},
processType: 'renderer',
done: testDone.bind(null, true)
})
})
it('should send minidump with updated extra parameters', function (done) {
if (process.env.APPVEYOR === 'True') return done()
if (process.env.TRAVIS === 'true') return done()
@ -215,4 +303,5 @@ const startServer = ({callback, processType, done}) => {
}
callback(port)
})
return server
}

View file

@ -1,20 +1,24 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
var port = require('url').parse(window.location.href, true).query.port;
var crashReporter = require('electron').crashReporter;
var url = require('url').parse(window.location.href, true);
var uploadToServer = !url.query.skipUpload;
var port = url.query.port;
var {crashReporter, ipcRenderer} = require('electron');
crashReporter.start({
productName: 'Zombies',
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1:' + port,
uploadToServer: true,
uploadToServer: uploadToServer,
ignoreSystemCrashHandler: true,
extra: {
'extra1': 'extra1',
'extra2': 'extra2',
}
});
if (!uploadToServer) {
ipcRenderer.sendSync('set-crash-directory', crashReporter.getCrashesDirectory())
}
setImmediate(function() { process.crash(); });
</script>
</body>