From dfca4dd0b56a114dcb7990413bb9ab65db7e49f8 Mon Sep 17 00:00:00 2001 From: Ben Taylor Date: Sun, 17 Nov 2024 13:00:45 +1100 Subject: [PATCH] Add simple integration tests for sandbox --- deno.lock | 11 +++ package.json | 3 +- packages/sandbox/__tests__/sandbox.test.js | 7 -- packages/sandbox/deno.json | 7 ++ packages/sandbox/lib/runtime.ts | 14 ++-- packages/sandbox/package.json | 2 +- packages/sandbox/tests/sandbox.test.ts | 79 ++++++++++++++++++++++ 7 files changed, 105 insertions(+), 18 deletions(-) delete mode 100644 packages/sandbox/__tests__/sandbox.test.js create mode 100644 packages/sandbox/tests/sandbox.test.ts diff --git a/deno.lock b/deno.lock index 2a17bfbc..357e731c 100644 --- a/deno.lock +++ b/deno.lock @@ -1,6 +1,8 @@ { "version": "4", "specifiers": { + "jsr:@std/assert@*": "1.0.8", + "jsr:@std/internal@^1.0.5": "1.0.5", "jsr:@std/streams@^1.0.7": "1.0.8", "jsr:@std/tar@0.1.3": "0.1.3", "jsr:@std/tar@~0.1.3": "0.1.3", @@ -49,6 +51,15 @@ "npm:xterm@^4.19.0": "4.19.0" }, "jsr": { + "@std/assert@1.0.8": { + "integrity": "ebe0bd7eb488ee39686f77003992f389a06c3da1bbd8022184804852b2fa641b", + "dependencies": [ + "jsr:@std/internal" + ] + }, + "@std/internal@1.0.5": { + "integrity": "54a546004f769c1ac9e025abd15a76b6671ddc9687e2313b67376125650dc7ba" + }, "@std/streams@1.0.8": { "integrity": "b41332d93d2cf6a82fe4ac2153b930adf1a859392931e2a19d9fabfb6f154fb3" }, diff --git a/package.json b/package.json index 4abcd475..2c27dca5 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,11 @@ "build:website": "cd packages/website && npm run build", "build": "npm run build:wasi && npm run build:runtime && npm run build:client && npm run build:docs && npm run build:website", "build:deploy": "npm run bootstrap && npm run build", + "test:sandbox": "cd packages/sandbox && npm run test", "test:client": "cd packages/client && npm run test", "test:wasi": "cd packages/wasi && npm run test:prepare && npm run test", "test:runtime": "cd packages/runtime && npm run test", - "test": "npm run test:client && npm run test:wasi && npm run test:runtime" + "test": "npm run test:client && npm run test:wasi && npm run test:runtime && npm run test:sandbox" }, "eslintConfig": { "extends": "preact", diff --git a/packages/sandbox/__tests__/sandbox.test.js b/packages/sandbox/__tests__/sandbox.test.js deleted file mode 100644 index 664fdad2..00000000 --- a/packages/sandbox/__tests__/sandbox.test.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; - -const sandbox = require(".."); -const assert = require("assert").strict; - -assert.strictEqual(sandbox(), "Hello from sandbox"); -console.info("sandbox tests passed"); diff --git a/packages/sandbox/deno.json b/packages/sandbox/deno.json index 57bf7482..165cb9d2 100644 --- a/packages/sandbox/deno.json +++ b/packages/sandbox/deno.json @@ -2,5 +2,12 @@ "imports": { "@runno/wasi": "npm:@runno/wasi@^0.7.0", "@std/tar": "jsr:@std/tar@^0.1.3" + }, + "compilerOptions": { + "lib": ["ESNext", "deno.window"], + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true } } diff --git a/packages/sandbox/lib/runtime.ts b/packages/sandbox/lib/runtime.ts index 730d439b..07f36e56 100644 --- a/packages/sandbox/lib/runtime.ts +++ b/packages/sandbox/lib/runtime.ts @@ -67,16 +67,16 @@ export async function runFS( args: [run.binaryName, ...(run.args ?? [])], env: run.env, fs, - stdin: (maxByteLength) => { + stdin: (maxByteLength: number) => { const chunk = stdinBytes.slice(0, maxByteLength); stdinBytes = stdinBytes.slice(maxByteLength); return new TextDecoder().decode(chunk); }, - stdout: (out) => { + stdout: (out: string) => { prepare.stdout += out; prepare.tty += out; }, - stderr: (err) => { + stderr: (err: string) => { prepare.stderr += err; prepare.tty += err; }, @@ -131,18 +131,14 @@ export async function headlessPrepareFS( args: [command.binaryName, ...(command.args ?? [])], env: command.env, fs: prepare.fs, - stdout: (out) => { + stdout: (out: string) => { prepare.stdout += out; prepare.tty += out; }, - stderr: (err) => { + stderr: (err: string) => { prepare.stderr += err; prepare.tty += err; }, - debug: (...args) => { - console.log("DEBUG", ...args); - return args[2]; - }, }); prepare.fs = result.fs; diff --git a/packages/sandbox/package.json b/packages/sandbox/package.json index a27ae0d0..71457b71 100644 --- a/packages/sandbox/package.json +++ b/packages/sandbox/package.json @@ -28,7 +28,7 @@ "url": "git+https://github.com/taybenlor/runno.git" }, "scripts": { - "test": "node ./__tests__/@runno/sandbox.test.js" + "test": "deno test --no-check --allow-net" }, "bugs": { "url": "https://github.com/taybenlor/runno/issues" diff --git a/packages/sandbox/tests/sandbox.test.ts b/packages/sandbox/tests/sandbox.test.ts new file mode 100644 index 00000000..c1efa6a0 --- /dev/null +++ b/packages/sandbox/tests/sandbox.test.ts @@ -0,0 +1,79 @@ +import { assertEquals, assertMatch } from "jsr:@std/assert"; + +import { runCode } from "../lib/main.ts"; + +Deno.test("a simple python example", async () => { + const result = await runCode("python", `print("Hello, World!")`); + assertEquals(result.resultType, "complete", JSON.stringify(result)); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertEquals(result.stdout, "Hello, World!\n"); +}); + +Deno.test("a simple ruby example", async () => { + const result = await runCode("ruby", `puts "Hello, World!"`); + + assertEquals(result.resultType, "complete"); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertEquals(result.stdout, "Hello, World!\n"); +}); + +Deno.test("a simple js example", async () => { + const result = await runCode("quickjs", `console.log("Hello, World!");`); + + assertEquals(result.resultType, "complete"); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertEquals(result.stdout, "Hello, World!\n"); +}); + +Deno.test("a simple php example", async () => { + const result = await runCode( + "php-cgi", + ` + +` + ); + + assertEquals(result.resultType, "complete"); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertMatch(result.stdout, /Hello, World!\n/); +}); + +Deno.test("a simple C example", async () => { + const result = await runCode( + "clang", + `#include +#include +int main() { + printf("Hello, World!\\n"); +}` + ); + + assertEquals(result.resultType, "complete"); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertEquals(result.stdout, "Hello, World!\n"); +}); + +Deno.test("a simple C++ example", async () => { + const result = await runCode( + "clangpp", + ` +#include +int main() { + std::cout << "Hello, World!" << std::endl; + return 0; +} +` + ); + + assertEquals(result.resultType, "complete"); + if (result.resultType !== "complete") throw new Error("wtf"); + assertEquals(result.stderr, ""); + assertEquals(result.stdout, "Hello, World!\n"); +});