Skip to content

Commit

Permalink
Add simple integration tests for sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
taybenlor committed Nov 17, 2024
1 parent c6cbcea commit dfca4dd
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 18 deletions.
11 changes: 11 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 0 additions & 7 deletions packages/sandbox/__tests__/sandbox.test.js

This file was deleted.

7 changes: 7 additions & 0 deletions packages/sandbox/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 5 additions & 9 deletions packages/sandbox/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
79 changes: 79 additions & 0 deletions packages/sandbox/tests/sandbox.test.ts
Original file line number Diff line number Diff line change
@@ -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",
`
<?php
print "Hello, World!\n";
?>
`
);

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 <stdio.h>
#include <string.h>
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 <iostream>
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");
});

0 comments on commit dfca4dd

Please sign in to comment.