test(generate): add unit tests for service and timer generation
- Introduces tests to validate service and timer unit generation - Covers description, dependencies, environment variables, and logging - Ensures generated units meet expected configurations
This commit is contained in:
63
src/templates/unit-generator.ts
Normal file
63
src/templates/unit-generator.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { TimerOptions } from '../types/mod.ts';
|
||||
import { deriveNameFromExec, writeUnitFiles } from '../utils/mod.ts';
|
||||
|
||||
export async function generateUnitFiles(options: TimerOptions): Promise<void> {
|
||||
const name = options.name || deriveNameFromExec(options.exec);
|
||||
|
||||
const { serviceUnit, timerUnit } = generateUnits(name, options);
|
||||
|
||||
if (options.dryRun) {
|
||||
console.log(`===== ${name}.service =====`);
|
||||
console.log(serviceUnit);
|
||||
console.log(`\n===== ${name}.timer =====`);
|
||||
console.log(timerUnit);
|
||||
} else {
|
||||
const { servicePath, timerPath } = await writeUnitFiles(
|
||||
name,
|
||||
serviceUnit,
|
||||
timerUnit,
|
||||
options,
|
||||
);
|
||||
console.log(`Service unit written to: ${servicePath}`);
|
||||
console.log(`Timer unit written to: ${timerPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function generateUnits(name: string, options: TimerOptions): {
|
||||
serviceUnit: string;
|
||||
timerUnit: string;
|
||||
} {
|
||||
const unitParts = [
|
||||
`[Unit]`,
|
||||
`Description=${options.description ?? name}`,
|
||||
...(options.after?.map((a) => `After=${a}`) ?? []),
|
||||
``,
|
||||
`[Service]`,
|
||||
`Type=oneshot`,
|
||||
`ExecStart=${options.exec}`,
|
||||
...(options.environment?.map((e) => `Environment=${e}`) ?? []),
|
||||
];
|
||||
|
||||
if (options.logfile) {
|
||||
unitParts.push(`StandardOutput=append:${options.logfile}`);
|
||||
unitParts.push(`StandardError=append:${options.logfile}`);
|
||||
}
|
||||
|
||||
const serviceUnit = unitParts.join('\n');
|
||||
|
||||
const timerParts = [
|
||||
`[Unit]`,
|
||||
`Description=Timer for ${name}`,
|
||||
``,
|
||||
`[Timer]`,
|
||||
`OnCalendar=${options.calendar}`,
|
||||
`Persistent=true`,
|
||||
``,
|
||||
`[Install]`,
|
||||
`WantedBy=timers.target`,
|
||||
];
|
||||
|
||||
const timerUnit = timerParts.join('\n');
|
||||
|
||||
return { serviceUnit, timerUnit };
|
||||
}
|
Reference in New Issue
Block a user