From 1eeb05aa605970bb7277eb53cfefa013d1fb6343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=B3n=20Molleda?= Date: Wed, 26 Feb 2020 12:59:05 -0800 Subject: [PATCH] fix: avoid crashing if build.gradle is missing --- .../healthchecks/__tests__/androidSDK.test.ts | 16 ++++++++++++++++ .../commands/doctor/healthchecks/androidSDK.ts | 12 ++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts index 380802dc8..7e867d4b2 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -79,4 +79,20 @@ describe('androidSDK', () => { androidSDK.runAutomaticFix({loader, environmentInfo}); expect(logSpy).toHaveBeenCalledTimes(1); }); + + it('returns true if a build.gradle is not found', async () => { + // To avoid having to provide fake versions for all the Android SDK tools + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Build Tools': ['28.0.3'], + }; + ((execa as unknown) as jest.Mock).mockResolvedValue({ + stdout: 'build-tools;28.0.3', + }); + + cleanup('android/build.gradle'); + + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); + expect(diagnostics.needsToBeFixed).toBe(true); + }); }); diff --git a/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts b/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts index 88f333c21..b1f0ed50c 100644 --- a/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts +++ b/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts @@ -12,6 +12,10 @@ const getBuildToolsVersion = (): string => { const buildToolsVersionEntry = 'buildToolsVersion'; + if (!fs.existsSync(gradleBuildFilePath)) { + return 'Not Found'; + } + // Read the content of the `build.gradle` file const gradleBuildFile = fs.readFileSync(gradleBuildFilePath, 'utf-8'); @@ -45,14 +49,6 @@ export default { ? SDKs['Android SDK'] : SDKs['Android SDK']['Build Tools']; - if (!requiredVersion) { - return { - versions: buildTools, - versionRange: undefined, - needsToBeFixed: true, - }; - } - const isAndroidSDKInstalled = Array.isArray(buildTools); const isRequiredVersionInstalled = isAndroidSDKInstalled