test(utils): add unit tests for systemd file handling

- Add tests for writing .service and .timer files with `writeUnitFiles`
- Add tests for resolving target paths with `resolveUnitTargetPath`
- Ensure proper file creation, content validation, and path resolution
This commit is contained in:
2025-05-21 02:57:53 +02:00
parent 6608f48840
commit ef2ac416d9
2 changed files with 89 additions and 0 deletions

30
src/utils/fs.ts Normal file
View File

@@ -0,0 +1,30 @@
import { ensureDir } from 'https://deno.land/std@0.224.0/fs/mod.ts';
import { join } from 'https://deno.land/std@0.224.0/path/mod.ts';
import { TimerOptions } from '../types/options.ts';
export async function writeUnitFiles(
name: string,
serviceContent: string,
timerContent: string,
options: TimerOptions,
): Promise<{ servicePath: string; timerPath: string }> {
const basePath = resolveUnitTargetPath(options);
await ensureDir(basePath);
const servicePath = join(basePath, `${name}.service`);
const timerPath = join(basePath, `${name}.timer`);
await Deno.writeTextFile(servicePath, serviceContent);
await Deno.writeTextFile(timerPath, timerContent);
return { servicePath, timerPath };
}
export function resolveUnitTargetPath(
options: Pick<TimerOptions, 'output' | 'user'>,
): string {
if (options.output) return options.output;
if (options.user) return `${Deno.env.get('HOME')}/.config/systemd/user`;
return '/etc/systemd/system';
}