From 6ee92aa6e7733300f54492dbd13a21a4a1fc6f1f Mon Sep 17 00:00:00 2001 From: nixar Date: Fri, 16 Oct 2015 16:00:57 +0200 Subject: [PATCH] exec/execFile result value is in fact string|Buffer See https://github.com/nodejs/node/issues/3389 --- node/node-tests.ts | 39 ++++++++++++++++++++++++++++++++++++++- node/node.d.ts | 14 +++++++------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/node/node-tests.ts b/node/node-tests.ts index bd80329dcadc3e..5efb5949971258 100644 --- a/node/node-tests.ts +++ b/node/node-tests.ts @@ -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"; @@ -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'); + }); +}) diff --git a/node/node.d.ts b/node/node.d.ts index 9448b413b1f7e5..33b8d3517b1eef 100644 --- a/node/node.d.ts +++ b/node/node.d.ts @@ -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;