11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst stdoutData = 'foo';
41cb0ef41Sopenharmony_ciconst stderrData = 'bar';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
71cb0ef41Sopenharmony_ci  // The following console calls are part of the test.
81cb0ef41Sopenharmony_ci  console.log(stdoutData);
91cb0ef41Sopenharmony_ci  console.error(stderrData);
101cb0ef41Sopenharmony_ci} else {
111cb0ef41Sopenharmony_ci  const assert = require('assert');
121cb0ef41Sopenharmony_ci  const cp = require('child_process');
131cb0ef41Sopenharmony_ci  const expectedStdout = `${stdoutData}\n`;
141cb0ef41Sopenharmony_ci  const expectedStderr = `${stderrData}\n`;
151cb0ef41Sopenharmony_ci  function run(options, callback) {
161cb0ef41Sopenharmony_ci    const cmd = `"${process.execPath}" "${__filename}" child`;
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci    cp.exec(cmd, options, common.mustSucceed((stdout, stderr) => {
191cb0ef41Sopenharmony_ci      callback(stdout, stderr);
201cb0ef41Sopenharmony_ci    }));
211cb0ef41Sopenharmony_ci  }
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  // Test default encoding, which should be utf8.
241cb0ef41Sopenharmony_ci  run({}, (stdout, stderr) => {
251cb0ef41Sopenharmony_ci    assert.strictEqual(typeof stdout, 'string');
261cb0ef41Sopenharmony_ci    assert.strictEqual(typeof stderr, 'string');
271cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, expectedStdout);
281cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, expectedStderr);
291cb0ef41Sopenharmony_ci  });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // Test explicit utf8 encoding.
321cb0ef41Sopenharmony_ci  run({ encoding: 'utf8' }, (stdout, stderr) => {
331cb0ef41Sopenharmony_ci    assert.strictEqual(typeof stdout, 'string');
341cb0ef41Sopenharmony_ci    assert.strictEqual(typeof stderr, 'string');
351cb0ef41Sopenharmony_ci    assert.strictEqual(stdout, expectedStdout);
361cb0ef41Sopenharmony_ci    assert.strictEqual(stderr, expectedStderr);
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  // Test cases that result in buffer encodings.
401cb0ef41Sopenharmony_ci  [undefined, null, 'buffer', 'invalid'].forEach((encoding) => {
411cb0ef41Sopenharmony_ci    run({ encoding }, (stdout, stderr) => {
421cb0ef41Sopenharmony_ci      assert(stdout instanceof Buffer);
431cb0ef41Sopenharmony_ci      assert(stdout instanceof Buffer);
441cb0ef41Sopenharmony_ci      assert.strictEqual(stdout.toString(), expectedStdout);
451cb0ef41Sopenharmony_ci      assert.strictEqual(stderr.toString(), expectedStderr);
461cb0ef41Sopenharmony_ci    });
471cb0ef41Sopenharmony_ci  });
481cb0ef41Sopenharmony_ci}
49