|
| 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