From 8f8355dc3f37b4b34ae58890fb3392393c632a74 Mon Sep 17 00:00:00 2001 From: thib92 Date: Mon, 23 Sep 2019 10:21:31 +0200 Subject: [PATCH 1/7] Add tests for androidSDK and androidNDK --- .../healthchecks/__tests__/androidNDK.test.ts | 66 +++++++++++++++++++ .../healthchecks/__tests__/androidSDK.test.ts | 57 ++++++++++++++++ .../doctor/healthchecks/androidSDK.ts | 1 - 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts create mode 100644 packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts new file mode 100644 index 000000000..dd912d44a --- /dev/null +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts @@ -0,0 +1,66 @@ +import androidNDK from '../androidNDK'; +import getEnvironmentInfo from '../../../../tools/envinfo'; +import {EnvironmentInfo} from '../../types'; +import {NoopLoader} from '../../../../tools/loader'; + +import * as common from '../common'; + +const logSpy = jest.spyOn(common, 'logManualInstallation'); + +describe('androidHomeEnvVariables', () => { + let initialEnvironmentInfo: EnvironmentInfo; + let environmentInfo: EnvironmentInfo; + + beforeAll(async () => { + initialEnvironmentInfo = await getEnvironmentInfo(); + }); + + beforeEach(() => { + environmentInfo = initialEnvironmentInfo; + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('returns a message if the Android SDK is not installed', async () => { + environmentInfo.SDKs['Android SDK'] = 'Not Found'; + const diagnostics = await androidNDK.getDiagnostics(environmentInfo); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); + }); + + it('returns a message if the Android NDK is not installed', async () => { + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Android NDK': 'Not Found', + }; + const diagnostics = await androidNDK.getDiagnostics(environmentInfo); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); + }); + + it('returns a message if the NDK version is not in range', async () => { + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Android NDK': '18', + }; + const diagnostics = await androidNDK.getDiagnostics(environmentInfo); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); + }); + + it('returns false if the NDK version is in range', async () => { + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Android NDK': '19', + }; + const diagnostics = await androidNDK.getDiagnostics(environmentInfo); + expect(diagnostics.needsToBeFixed).toBe(false); + }); + + it('logs manual installation steps to the screen', async () => { + const loader = new NoopLoader(); + + androidNDK.runAutomaticFix({loader, environmentInfo}); + + expect(logSpy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts new file mode 100644 index 000000000..2a7cc7e20 --- /dev/null +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -0,0 +1,57 @@ +import androidSDK from '../androidSDK'; +import getEnvironmentInfo from '../../../../tools/envinfo'; +import {EnvironmentInfo} from '../../types'; +import {NoopLoader} from '../../../../tools/loader'; + +import * as common from '../common'; + +const logSpy = jest.spyOn(common, 'logManualInstallation'); + +describe('androidHomeEnvVariables', () => { + let initialEnvironmentInfo: EnvironmentInfo; + let environmentInfo: EnvironmentInfo; + + beforeAll(async () => { + initialEnvironmentInfo = await getEnvironmentInfo(); + }); + + beforeEach(() => { + environmentInfo = initialEnvironmentInfo; + }); + + afterEach(() => { + jest.resetAllMocks(); + }); + + it('returns a message if the Android SDK is not installed', async () => { + environmentInfo.SDKs['Android SDK'] = 'Not Found'; + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); + }); + + it('returns a message if the SDK version is not in range', async () => { + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Build Tools': [25], + }; + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); + }); + + it('returns false if the SDK version is in range', async () => { + // @ts-ignore + environmentInfo.SDKs['Android SDK'] = { + 'Build Tools': ['27.0'], + }; + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); + expect(diagnostics.needsToBeFixed).toBe(false); + }); + + it('logs manual installation steps to the screen', async () => { + const loader = new NoopLoader(); + + androidSDK.runAutomaticFix({loader, environmentInfo}); + + expect(logSpy).toHaveBeenCalledTimes(1); + }); +}); diff --git a/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts b/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts index 7424995dd..2aadf4343 100644 --- a/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts +++ b/packages/cli/src/commands/doctor/healthchecks/androidSDK.ts @@ -19,7 +19,6 @@ export default { // See the PR: https://github.com/tabrindle/envinfo/pull/119 if (sdks === 'Not Found' && process.platform !== 'darwin') { try { - // $FlowFixMe bad execa types const {stdout} = await execa( process.env.ANDROID_HOME ? `${process.env.ANDROID_HOME}/tools/bin/sdkmanager` From 9a0eb35faff735dd848c85a414fad7c9ce636285 Mon Sep 17 00:00:00 2001 From: thib92 Date: Mon, 23 Sep 2019 13:43:51 +0200 Subject: [PATCH 2/7] Add mocks to execa for Windows tests --- .../healthchecks/__tests__/androidSDK.test.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) 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 2a7cc7e20..11a733eb1 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -26,6 +26,13 @@ describe('androidHomeEnvVariables', () => { it('returns a message if the Android SDK is not installed', async () => { environmentInfo.SDKs['Android SDK'] = 'Not Found'; const diagnostics = await androidSDK.getDiagnostics(environmentInfo); + + jest.mock('execa', () => ({ + default: () => { + throw new Error('No SDK found'); + }, + })); + expect(typeof diagnostics.needsToBeFixed).toBe('string'); }); @@ -34,6 +41,13 @@ describe('androidHomeEnvVariables', () => { environmentInfo.SDKs['Android SDK'] = { 'Build Tools': [25], }; + + jest.mock('execa', () => ({ + default: () => { + return 'build-tools;25'; + }, + })); + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(typeof diagnostics.needsToBeFixed).toBe('string'); }); @@ -43,6 +57,13 @@ describe('androidHomeEnvVariables', () => { environmentInfo.SDKs['Android SDK'] = { 'Build Tools': ['27.0'], }; + + jest.mock('execa', () => ({ + default: () => { + return 'build-tools;27'; + }, + })); + const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(diagnostics.needsToBeFixed).toBe(false); }); From 8192e52b07a0105c54a8dace77ec6a74fcb5a3ce Mon Sep 17 00:00:00 2001 From: thib92 Date: Tue, 24 Sep 2019 11:22:10 +0200 Subject: [PATCH 3/7] Finish mock of execa for androidSDK test --- .../healthchecks/__tests__/androidSDK.test.ts | 34 ++++++------------- 1 file changed, 10 insertions(+), 24 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 11a733eb1..957972b82 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -1,3 +1,4 @@ +import execa from 'execa'; import androidSDK from '../androidSDK'; import getEnvironmentInfo from '../../../../tools/envinfo'; import {EnvironmentInfo} from '../../types'; @@ -7,6 +8,11 @@ import * as common from '../common'; const logSpy = jest.spyOn(common, 'logManualInstallation'); +const mockExeca = (stdout: string) => { + jest.mock('execa', () => jest.fn()); + ((execa as unknown) as jest.Mock).mockResolvedValue({stdout}); +}; + describe('androidHomeEnvVariables', () => { let initialEnvironmentInfo: EnvironmentInfo; let environmentInfo: EnvironmentInfo; @@ -25,14 +31,8 @@ describe('androidHomeEnvVariables', () => { it('returns a message if the Android SDK is not installed', async () => { environmentInfo.SDKs['Android SDK'] = 'Not Found'; + mockExeca(''); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); - - jest.mock('execa', () => ({ - default: () => { - throw new Error('No SDK found'); - }, - })); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); }); @@ -41,13 +41,7 @@ describe('androidHomeEnvVariables', () => { environmentInfo.SDKs['Android SDK'] = { 'Build Tools': [25], }; - - jest.mock('execa', () => ({ - default: () => { - return 'build-tools;25'; - }, - })); - + mockExeca('build-tools;25.0'); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(typeof diagnostics.needsToBeFixed).toBe('string'); }); @@ -55,24 +49,16 @@ describe('androidHomeEnvVariables', () => { it('returns false if the SDK version is in range', async () => { // @ts-ignore environmentInfo.SDKs['Android SDK'] = { - 'Build Tools': ['27.0'], + 'Build Tools': ['26.0'], }; - - jest.mock('execa', () => ({ - default: () => { - return 'build-tools;27'; - }, - })); - + mockExeca('build-tools;26.0'); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(diagnostics.needsToBeFixed).toBe(false); }); it('logs manual installation steps to the screen', async () => { const loader = new NoopLoader(); - androidSDK.runAutomaticFix({loader, environmentInfo}); - expect(logSpy).toHaveBeenCalledTimes(1); }); }); From 2919be543cb4dfd47ce4881a3e4f222b20eb03d0 Mon Sep 17 00:00:00 2001 From: thib92 Date: Tue, 24 Sep 2019 11:27:39 +0200 Subject: [PATCH 4/7] PR comments @lucasbento --- .../commands/doctor/healthchecks/__tests__/androidNDK.test.ts | 4 ++-- .../commands/doctor/healthchecks/__tests__/androidSDK.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts index dd912d44a..5132ca615 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts @@ -7,7 +7,7 @@ import * as common from '../common'; const logSpy = jest.spyOn(common, 'logManualInstallation'); -describe('androidHomeEnvVariables', () => { +describe('androidNDK', () => { let initialEnvironmentInfo: EnvironmentInfo; let environmentInfo: EnvironmentInfo; @@ -56,7 +56,7 @@ describe('androidHomeEnvVariables', () => { expect(diagnostics.needsToBeFixed).toBe(false); }); - it('logs manual installation steps to the screen', async () => { + it('logs manual installation steps to the screen', () => { const loader = new NoopLoader(); androidNDK.runAutomaticFix({loader, environmentInfo}); 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 957972b82..a3a3bd5dc 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -13,7 +13,7 @@ const mockExeca = (stdout: string) => { ((execa as unknown) as jest.Mock).mockResolvedValue({stdout}); }; -describe('androidHomeEnvVariables', () => { +describe('androidSDK', () => { let initialEnvironmentInfo: EnvironmentInfo; let environmentInfo: EnvironmentInfo; @@ -56,7 +56,7 @@ describe('androidHomeEnvVariables', () => { expect(diagnostics.needsToBeFixed).toBe(false); }); - it('logs manual installation steps to the screen', async () => { + it('logs manual installation steps to the screen', () => { const loader = new NoopLoader(); androidSDK.runAutomaticFix({loader, environmentInfo}); expect(logSpy).toHaveBeenCalledTimes(1); From 1a956a86157cc2fa4afbac97839b81fb04dc8afe Mon Sep 17 00:00:00 2001 From: thib92 Date: Tue, 24 Sep 2019 11:29:35 +0200 Subject: [PATCH 5/7] Change to true value instead of message --- .../doctor/healthchecks/__tests__/androidNDK.test.ts | 6 +++--- .../doctor/healthchecks/__tests__/androidSDK.test.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts index 5132ca615..74a46b021 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts @@ -26,7 +26,7 @@ describe('androidNDK', () => { it('returns a message if the Android SDK is not installed', async () => { environmentInfo.SDKs['Android SDK'] = 'Not Found'; const diagnostics = await androidNDK.getDiagnostics(environmentInfo); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); + expect(diagnostics.needsToBeFixed).toBe(true); }); it('returns a message if the Android NDK is not installed', async () => { @@ -35,7 +35,7 @@ describe('androidNDK', () => { 'Android NDK': 'Not Found', }; const diagnostics = await androidNDK.getDiagnostics(environmentInfo); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); + expect(diagnostics.needsToBeFixed).toBe(true); }); it('returns a message if the NDK version is not in range', async () => { @@ -44,7 +44,7 @@ describe('androidNDK', () => { 'Android NDK': '18', }; const diagnostics = await androidNDK.getDiagnostics(environmentInfo); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); + expect(diagnostics.needsToBeFixed).toBe(true); }); it('returns false if the NDK version is in range', async () => { 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 a3a3bd5dc..a18135aa7 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -33,7 +33,7 @@ describe('androidSDK', () => { environmentInfo.SDKs['Android SDK'] = 'Not Found'; mockExeca(''); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); + expect(diagnostics.needsToBeFixed).toBe(true); }); it('returns a message if the SDK version is not in range', async () => { @@ -43,7 +43,7 @@ describe('androidSDK', () => { }; mockExeca('build-tools;25.0'); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); - expect(typeof diagnostics.needsToBeFixed).toBe('string'); + expect(diagnostics.needsToBeFixed).toBe(true); }); it('returns false if the SDK version is in range', async () => { From 0371d6b0e90387d78df23d0f7c1efa57038f05fb Mon Sep 17 00:00:00 2001 From: thib92 Date: Tue, 24 Sep 2019 11:31:59 +0200 Subject: [PATCH 6/7] Add comments about ts-ignore --- .../commands/doctor/healthchecks/__tests__/androidNDK.test.ts | 3 +++ .../commands/doctor/healthchecks/__tests__/androidSDK.test.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts index 74a46b021..3aa9cf9bc 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidNDK.test.ts @@ -30,6 +30,7 @@ describe('androidNDK', () => { }); it('returns a message if the Android NDK is not installed', async () => { + // To avoid having to provide fake versions for all the Android SDK tools // @ts-ignore environmentInfo.SDKs['Android SDK'] = { 'Android NDK': 'Not Found', @@ -39,6 +40,7 @@ describe('androidNDK', () => { }); it('returns a message if the NDK version is not in range', async () => { + // To avoid having to provide fake versions for all the Android SDK tools // @ts-ignore environmentInfo.SDKs['Android SDK'] = { 'Android NDK': '18', @@ -48,6 +50,7 @@ describe('androidNDK', () => { }); it('returns false if the NDK version is in range', async () => { + // To avoid having to provide fake versions for all the Android SDK tools // @ts-ignore environmentInfo.SDKs['Android SDK'] = { 'Android NDK': '19', 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 a18135aa7..6bf892795 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -37,6 +37,7 @@ describe('androidSDK', () => { }); it('returns a message if the SDK version is not in range', async () => { + // To avoid having to provide fake versions for all the Android SDK tools // @ts-ignore environmentInfo.SDKs['Android SDK'] = { 'Build Tools': [25], @@ -47,6 +48,7 @@ describe('androidSDK', () => { }); it('returns false if the SDK version is in range', async () => { + // To avoid having to provide fake versions for all the Android SDK tools // @ts-ignore environmentInfo.SDKs['Android SDK'] = { 'Build Tools': ['26.0'], From 671906f125316d99e8e7a097ce445c9132cba779 Mon Sep 17 00:00:00 2001 From: thib92 Date: Mon, 14 Oct 2019 10:18:27 +0200 Subject: [PATCH 7/7] Change mock logic --- .../healthchecks/__tests__/androidSDK.test.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 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 6bf892795..f1db5249e 100644 --- a/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts +++ b/packages/cli/src/commands/doctor/healthchecks/__tests__/androidSDK.test.ts @@ -8,10 +8,7 @@ import * as common from '../common'; const logSpy = jest.spyOn(common, 'logManualInstallation'); -const mockExeca = (stdout: string) => { - jest.mock('execa', () => jest.fn()); - ((execa as unknown) as jest.Mock).mockResolvedValue({stdout}); -}; +jest.mock('execa', () => jest.fn()); describe('androidSDK', () => { let initialEnvironmentInfo: EnvironmentInfo; @@ -31,7 +28,7 @@ describe('androidSDK', () => { it('returns a message if the Android SDK is not installed', async () => { environmentInfo.SDKs['Android SDK'] = 'Not Found'; - mockExeca(''); + ((execa as unknown) as jest.Mock).mockResolvedValue({stdout: ''}); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(diagnostics.needsToBeFixed).toBe(true); }); @@ -42,7 +39,9 @@ describe('androidSDK', () => { environmentInfo.SDKs['Android SDK'] = { 'Build Tools': [25], }; - mockExeca('build-tools;25.0'); + ((execa as unknown) as jest.Mock).mockResolvedValue({ + stdout: 'build-tools;25.0', + }); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(diagnostics.needsToBeFixed).toBe(true); }); @@ -53,7 +52,9 @@ describe('androidSDK', () => { environmentInfo.SDKs['Android SDK'] = { 'Build Tools': ['26.0'], }; - mockExeca('build-tools;26.0'); + ((execa as unknown) as jest.Mock).mockResolvedValue({ + stdout: 'build-tools;26.0', + }); const diagnostics = await androidSDK.getDiagnostics(environmentInfo); expect(diagnostics.needsToBeFixed).toBe(false); });