b2g/simulator/packages/subprocess/tests/test-subprocess.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/simulator/packages/subprocess/tests/test-subprocess.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +
     1.9 +const { Cc, Ci } = require("chrome");
    1.10 +const subprocess = require("subprocess");
    1.11 +const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
    1.12 +
    1.13 +// For now, only test on windows
    1.14 +if (env.get('OS') && env.get('OS').match(/Windows/)) {
    1.15 +
    1.16 +exports.testWindows = function (test) {
    1.17 +  test.waitUntilDone();
    1.18 +  let envTestValue = "OK";
    1.19 +  let gotStdout = false;
    1.20 +
    1.21 +  var p = subprocess.call({
    1.22 +    // Retrieve windows cmd.exe path from env
    1.23 +    command:     env.get('ComSpec'),
    1.24 +    // In order to execute a simple "echo" function
    1.25 +    arguments:   ['/C', 'echo %ENV_TEST%'], // ' & type CON' should display stdin, but doesn't work
    1.26 +    // Printing an environnement variable set here by the parent process
    1.27 +    environment: ['ENV_TEST='+envTestValue],
    1.28 +
    1.29 +    stdin: function(stdin) {
    1.30 +      // Win32 command line is not really made for stdin
    1.31 +      // So it doesn't seems to work as it's hard to retrieve stdin
    1.32 +      stdin.write("stdin");
    1.33 +      stdin.close();
    1.34 +    },
    1.35 +    stdout: function(data) {
    1.36 +      test.assert(!gotStdout,"don't get stdout twice");
    1.37 +      test.assertEqual(data,envTestValue+"\r\n","stdout contains the environment variable");
    1.38 +      gotStdout = true;
    1.39 +    },
    1.40 +    stderr: function(data) {
    1.41 +      test.fail("shouldn't get stderr");
    1.42 +    },
    1.43 +    done: function() {
    1.44 +      test.assert(gotStdout, "got stdout before finished");
    1.45 +      test.done();
    1.46 +    },
    1.47 +    mergeStderr: false
    1.48 +  });
    1.49 +
    1.50 +}
    1.51 +
    1.52 +exports.testWindowsStderr = function (test) {
    1.53 +  test.waitUntilDone();
    1.54 +  let gotStderr = false;
    1.55 +
    1.56 +  var p = subprocess.call({
    1.57 +    command:     env.get('ComSpec'),
    1.58 +    arguments:   ['/C', 'nonexistent'],
    1.59 +
    1.60 +    stdout: function(data) {
    1.61 +      test.fail("shouldn't get stdout");
    1.62 +    },
    1.63 +    stderr: function(data) {
    1.64 +      test.assert(!gotStderr,"don't get stderr twice");
    1.65 +      test.assertEqual(
    1.66 +        data,
    1.67 +        "'nonexistent' is not recognized as an internal or external command,\r\n" +
    1.68 +        "operable program or batch file.\r\n",
    1.69 +        "stderr contains the error message"
    1.70 +      );
    1.71 +      gotStderr = true;
    1.72 +    },
    1.73 +    done: function() {
    1.74 +      test.assert(gotStderr, "got stderr before finished");
    1.75 +      test.done();
    1.76 +    },
    1.77 +    mergeStderr: false
    1.78 +  });
    1.79 +
    1.80 +}
    1.81 +
    1.82 +}
    1.83 +
    1.84 +if (env.get('USER') && env.get('SHELL')) {
    1.85 +
    1.86 +exports.testUnix = function (test) {
    1.87 +  test.waitUntilDone();
    1.88 +  let envTestValue = "OK";
    1.89 +  let gotStdout = false;
    1.90 +
    1.91 +  var p = subprocess.call({
    1.92 +    command:     '/bin/sh',
    1.93 +    // Print stdin and our env variable
    1.94 +    //arguments:   ['-c', 'echo $@ $ENV_TEST'],
    1.95 +    environment: ['ENV_TEST='+envTestValue],
    1.96 +
    1.97 +    stdin: function(stdin) {
    1.98 +      stdin.write("echo $ENV_TEST");
    1.99 +      stdin.close();
   1.100 +    },
   1.101 +    stdout: function(data) {
   1.102 +      test.assert(!gotStdout,"don't get stdout twice");
   1.103 +      test.assertEqual(data,envTestValue+"\n","stdout contains the environment variable");
   1.104 +      gotStdout = true;
   1.105 +    },
   1.106 +    stderr: function(data) {
   1.107 +      test.fail("shouldn't get stderr");
   1.108 +    },
   1.109 +    done: function() {
   1.110 +      test.assert(gotStdout, "got stdout before finished");
   1.111 +      test.done();
   1.112 +    },
   1.113 +    mergeStderr: false
   1.114 +  });
   1.115 +}
   1.116 +
   1.117 +exports.testUnixStderr = function (test) {
   1.118 +  test.waitUntilDone();
   1.119 +  let gotStderr = false;
   1.120 +
   1.121 +  var p = subprocess.call({
   1.122 +    // Hope that we don't have to give absolute path on linux ...
   1.123 +    command:     '/bin/sh',
   1.124 +    arguments:   ['nonexistent'],
   1.125 +
   1.126 +    stdout: function(data) {
   1.127 +      test.fail("shouldn't get stdout");
   1.128 +    },
   1.129 +    stderr: function(data) {
   1.130 +      test.assert(!gotStderr,"don't get stderr twice");
   1.131 +      // There is two variant of error message
   1.132 +      if (data == "/bin/sh: 0: Can't open nonexistent\n")
   1.133 +        test.pass("stderr containes the expected error message");
   1.134 +      else
   1.135 +        test.assertEqual(data, "/bin/sh: nonexistent: No such file or directory\n",
   1.136 +                         "stderr contains the error message");
   1.137 +      gotStderr = true;
   1.138 +    },
   1.139 +    done: function() {
   1.140 +      test.assert(gotStderr, "got stderr before finished");
   1.141 +      test.done();
   1.142 +    },
   1.143 +    mergeStderr: false
   1.144 +  });
   1.145 +}
   1.146 +
   1.147 +}

mercurial