//go:build linux

package process_test

import (
	"fmt"
	"os"
	"testing"

	"github.com/alexfalkowski/go-linux/v2/internal/test"
	"github.com/alexfalkowski/go-linux/v2/process"
)

// The supervisor prints the exit code it reaped.
//
//	23
func ExampleWait() {
	// Run this in a dedicated supervisor that owns wait status for its children.
	if err := process.EnableChildSubreaper(); err != nil {
		panic(err)
	}

	child, err := os.StartProcess("/bin/sh", []string{"sh", "-c", "exit 23"}, &os.ProcAttr{})
	if err != nil {
		panic(err)
	}
	defer func() { _ = child.Release() }()

	exit, err := process.Wait()
	if err != nil {
		panic(err)
	}

	fmt.Println(exit.Code)
}

func TestWaitExample(t *testing.T) {
	test.RequireExampleOutput(t, ExampleWait, "23")
}
