Skip to content

Commit

Permalink
test -X pythonx.y
Browse files Browse the repository at this point in the history
  • Loading branch information
mxcl committed Dec 10, 2022
1 parent 78560b0 commit e389e77
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
25 changes: 25 additions & 0 deletions tests/integration/tea-XX.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { assertEquals } from "deno/testing/asserts.ts"
import { sandbox } from '../utils.ts'

//TODO verify that python version is actually what we request

Deno.test("tea -X python", async () => {
await sandbox(async ({ run }) => {
const out = await run({args: ["-SX", "python", "-c", "print(1)"], net: true }).stdout()
assertEquals(out, "1\n")
})
})

Deno.test("tea -SX python3", async () => {
await sandbox(async ({ run }) => {
const out = await run({args: ["-SX", "python3", "-c", "print(2)"], net: true }).stdout()
assertEquals(out, "2\n")
})
})

Deno.test("tea -SX python3.11", async () => {
await sandbox(async ({ run }) => {
const out = await run({args: ["-SX", "python3.11", "-c", "print(3)"], net: true }).stdout()
assertEquals(out, "3\n")
})
})
30 changes: 18 additions & 12 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface Tea {
tmpdir: Path
}

export async function sandbox<T>(body: (tea: Tea) => Promise<T>) {
export async function sandbox<T>(body: (tea: Tea) => Promise<T>, { throws }: { throws: boolean } = {throws: true}) {
const TEA_PREFIX = new Path(await Deno.makeTempDir({ prefix: "tea" }))

const existing_www_cache = Path.home().join(".tea/tea.xyz/var/www")
Expand Down Expand Up @@ -54,26 +54,32 @@ export async function sandbox<T>(body: (tea: Tea) => Promise<T>) {
)

let stdout: "piped" | undefined
let proc: Deno.Process | undefined

// we delay instantiating the proc so we can set `stdout` if the user calls that function
// so the contract is the user must call `stdout` within this event loop iteration
const p = Promise.resolve().then(() => {
proc = Deno.run({ cmd, cwd: TEA_PREFIX.string, stdout, env, clearEnv: true})
return proc.status()
const p = Promise.resolve().then(async () => {
const proc = Deno.run({ cmd, cwd: TEA_PREFIX.string, stdout, env, clearEnv: true})
try {
const status = await proc.status()
if (throws && !status.success) {
throw status
}
if (stdout == 'piped') {
const out = await proc.output()
return new TextDecoder().decode(out)
} else {
return status
}
} finally {
proc.close()
}
}) as Promise<number> & Enhancements

p.stdout = () => {
stdout = "piped"
return p.then(async () => {
const out = await proc!.output()
return new TextDecoder().decode(out)
})
return p as unknown as Promise<string>
}

p.then(() => proc!.close())
p.catch(() => proc?.close())

return p
}

Expand Down

0 comments on commit e389e77

Please sign in to comment.