Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support -9e as an argument for xz compression #101

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 95 additions & 23 deletions __test__/pi-gen-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,105 @@ describe('PiGenConfig', () => {
})

it.each([
['imgName', ''],
['release', 'foo'],
['deployCompression', 'bzip2'],
['compressionLevel', '15'],
['localeDefault', 'en_DE.UTF-42'],
['targetHostname', undefined],
['keyboardKeymap', ''],
['keyboardLayout', undefined],
['timezoneDefault', 'Europe/Munich'],
['firstUserName', ''],
['wpaEssid', '0'.repeat(33)],
['wpaPassword', '12345'],
['wpaPassword', '0'.repeat(64)],
['setfcap', '0'],
['stageList', []],
['stageList', ['foo']],
['stageList', [tmp.fileSync().name]],
['enableNoobs', 'yes'],
['exportLastStageOnly', 'no']
['imgName', '', 'image-name must not be empty'],
[
'release',
'foo',
'release must be one of ["bookworm", "bullseye", "jessie", "stretch", "buster", "testing"]'
],
[
'deployCompression',
'bzip2',
'compression must be one of ["none", "zip", "gz", "xz"]'
],
[
'compressionLevel',
'15',
'compression-level must be between 0 and 9 (or 9e for xz)'
],
[
'localeDefault',
'en_DE.UTF-42',
'locale is not included in the list of supported locales (retrieved from /usr/share/i18n/SUPPORTED)'
],
['targetHostname', undefined, 'hostname must not be empty'],
['keyboardKeymap', '', 'keyboard-keymap must not be empty'],
['keyboardLayout', undefined, 'keyboard-layout must not be empty'],
[
'timezoneDefault',
'Europe/Munich',
'timezone is not included in output of "timedatectl list-timezones"'
],
['firstUserName', '', 'username must not be empty'],
[
'wpaEssid',
'0'.repeat(33),
'wpa-essid must not be longer than 32 characters'
],
[
'wpaPassword',
'12345',
'wpa-password must be between 8 and 63 characters (or unset)'
],
[
'wpaPassword',
'0'.repeat(64),
'wpa-password must be between 8 and 63 characters (or unset)'
],
['setfcap', '0', 'setfcap should only be set to "1", nothing else'],
['stageList', [], 'stage-list must not be empty'],
[
'stageList',
['foo'],
'stage-list must contain valid pi-gen stage names "stage[0-5]" and/or valid directories'
],
[
'stageList',
[tmp.fileSync().name],
'stage-list must contain valid pi-gen stage names "stage[0-5]" and/or valid directories'
],
[
'enableNoobs',
'yes',
'enable-noobs must be either set to "true" or "false", was: yes'
],
[
'exportLastStageOnly',
'no',
'export-last-stage-only must be either set to "true" or "false", was: no'
]
])(
'rejects %s with invalid value %s',
async (property: string, value: any) => {
const piGenConfig = {...DEFAULT_CONFIG}
async (property: string, value: any, error: string) => {
const piGenConfig = {
...DEFAULT_CONFIG,
stageList: ['stage0', 'stage1', 'stage2']
}
expect(await validateConfig(piGenConfig)).toBeUndefined()

piGenConfig[property as keyof PiGenConfig] = value
expect(async () => await validateConfig(piGenConfig)).rejects.toThrow(
error
)
}
)

it('should work with compressionLevel properly', async () => {
const piGenConfig = {
...DEFAULT_CONFIG,
stageList: ['stage0', 'stage1', 'stage2'],
deployCompression: 'xz',
compressionLevel: '9e'
}

expect(await validateConfig(piGenConfig)).toBeUndefined()
for (const deployCompression of ['none', 'zip', 'gz']) {
piGenConfig.deployCompression = deployCompression
await expect(
async () => await validateConfig(piGenConfig)
).rejects.toThrow()
).rejects.toThrow(
'compression-level must be between 0 and 9 (or 9e for xz)'
)
}
)
})
})
16 changes: 9 additions & 7 deletions src/pi-gen-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ export async function validateConfig(config: PiGenConfig): Promise<void> {
)
}

if (
!['none', 'zip', 'gz', 'xz'].includes(
config.deployCompression?.toLowerCase()
)
) {
const deployCompression = config.deployCompression?.toLowerCase()
if (!['none', 'zip', 'gz', 'xz'].includes(deployCompression)) {
throw new Error('compression must be one of ["none", "zip", "gz", "xz"]')
}

if (!/^[0-9]$/.test(config.compressionLevel)) {
throw new Error('compression-level must be between 0 and 9')
if (
!(
/^[0-9]$/.test(config.compressionLevel) ||
(deployCompression === 'xz' && config.compressionLevel === '9e')
)
) {
throw new Error('compression-level must be between 0 and 9 (or 9e for xz)')
}

const cutCmd = await io.which('cut', true)
Expand Down
Loading