pantry/scripts/test.ts

104 lines
2.5 KiB
TypeScript
Raw Normal View History

2022-08-01 22:43:40 +03:00
#!/usr/bin/env -S tea -E
/*---
args:
- deno
- run
- --allow-net
- --allow-run
- --allow-read
2022-09-01 00:44:34 +03:00
- --allow-write
2022-08-01 22:43:40 +03:00
- --allow-env
- --import-map={{ srcroot }}/import-map.json
---*/
2022-09-29 19:07:07 +03:00
import { Installation, Package, PackageRequirement } from "types"
import { usePantry, useFlags, usePrefix } from "hooks"
2022-08-01 22:43:40 +03:00
import useShellEnv, { expand } from "hooks/useShellEnv.ts"
2022-09-21 10:46:24 +03:00
import { run, undent, pkg as pkgutils } from "utils"
2022-09-20 14:53:40 +03:00
import { resolve, install, hydrate, link } from "prefab"
import Path from "path"
2022-09-30 16:03:03 +03:00
import * as ARGV from "./utils/args.ts"
2022-09-20 14:53:40 +03:00
const { debug } = useFlags()
2022-09-02 17:51:31 +03:00
2022-09-30 16:03:03 +03:00
const pantry = usePantry()
2022-08-27 21:11:25 +03:00
2022-09-30 16:03:03 +03:00
for await (const pkg of ARGV.installs()) {
2022-09-29 19:07:07 +03:00
await test(pkg)
}
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
async function test(self: Installation) {
const yml = await pantry.getYAML(self.pkg).parse()
const deps = await deps4(self.pkg)
const installations = await prepare(deps)
// if we are testing multiple packages, they might not
// get linked when they're tested.
await link(self)
2022-10-07 17:51:40 +03:00
const env = useShellEnv({ installations: [self, ...installations] })
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
let text = undent`
#!/bin/bash
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
set -e
set -o pipefail
set -x
2022-08-01 22:43:40 +03:00
export TEA_PREFIX=${usePrefix()}
2022-10-07 17:51:40 +03:00
${expand(env)}
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
`
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
const tmp = Path.mktmp({ prefix: pkgutils.str(self.pkg) })
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
try {
if (yml.test.fixture) {
const fixture = tmp.join("fixture.tea").write({ text: yml.test.fixture.toString() })
text += `export FIXTURE="${fixture}"\n\n`
}
2022-08-27 21:11:25 +03:00
2022-09-29 19:07:07 +03:00
const cwd = tmp.join("wd").mkdir()
2022-08-27 21:11:25 +03:00
2022-09-29 19:07:07 +03:00
text += `cd "${cwd}"\n\n`
2022-08-01 22:43:40 +03:00
2022-09-29 19:07:07 +03:00
text += await pantry.getScript(self.pkg, 'test', installations)
text += "\n"
2022-08-27 21:11:25 +03:00
2022-09-29 19:07:07 +03:00
for await (const [path, {name, isFile}] of pantry.getYAML(self.pkg).path.parent().ls()) {
if (isFile && name != 'package.yml') path.cp({ into: cwd })
}
const cmd = tmp
.join("test.sh")
.write({ text, force: true })
.chmod(0o500)
await run({ cmd, cwd })
tmp.rm({ recursive: true })
} catch (e) {
console.info("due to error, didnt delete:", tmp)
throw e
2022-09-29 19:07:07 +03:00
}
2022-08-01 22:43:40 +03:00
}
2022-09-29 19:07:07 +03:00
2022-09-25 15:41:58 +03:00
//TODO install step in CI should do this for test requirements also
async function prepare(reqs: (Package | PackageRequirement)[]) {
const { pending, installed } = await resolve(reqs)
for await (const pkg of pending) {
2022-09-20 14:53:40 +03:00
const installation = await install(pkg)
2022-09-01 00:44:34 +03:00
await link(installation)
2022-09-25 15:41:58 +03:00
installed.push(installation)
2022-09-01 00:44:34 +03:00
}
2022-09-25 15:41:58 +03:00
return installed
}
async function deps4(pkg: Package) {
return (await hydrate(pkg, async (pkg, dry) => {
const { runtime, test } = await pantry.getDeps(pkg)
return dry ? [...runtime, ...test] : runtime
})).pkgs
2022-09-01 00:44:34 +03:00
}