From fd36a5c0ad294733a9f257ffe7b57e5b9d24d417 Mon Sep 17 00:00:00 2001 From: Vilius Sutkus '89 Date: Tue, 1 Feb 2022 17:46:29 +0200 Subject: [PATCH 1/5] Allow increasing disk size - Issue #184 --- README.md | 1 + action.yml | 2 ++ src/emulator-manager.ts | 5 +++++ src/input-validator.ts | 18 ++++++++++++++++++ src/main.ts | 8 +++++++- 5 files changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8de39b5b2..429b6c713 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ jobs: | `cores` | Optional | 2 | Number of cores to use for the emulator (`hw.cpu.ncore` in config.ini). | | `ram-size` | Optional | N/A | Size of RAM to use for this AVD, in KB or MB, denoted with K or M. - e.g. `2048M` | | `sdcard-path-or-size` | Optional | N/A | Path to the SD card image for this AVD or the size of a new SD card image to create for this AVD, in KB or MB, denoted with K or M. - e.g. `path/to/sdcard`, or `1000M`. | +| `disk-size` | Optional | N/A | Disk size to use for this AVD. Either in bytes or KB, MB or GB, when denoted with K, M or G. - e.g. `2048M` | | `avd-name` | Optional | `test` | Custom AVD name used for creating the Android Virtual Device. | | `force-avd-creation` | Optional | `true` | Whether to force create the AVD by overwriting an existing AVD with the same name as `avd-name` - `true` or `false`. | | `emulator-options` | Optional | See below | Command-line options used when launching the emulator (replacing all default options) - e.g. `-no-window -no-snapshot -camera-back emulated`. | diff --git a/action.yml b/action.yml index 71467db56..e8b418f65 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,8 @@ inputs: description: 'size of RAM to use for this AVD, in KB or MB, denoted with K or M. - e.g. `2048M`' sdcard-path-or-size: description: 'path to the SD card image for this AVD or the size of a new SD card image to create for this AVD, in KB or MB, denoted with K or M. - e.g. `path/to/sdcard`, or `1000M`' + disk-size: + description: 'disk size to use for this AVD. Either in bytes or KB, MB or GB, when denoted with K, M or G' avd-name: description: 'custom AVD name used for creating the Android Virtual Device' default: 'test' diff --git a/src/emulator-manager.ts b/src/emulator-manager.ts index fe56ee9ba..9a2938e8e 100644 --- a/src/emulator-manager.ts +++ b/src/emulator-manager.ts @@ -14,6 +14,7 @@ export async function launchEmulator( cores: string, ramSize: string, sdcardPathOrSize: string, + diskSize: string, avdName: string, forceAvdCreation: boolean, emulatorOptions: string, @@ -45,6 +46,10 @@ export async function launchEmulator( await exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); } + if (diskSize) { + await exec.exec(`sh -c \\"printf 'disk.dataPartition.size=${diskSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); + } + //turn off hardware acceleration on Linux if (process.platform === 'linux' && disableLinuxHardwareAcceleration) { console.log('Disabling Linux hardware acceleration.'); diff --git a/src/input-validator.ts b/src/input-validator.ts index ac74762d3..2d80c25b3 100644 --- a/src/input-validator.ts +++ b/src/input-validator.ts @@ -69,3 +69,21 @@ export function checkEmulatorBuild(emulatorBuild: string): void { function isValidBoolean(value: string): boolean { return value === 'true' || value === 'false'; } + +export function checkDiskSize(diskSize: string): void { + // Disk size can be empty - the default value + if (diskSize) { + // Can also be number of bytes + const diskSizeNumber: Number = Number(diskSize); + if (isNaN(diskSizeNumber) || !Number.isInteger(diskSizeNumber)) { + // Disk size can have a size multiplier at the end K, M or G + const diskSizeUpperCase = diskSize.toUpperCase(); + if (diskSizeUpperCase.endsWith("K") || diskSizeUpperCase.endsWith("M") || diskSizeUpperCase.endsWith("G")) { + const diskSizeNumberNoModifier: Number = Number(diskSize.slice(0, -1)); + if (isNaN(diskSizeNumberNoModifier) || !Number.isInteger(diskSizeNumberNoModifier)) { + throw new Error(`Unexpected disk size: '${diskSize}'.`); + } + } + } + } +} diff --git a/src/main.ts b/src/main.ts index e167b93c8..0c176bbaa 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,8 @@ import { checkDisableLinuxHardwareAcceleration, checkForceAvdCreation, checkChannel, - checkEnableHardwareKeyboard + checkEnableHardwareKeyboard, + checkDiskSize } from './input-validator'; import { launchEmulator, killEmulator } from './emulator-manager'; import * as exec from '@actions/exec'; @@ -63,6 +64,10 @@ async function run() { const sdcardPathOrSize = core.getInput('sdcard-path-or-size'); console.log(`SD card path or size: ${sdcardPathOrSize}`); + const diskSize = core.getInput('disk-size'); + checkDiskSize(diskSize); + console.log(`Disk size: ${diskSize}`) + // custom name used for creating the AVD const avdName = core.getInput('avd-name'); console.log(`AVD name: ${avdName}`); @@ -156,6 +161,7 @@ async function run() { cores, ramSize, sdcardPathOrSize, + diskSize, avdName, forceAvdCreation, emulatorOptions, From a2b18541f72218a00bc6a4716b35fa58556d6b76 Mon Sep 17 00:00:00 2001 From: Vilius Sutkus '89 Date: Tue, 1 Feb 2022 17:51:54 +0200 Subject: [PATCH 2/5] Fix checkDiskSize validator --- src/input-validator.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/input-validator.ts b/src/input-validator.ts index 2d80c25b3..b69792240 100644 --- a/src/input-validator.ts +++ b/src/input-validator.ts @@ -74,13 +74,12 @@ export function checkDiskSize(diskSize: string): void { // Disk size can be empty - the default value if (diskSize) { // Can also be number of bytes - const diskSizeNumber: Number = Number(diskSize); - if (isNaN(diskSizeNumber) || !Number.isInteger(diskSizeNumber)) { + if (isNaN(Number(diskSize)) || !Number.isInteger(Number(diskSize))) { // Disk size can have a size multiplier at the end K, M or G const diskSizeUpperCase = diskSize.toUpperCase(); if (diskSizeUpperCase.endsWith("K") || diskSizeUpperCase.endsWith("M") || diskSizeUpperCase.endsWith("G")) { - const diskSizeNumberNoModifier: Number = Number(diskSize.slice(0, -1)); - if (isNaN(diskSizeNumberNoModifier) || !Number.isInteger(diskSizeNumberNoModifier)) { + const diskSizeNoModifier: string = diskSize.slice(0, -1); + if (isNaN(Number(diskSizeNoModifier)) || !Number.isInteger(Number(diskSizeNoModifier))) { throw new Error(`Unexpected disk size: '${diskSize}'.`); } } From ec9d7f2af506e825cb3a32101d65d72fe405fecd Mon Sep 17 00:00:00 2001 From: Vilius Sutkus '89 Date: Tue, 1 Feb 2022 18:28:19 +0200 Subject: [PATCH 3/5] Fix lint errors --- src/input-validator.ts | 2 +- src/main.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/input-validator.ts b/src/input-validator.ts index b69792240..4fcbf148b 100644 --- a/src/input-validator.ts +++ b/src/input-validator.ts @@ -77,7 +77,7 @@ export function checkDiskSize(diskSize: string): void { if (isNaN(Number(diskSize)) || !Number.isInteger(Number(diskSize))) { // Disk size can have a size multiplier at the end K, M or G const diskSizeUpperCase = diskSize.toUpperCase(); - if (diskSizeUpperCase.endsWith("K") || diskSizeUpperCase.endsWith("M") || diskSizeUpperCase.endsWith("G")) { + if (diskSizeUpperCase.endsWith('K') || diskSizeUpperCase.endsWith('M') || diskSizeUpperCase.endsWith('G')) { const diskSizeNoModifier: string = diskSize.slice(0, -1); if (isNaN(Number(diskSizeNoModifier)) || !Number.isInteger(Number(diskSizeNoModifier))) { throw new Error(`Unexpected disk size: '${diskSize}'.`); diff --git a/src/main.ts b/src/main.ts index 0c176bbaa..596917df1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -66,7 +66,7 @@ async function run() { const diskSize = core.getInput('disk-size'); checkDiskSize(diskSize); - console.log(`Disk size: ${diskSize}`) + console.log(`Disk size: ${diskSize}`); // custom name used for creating the AVD const avdName = core.getInput('avd-name'); From 38c791dc47141de8237b0409ead6e0c2758b9d00 Mon Sep 17 00:00:00 2001 From: Vilius Sutkus '89 Date: Sat, 5 Feb 2022 04:08:48 +0200 Subject: [PATCH 4/5] Size modifier without a number is unacceptable "G" is not the same as "1G" --- src/input-validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input-validator.ts b/src/input-validator.ts index 4fcbf148b..cdafa7f19 100644 --- a/src/input-validator.ts +++ b/src/input-validator.ts @@ -79,7 +79,7 @@ export function checkDiskSize(diskSize: string): void { const diskSizeUpperCase = diskSize.toUpperCase(); if (diskSizeUpperCase.endsWith('K') || diskSizeUpperCase.endsWith('M') || diskSizeUpperCase.endsWith('G')) { const diskSizeNoModifier: string = diskSize.slice(0, -1); - if (isNaN(Number(diskSizeNoModifier)) || !Number.isInteger(Number(diskSizeNoModifier))) { + if (0 == diskSizeNoModifier.length || isNaN(Number(diskSizeNoModifier)) || !Number.isInteger(Number(diskSizeNoModifier))) { throw new Error(`Unexpected disk size: '${diskSize}'.`); } } From 40cab70e2040664bc01745ed3c738a92885bbf0d Mon Sep 17 00:00:00 2001 From: Vilius Sutkus '89 Date: Sat, 5 Feb 2022 04:09:16 +0200 Subject: [PATCH 5/5] Implement checkDiskSize validator tests --- __tests__/input-validator.test.ts | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/__tests__/input-validator.test.ts b/__tests__/input-validator.test.ts index 8aeb6875d..5c52a2c53 100644 --- a/__tests__/input-validator.test.ts +++ b/__tests__/input-validator.test.ts @@ -270,3 +270,54 @@ describe('emulator-build validator tests', () => { expect(func).not.toThrow(); }); }); + +describe('checkDiskSize validator tests', () => { + it('Empty size is acceptable, means default', () => { + const func = () => { + validator.checkDiskSize(''); + }; + expect(func).not.toThrow(); + }); + + it('Numbers means bytes', () => { + expect(() => { + validator.checkDiskSize('8000000000'); + }).not.toThrow(); + }); + + it('Uppercase size modifier', () => { + expect(() => { + validator.checkDiskSize('8000000K'); + }).not.toThrow(); + expect(() => { + validator.checkDiskSize('8000M'); + }).not.toThrow(); + expect(() => { + validator.checkDiskSize('8G'); + }).not.toThrow(); + }); + + it('Lowercase size modifier', () => { + expect(() => { + validator.checkDiskSize('8000000k'); + }).not.toThrow(); + expect(() => { + validator.checkDiskSize('8000m'); + }).not.toThrow(); + expect(() => { + validator.checkDiskSize('8g'); + }).not.toThrow(); + }); + + it('Modifier without a number is unacceptable', () => { + expect(() => { + validator.checkDiskSize('G'); + }).toThrowError(`Unexpected disk size: 'G'.`); + }); + + it('Double modifier is unacceptable', () => { + expect(() => { + validator.checkDiskSize('14gg'); + }).toThrowError(`Unexpected disk size: '14gg'.`); + }); +});