add new tests

This commit is contained in:
Shelley Vohr 2017-10-30 22:51:22 -04:00
parent c9926bed9f
commit 603060f051
No known key found for this signature in database
GPG key ID: F13993A75599653C
2 changed files with 107 additions and 45 deletions

View file

@ -9,36 +9,25 @@ const binding = process.atomBinding('crash_reporter')
class CrashReporter { class CrashReporter {
start (options) { start (options) {
if (options == null) { if (options == null) options = {}
options = {}
}
this.productName = options.productName != null ? options.productName : app.getName() this.productName = options.productName != null ? options.productName : app.getName()
let {companyName, extra, ignoreSystemCrashHandler, submitURL, uploadToServer} = options
if (uploadToServer == null) { let {
// TODO: Remove deprecated autoSubmit property in 2.0 companyName,
uploadToServer = options.autoSubmit extra,
} ignoreSystemCrashHandler,
submitURL,
uploadToServer
} = options
if (uploadToServer == null) { if (uploadToServer == null) uploadToServer = options.autoSubmit || true
uploadToServer = true if (ignoreSystemCrashHandler == null) ignoreSystemCrashHandler = false
} if (extra == null) extra = {}
if (extra._productName == null) extra._productName = this.getProductName()
if (extra._companyName == null) extra._companyName = companyName
if (extra._version == null) extra._version = app.getVersion()
if (ignoreSystemCrashHandler == null) {
ignoreSystemCrashHandler = false
}
if (extra == null) {
extra = {}
}
if (extra._productName == null) {
extra._productName = this.getProductName()
}
if (extra._companyName == null) {
extra._companyName = companyName
}
if (extra._version == null) {
extra._version = app.getVersion()
}
if (companyName == null) { if (companyName == null) {
throw new Error('companyName is a required option to crashReporter.start') throw new Error('companyName is a required option to crashReporter.start')
} }
@ -47,15 +36,14 @@ class CrashReporter {
} }
if (process.platform === 'win32') { if (process.platform === 'win32') {
const env = { ELECTRON_INTERNAL_CRASH_SERVICE: 1 }
const args = [ const args = [
'--reporter-url=' + submitURL, '--reporter-url=' + submitURL,
'--application-name=' + this.getProductName(), '--application-name=' + this.getProductName(),
'--crashes-directory=' + this.getCrashesDirectory(), '--crashes-directory=' + this.getCrashesDirectory(),
'--v=1' '--v=1'
] ]
const env = {
ELECTRON_INTERNAL_CRASH_SERVICE: 1
}
this._crashServiceProcess = spawn(process.execPath, args, { this._crashServiceProcess = spawn(process.execPath, args, {
env: env, env: env,
detached: true detached: true
@ -67,11 +55,7 @@ class CrashReporter {
getLastCrashReport () { getLastCrashReport () {
const reports = this.getUploadedReports() const reports = this.getUploadedReports()
if (reports.length > 0) { return (reports.length > 0) ? reports[0] : null
return reports[0]
} else {
return null
}
} }
getUploadedReports () { getUploadedReports () {
@ -79,7 +63,7 @@ class CrashReporter {
} }
getCrashesDirectory () { getCrashesDirectory () {
const crashesDir = this.getProductName() + ' Crashes' const crashesDir = `${this.getProductName()} Crashes`
return path.join(this.getTempDirectory(), crashesDir) return path.join(this.getTempDirectory(), crashesDir)
} }
@ -95,7 +79,6 @@ class CrashReporter {
try { try {
this.tempDirectory = app.getPath('temp') this.tempDirectory = app.getPath('temp')
} catch (error) { } catch (error) {
// app.getPath may throw so fallback to OS temp directory
this.tempDirectory = os.tmpdir() this.tempDirectory = os.tmpdir()
} }
} }
@ -118,6 +101,10 @@ class CrashReporter {
} }
} }
removeExtraParameter (key) {
binding.setExtraParameter(key)
}
setExtraParameter (key, value) { setExtraParameter (key, value) {
binding.setExtraParameter(key, value) binding.setExtraParameter(key, value)
} }

View file

@ -35,9 +35,7 @@ describe('crashReporter module', () => {
beforeEach(() => { beforeEach(() => {
stopServer = null stopServer = null
w = new BrowserWindow(Object.assign({ w = new BrowserWindow(Object.assign({ show: false }, browserWindowOpts))
show: false
}, browserWindowOpts))
}) })
afterEach(() => closeWindow(w).then(() => { w = null })) afterEach(() => closeWindow(w).then(() => { w = null }))
@ -199,7 +197,22 @@ describe('crashReporter module', () => {
} }
}) })
describe('.start(options)', () => { // complete
describe('getProductName', () => {
it('returns the product name if one is specified', () => {
const name = crashReporter.getProductName()
assert.equal(name, 'Zombies')
})
})
describe('getTempDirectory', () => {
it('returns temp directory for app if one is specified', () => {
const tempDir = crashReporter.getTempDirectory()
assert.equal(tempDir, app.getPath('temp'))
})
})
describe('start(options)', () => {
it('requires that the companyName and submitURL options be specified', () => { it('requires that the companyName and submitURL options be specified', () => {
assert.throws(() => { assert.throws(() => {
crashReporter.start({companyName: 'Missing submitURL'}) crashReporter.start({companyName: 'Missing submitURL'})
@ -208,7 +221,6 @@ describe('crashReporter module', () => {
crashReporter.start({submitURL: 'Missing companyName'}) crashReporter.start({submitURL: 'Missing companyName'})
}, /companyName is a required option to crashReporter\.start/) }, /companyName is a required option to crashReporter\.start/)
}) })
it('can be called multiple times', () => { it('can be called multiple times', () => {
assert.doesNotThrow(() => { assert.doesNotThrow(() => {
crashReporter.start({ crashReporter.start({
@ -223,13 +235,37 @@ describe('crashReporter module', () => {
}) })
}) })
}) })
// complete
describe('getCrashesDirectory', () => {
it('correctly returns the directory', () => {
const crashesDir = crashReporter.getCrashesDirectory()
const dir = `${app.getPath('temp')}Zombies Crashes`
assert.equal(crashesDir, dir)
})
})
describe('.get/setUploadToServer', () => { describe('getUploadedReports', () => {
it('returns an array of reports', () => {
const reports = crashReporter.getUploadedReports()
assert(typeof reports === 'object')
})
})
describe('getLastCrashReport', () => {
it('correctly returns the most recent report', () => {
// TODO(codebytere): figure this out
const reports = crashReporter.getUploadedReports()
const lastReport = reports[0]
assert(lastReport != null)
})
})
// complete
describe('getUploadToServer()', () => {
it('throws an error when called from the renderer process', () => { it('throws an error when called from the renderer process', () => {
assert.throws(() => require('electron').crashReporter.getUploadToServer()) assert.throws(() => require('electron').crashReporter.getUploadToServer())
}) })
it('returns true when uploadToServer is true', () => {
it('can be read/set from the main process', () => {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
crashReporter.start({ crashReporter.start({
companyName: 'Umbrella Corporation', companyName: 'Umbrella Corporation',
@ -237,13 +273,52 @@ describe('crashReporter module', () => {
uploadToServer: true uploadToServer: true
}) })
assert.equal(crashReporter.getUploadToServer(), true) assert.equal(crashReporter.getUploadToServer(), true)
}
})
it('returns false when uploadToServer is false', () => {
if (process.platform === 'darwin') {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes',
uploadToServer: false
})
assert.equal(crashReporter.getUploadToServer(), false)
}
})
})
// complete
describe('setUploadToServer(uploadToServer)', () => {
it('throws an error when called from the renderer process', () => {
assert.throws(() => require('electron').crashReporter.setUploadToServer('arg'))
})
it('sets uploadToServer false when called with false', () => {
if (process.platform === 'darwin') {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes',
uploadToServer: true
})
crashReporter.setUploadToServer(false) crashReporter.setUploadToServer(false)
assert.equal(crashReporter.getUploadToServer(), false) assert.equal(crashReporter.getUploadToServer(), false)
} else { }
})
it('sets uploadToServer true when called with true', () => {
if (process.platform === 'darwin') {
crashReporter.start({
companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes',
uploadToServer: false
})
crashReporter.setUploadToServer(true)
assert.equal(crashReporter.getUploadToServer(), true) assert.equal(crashReporter.getUploadToServer(), true)
} }
}) })
}) })
describe('setExtraParameter', () => {
//
})
}) })
const waitForCrashReport = () => { const waitForCrashReport = () => {