Skip to content

Commit

Permalink
Load code from paths relative the project root
Browse files Browse the repository at this point in the history
This allows the plugin to work if the project root has already been
stripped before it runs
  • Loading branch information
imjoehaines committed May 13, 2021
1 parent b5ec2a7 commit 1170ae7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
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

0 comments on commit 1170ae7

Please sign in to comment.