CB-12557: add both stdout and stderr properties to the error object passed to supersp...
authorfilmaj <maj.fil@gmail.com>
Thu, 9 Mar 2017 15:18:34 +0000 (07:18 -0800)
committerfilmaj <maj.fil@gmail.com>
Thu, 9 Mar 2017 15:51:16 +0000 (07:51 -0800)
spec/superspawn.spec.js
src/superspawn.js

index 73a85d9..104073b 100644 (file)
@@ -53,4 +53,39 @@ describe('spawn method', function() {
         });\r
     });\r
 \r
+    it('Test 004 : reject handler should pass in Error object with stdout and stderr properties', function(done) {\r
+        var cp = require('child_process');\r
+        spyOn(cp, 'spawn').and.callFake(function(cmd, args, opts) {\r
+            return {\r
+                stdout:{\r
+                    setEncoding: function(){},\r
+                    on: function(evt, handler) {\r
+                        // some sample stdout output\r
+                        handler('business as usual');\r
+                    }\r
+                },\r
+                stderr:{\r
+                    setEncoding: function(){},\r
+                    on: function(evt, handler) {\r
+                        // some sample stderr output\r
+                        handler('mayday mayday');\r
+                    }\r
+                },\r
+                on: function(evt, handler) {\r
+                    // What's passed to handler here is the exit code, so we can control\r
+                    // resolve/reject flow via this argument.\r
+                    handler(1); // this will trigger error flow\r
+                },\r
+                removeListener: function() {}\r
+            };\r
+        });\r
+        superspawn.spawn('this aggression', ['will', 'not', 'stand', 'man'], {})\r
+        .catch(function(err) {\r
+            expect(err).toBeDefined();\r
+            expect(err.stdout).toContain('usual');\r
+            expect(err.stderr).toContain('mayday');\r
+            done();\r
+        });\r
+    });\r
+\r
 });\r
index a3f1431..96ec09d 100644 (file)
@@ -167,6 +167,12 @@ exports.spawn = function(cmd, args, opts) {
                 errMsg += ' Error output:\n' + capturedErr.trim();
             }
             var err = new Error(errMsg);
+            if (capturedErr) {
+                err.stderr = capturedErr;
+            }
+            if (capturedOut) {
+                err.stdout = capturedOut;
+            }
             err.code = code;
             d.reject(err);
         }