|
| 1 | +const std = @import("std"); |
| 2 | +const testing = std.testing; |
| 3 | + |
| 4 | +test "fx platform effectful functions" { |
| 5 | + const allocator = testing.allocator; |
| 6 | + |
| 7 | + // Build the fx app |
| 8 | + const build_result = try std.process.Child.run(.{ |
| 9 | + .allocator = allocator, |
| 10 | + .argv = &[_][]const u8{ |
| 11 | + "./zig-out/bin/roc", |
| 12 | + "build", |
| 13 | + "test/fx/app.roc", |
| 14 | + }, |
| 15 | + }); |
| 16 | + defer allocator.free(build_result.stdout); |
| 17 | + defer allocator.free(build_result.stderr); |
| 18 | + |
| 19 | + switch (build_result.term) { |
| 20 | + .Exited => |code| { |
| 21 | + if (code != 0) { |
| 22 | + std.debug.print("Build failed with exit code {}\n", .{code}); |
| 23 | + std.debug.print("STDOUT: {s}\n", .{build_result.stdout}); |
| 24 | + std.debug.print("STDERR: {s}\n", .{build_result.stderr}); |
| 25 | + return error.BuildFailed; |
| 26 | + } |
| 27 | + }, |
| 28 | + else => { |
| 29 | + std.debug.print("Build terminated abnormally: {}\n", .{build_result.term}); |
| 30 | + std.debug.print("STDOUT: {s}\n", .{build_result.stdout}); |
| 31 | + std.debug.print("STDERR: {s}\n", .{build_result.stderr}); |
| 32 | + return error.BuildFailed; |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + // Run the app and capture stdout/stderr separately |
| 37 | + const run_result = try std.process.Child.run(.{ |
| 38 | + .allocator = allocator, |
| 39 | + .argv = &[_][]const u8{"./app"}, |
| 40 | + }); |
| 41 | + defer allocator.free(run_result.stdout); |
| 42 | + defer allocator.free(run_result.stderr); |
| 43 | + |
| 44 | + // Clean up the app binary |
| 45 | + std.fs.cwd().deleteFile("./app") catch {}; |
| 46 | + |
| 47 | + switch (run_result.term) { |
| 48 | + .Exited => |code| { |
| 49 | + if (code != 0) { |
| 50 | + std.debug.print("Run failed with exit code {}\n", .{code}); |
| 51 | + std.debug.print("STDOUT: {s}\n", .{run_result.stdout}); |
| 52 | + std.debug.print("STDERR: {s}\n", .{run_result.stderr}); |
| 53 | + return error.RunFailed; |
| 54 | + } |
| 55 | + }, |
| 56 | + else => { |
| 57 | + std.debug.print("Run terminated abnormally: {}\n", .{run_result.term}); |
| 58 | + std.debug.print("STDOUT: {s}\n", .{run_result.stdout}); |
| 59 | + std.debug.print("STDERR: {s}\n", .{run_result.stderr}); |
| 60 | + return error.RunFailed; |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + // Verify stdout contains expected messages |
| 65 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "Hello from stdout!") != null); |
| 66 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "Line 1 to stdout") != null); |
| 67 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "Line 3 to stdout") != null); |
| 68 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "ALL TESTS COMPLETED") != null); |
| 69 | + |
| 70 | + // Verify stderr contains expected messages |
| 71 | + try testing.expect(std.mem.indexOf(u8, run_result.stderr, "Error from stderr!") != null); |
| 72 | + try testing.expect(std.mem.indexOf(u8, run_result.stderr, "Line 2 to stderr") != null); |
| 73 | + |
| 74 | + // Verify stderr messages are NOT in stdout |
| 75 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "Error from stderr!") == null); |
| 76 | + try testing.expect(std.mem.indexOf(u8, run_result.stdout, "Line 2 to stderr") == null); |
| 77 | + |
| 78 | + // Verify stdout messages are NOT in stderr (except the ones we intentionally put there for display) |
| 79 | + // Note: The host.zig writes "STDOUT: ..." to stdout and "STDERR: ..." to stderr |
| 80 | + // so we check that the actual content is separated correctly |
| 81 | +} |
0 commit comments