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:
30
src/utils/fs.ts
Normal file
30
src/utils/fs.ts
Normal 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';
|
||||
}
|
Reference in New Issue
Block a user