michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: const { Cc, Ci } = require("chrome"); michael@0: const subprocess = require("subprocess"); michael@0: const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment); michael@0: michael@0: // For now, only test on windows michael@0: if (env.get('OS') && env.get('OS').match(/Windows/)) { michael@0: michael@0: exports.testWindows = function (test) { michael@0: test.waitUntilDone(); michael@0: let envTestValue = "OK"; michael@0: let gotStdout = false; michael@0: michael@0: var p = subprocess.call({ michael@0: // Retrieve windows cmd.exe path from env michael@0: command: env.get('ComSpec'), michael@0: // In order to execute a simple "echo" function michael@0: arguments: ['/C', 'echo %ENV_TEST%'], // ' & type CON' should display stdin, but doesn't work michael@0: // Printing an environnement variable set here by the parent process michael@0: environment: ['ENV_TEST='+envTestValue], michael@0: michael@0: stdin: function(stdin) { michael@0: // Win32 command line is not really made for stdin michael@0: // So it doesn't seems to work as it's hard to retrieve stdin michael@0: stdin.write("stdin"); michael@0: stdin.close(); michael@0: }, michael@0: stdout: function(data) { michael@0: test.assert(!gotStdout,"don't get stdout twice"); michael@0: test.assertEqual(data,envTestValue+"\r\n","stdout contains the environment variable"); michael@0: gotStdout = true; michael@0: }, michael@0: stderr: function(data) { michael@0: test.fail("shouldn't get stderr"); michael@0: }, michael@0: done: function() { michael@0: test.assert(gotStdout, "got stdout before finished"); michael@0: test.done(); michael@0: }, michael@0: mergeStderr: false michael@0: }); michael@0: michael@0: } michael@0: michael@0: exports.testWindowsStderr = function (test) { michael@0: test.waitUntilDone(); michael@0: let gotStderr = false; michael@0: michael@0: var p = subprocess.call({ michael@0: command: env.get('ComSpec'), michael@0: arguments: ['/C', 'nonexistent'], michael@0: michael@0: stdout: function(data) { michael@0: test.fail("shouldn't get stdout"); michael@0: }, michael@0: stderr: function(data) { michael@0: test.assert(!gotStderr,"don't get stderr twice"); michael@0: test.assertEqual( michael@0: data, michael@0: "'nonexistent' is not recognized as an internal or external command,\r\n" + michael@0: "operable program or batch file.\r\n", michael@0: "stderr contains the error message" michael@0: ); michael@0: gotStderr = true; michael@0: }, michael@0: done: function() { michael@0: test.assert(gotStderr, "got stderr before finished"); michael@0: test.done(); michael@0: }, michael@0: mergeStderr: false michael@0: }); michael@0: michael@0: } michael@0: michael@0: } michael@0: michael@0: if (env.get('USER') && env.get('SHELL')) { michael@0: michael@0: exports.testUnix = function (test) { michael@0: test.waitUntilDone(); michael@0: let envTestValue = "OK"; michael@0: let gotStdout = false; michael@0: michael@0: var p = subprocess.call({ michael@0: command: '/bin/sh', michael@0: // Print stdin and our env variable michael@0: //arguments: ['-c', 'echo $@ $ENV_TEST'], michael@0: environment: ['ENV_TEST='+envTestValue], michael@0: michael@0: stdin: function(stdin) { michael@0: stdin.write("echo $ENV_TEST"); michael@0: stdin.close(); michael@0: }, michael@0: stdout: function(data) { michael@0: test.assert(!gotStdout,"don't get stdout twice"); michael@0: test.assertEqual(data,envTestValue+"\n","stdout contains the environment variable"); michael@0: gotStdout = true; michael@0: }, michael@0: stderr: function(data) { michael@0: test.fail("shouldn't get stderr"); michael@0: }, michael@0: done: function() { michael@0: test.assert(gotStdout, "got stdout before finished"); michael@0: test.done(); michael@0: }, michael@0: mergeStderr: false michael@0: }); michael@0: } michael@0: michael@0: exports.testUnixStderr = function (test) { michael@0: test.waitUntilDone(); michael@0: let gotStderr = false; michael@0: michael@0: var p = subprocess.call({ michael@0: // Hope that we don't have to give absolute path on linux ... michael@0: command: '/bin/sh', michael@0: arguments: ['nonexistent'], michael@0: michael@0: stdout: function(data) { michael@0: test.fail("shouldn't get stdout"); michael@0: }, michael@0: stderr: function(data) { michael@0: test.assert(!gotStderr,"don't get stderr twice"); michael@0: // There is two variant of error message michael@0: if (data == "/bin/sh: 0: Can't open nonexistent\n") michael@0: test.pass("stderr containes the expected error message"); michael@0: else michael@0: test.assertEqual(data, "/bin/sh: nonexistent: No such file or directory\n", michael@0: "stderr contains the error message"); michael@0: gotStderr = true; michael@0: }, michael@0: done: function() { michael@0: test.assert(gotStderr, "got stderr before finished"); michael@0: test.done(); michael@0: }, michael@0: mergeStderr: false michael@0: }); michael@0: } michael@0: michael@0: }