Skip to content

Commit

Permalink
add builtin template tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed Jul 19, 2024
1 parent c20d8f4 commit b3b6f19
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,13 @@ if ("vite" in template) {

const runInstance = spawner(template.entry)

process.stdin.setRawMode(true)
process.stdin.resume()
if (process.stdin.isTTY) {
process.stdin.setRawMode(true)
process.stdin.resume()
}
process.stdin.on("data", async (key) => {
switch (key.toString()) {
case "q\n":
case "q":
case "\u0003": // ctrl-c
codeInstance?.kill()
Expand Down
88 changes: 88 additions & 0 deletions tests/builtin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { expect, test } from "bun:test"
import { access, writeFile } from "node:fs/promises"
import { join } from "node:path"

const testFactory = (
template: string,
entryFile: string,
initialText: string,
) => {
return async () => {
const child = Bun.spawn(
[
"bun",
"run",
"src/index.ts",
"--template",
template,
"--no-code",
"--delete",
],
{
stdin: "pipe",
stdout: "pipe",
},
)

const randomString = Math.random().toString(36).slice(2)

const reader = child.stdout.getReader()
let chunk = await reader.read()

let path: string | undefined

// read the stdout chunkwise
while (chunk.done === false) {
chunk = await reader.read()
if (chunk.value) {
const text = Buffer.from(chunk.value).toString()

// save the path of the running project
if (text.includes("Project is running at:")) {
;[, path] = text.split("\n")
}

// after the default output is printed, update the file
if (text.includes(initialText)) {
expect(text).toContain(initialText)

if (!path) throw new Error("path not found")
const filePath = join(path, entryFile)
await writeFile(filePath, `console.log("${randomString}")`)
}

// after the file is updated, check if the output is correct
if (text.includes(randomString)) {
expect(text).toContain(randomString)
break
}
}
}

child.stdin.write("q\n")

await child.exited
expect(child.exitCode).toBe(0)

if (!path) throw new Error("path not found")
expect(() => access(path)).toThrow()
}
}

test(
"run TypeScript template",
testFactory(
"typescript",
"index.ts",
"Localpen TypeScript template running...",
),
)

test(
"run JavaScript template",
testFactory(
"javascript",
"index.js",
"Localpen JavaScript template running...",
),
)
4 changes: 2 additions & 2 deletions tests/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ test("print help message on wrong template", async () => {

await child.exited

expect(text.includes(helpOutput)).toBe(true)
expect(text.includes(" not found")).toBe(true)
expect(text).toContain(helpOutput)
expect(text).toContain(" not found")
})

0 comments on commit b3b6f19

Please sign in to comment.