Skip to content

Commit

Permalink
Speed up tests, add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
taybenlor committed Oct 29, 2023
1 parent 6201cd8 commit 9a16a64
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/runtime/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
VITE_RUNTIME=http://localhost:1234
VITE_HOST=http://localhost:4321
1 change: 0 additions & 1 deletion packages/runtime/.env.production
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
VITE_RUNTIME=https://runno.run
VITE_HOST=https://runno.dev
1 change: 0 additions & 1 deletion packages/runtime/lib/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export function commandsForRuntime(
binaryName: "python",
args: [entryPath],
env: {},
baseFSURL: `${baseURL}/python-3.11.3.tar.gz`,
},
};

Expand Down
43 changes: 42 additions & 1 deletion packages/runtime/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
import "../lib/main";
import { headlessRunCode } from "../lib/main";
import {
CompleteResult,
CrashResult,
Runtime,
TerminatedResult,
headlessRunCode,
} from "../lib/main";

globalThis.Runno = {
headlessRunCode,
};

export type TestRunResult =
| CrashResult
| TerminatedResult
| Omit<CompleteResult, "fs">;

globalThis.test = {
/**
* Returning the fs in a test environment from playwright can result in a massive transfer
* of files over the boundary between the headless browser and the playwright test runner.
* This can cause tests to slow down significantly.
*
* So this wrapper strips out the `fs` from the result.
*/
async headlessRunCode(
runtime: Runtime,
code: string,
stdin?: string
): Promise<TestRunResult> {
const result = await headlessRunCode(runtime, code, stdin);

if (result.resultType !== "complete") {
return result;
}

return {
resultType: "complete",
stdin: result.stdin,
stdout: result.stdout,
stderr: result.stderr,
tty: result.tty,
exitCode: result.exitCode,
};
},
};
55 changes: 45 additions & 10 deletions packages/runtime/tests/headless.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { test, expect } from "@playwright/test";
import { RunResult } from "../lib/types";
import { TestRunResult } from "../src/main";

// TODO: These are dependent on `@runno/website` being run on localhost:4321
// See: https://github.com/taybenlor/runno/issues/258

// TODO: This python test times out, I think because of the massive base tar file.
// When I comment out the base FS it works fine.
test.skip("a simple python example", async ({ page }) => {
test("a simple python example", async ({ page }) => {
await page.goto("/");

const result: RunResult = await page.evaluate(async () => {
return await globalThis.Runno.headlessRunCode(
"python",
`print("Hello, World!")`
);
return globalThis.test.headlessRunCode("python", `print("Hello, World!")`);
});

expect(result.resultType).toBe("complete");
Expand All @@ -26,9 +22,26 @@ test("a simple ruby example", async ({ page }) => {
await page.goto("/");

const result: RunResult = await page.evaluate(async () => {
return await globalThis.Runno.headlessRunCode(
"ruby",
`puts "Hello, World!"`
return globalThis.test.headlessRunCode("ruby", `puts "Hello, World!"`);
});

expect(result.resultType).toBe("complete");
if (result.resultType !== "complete") throw new Error("wtf");
expect(result.stderr).toBe("");
expect(result.stdout).toBe("Hello, World!\n");
});

test("a simple C example", async ({ page }) => {
await page.goto("/");

const result: TestRunResult = await page.evaluate(async () => {
return globalThis.test.headlessRunCode(
"clang",
`#include <stdio.h>
#include <string.h>
int main() {
printf("Hello, World!\\n");
}`
);
});

Expand All @@ -37,3 +50,25 @@ test("a simple ruby example", async ({ page }) => {
expect(result.stderr).toBe("");
expect(result.stdout).toBe("Hello, World!\n");
});

test("declaring a C variable globally without initializer", async ({
page,
}) => {
// https://github.com/taybenlor/runno/issues/282
await page.goto("/");

const result: RunResult = await page.evaluate(async () => {
return globalThis.test.headlessRunCode(
"clang",
`
int error;
int main() {
}`
);
});

expect(result.resultType).toBe("complete");
if (result.resultType !== "complete") throw new Error("wtf");
expect(result.stderr).toBe("");
expect(result.stdout).toBe("");
});
1 change: 0 additions & 1 deletion packages/website/.env.development
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
VITE_RUNTIME=http://localhost:1234/
VITE_HOST=http://localhost:4321/
1 change: 0 additions & 1 deletion packages/website/.env.production
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
VITE_RUNTIME=https://runno.run/
VITE_HOST=https://runno.dev/
4 changes: 1 addition & 3 deletions packages/website/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
/// <reference types="vite/client" />
interface ImportMetaEnv {
VITE_RUNTIME: string;
}
interface ImportMetaEnv {}

0 comments on commit 9a16a64

Please sign in to comment.