11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { spawnSync, spawn } = require('child_process');
61cb0ef41Sopenharmony_ciconst { once } = require('events');
71cb0ef41Sopenharmony_ciconst { finished } = require('stream/promises');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciasync function runAndKill(file) {
101cb0ef41Sopenharmony_ci  if (common.isWindows) {
111cb0ef41Sopenharmony_ci    common.printSkipMessage(`signals are not supported in windows, skipping ${file}`);
121cb0ef41Sopenharmony_ci    return;
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci  let stdout = '';
151cb0ef41Sopenharmony_ci  const child = spawn(process.execPath, ['--test', file]);
161cb0ef41Sopenharmony_ci  child.stdout.setEncoding('utf8');
171cb0ef41Sopenharmony_ci  child.stdout.on('data', (chunk) => {
181cb0ef41Sopenharmony_ci    if (!stdout.length) child.kill('SIGINT');
191cb0ef41Sopenharmony_ci    stdout += chunk;
201cb0ef41Sopenharmony_ci  });
211cb0ef41Sopenharmony_ci  const [code, signal] = await once(child, 'exit');
221cb0ef41Sopenharmony_ci  await finished(child.stdout);
231cb0ef41Sopenharmony_ci  assert(stdout.startsWith('TAP version 13\n'));
241cb0ef41Sopenharmony_ci  assert.strictEqual(signal, null);
251cb0ef41Sopenharmony_ci  assert.strictEqual(code, 1);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
291cb0ef41Sopenharmony_ci  const test = require('node:test');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  if (process.argv[3] === 'pass') {
321cb0ef41Sopenharmony_ci    test('passing test', () => {
331cb0ef41Sopenharmony_ci      assert.strictEqual(true, true);
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci  } else if (process.argv[3] === 'fail') {
361cb0ef41Sopenharmony_ci    assert.strictEqual(process.argv[3], 'fail');
371cb0ef41Sopenharmony_ci    test('failing test', () => {
381cb0ef41Sopenharmony_ci      assert.strictEqual(true, false);
391cb0ef41Sopenharmony_ci    });
401cb0ef41Sopenharmony_ci  } else assert.fail('unreachable');
411cb0ef41Sopenharmony_ci} else {
421cb0ef41Sopenharmony_ci  let child = spawnSync(process.execPath, [__filename, 'child', 'pass']);
431cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
441cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [
471cb0ef41Sopenharmony_ci    '--test',
481cb0ef41Sopenharmony_ci    fixtures.path('test-runner', 'default-behavior', 'subdir', 'subdir_test.js'),
491cb0ef41Sopenharmony_ci  ]);
501cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
511cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [
551cb0ef41Sopenharmony_ci    '--test',
561cb0ef41Sopenharmony_ci    fixtures.path('test-runner', 'todo_exit_code.js'),
571cb0ef41Sopenharmony_ci  ]);
581cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 0);
591cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
601cb0ef41Sopenharmony_ci  const stdout = child.stdout.toString();
611cb0ef41Sopenharmony_ci  assert.match(stdout, /# tests 3/);
621cb0ef41Sopenharmony_ci  assert.match(stdout, /# pass 0/);
631cb0ef41Sopenharmony_ci  assert.match(stdout, /# fail 0/);
641cb0ef41Sopenharmony_ci  assert.match(stdout, /# todo 3/);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  child = spawnSync(process.execPath, [__filename, 'child', 'fail']);
671cb0ef41Sopenharmony_ci  assert.strictEqual(child.status, 1);
681cb0ef41Sopenharmony_ci  assert.strictEqual(child.signal, null);
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  runAndKill(fixtures.path('test-runner', 'never_ending_sync.js')).then(common.mustCall());
711cb0ef41Sopenharmony_ci  runAndKill(fixtures.path('test-runner', 'never_ending_async.js')).then(common.mustCall());
721cb0ef41Sopenharmony_ci}
73