Skip to content

Commit

Permalink
fix: tolerate space when parsing file directory (#136)
Browse files Browse the repository at this point in the history
* fix: tolerate space when preloading ipc.js

Fixes: clinicjs/node-clinic#448

* test: stricter check on exit code

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

---------

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
jazelly and Eomm authored Apr 29, 2023
1 parent 60b2115 commit 91fe729
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ class ClinicHeapProfiler extends events.EventEmitter {
}

collect (args, cb) {
const nodeOptions = ` -r ${path.join(__dirname, './injects/ipc.js')}`
let preloadPath = path.join(__dirname, './injects/ipc.js')
if (process.platform === 'win32') {
preloadPath = preloadPath.replace(/\\/g, '\\\\')
}

const nodeOptions = `-r "${preloadPath}"`

const env = {
...process.env,
Expand Down
22 changes: 22 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const { spawn } = require('child_process')
const fs = require('fs')
const path = require('path')
const { test } = require('tap')
Expand Down Expand Up @@ -73,6 +74,27 @@ test('cmd - test collect - data exists, html generated', t => {
})
})

test('child_process - test spawn - filepath with spaces should be preloaded', t => {
const env = process.env

// double quotes are required for paths with spaces
env.NODE_OPTIONS = `-r "${path.join(__dirname, 'fixtures', 'file with space.js')}"`
if (process.platform === 'win32') {
env.NODE_OPTIONS = env.NODE_OPTIONS.replace(/\\/g, '\\\\')
}

this.process = spawn(
process.execPath,
[path.join('test', 'fixtures', 'randomHashes.js')],
{ stdio: ['ignore', 'inherit', 'inherit'], env }
)

this.process.once('exit', (code) => {
t.equal(code, 0)
t.end()
})
})

test('cmd - test visualization - missing data', t => {
const tool = new ClinicHeapProfiler({ debug: true })

Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/file with space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'

function noop () {}
noop()

0 comments on commit 91fe729

Please sign in to comment.