1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10'use strict';
11
12/**
13 * This script runs instrumentation tests one by one with retries
14 * Instrumentation tests tend to be flaky, so rerunning them individually increases
15 * chances for success and reduces total average execution time.
16 *
17 * We assume that all instrumentation tests are flat in one folder
18 * Available arguments:
19 * --path - path to all .java files with tests
20 * --package - com.facebook.react.tests
21 * --retries [num] - how many times to retry possible flaky commands: npm install and running tests, default 1
22 */
23
24const {echo, exec, exit, ls} = require('shelljs');
25
26const argv = require('yargs').argv;
27const numberOfRetries = argv.retries || 1;
28const tryExecNTimes = require('./try-n-times');
29const path = require('path');
30
31// Flaky tests ignored on Circle CI. They still run internally at fb.
32const ignoredTests = [
33  'ReactScrollViewTestCase',
34  'ReactHorizontalScrollViewTestCase',
35];
36
37// ReactAndroid/src/androidTest/java/com/facebook/react/tests/ReactHorizontalScrollViewTestCase.java
38const testClasses = ls(`${argv.path}/*.java`)
39  .map(javaFile => {
40    // ReactHorizontalScrollViewTestCase
41    return path.basename(javaFile, '.java');
42  })
43  .filter(className => {
44    return ignoredTests.indexOf(className) === -1;
45  })
46  .map(className => {
47    // com.facebook.react.tests.ReactHorizontalScrollViewTestCase
48    return argv.package + '.' + className;
49  });
50
51let exitCode = 0;
52testClasses.forEach(testClass => {
53  if (
54    tryExecNTimes(() => {
55      echo(`Starting ${testClass}`);
56      // any faster means Circle CI crashes
57      exec('sleep 10s');
58      return exec(
59        `./scripts/run-instrumentation-tests-via-adb-shell.sh ${argv.package} ${testClass}`,
60      ).code;
61    }, numberOfRetries)
62  ) {
63    echo(`${testClass} failed ${numberOfRetries} times`);
64    exitCode = 1;
65  }
66});
67
68exit(exitCode);
69