Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load surrounding code from paths relative the project root #1405

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/plugin-node-surrounding-code/surrounding-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { createReadStream } = require('fs')
const { Writable } = require('stream')
const pump = require('pump')
const byline = require('byline')
const path = require('path')

module.exports = {
load: client => {
Expand All @@ -13,12 +14,13 @@ module.exports = {
const loadSurroundingCode = (stackframe, cache) => new Promise((resolve, reject) => {
try {
if (!stackframe.lineNumber || !stackframe.file) return resolve(stackframe)
const cacheKey = `${stackframe.file}@${stackframe.lineNumber}`
const file = path.resolve(client._config.projectRoot, stackframe.file)
const cacheKey = `${file}@${stackframe.lineNumber}`
if (cacheKey in cache) {
stackframe.code = cache[cacheKey]
return resolve(stackframe)
}
getSurroundingCode(stackframe.file, stackframe.lineNumber, (err, code) => {
getSurroundingCode(file, stackframe.lineNumber, (err, code) => {
if (err) return resolve(stackframe)
stackframe.code = cache[cacheKey] = code
return resolve(stackframe)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import plugin from '../'
import { join } from 'path'
import Event from '@bugsnag/core/event'
import Client from '@bugsnag/core/client'
import { schema as defaultSchema } from '@bugsnag/core/config'

let createReadStreamCount = 0
const originalReadStream = fs.createReadStream
Expand Down Expand Up @@ -56,6 +57,60 @@ describe('plugin: node surrounding code', () => {
]))
})

it('should load code successfully for stackframes whose files are relative to the project root', done => {
const client = new Client(
{ apiKey: 'api_key', projectRoot: __dirname },
{
...defaultSchema,
projectRoot: {
defaultValue: () => null,
validate: () => true,
message: 'should be a directory'
}
},
[plugin]
)

client._setDelivery(client => ({
sendEvent: (payload) => {
const evt = payload.events[0]
expect(Object.keys(evt.errors[0].stacktrace[0].code!))
.toEqual(['19', '20', '21', '22', '23', '24', '25'])
expect(evt.errors[0].stacktrace[0].code!['22'])
.toBe(' if (cb) this.on(\'finish\', () => cb(this.output()))')

expect(Object.keys(evt.errors[0].stacktrace[1].code!))
.toEqual(['28', '29', '30', '31', '32', '33', '34'])
expect(evt.errors[0].stacktrace[1].code!['31'])
.toBe(' return nextLevelUp()')

expect(Object.keys(evt.errors[0].stacktrace[2].code!))
.toEqual(['115', '116', '117', '118', '119', '120', '121'])
expect(evt.errors[0].stacktrace[2].code!['118'])
.toBe(' \'Ķ\': \'k\', \'Ļ\': \'L\', \'Ņ\': \'N\', \'Ū\': \'u\'')

done()
},
sendSession: () => {}
}))

client._notify(new Event('Error', 'surrounding code loading test', [
{
lineNumber: 22,
columnNumber: 18,
fileName: join('fixtures', '01.js')
}, {
lineNumber: 31,
columnNumber: 7,
fileName: join('fixtures', '02.js')
}, {
lineNumber: 118,
columnNumber: 28,
fileName: join('fixtures', '03.js')
}
]))
})

it('should tolerate missing files for some stackframes', done => {
const client = new Client({ apiKey: 'api_key' }, undefined, [plugin])

Expand Down