|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 */ |
|
5 |
|
6 const { Cc, Ci } = require("chrome"); |
|
7 const subprocess = require("subprocess"); |
|
8 const env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment); |
|
9 |
|
10 // For now, only test on windows |
|
11 if (env.get('OS') && env.get('OS').match(/Windows/)) { |
|
12 |
|
13 exports.testWindows = function (test) { |
|
14 test.waitUntilDone(); |
|
15 let envTestValue = "OK"; |
|
16 let gotStdout = false; |
|
17 |
|
18 var p = subprocess.call({ |
|
19 // Retrieve windows cmd.exe path from env |
|
20 command: env.get('ComSpec'), |
|
21 // In order to execute a simple "echo" function |
|
22 arguments: ['/C', 'echo %ENV_TEST%'], // ' & type CON' should display stdin, but doesn't work |
|
23 // Printing an environnement variable set here by the parent process |
|
24 environment: ['ENV_TEST='+envTestValue], |
|
25 |
|
26 stdin: function(stdin) { |
|
27 // Win32 command line is not really made for stdin |
|
28 // So it doesn't seems to work as it's hard to retrieve stdin |
|
29 stdin.write("stdin"); |
|
30 stdin.close(); |
|
31 }, |
|
32 stdout: function(data) { |
|
33 test.assert(!gotStdout,"don't get stdout twice"); |
|
34 test.assertEqual(data,envTestValue+"\r\n","stdout contains the environment variable"); |
|
35 gotStdout = true; |
|
36 }, |
|
37 stderr: function(data) { |
|
38 test.fail("shouldn't get stderr"); |
|
39 }, |
|
40 done: function() { |
|
41 test.assert(gotStdout, "got stdout before finished"); |
|
42 test.done(); |
|
43 }, |
|
44 mergeStderr: false |
|
45 }); |
|
46 |
|
47 } |
|
48 |
|
49 exports.testWindowsStderr = function (test) { |
|
50 test.waitUntilDone(); |
|
51 let gotStderr = false; |
|
52 |
|
53 var p = subprocess.call({ |
|
54 command: env.get('ComSpec'), |
|
55 arguments: ['/C', 'nonexistent'], |
|
56 |
|
57 stdout: function(data) { |
|
58 test.fail("shouldn't get stdout"); |
|
59 }, |
|
60 stderr: function(data) { |
|
61 test.assert(!gotStderr,"don't get stderr twice"); |
|
62 test.assertEqual( |
|
63 data, |
|
64 "'nonexistent' is not recognized as an internal or external command,\r\n" + |
|
65 "operable program or batch file.\r\n", |
|
66 "stderr contains the error message" |
|
67 ); |
|
68 gotStderr = true; |
|
69 }, |
|
70 done: function() { |
|
71 test.assert(gotStderr, "got stderr before finished"); |
|
72 test.done(); |
|
73 }, |
|
74 mergeStderr: false |
|
75 }); |
|
76 |
|
77 } |
|
78 |
|
79 } |
|
80 |
|
81 if (env.get('USER') && env.get('SHELL')) { |
|
82 |
|
83 exports.testUnix = function (test) { |
|
84 test.waitUntilDone(); |
|
85 let envTestValue = "OK"; |
|
86 let gotStdout = false; |
|
87 |
|
88 var p = subprocess.call({ |
|
89 command: '/bin/sh', |
|
90 // Print stdin and our env variable |
|
91 //arguments: ['-c', 'echo $@ $ENV_TEST'], |
|
92 environment: ['ENV_TEST='+envTestValue], |
|
93 |
|
94 stdin: function(stdin) { |
|
95 stdin.write("echo $ENV_TEST"); |
|
96 stdin.close(); |
|
97 }, |
|
98 stdout: function(data) { |
|
99 test.assert(!gotStdout,"don't get stdout twice"); |
|
100 test.assertEqual(data,envTestValue+"\n","stdout contains the environment variable"); |
|
101 gotStdout = true; |
|
102 }, |
|
103 stderr: function(data) { |
|
104 test.fail("shouldn't get stderr"); |
|
105 }, |
|
106 done: function() { |
|
107 test.assert(gotStdout, "got stdout before finished"); |
|
108 test.done(); |
|
109 }, |
|
110 mergeStderr: false |
|
111 }); |
|
112 } |
|
113 |
|
114 exports.testUnixStderr = function (test) { |
|
115 test.waitUntilDone(); |
|
116 let gotStderr = false; |
|
117 |
|
118 var p = subprocess.call({ |
|
119 // Hope that we don't have to give absolute path on linux ... |
|
120 command: '/bin/sh', |
|
121 arguments: ['nonexistent'], |
|
122 |
|
123 stdout: function(data) { |
|
124 test.fail("shouldn't get stdout"); |
|
125 }, |
|
126 stderr: function(data) { |
|
127 test.assert(!gotStderr,"don't get stderr twice"); |
|
128 // There is two variant of error message |
|
129 if (data == "/bin/sh: 0: Can't open nonexistent\n") |
|
130 test.pass("stderr containes the expected error message"); |
|
131 else |
|
132 test.assertEqual(data, "/bin/sh: nonexistent: No such file or directory\n", |
|
133 "stderr contains the error message"); |
|
134 gotStderr = true; |
|
135 }, |
|
136 done: function() { |
|
137 test.assert(gotStderr, "got stderr before finished"); |
|
138 test.done(); |
|
139 }, |
|
140 mergeStderr: false |
|
141 }); |
|
142 } |
|
143 |
|
144 } |