Skip to content

Commit b867dc1

Browse files
committed
Test runExec and run the unit tests in CI
1 parent c592daf commit b867dc1

4 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎.github/workflows/ci.yml‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,33 @@ jobs:
7171
exit 1
7272
fi
7373
74+
unit_tests:
75+
name: Unit tests on ${{ matrix.os }}
76+
# Tags only publish; tests run on branches/PRs before the tag is cut.
77+
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
78+
runs-on: ${{ matrix.os }}
79+
strategy:
80+
fail-fast: false
81+
matrix:
82+
os: [ubuntu-24.04, windows-2025-vs2026]
83+
steps:
84+
- name: Checkout repository
85+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
86+
with:
87+
persist-credentials: false
88+
89+
- name: Setup Flutter
90+
uses: kuhnroyal/flutter-fvm-config-action/setup@c378498f1d1962d33039c3989411093ef8a17b2c # v3.3
91+
with:
92+
path: '.fvmrc'
93+
cache: true
94+
95+
- name: Run unit tests
96+
working-directory: src/serious_python
97+
run: |
98+
flutter pub get
99+
flutter test test/
100+
74101
bridge_example_macos:
75102
name: Test Bridge example on macOS (${{ matrix.build_system }}, Python ${{ matrix.python_version }})
76103
if: ${{ !startsWith(github.ref, 'refs/tags/') }}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'dart:io';
2+
3+
import '../../bin/package_command.dart';
4+
5+
/// Runs the command given as arguments through `PackageCommand.runExec` and
6+
/// prints the status it returns. `runExec` exits the process when the command
7+
/// fails, so tests run this program in its own process.
8+
Future<void> main(List<String> args) async {
9+
final status = await PackageCommand().runExec(args.first, args.sublist(1));
10+
stdout.writeln("runExec returned $status");
11+
}

‎src/serious_python/test/glob_test.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ void main() {
2020
expect(pyCache.matches("a/__pycache"), false);
2121

2222
final pyCache2 = Glob("**/__pycache__");
23-
expect(pyCache2.matches("c:/aaa/bbb/__pycache__"), true);
2423
expect(pyCache2.matches("a/b/__pycache__"), true);
2524

2625
final binPyCache = Glob("**/bin/__pycache__");
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:path/path.dart' as path;
5+
6+
/// Python ships on every GitHub-hosted runner, as `python` on Windows.
7+
final python = Platform.isWindows ? "python" : "python3";
8+
9+
/// Longer than any case needs; a command that blocks on a full pipe never
10+
/// finishes, so it fails here instead of hanging the test run.
11+
const hangTimeout = Duration(seconds: 30);
12+
13+
late Directory tempDir;
14+
late String harness;
15+
16+
/// Runs [script] with Python through `runExec`, inside the compiled harness.
17+
Future<ProcessResult> runExec(String script) async {
18+
final proc = await Process.start(harness, [python, "-c", script]);
19+
final stdoutText = proc.stdout.transform(systemEncoding.decoder).join();
20+
final stderrText = proc.stderr.transform(systemEncoding.decoder).join();
21+
final status = await proc.exitCode.timeout(hangTimeout, onTimeout: () {
22+
proc.kill(ProcessSignal.sigkill);
23+
fail("runExec didn't return within $hangTimeout");
24+
});
25+
return ProcessResult(proc.pid, status, await stdoutText, await stderrText);
26+
}
27+
28+
void main() {
29+
setUpAll(() async {
30+
tempDir = await Directory.systemTemp.createTemp("run_exec_test");
31+
harness =
32+
path.join(tempDir.path, Platform.isWindows ? "harness.exe" : "harness");
33+
final compile = await Process.run(
34+
"dart",
35+
[
36+
"compile",
37+
"exe",
38+
"test/fixtures/run_exec_harness.dart",
39+
"-o",
40+
harness,
41+
],
42+
runInShell: true);
43+
expect(compile.exitCode, 0, reason: "${compile.stdout}${compile.stderr}");
44+
});
45+
46+
tearDownAll(() async {
47+
await tempDir.delete(recursive: true);
48+
});
49+
50+
// 200 KB is more than a pipe holds on any OS (about 1 KB on Windows,
51+
// 64 KB on macOS and Linux).
52+
53+
test("a command writing a lot to stderr finishes, and its output is hidden",
54+
() async {
55+
final result = await runExec(
56+
"import sys; sys.stderr.write('w' * 200000); print('done')");
57+
58+
expect(result.exitCode, 0);
59+
expect(result.stdout, contains("runExec returned 0"));
60+
expect(result.stderr, isEmpty);
61+
});
62+
63+
test("a failing command's stdout and stderr are shown", () async {
64+
final result = await runExec("import sys; print('OUT-MARK'); "
65+
"sys.stderr.write('e' * 200000 + 'ERR-MARK'); sys.exit(3)");
66+
67+
expect(result.exitCode, 1);
68+
expect(result.stderr, contains("OUT-MARK"));
69+
expect(result.stderr, contains("ERR-MARK"));
70+
});
71+
72+
test("a command reading stdin gets end of input", () async {
73+
final result = await runExec(
74+
"import sys; data = sys.stdin.read(); sys.exit(0 if data == '' else 1)");
75+
76+
expect(result.exitCode, 0);
77+
expect(result.stdout, contains("runExec returned 0"));
78+
});
79+
80+
test("output that isn't UTF-8 is shown", () async {
81+
final result = await runExec("import sys; "
82+
"sys.stdout.buffer.write(b'caf\\xe9 BAD-BYTES'); sys.exit(2)");
83+
84+
expect(result.exitCode, 1);
85+
expect(result.stderr, contains("BAD-BYTES"));
86+
});
87+
88+
test("a process left running with the pipes open doesn't block", () async {
89+
// The process outlives hangTimeout, so waiting for its pipes to close
90+
// fails the test.
91+
final result = await runExec("import subprocess, sys; "
92+
"subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'])");
93+
94+
expect(result.exitCode, 0);
95+
expect(result.stdout, contains("runExec returned 0"));
96+
});
97+
}

0 commit comments

Comments
 (0)