From 02929fb1c6585bdde3451612e933466b977b30ec Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Mon, 25 Sep 2023 19:25:39 +0530 Subject: [PATCH] feat: initial tests implementation --- README.md | 3 +- build.ts | 13 ------- bun.lockb | Bin 4051 -> 1284 bytes examples/run.ts | 3 -- package.json | 2 +- src/index.ts | 2 +- test.ts | 8 ++++ {examples => tests}/data.luau | 0 tests/import.test.ts | 3 ++ tests/runner.ts | 67 ++++++++++++++++++++++++++++++++++ utils/build.ts | 19 ++++++++++ 11 files changed, 101 insertions(+), 19 deletions(-) delete mode 100644 build.ts mode change 100644 => 100755 bun.lockb delete mode 100644 examples/run.ts create mode 100644 test.ts rename {examples => tests}/data.luau (100%) create mode 100644 tests/import.test.ts create mode 100644 tests/runner.ts create mode 100644 utils/build.ts diff --git a/README.md b/README.md index adfaafb..f93cbee 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,5 @@ To run: bun run src/index.ts ``` -This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime. +This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) +is a fast all-in-one JavaScript runtime. diff --git a/build.ts b/build.ts deleted file mode 100644 index f15cebc..0000000 --- a/build.ts +++ /dev/null @@ -1,13 +0,0 @@ -import plugin from "./src/index"; - -const out = await Bun.build({ - plugins: [plugin], - entrypoints: ["./examples/run.ts"], -}); - -if (out.success) { - eval(await out.outputs[0].text()) -} else { - console.warn("Build failed:") - console.warn(out.logs.map((v) => " " + v.message).join("\n")) -} \ No newline at end of file diff --git a/bun.lockb b/bun.lockb old mode 100644 new mode 100755 index 4ad2e83b8841c87109e604b6a909ea3bc250b48e..3c51561b26bf5dad665a195519b7306c046bb7e9 GIT binary patch delta 349 zcmcaC-@-LPPxHu=Ma%pp-bkGk`2OYWKQ6xHpprj3xLUSw#N5kDclp~`!omOsOcTT9 zovWB290i%5SwZ8P3A=QfBgS|CNfT5$re0Wf>B}e3idRv15l+` zm?r0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P=S}=7M`dXm0xO`U{~jpKWm8;~ zte2TrT#}fRqX%ni>4g-frrIeO87LHIR;A{r=_r^eB<5tM=jEqyft~m7KLmg{p#1Ox zY8(ednt{uv)Y!-ls0)jJP+9<$si5)?R<3~3GAO@-%(8%*5krJoK;;Y|mw@zv%mtYN zG7D5zfy@Tk0kUfX)T}CQAO{GzY@&_KVoNQ6el&x-(Gb;-pavvRTf=?SW6GqQsZFCNi0b%E@5DRwH;w?KPJXFL!cf*a0>?9N`$rYSQw!a3>iRs zKI|V~Tz*(RK9C{dl8uTEsS1;Cvxb8IB4`f$> zY+2KE@3!zNmCxw%7Eo7MgCYP3fN2RBPlmXn2^8kKIi-oEz@9>4N@`I_WkG6ja#3bM z2}lCW%P1)+D7MnqFG@|%EG{Xk)XOW#%_`Q*FG>eFOkWp7>Vl&~uOz+7A-^mY>|))N z#G>-dJl({^qT~!S6AbYR>|(k}iN&conR%%Q4M1LL1*SO=VH9KFLU2>?I3C>sph<9T zP+wu1hwvzACSw=~4=@tUfd(atSx_+&-35&sEMCSUL$=vahawqCny*n)5UOdU_#2*- Qp+ Received data: ", SomeData); diff --git a/tests/runner.ts b/tests/runner.ts new file mode 100644 index 0000000..96709fe --- /dev/null +++ b/tests/runner.ts @@ -0,0 +1,67 @@ +import { buildFromSource as build } from "../utils/build"; + +export class TestRunner { + private tests: Record; + private dryRun: boolean; + private ignoreFail: boolean; + private callback: ( + meta: { name: string; success: boolean; timeElapsed: number }, + ) => void; + + constructor(opts: { + dryRun?: boolean; + ignoreFail?: boolean; + collection: Record; + callback?: (meta: { name: string; success: boolean }) => void; + }) { + this.tests = opts.collection; + this.dryRun = opts.dryRun ?? false; + this.ignoreFail = opts.ignoreFail ?? false; + this.callback = ({ name, success, timeElapsed }) => { + console.log( + `${ + success ? "[OK]" : "[FAILED]" + } tests::${name} ...(${timeElapsed}ms)`, + ); + }; + } + + async run() { + console.log(`Running ${Object.keys(this.tests).length} tests...`) + + for (let testName in this.tests) { + const callbackOpts = { + name: testName, + success: true, + timeElapsed: 0, + }; + + const startTime = performance.now(); + + if (!this.dryRun) { + // Build and execute test from source + let testFailed: boolean; + + await build(this.tests[testName]).catch(() => { + // Test failed + testFailed = true; + + this.callback({ + ...callbackOpts, + timeElapsed: Number((performance.now() - startTime).toFixed(2)), + success: this.ignoreFail ? true : false, + }); + }).then(() => { + !testFailed + ? this.callback({ + ...callbackOpts, + timeElapsed: Number((performance.now() - startTime).toFixed(2)), + }) + : {}; + }); + } else { + this.callback(callbackOpts); + } + } + } +} diff --git a/utils/build.ts b/utils/build.ts new file mode 100644 index 0000000..d595357 --- /dev/null +++ b/utils/build.ts @@ -0,0 +1,19 @@ +import plugin from "../src/index"; + +export async function buildFromSource(entrypoint: string): Promise { + const out = await Bun.build({ + plugins: [plugin], + entrypoints: [entrypoint], + }); + + if (out.success) { + eval(await out.outputs[0].text()); + + Promise.resolve(); + } else { + console.warn("Build failed:"); + console.warn(out.logs.map((v) => " " + v.message).join("\n")); + + Promise.reject("failed to build"); + } +}