electron/spec/api-crash-reporter-spec.js

208 lines
6.2 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
2017-01-24 21:49:45 +00:00
const childProcess = require('child_process')
2016-03-25 20:03:49 +00:00
const http = require('http')
const multiparty = require('multiparty')
const path = require('path')
const temp = require('temp').track()
2016-03-25 20:03:49 +00:00
const url = require('url')
const {closeWindow} = require('./window-helpers')
2016-01-12 02:40:23 +00:00
const {remote} = require('electron')
const {app, BrowserWindow, crashReporter} = remote.require('electron')
2016-01-12 02:40:23 +00:00
2016-08-09 22:41:46 +00:00
describe('crashReporter module', function () {
2016-03-25 20:03:49 +00:00
var fixtures = path.resolve(__dirname, 'fixtures')
var w = null
var originalTempDirectory = null
var tempDirectory = null
2016-03-25 20:03:49 +00:00
beforeEach(function () {
2016-02-17 01:39:11 +00:00
w = new BrowserWindow({
2016-01-12 02:40:23 +00:00
show: false
2016-03-25 20:03:49 +00:00
})
tempDirectory = temp.mkdirSync('electronCrashReporterSpec-')
originalTempDirectory = app.getPath('temp')
app.setPath('temp', tempDirectory)
2016-03-25 20:03:49 +00:00
})
2016-03-25 20:03:49 +00:00
afterEach(function () {
app.setPath('temp', originalTempDirectory)
return closeWindow(w).then(function () { w = null })
2016-03-25 20:03:49 +00:00
})
2016-01-12 02:40:23 +00:00
if (process.mas) {
2016-03-25 20:03:49 +00:00
return
2016-01-12 02:40:23 +00:00
}
2016-03-25 20:03:49 +00:00
it('should send minidump when renderer crashes', function (done) {
if (process.env.APPVEYOR === 'True') return done()
2017-01-24 22:12:35 +00:00
if (process.env.TRAVIS === 'true') return done()
2016-11-29 22:18:42 +00:00
2016-03-25 20:03:49 +00:00
this.timeout(120000)
2017-01-24 21:49:45 +00:00
startServer({
callback (port) {
const crashUrl = url.format({
protocol: 'file',
pathname: path.join(fixtures, 'api', 'crash.html'),
search: '?port=' + port
2016-08-09 22:41:46 +00:00
})
2017-01-24 21:49:45 +00:00
w.loadURL(crashUrl)
},
processType: 'renderer',
done: done
2016-03-25 20:03:49 +00:00
})
2017-01-24 21:49:45 +00:00
})
it('should send minidump when node processes crash', function (done) {
if (process.env.APPVEYOR === 'True') return done()
2017-01-24 22:12:35 +00:00
if (process.env.TRAVIS === 'true') return done()
2017-01-24 21:49:45 +00:00
this.timeout(120000)
startServer({
callback (port) {
const crashesDir = path.join(app.getPath('temp'), `${app.getName()} Crashes`)
const version = app.getVersion()
const crashPath = path.join(fixtures, 'module', 'crash.js')
childProcess.fork(crashPath, [port, version, crashesDir], {silent: true})
},
processType: 'browser',
done: done
2016-03-25 20:03:49 +00:00
})
})
2017-02-13 18:46:19 +00:00
it('should send minidump with updated extra parameters', function (done) {
if (process.env.APPVEYOR === 'True') return done()
if (process.env.TRAVIS === 'true') return done()
this.timeout(10000)
startServer({
callback (port) {
const crashUrl = url.format({
protocol: 'file',
pathname: path.join(fixtures, 'api', 'crash-restart.html'),
search: '?port=' + port
})
w.loadURL(crashUrl)
},
processType: 'renderer',
done: done
})
})
2016-04-28 16:58:04 +00:00
describe('.start(options)', function () {
2016-03-25 20:03:49 +00:00
it('requires that the companyName and submitURL options be specified', function () {
assert.throws(function () {
2016-02-17 01:39:11 +00:00
crashReporter.start({
2016-01-12 02:40:23 +00:00
companyName: 'Missing submitURL'
2016-03-25 20:03:49 +00:00
})
}, /submitURL is a required option to crashReporter\.start/)
2016-03-25 20:03:49 +00:00
assert.throws(function () {
2016-02-17 01:39:11 +00:00
crashReporter.start({
2016-01-12 02:40:23 +00:00
submitURL: 'Missing companyName'
2016-03-25 20:03:49 +00:00
})
}, /companyName is a required option to crashReporter\.start/)
2016-03-25 20:03:49 +00:00
})
it('can be called multiple times', function () {
assert.doesNotThrow(function () {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes'
})
crashReporter.start({
companyName: 'Umbrella Corporation 2',
submitURL: 'http://127.0.0.1/more-crashes'
})
})
})
2016-03-25 20:03:49 +00:00
})
2016-11-28 23:01:38 +00:00
describe('.get/setUploadToServer', function () {
it('throws an error when called from the renderer process', function () {
assert.throws(() => require('electron').crashReporter.getUploadToServer())
})
it('can be read/set from the main process', function () {
if (process.platform === 'darwin') {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes',
2017-02-13 18:08:43 +00:00
uploadToServer: true
2016-11-28 23:01:38 +00:00
})
assert.equal(crashReporter.getUploadToServer(), true)
crashReporter.setUploadToServer(false)
assert.equal(crashReporter.getUploadToServer(), false)
} else {
assert.equal(crashReporter.getUploadToServer(), true)
2016-11-28 23:01:38 +00:00
}
})
})
2016-03-25 20:03:49 +00:00
})
const waitForCrashReport = () => {
return new Promise((resolve, reject) => {
let times = 0
const checkForReport = () => {
if (crashReporter.getLastCrashReport() != null) {
resolve()
} else if (times >= 10) {
reject(new Error('No crash report available'))
} else {
times++
setTimeout(checkForReport, 100)
}
}
checkForReport()
})
}
2017-01-24 21:49:45 +00:00
const startServer = ({callback, processType, done}) => {
var called = false
var server = http.createServer((req, res) => {
server.close()
var form = new multiparty.Form()
form.parse(req, (error, fields) => {
if (error) throw error
if (called) return
called = true
assert.equal(fields.prod, 'Electron')
assert.equal(fields.ver, process.versions.electron)
assert.equal(fields.process_type, processType)
assert.equal(fields.platform, process.platform)
assert.equal(fields.extra1, 'extra1')
assert.equal(fields.extra2, 'extra2')
2017-02-13 18:46:19 +00:00
assert.equal(fields.extra3, undefined)
2017-01-24 21:49:45 +00:00
assert.equal(fields._productName, 'Zombies')
assert.equal(fields._companyName, 'Umbrella Corporation')
assert.equal(fields._version, app.getVersion())
const reportId = 'abc-123-def-456-abc-789-abc-123-abcd'
res.end(reportId, () => {
waitForCrashReport().then(() => {
assert.equal(crashReporter.getLastCrashReport().id, reportId)
assert.notEqual(crashReporter.getUploadedReports().length, 0)
assert.equal(crashReporter.getUploadedReports()[0].id, reportId)
2017-02-13 18:46:19 +00:00
req.socket.destroy()
2017-01-24 21:49:45 +00:00
done()
}, done)
})
})
})
let {port} = remote.process
server.listen(port, '127.0.0.1', () => {
port = server.address().port
remote.process.port = port
if (process.platform === 'darwin') {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1:' + port
})
}
callback(port)
})
}