Fix doc tester

This commit is contained in:
Jeroen van Rijn
2025-10-30 12:29:23 +01:00
parent d701d5b145
commit 226cb01275

View File

@@ -267,7 +267,7 @@ write_test_suite :: proc(example_tests: []Example_Test) {
`#+private
package documentation_verification
import "core:os"
import os "core:os/os2"
import "core:mem"
import "core:io"
import "core:fmt"
@@ -276,9 +276,11 @@ import "core:sync"
import "base:intrinsics"
@(private="file")
_read_pipe: os.Handle
_read_pipe: ^os.File
@(private="file")
_write_pipe: os.Handle
_write_pipe: ^os.File
@(private="file")
_old_stdout: ^os.File
@(private="file")
_pipe_reader_semaphore: sync.Sema
@(private="file")
@@ -286,20 +288,20 @@ _out_data: string
@(private="file")
_out_buffer: [mem.Megabyte]byte
@(private="file")
_bad_test_found: bool
_bad_count: int
@(private="file")
_good_count: int
@(private="file")
_spawn_pipe_reader :: proc() {
thread.run(proc() {
stream := os.stream_from_handle(_read_pipe)
reader := io.to_reader(stream)
sync.post(&_pipe_reader_semaphore) // notify thread is ready
for {
n_read := 0
read_to_null_byte := 0
finished_reading := false
for ! finished_reading {
just_read, err := io.read(reader, _out_buffer[n_read:], &n_read); if err != .None {
just_read, err := io.read(_read_pipe.stream, _out_buffer[n_read:], &n_read); if err != .None {
panic("We got an IO error!")
}
for b in _out_buffer[n_read - just_read: n_read] {
@@ -328,11 +330,14 @@ _check :: proc(test_name: string, expected: string) {
if expected != output {
fmt.eprintf("Test %q got unexpected output:\n%q\n", test_name, output)
fmt.eprintf("Expected:\n%q\n", expected)
_bad_test_found = true
_bad_count += 1
} else {
_good_count += 1
}
}
main :: proc() {
_old_stdout = os.stdout
_read_pipe, _write_pipe, _ = os.pipe()
os.stdout = _write_pipe
_spawn_pipe_reader()
@@ -457,7 +462,8 @@ main :: proc() {
strings.write_string(&test_runner,
`
if _bad_test_found {
fmt.wprintfln(_old_stdout.stream, "Passes: %v. Fails: %v", _good_count, _bad_count)
if _bad_count > 0 {
fmt.eprintln("One or more tests failed")
os.exit(1)
}