Skip to content

Commit

Permalink
fix: Don't copy the prototype of Object to opt (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore authored Sep 19, 2023
1 parent be9a359 commit c0babe9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ exports.spawn = function(cmd, args, opt) {
opt = args;
args = undefined;
}

return new Coffee({
method: 'spawn',
cmd,
Expand Down
9 changes: 7 additions & 2 deletions lib/coffee.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Coffee extends EventEmitter {

constructor(options = {}) {
super();
const { method, cmd, args, opt = {} } = options;
const { method, cmd, args, opt = getPlainObject() } = options;

assert(method && cmd, 'should specify method and cmd');
assert(!opt.cwd || fs.existsSync(opt.cwd), `opt.cwd(${opt.cwd}) not exists`);
Expand Down Expand Up @@ -360,7 +360,8 @@ function run(method, cmd, args, opt) {
}

args = args || [];
opt = opt || {};

opt = opt || getPlainObject();

// Force pipe to parent
if (method === 'fork') {
Expand All @@ -375,3 +376,7 @@ function run(method, cmd, args, opt) {
if (process.platform === 'win32' && method === 'spawn') handler = spawn;
return handler(cmd, args, opt);
}

function getPlainObject() {
return Object.create(null);
}
12 changes: 12 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,18 @@ describe('coffee', () => {
.expect('code', 0)
.end(done);
});

it('should success', done => {
mm(Object.prototype, 'shell', 'node');

coffee.spawn('cat')
// .debug()
.write('1\n')
.write('2')
.expect('stdout', '1\n2')
.expect('code', 0)
.end(done);
});
});

describe('extendable', () => {
Expand Down

0 comments on commit c0babe9

Please sign in to comment.