Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- id: build
run: ./ci-build.sh
- name: Run Playwright tests
timeout-minutes: 10
timeout-minutes: 20
uses: docker://mcr.microsoft.com/playwright:v1.64.1-noble
with:
args: npx playwright test
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
term.js
src/build
lib
micropython-tests/expected
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ abort fails the test. Every file in `src/examples` must have an entry in
`e2e/audio.spec.ts` records the simulator's audio output and checks broad
statistics (duration, level, pitch, dropouts) rather than exact samples.

The same run includes MicroPython's own tests
(`micropython-tests/`), as the `micropython-tests` Playwright project. Each
test file runs as `main.py` and its serial output must match the test's `.exp`
file or, where it has none, CPython's output for the same file. The CPython
output is checked in under `micropython-tests/expected` so that test runs
don't need a particular Python version. Tests that don't apply to this port
are listed with reasons in `micropython-tests/skip.json`.

$ npx playwright test --project=micropython-tests

### Deployments

GitHub Actions (`.github/workflows/build.yml`) builds, tests and deploys every
Expand All @@ -367,6 +377,12 @@ release. See [Versions](#versions) for when to bump the minor version.
1. main.c, src/Makefile and mpconfigport.h all which have simulator versions that may need updates
2. the HAL, which may require implementing in the simulator
3. the filesystem, which has a JavaScript implementation.
3. Regenerate the expected output for MicroPython's tests and review the
changes, along with `micropython-tests/skip.json`. The script asserts a
specific CPython version; update it to match the version MicroPython
uses.

$ python3.8 micropython-tests/update-expected.py

### Web Assembly debugging

Expand Down
16 changes: 15 additions & 1 deletion e2e/audio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,22 @@ import { expect, Simulator, test } from "./fixtures";

test.use({ recordAudio: true });

// The recorder runs on the main thread, which is busy while the simulator
// starts, so it misses the start of a sound made straight away. This holds
// the program until there's serial input, sent once the recorder is running.
const waitForRecorder =
"from microbit import uart as _uart, sleep as _sleep\nwhile not _uart.any():\n _sleep(10)\n_uart.read()\n";

async function runToEnd(simulator: Simulator, program: string) {
await simulator.run(program);
await simulator.run(waitForRecorder + program);
await expect
.poll(() =>
simulator.frame
.locator("body")
.evaluate(() => (window as any).__audioRecording.chunks.length),
)
.toBeGreaterThan(0);
await simulator.sendSerial("\n");
await simulator.expectFinished({ timeout: 20_000 });
// The last buffer can still be playing when the prompt appears.
await simulator.page.waitForTimeout(300);
Expand Down
43 changes: 26 additions & 17 deletions e2e/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,30 +298,39 @@ export class Simulator {
}
}

/** Loads the simulator in a page, recording console errors and exceptions. */
export const createSimulator = async (
page: Page,
flags: string[],
): Promise<Simulator> => {
// Served by the test rather than the build so it doesn't ship.
await page.route("**/e2e-host.html*", (route) =>
route.fulfill({ body: hostHtml, contentType: "text/html" }),
);
await page.route("**/favicon.ico", (route) => route.fulfill({ body: "" }));

const simulator = new Simulator(page, flags);
page.on("console", (msg) => {
if (msg.type() === "error") {
simulator.errors.push(`console: ${msg.text()}`);
}
});
page.on("pageerror", (error) => {
simulator.errors.push(`pageerror: ${error.stack ?? error.message}`);
});

await simulator.load();
return simulator;
};

export const test = base.extend<SimulatorOptions & { simulator: Simulator }>({
simulatorFlags: [[], { option: true }],
recordAudio: [false, { option: true }],
simulator: async ({ page, simulatorFlags, recordAudio }, use) => {
if (recordAudio) {
await page.addInitScript(installAudioRecorder);
}
// Served by the test rather than the build so it doesn't ship.
await page.route("**/e2e-host.html*", (route) =>
route.fulfill({ body: hostHtml, contentType: "text/html" }),
);
await page.route("**/favicon.ico", (route) => route.fulfill({ body: "" }));

const simulator = new Simulator(page, simulatorFlags);
page.on("console", (msg) => {
if (msg.type() === "error") {
simulator.errors.push(`console: ${msg.text()}`);
}
});
page.on("pageerror", (error) => {
simulator.errors.push(`pageerror: ${error.stack ?? error.message}`);
});

await simulator.load();
const simulator = await createSimulator(page, simulatorFlags);
await use(simulator);

expect(
Expand Down
33 changes: 14 additions & 19 deletions e2e/micropython.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { test } from "./fixtures";

// Loops over range() compile to a counted loop, so these cover the iterator
// protocol that other tests' loops never reach.
test("for loop over a list", async ({ simulator }) => {
await simulator.run("for x in [1, 2, 3]:\n print(x)\nprint('done')\n");
await simulator.expectSerial(/1\n2\n3\ndone/);
});

test("for loop over a list inside a function", async ({ simulator }) => {
await simulator.run(
"def total(values):\n t = 0\n for v in values:\n t += v\n return t\nprint(total([1, 2, 3]))\n",
);
await simulator.expectSerial(/^6\n/);
});

test("list comprehension", async ({ simulator }) => {
await simulator.run("print([x * 2 for x in 'ab'])\n");
await simulator.expectSerial(/\['aa', 'bb'\]/);
});

test("machine.time_pulse_us times out on an idle pin", async ({
simulator,
}) => {
Expand Down Expand Up @@ -52,3 +33,17 @@ test("uncaught exception whose argument's __str__ raises", async ({
await simulator.expectSerial(/line 5, in <module>\nException: \n/);
await simulator.expectFinished();
});

test("program that ends with the heap locked", async ({ simulator }) => {
await simulator.run("import micropython\nmicropython.heap_lock()\n");
await simulator.expectFinished();
});

test("uncaught exception with the heap locked", async ({ simulator }) => {
await simulator.run(
"import micropython\nmicropython.heap_lock()\nraise ValueError\n",
);
await simulator.expectSerial(/ValueError/);
await simulator.skipErrorScroll();
await simulator.expectFinished();
});
12 changes: 12 additions & 0 deletions e2e/simulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ test.describe("limits", () => {
await simulator.skipErrorScroll();
});

// C recursion that doesn't go via Python function calls, so isn't bounded
// by the Python stack.
test("printing a list that contains itself raises RuntimeError", async ({
simulator,
}) => {
await simulator.run("l = []\nl.append(l)\nprint(l)\n");
await simulator.expectSerial(
/RuntimeError: maximum recursion depth exceeded/,
);
await simulator.skipErrorScroll();
});

test("running out of heap raises MemoryError", async ({ simulator }) => {
await simulator.run("l = []\nwhile True:\n l = [l]\n");
await simulator.expectSerial(/MemoryError/);
Expand Down
2 changes: 2 additions & 0 deletions micropython-tests/expected/basics/0prelim.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1
abc
4 changes: 4 additions & 0 deletions micropython-tests/expected/basics/andor.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
(1,)
()
1
100 changes: 100 additions & 0 deletions micropython-tests/expected/basics/array1.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
array('B', [1, 2, 3]) 3
array('I', [1, 2, 3]) 3
1
3
1 -1
2
True
0
array('i')
False
True
False
False
ValueError
True
False
False
True
False
True
False
False
True
False
True
True
True
True
True
True
True
True
True
True
True
True
False
True
True
False
False
True
False
True
False
False
True
True
True
True
False
False
False
True
False
True
False
False
True
True
True
True
False
False
False
True
False
True
False
False
True
True
True
True
False
False
False
True
False
True
False
False
True
True
True
True
False
False
False
True
False
True
False
False
True
True
True
True
False
False
3 changes: 3 additions & 0 deletions micropython-tests/expected/basics/array_add.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
array('I', [1, 2])
array('I', [1, 3, 4])
array('I', [1, 3, 4, 5])
7 changes: 7 additions & 0 deletions micropython-tests/expected/basics/array_construct.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
array('b', [1, 2])
array('h', [1, 2])
array('h', [12850])
array('h', [0])
array('i', [0])
array('H', [1, 2])
array('b', [1, 2])
1 change: 1 addition & 0 deletions micropython-tests/expected/basics/array_construct2.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
array('i', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
array('h', [12849])
10 changes: 10 additions & 0 deletions micropython-tests/expected/basics/array_intbig.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
array('L', [0, 4294967295])
array('l', [-2147483648, 0, 2147483647])
array('q')
array('Q')
array('q', [0])
array('Q', [0])
array('q', [-9223372036854775808, -1, 0, 1, 2, 9223372036854775807])
array('Q', [0, 1, 2, 18446744073709551615])
b'\xff\xff\xff\xff\xff\xff\xff\xff'
b'\xff\xff\xff\xff\xff\xff\xff\xff'
3 changes: 3 additions & 0 deletions micropython-tests/expected/basics/assign1.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
2 2
3 3 3
2 changes: 2 additions & 0 deletions micropython-tests/expected/basics/attrtuple1.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
True
True
27 changes: 27 additions & 0 deletions micropython-tests/expected/basics/bool1.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
True
False
False
True
1
-1
True
False
False
True
False
True
True
False
True
True
True
True
False
False
False
False
False
False
True
True
TypeError
5 changes: 5 additions & 0 deletions micropython-tests/expected/basics/boundmeth1.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<class 'str'>
0
1
21
AttributeError
9 changes: 9 additions & 0 deletions micropython-tests/expected/basics/break.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
one 0
two 0
one 1
two 1
one 2
two 2
one 3
1
2
4 changes: 4 additions & 0 deletions micropython-tests/expected/basics/builtin_abs.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0
1
1
1
5 changes: 5 additions & 0 deletions micropython-tests/expected/basics/builtin_abs_intbig.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
123456789012345678901234567890
123456789012345678901234567890
1073741824
4611686018427387904
70368744177664
Loading
Loading