1import spawnAsync from '@expo/spawn-async';
2import chalk from 'chalk';
3import path from 'path';
4
5import * as Directories from '../Directories';
6import * as Packages from '../Packages';
7import { filterAsync } from '../Utils';
8
9const ANDROID_DIR = Directories.getAndroidDir();
10
11const BARE_EXPO_DIR = path.join(Directories.getAppsDir(), 'bare-expo', 'android');
12
13const excludedInTests = [
14  'expo-module-template',
15  'expo-notifications',
16  'expo-in-app-purchases',
17  'expo-splash-screen',
18  'expo-modules-test-core',
19  'expo-dev-client',
20];
21
22const packagesNeedToBeTestedUsingBareExpo = [
23  'expo-dev-menu',
24  'expo-dev-launcher',
25  'expo-dev-menu-interface',
26];
27
28type TestType = 'local' | 'instrumented';
29
30function consoleErrorOutput(output: string, label: string, colorifyLine: (string) => string): void {
31  const lines = output.trim().split(/\r\n?|\n/g);
32  console.error(lines.map((line) => `${chalk.gray(label)} ${colorifyLine(line)}`).join('\n'));
33}
34
35export async function androidNativeUnitTests({
36  type,
37  packages,
38}: {
39  type: TestType;
40  packages?: string;
41}) {
42  if (!type) {
43    throw new Error(
44      'Must specify which type of unit test to run with `--type local` or `--type instrumented`.'
45    );
46  }
47  if (type !== 'local' && type !== 'instrumented') {
48    throw new Error('Invalid type specified. Must use `--type local` or `--type instrumented`.');
49  }
50
51  const allPackages = await Packages.getListOfPackagesAsync();
52  const packageNamesFilter = packages ? packages.split(',') : [];
53
54  const androidPackages = await filterAsync(allPackages, async (pkg) => {
55    if (packageNamesFilter.length > 0 && !packageNamesFilter.includes(pkg.packageName)) {
56      return false;
57    }
58
59    let includesTests;
60    if (pkg.isSupportedOnPlatform('android') && !excludedInTests.includes(pkg.packageSlug)) {
61      if (type === 'instrumented') {
62        includesTests = await pkg.hasNativeInstrumentationTestsAsync('android');
63      } else {
64        includesTests = await pkg.hasNativeTestsAsync('android');
65      }
66    }
67
68    if (!includesTests && packageNamesFilter.includes(pkg.packageName)) {
69      throw new Error(
70        `The package ${pkg.packageName} does not include Android ${type} unit tests.`
71      );
72    }
73
74    return includesTests;
75  });
76
77  console.log(chalk.green('Packages to test: '));
78  androidPackages.forEach((pkg) => {
79    console.log(chalk.yellow(pkg.packageSlug));
80  });
81
82  const testCommand = type === 'instrumented' ? 'connectedAndroidTest' : 'testDebugUnitTest';
83
84  const partition = <T>(arr: T[], condition: (T) => boolean) => {
85    const trues = arr.filter((el) => condition(el));
86    const falses = arr.filter((el) => !condition(el));
87    return [trues, falses];
88  };
89
90  const [androidPackagesTestedUsingBareProject, androidPackagesTestedUsingExpoProject] = partition(
91    androidPackages,
92    (element) => packagesNeedToBeTestedUsingBareExpo.includes(element.packageName)
93  );
94
95  if (type === 'instrumented') {
96    // Uninstall tests first to fix the `INSTALL_FAILED_UPDATE_INCOMPATIBLE` error from cached AVD in CI environment.
97    const uninstallTestCommand = 'uninstallDebugAndroidTest';
98    await runGradlew(androidPackagesTestedUsingExpoProject, uninstallTestCommand, ANDROID_DIR);
99    await runGradlew(androidPackagesTestedUsingBareProject, uninstallTestCommand, BARE_EXPO_DIR);
100  }
101
102  await runGradlew(androidPackagesTestedUsingExpoProject, testCommand, ANDROID_DIR);
103  await runGradlew(androidPackagesTestedUsingBareProject, testCommand, BARE_EXPO_DIR);
104  console.log(chalk.green('Finished android unit tests successfully.'));
105}
106
107async function runGradlew(packages: Packages.Package[], testCommand: string, cwd: string) {
108  if (!packages.length) {
109    return;
110  }
111
112  try {
113    await spawnAsync(
114      './gradlew',
115      packages.map((pkg) => `:${pkg.packageSlug}:${testCommand}`),
116      {
117        cwd,
118        stdio: 'inherit',
119        env: { ...process.env },
120      }
121    );
122  } catch (error) {
123    console.error('Failed while executing android unit tests');
124    consoleErrorOutput(error.stdout, 'stdout >', chalk.reset);
125    consoleErrorOutput(error.stderr, 'stderr >', chalk.red);
126    throw error;
127  }
128}
129
130export default (program: any) => {
131  program
132    .command('android-native-unit-tests')
133    .option('-t, --type <string>', 'Type of unit test to run: local or instrumented')
134    .option(
135      '--packages <string>',
136      '[optional] Comma-separated list of package names to run unit tests for. Defaults to all packages with unit tests.'
137    )
138    .description('Runs Android native unit tests for each package that provides them.')
139    .asyncAction(androidNativeUnitTests);
140};
141