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

View File

@@ -0,0 +1,59 @@
import {
assertEquals,
assertExists,
assertStringIncludes,
} from 'https://deno.land/std@0.224.0/assert/mod.ts';
import { join } from 'https://deno.land/std@0.224.0/path/mod.ts';
import { resolveUnitTargetPath, writeUnitFiles } from '../mod.ts';
import { TimerOptions } from '../../types/options.ts';
Deno.test('writeUnitFiles schreibt .service und .timer korrekt', async () => {
const tmp = await Deno.makeTempDir({ prefix: 'test-units-' });
const options = {
output: tmp,
user: false,
};
const name = 'test-backup';
const serviceContent = '[Service]\nExecStart=/bin/true';
const timerContent = '[Timer]\nOnCalendar=daily';
const { servicePath, timerPath } = await writeUnitFiles(
name,
serviceContent,
timerContent,
options as TimerOptions,
);
// Überprüfe Pfade
assertEquals(servicePath, join(tmp, 'test-backup.service'));
assertEquals(timerPath, join(tmp, 'test-backup.timer'));
// Existieren Dateien?
assertExists(await Deno.stat(servicePath));
assertExists(await Deno.stat(timerPath));
// Enthält die Datei den erwarteten Inhalt?
const readService = await Deno.readTextFile(servicePath);
const readTimer = await Deno.readTextFile(timerPath);
assertStringIncludes(readService, 'ExecStart=/bin/true');
assertStringIncludes(readTimer, 'OnCalendar=daily');
});
Deno.test('resolveUnitTargetPath mit --output', () => {
const result = resolveUnitTargetPath({ output: '/tmp/units', user: false });
assertEquals(result, '/tmp/units');
});
Deno.test('resolveUnitTargetPath mit --user ohne output', () => {
Deno.env.set('HOME', '/home/maxp');
const result = resolveUnitTargetPath({ output: undefined, user: true });
assertEquals(result, '/home/maxp/.config/systemd/user');
});
Deno.test('resolveUnitTargetPath ohne output und ohne user', () => {
const result = resolveUnitTargetPath({ output: undefined, user: false });
assertEquals(result, '/etc/systemd/system');
});

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';
}