Skip to content

Commit

Permalink
Port the rest of template tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szymmis committed Jan 8, 2024
1 parent c92fcd6 commit 0d9b342
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 62 deletions.
63 changes: 63 additions & 0 deletions tests/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,67 @@ class CommandRunBuilder {
});
});
}

run(): Promise<{
process: ChildProcessWithoutNullStreams;
close: () => Promise<void>;
}> {
return new Promise((resolve, reject) => {
const proc = spawn(this.command, this.args, {
...this.options,
detached: true,
});

process.on("exit", () => {
if (proc.pid) process.kill(-proc.pid);
});

proc.stdout?.on("data", (data) => {
this.stdout.write(data.toString());
if (!this.quiet) console.log(data.toString());
});
proc.stderr?.on("data", (err) => {
this.stderr.write(err.toString());
if (!this.quiet) console.error(err.toString());
});

new Promise<void>(() => proc.on("close", () => reject()));

proc.on("spawn", () => {
this.queue.run(proc, () => {
resolve({
process: proc,
close() {
return new Promise((resolve) => {
process.kill(-proc.pid!);
proc.on("exit", () => {
resolve();
});
});
},
});
});
});
});
}
}

export function replaceStringInFile(
path: string,
searchValue: string | RegExp | null,
replaceValue: string,
) {
fs.writeFileSync(
path,
searchValue !== null
? fs.readFileSync(path, "utf-8").replace(searchValue, replaceValue)
: replaceValue,
"utf-8",
);
}

export async function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
Loading

0 comments on commit 0d9b342

Please sign in to comment.