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 await runGradlew(androidPackagesTestedUsingExpoProject, testCommand, ANDROID_DIR); 96 await runGradlew(androidPackagesTestedUsingBareProject, testCommand, BARE_EXPO_DIR); 97 console.log(chalk.green('Finished android unit tests successfully.')); 98} 99 100async function runGradlew(packages: Packages.Package[], testCommand: string, cwd: string) { 101 if (!packages.length) { 102 return; 103 } 104 105 try { 106 await spawnAsync( 107 './gradlew', 108 packages.map((pkg) => `:${pkg.packageSlug}:${testCommand}`), 109 { 110 cwd, 111 stdio: 'inherit', 112 env: { ...process.env }, 113 } 114 ); 115 } catch (error) { 116 console.error('Failed while executing android unit tests'); 117 consoleErrorOutput(error.stdout, 'stdout >', chalk.reset); 118 consoleErrorOutput(error.stderr, 'stderr >', chalk.red); 119 throw error; 120 } 121} 122 123export default (program: any) => { 124 program 125 .command('android-native-unit-tests') 126 .option('-t, --type <string>', 'Type of unit test to run: local or instrumented') 127 .option( 128 '--packages <string>', 129 '[optional] Comma-separated list of package names to run unit tests for. Defaults to all packages with unit tests.' 130 ) 131 .description('Runs Android native unit tests for each package that provides them.') 132 .asyncAction(androidNativeUnitTests); 133}; 134