| 1 | import os |
| 2 | |
| 3 | fn testsuite_begin() { |
| 4 | // Calling unbuffer_stdout() here is important, in order |
| 5 | // for `v test pipe_test.v` to work, since the test runner |
| 6 | // will start a non interactive process for the test, in which |
| 7 | // case libc buffers the output lines by default -> failures. |
| 8 | unbuffer_stdout() |
| 9 | } |
| 10 | |
| 11 | fn test_pipe_basic() { |
| 12 | println('=== Testing basic pipe functionality ===') |
| 13 | mut pipe := os.pipe()! |
| 14 | defer { |
| 15 | pipe.close() |
| 16 | } |
| 17 | test_data := 'Hello, Pipe!' |
| 18 | bytes_written := pipe.write(test_data.bytes()) or { |
| 19 | eprintln('Failed to write to pipe: ${err}') |
| 20 | return |
| 21 | } |
| 22 | println('Written ${bytes_written} bytes to pipe') |
| 23 | mut buffer := []u8{len: 1024} |
| 24 | bytes_read := pipe.read(mut buffer) or { |
| 25 | eprintln('Failed to read from pipe: ${err}') |
| 26 | return |
| 27 | } |
| 28 | received_data := buffer[0..bytes_read].bytestr() |
| 29 | println('Read ${bytes_read} bytes from pipe: "${received_data}"') |
| 30 | assert bytes_written == bytes_read |
| 31 | assert test_data == received_data |
| 32 | println('✓ Basic pipe test passed') |
| 33 | } |
| 34 | |
| 35 | fn test_pipe_large_data() { |
| 36 | println('=== Testing pipe with large data ===') |
| 37 | mut pipe := os.pipe()! |
| 38 | defer { |
| 39 | pipe.close() |
| 40 | } |
| 41 | large_data := 'X'.repeat(1000) |
| 42 | bytes_written := pipe.write(large_data.bytes()) or { |
| 43 | eprintln('Failed to write large data to pipe: ${err}') |
| 44 | return |
| 45 | } |
| 46 | println('Written ${bytes_written} bytes of large data to pipe') |
| 47 | mut buffer := []u8{len: 1024} |
| 48 | bytes_read := pipe.read(mut buffer) or { |
| 49 | eprintln('Failed to read large data from pipe: ${err}') |
| 50 | return |
| 51 | } |
| 52 | assert bytes_written == bytes_read |
| 53 | assert bytes_read == 1000 |
| 54 | println('✓ Large data pipe test passed') |
| 55 | } |
| 56 | |
| 57 | fn test_capture_stdout() { |
| 58 | println('=== Testing stdout capture ===') |
| 59 | mut cap := os.stdio_capture()! |
| 60 | test_output := 'This is stdout test message' |
| 61 | println(test_output) |
| 62 | println('Another stdout line') |
| 63 | cap.stop() |
| 64 | stdout := cap.stdout.slurp().join('\n') |
| 65 | stderr := cap.stderr.slurp().join('\n') |
| 66 | cap.close() |
| 67 | println('Captured stdout: "${stdout}"') |
| 68 | println('Captured stderr: "${stderr}"') |
| 69 | assert stdout.contains(test_output) |
| 70 | assert stdout.contains('Another stdout line') |
| 71 | assert stderr == '' |
| 72 | println('✓ Stdout capture test passed') |
| 73 | } |
| 74 | |
| 75 | fn test_capture_stderr() { |
| 76 | println('=== Testing stderr capture ===') |
| 77 | mut cap := os.stdio_capture()! |
| 78 | test_error := 'This is stderr test message' |
| 79 | eprintln(test_error) |
| 80 | eprintln('Another stderr line') |
| 81 | cap.stop() |
| 82 | stdout := cap.stdout.slurp().join('\n') |
| 83 | stderr := cap.stderr.slurp().join('\n') |
| 84 | cap.close() |
| 85 | println('Captured stdout: "${stdout}"') |
| 86 | println('Captured stderr: "${stderr}"') |
| 87 | assert stderr.contains(test_error) |
| 88 | assert stderr.contains('Another stderr line') |
| 89 | assert stdout == '' |
| 90 | println('✓ Stderr capture test passed') |
| 91 | } |
| 92 | |
| 93 | fn test_capture_both() { |
| 94 | println('=== Testing both stdout and stderr capture ===') |
| 95 | mut cap := os.stdio_capture()! |
| 96 | stdout_msg := 'Standard output message' |
| 97 | stderr_msg := 'Standard error message' |
| 98 | println(stdout_msg) |
| 99 | eprintln(stderr_msg) |
| 100 | println('More stdout') |
| 101 | eprintln('More stderr') |
| 102 | cap.stop() |
| 103 | stdout := cap.stdout.slurp().join('\n') |
| 104 | stderr := cap.stderr.slurp().join('\n') |
| 105 | cap.close() |
| 106 | println('Captured stdout: "${stdout}"') |
| 107 | println('Captured stderr: "${stderr}"') |
| 108 | assert stdout.contains(stdout_msg) |
| 109 | assert stdout.contains('More stdout') |
| 110 | assert stderr.contains(stderr_msg) |
| 111 | assert stderr.contains('More stderr') |
| 112 | assert !stdout.contains(stderr_msg) |
| 113 | assert !stderr.contains(stdout_msg) |
| 114 | println('✓ Both stdout and stderr capture test passed') |
| 115 | } |
| 116 | |
| 117 | fn test_capture_both_finish() { |
| 118 | println('=== Testing both stdout and stderr capture (finish)===') |
| 119 | mut cap := os.stdio_capture()! |
| 120 | stdout_msg := 'Standard output message' |
| 121 | stderr_msg := 'Standard error message' |
| 122 | println(stdout_msg) |
| 123 | eprintln(stderr_msg) |
| 124 | println('More stdout') |
| 125 | eprintln('More stderr') |
| 126 | stdout_list, stderr_list := cap.finish() |
| 127 | stdout := stdout_list.join('\n') |
| 128 | stderr := stderr_list.join('\n') |
| 129 | println('Captured stdout: "${stdout}"') |
| 130 | println('Captured stderr: "${stderr}"') |
| 131 | assert stdout.contains(stdout_msg) |
| 132 | assert stdout.contains('More stdout') |
| 133 | assert stderr.contains(stderr_msg) |
| 134 | assert stderr.contains('More stderr') |
| 135 | assert !stdout.contains(stderr_msg) |
| 136 | assert !stderr.contains(stdout_msg) |
| 137 | println('✓ Both stdout and stderr capture test passed') |
| 138 | } |
| 139 | |
| 140 | fn test_capture_restoration() { |
| 141 | println('=== Testing output restoration after capture ===') |
| 142 | original_stdout_msg := 'Before capture - stdout' |
| 143 | original_stderr_msg := 'Before capture - stderr' |
| 144 | println(original_stdout_msg) |
| 145 | eprintln(original_stderr_msg) |
| 146 | mut cap := os.stdio_capture()! |
| 147 | captured_stdout_msg := 'During capture - stdout' |
| 148 | captured_stderr_msg := 'During capture - stderr' |
| 149 | println(captured_stdout_msg) |
| 150 | eprintln(captured_stderr_msg) |
| 151 | cap.stop() |
| 152 | stdout := cap.stdout.slurp().join('\n') |
| 153 | stderr := cap.stderr.slurp().join('\n') |
| 154 | cap.close() |
| 155 | after_capture_stdout_msg := 'After capture - stdout' |
| 156 | after_capture_stderr_msg := 'After capture - stderr' |
| 157 | println(after_capture_stdout_msg) |
| 158 | eprintln(after_capture_stderr_msg) |
| 159 | assert stdout.contains(captured_stdout_msg) |
| 160 | assert stderr.contains(captured_stderr_msg) |
| 161 | assert !stdout.contains(original_stdout_msg) |
| 162 | assert !stderr.contains(original_stderr_msg) |
| 163 | println('✓ Output restoration test passed') |
| 164 | } |
| 165 | |