summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/tools/assets/verify_issue.ts
blob: 5814eff66c39829cd1fe7e3fab6fb9b03c638bc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {spawnSync} from 'child_process';
import {readFile, writeFile} from 'fs/promises';

(async () => {
  const error = await readFile('puppeteer-error.txt', 'utf-8');
  const behavior = JSON.parse(
    await readFile('puppeteer-behavior.json', 'utf-8')
  ) as {flaky?: boolean; noError?: boolean};

  let maxRepetitions = 1;
  if (behavior.flaky) {
    maxRepetitions = 100;
  }

  let status: number | null = null;
  let stderr = '';
  let stdout = '';

  const preHook = async () => {
    console.log('Writing output and error logs...');
    await Promise.all([
      writeFile('output.log', stdout),
      writeFile('error.log', stderr),
    ]);
  };

  let checkStatusWithError: () => Promise<void>;
  if (behavior.noError) {
    checkStatusWithError = async () => {
      if (status === 0) {
        await preHook();
        console.log('Script ran successfully; no error found.');
        process.exit(0);
      }
    };
  } else {
    checkStatusWithError = async () => {
      if (status !== 0) {
        await preHook();
        if (stderr.toLowerCase().includes(error.toLowerCase())) {
          console.log('Script failed; error found.');
          process.exit(0);
        }
        console.error('Script failed; unknown error found.');
        process.exit(1);
      }
    };
  }

  for (let i = 0; i < maxRepetitions; ++i) {
    const result = spawnSync('npm', ['start'], {
      shell: true,
      encoding: 'utf-8',
    });
    status = result.status;
    stdout = result.stdout ?? '';
    stderr = result.stderr ?? '';
    await checkStatusWithError();
  }

  await preHook();
  if (behavior.noError) {
    console.error('Script failed; unknown error found.');
  } else {
    console.error('Script ran successfully; no error found.');
  }
  process.exit(1);
})();