name: "BASH_XTRACEFD tests"
cases:
  - name: "basic redirection to fd 3"
    stdin: |
      exec 3>trace.txt
      BASH_XTRACEFD=3

      set -x
      echo "hello"
      echo "world"

      set +x
      cat trace.txt

  - name: "redirection takes effect immediately"
    stdin: |
      exec 3>trace.txt

      set -x
      echo "before setting BASH_XTRACEFD"

      BASH_XTRACEFD=3
      echo "after setting BASH_XTRACEFD"

      set +x

      echo "Content of trace file:"
      cat trace.txt

  - name: "unsetting BASH_XTRACEFD returns to stderr"
    stdin: |
      exec 3>trace.txt
      BASH_XTRACEFD=3

      set -x
      echo "redirected"

      unset BASH_XTRACEFD
      echo "back to stderr"

      set +x

      echo "Content of trace file:"
      cat trace.txt

  - name: "invalid fd value falls back to stderr"
    known_failure: true  # bash validates BASH_XTRACEFD at assignment time, brush doesn't have that mechanism
    stdin: |
      BASH_XTRACEFD=999

      set -x
      echo "should go to stderr"

      set +x

  - name: "non-numeric value falls back to stderr"
    known_failure: true  # bash validates BASH_XTRACEFD at assignment time, brush doesn't have that mechanism
    stdin: |
      BASH_XTRACEFD=invalid

      set -x
      echo "should go to stderr"

      set +x

  - name: "empty value falls back to stderr"
    stdin: |
      BASH_XTRACEFD=

      set -x
      echo "should go to stderr"

  - name: "redirect to file that fails writes"
    stdin: |
      # This test relies on /dev/full being present. We degrade gracefully
      # if it doesn't.
      [[ -e /dev/full ]] || exit

      exec 3>/dev/full
      BASH_XTRACEFD=3

      set -x
      echo "first echo"
      echo "first echo result: $?"
