Browse Source

add basic inspector module test

deepak1556 7 years ago
parent
commit
0b88873e77
2 changed files with 128 additions and 18 deletions
  1. 84 0
      spec/fixtures/module/inspector-binding.js
  2. 44 18
      spec/node-spec.js

+ 84 - 0
spec/fixtures/module/inspector-binding.js

@@ -0,0 +1,84 @@
+const inspector = require('inspector')
+const path = require('path')
+
+// This test case will set a breakpoint 4 lines below
+function debuggedFunction () {
+  let i
+  let accum = 0
+  for (i = 0; i < 5; i++) {
+    accum += i
+  }
+  return accum
+}
+
+let scopeCallback = null
+
+function checkScope (session, scopeId) {
+  session.post('Runtime.getProperties', {
+    'objectId': scopeId,
+    'ownProperties': false,
+    'accessorPropertiesOnly': false,
+    'generatePreview': true
+  }, scopeCallback)
+}
+
+function debuggerPausedCallback (session, notification) {
+  const params = notification['params']
+  const callFrame = params['callFrames'][0]
+  const scopeId = callFrame['scopeChain'][0]['object']['objectId']
+  checkScope(session, scopeId)
+}
+
+function testSampleDebugSession () {
+  let cur = 0
+  const failures = []
+  const expects = {
+    i: [0, 1, 2, 3, 4],
+    accum: [0, 0, 1, 3, 6]
+  }
+  scopeCallback = function (error, result) {
+    if (error) failures.push(error)
+    const i = cur++
+    let v, actual, expected
+    for (v of result['result']) {
+      actual = v['value']['value']
+      expected = expects[v['name']][i]
+      if (actual !== expected) {
+        failures.push(`Iteration ${i} variable: ${v['name']} ` +
+          `expected: ${expected} actual: ${actual}`)
+      }
+    }
+  }
+  const session = new inspector.Session()
+  session.connect()
+  let secondSessionOpened = false
+  const secondSession = new inspector.Session()
+  try {
+    secondSession.connect()
+    secondSessionOpened = true
+  } catch (error) {
+    // expected as the session already exists
+  }
+  session.on('Debugger.paused',
+             (notification) => debuggerPausedCallback(session, notification))
+  let cbAsSecondArgCalled = false
+  session.post('Debugger.enable', () => { cbAsSecondArgCalled = true })
+  session.post('Debugger.setBreakpointByUrl', {
+    'lineNumber': 8,
+    'url': path.resolve(__dirname, __filename),
+    'columnNumber': 0,
+    'condition': ''
+  })
+
+  debuggedFunction()
+  scopeCallback = null
+  session.disconnect()
+  process.send({
+    'cmd': 'assert',
+    'debuggerEnabled': cbAsSecondArgCalled,
+    'secondSessionOpened': secondSessionOpened,
+    'success': (cur === 5) && (failures.length === 0)
+  })
+}
+
+testSampleDebugSession()

+ 44 - 18
spec/node-spec.js

@@ -130,24 +130,6 @@ describe('node feature', () => {
           done()
         })
       })
-
-      it('supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
-        child = ChildProcess.spawn(process.execPath, ['--inspect-brk', path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
-          env: {
-            ELECTRON_RUN_AS_NODE: true
-          }
-        })
-
-        let output = ''
-        child.stderr.on('data', (data) => {
-          output += data
-          if (output.trim().startsWith('Debugger listening on ws://')) done()
-        })
-
-        child.stdout.on('data', (data) => {
-          done(new Error(`Unexpected output: ${data.toString()}`))
-        })
-      })
     })
   })
 
@@ -218,6 +200,50 @@ describe('node feature', () => {
     })
   })
 
+  describe('inspector', () => {
+    let child
+
+    afterEach(() => {
+      if (child != null) child.kill()
+    })
+
+    it('supports starting the v8 inspector with --inspect/--inspect-brk', (done) => {
+      child = ChildProcess.spawn(process.execPath, ['--inspect-brk', path.join(__dirname, 'fixtures', 'module', 'run-as-node.js')], {
+        env: {
+          ELECTRON_RUN_AS_NODE: true
+        }
+      })
+
+      let output = ''
+      child.stderr.on('data', (data) => {
+        output += data
+        if (output.trim().startsWith('Debugger listening on ws://')) done()
+      })
+
+      child.stdout.on('data', (data) => {
+        done(new Error(`Unexpected output: ${data.toString()}`))
+      })
+    })
+
+    it('supports js binding', (done) => {
+      child = ChildProcess.spawn(process.execPath, ['--inspect', path.join(__dirname, 'fixtures', 'module', 'inspector-binding.js')], {
+        env: {
+          ELECTRON_RUN_AS_NODE: true
+        },
+        stdio: ['ipc']
+      })
+
+      child.on('message', ({cmd, debuggerEnabled, secondSessionOpened, success}) => {
+        if (cmd === 'assert') {
+          assert.equal(debuggerEnabled, true)
+          assert.equal(secondSessionOpened, false)
+          assert.equal(success, true)
+          done()
+        }
+      })
+    })
+  })
+
   describe('message loop', () => {
     describe('process.nextTick', () => {
       it('emits the callback', (done) => {