|
| 1 | +import type { PathLike } from 'node:fs'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 4 | +import { getInstallOutputFileName } from '../findOutputFile.js'; |
| 5 | + |
| 6 | +function mockExistingFiles(buildDirectory: string, buildFileName: string) { |
| 7 | + const existingFilesPaths = [ |
| 8 | + `${buildDirectory}/${buildFileName}`, |
| 9 | + buildDirectory, |
| 10 | + ]; |
| 11 | + |
| 12 | + vi.mocked(fs.existsSync).mockImplementation((file: PathLike) => |
| 13 | + existingFilesPaths.includes(file.toString()), |
| 14 | + ); |
| 15 | +} |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + vi.resetAllMocks(); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('getInstallOutputFileName', () => { |
| 22 | + const variant = 'debug'; |
| 23 | + const apkOrAab = 'apk'; |
| 24 | + |
| 25 | + test('returns CPU-specific file', async () => { |
| 26 | + const appName = 'app'; |
| 27 | + const buildDirectory = 'android/app/build/outputs/apk/debug'; |
| 28 | + const buildFileNameOutput = 'app-universal-debug.apk'; |
| 29 | + mockExistingFiles(buildDirectory, buildFileNameOutput); |
| 30 | + |
| 31 | + const result = await getInstallOutputFileName( |
| 32 | + appName, |
| 33 | + variant, |
| 34 | + buildDirectory, |
| 35 | + apkOrAab, |
| 36 | + undefined, |
| 37 | + ); |
| 38 | + |
| 39 | + expect(result).toBe(buildFileNameOutput); |
| 40 | + }); |
| 41 | + |
| 42 | + test('returns build file if appName is provided', async () => { |
| 43 | + const appName = 'app'; |
| 44 | + const buildDirectory = 'android/app/build/outputs/apk/debug'; |
| 45 | + const buildFileNameOutput = 'app-debug.apk'; |
| 46 | + mockExistingFiles(buildDirectory, buildFileNameOutput); |
| 47 | + |
| 48 | + const result = await getInstallOutputFileName( |
| 49 | + appName, |
| 50 | + variant, |
| 51 | + buildDirectory, |
| 52 | + apkOrAab, |
| 53 | + undefined, |
| 54 | + ); |
| 55 | + |
| 56 | + expect(result).toBe(buildFileNameOutput); |
| 57 | + }); |
| 58 | + |
| 59 | + test('returns build file if appName is missing', async () => { |
| 60 | + const appName = ''; |
| 61 | + const buildDirectory = 'Android/build/outputs/apk/debug'; |
| 62 | + const buildFileNameOutput = 'HybridApp-debug.apk'; |
| 63 | + mockExistingFiles(buildDirectory, buildFileNameOutput); |
| 64 | + vi.mocked(fs.readdirSync).mockReturnValueOnce([buildFileNameOutput as any]); |
| 65 | + |
| 66 | + const result = await getInstallOutputFileName( |
| 67 | + appName, |
| 68 | + variant, |
| 69 | + buildDirectory, |
| 70 | + apkOrAab, |
| 71 | + undefined, |
| 72 | + ); |
| 73 | + |
| 74 | + expect(result).toBe(buildFileNameOutput); |
| 75 | + }); |
| 76 | +}); |
0 commit comments