Skip to content

Commit

Permalink
exec/execFile result value is in fact string|Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
nixar committed Oct 16, 2015
1 parent 43f4fc5 commit 6ee92aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
39 changes: 38 additions & 1 deletion node/node-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as crypto from "crypto";
import * as tls from "tls";
import * as http from "http";
import * as net from "net";
import * as dgram from "dgram";
import * as dgram from "dgram";3
import * as querystring from "querystring";
import * as path from "path";
import * as readline from "readline";
Expand Down Expand Up @@ -409,3 +409,40 @@ rl.question("do you like typescript?", function(answer: string) {

childProcess.exec("echo test");
childProcess.spawnSync("echo test");

//////////////////////////////////////////////////////////////////
/// Check for exec / execFile result type, in case spec changes///
/// https://github.com/nodejs/node/issues/3389 ///
//////////////////////////////////////////////////////////////////

function execFileTest(opt:Object,cb:(stdout:any)=>void) {
childProcess.execFile('/bin/echo', ['test'], opt,
(error, stdout, stderr) => {
assert(!error);
return cb(stdout);
});
}

function execTest(opt:Object,cb:(stdout:any)=>void) {
childProcess.exec('/bin/echo', opt,
(error, stdout, stderr) => {
assert(!error);
return cb(stdout);
});
}

[execFileTest,execTest].forEach(test=>{
test({}, stdout => {
assert.equal(typeof stdout, 'string', 'Default encoding is utf8, should return a string');
});

test({encoding:'utf8'}, stdout => {
assert.equal(typeof stdout, 'string', 'Encoding is explicitly set as utf8, should return a string');
});

test({encoding:'buffer'}, stdout => {
assert.equal(typeof stdout, 'object', 'Encoding set as "buffer", should return an object');
assert.equal(Object.getPrototypeOf(stdout).constructor.name, 'Buffer',
'Encoding set as "buffer", result\'s constructor should be named Buffer');
});
})
14 changes: 7 additions & 7 deletions node/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,26 +843,26 @@ declare module "child_process" {
stdio?: any;
customFds?: any;
env?: any;
encoding?: string;
encoding?: string; /// default: 'utf8'. Use 'buffer' to get a Buffer result instead of a string
timeout?: number;
maxBuffer?: number;
killSignal?: string;
}, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
}, callback?: (error: Error, stdout: string|Buffer, stderr: string | Buffer) =>void ): ChildProcess;
export function exec(command: string, callback?: (error: Error, stdout: string | Buffer, stderr: string | Buffer) =>void ): ChildProcess;
export function execFile(file: string,
callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
callback?: (error: Error, stdout: string | Buffer, stderr: string | Buffer) =>void ): ChildProcess;
export function execFile(file: string, args?: string[],
callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
callback?: (error: Error, stdout: string | Buffer, stderr: string | Buffer) =>void ): ChildProcess;
export function execFile(file: string, args?: string[], options?: {
cwd?: string;
stdio?: any;
customFds?: any;
env?: any;
encoding?: string;
encoding?: string; /// default: 'utf8'. Use 'buffer' to get a Buffer result instead of a string
timeout?: number;
maxBuffer?: number;
killSignal?: string;
}, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
}, callback?: (error: Error, stdout: string | Buffer, stderr: string | Buffer) =>void ): ChildProcess;
export function fork(modulePath: string, args?: string[], options?: {
cwd?: string;
env?: any;
Expand Down

0 comments on commit 6ee92aa

Please sign in to comment.