; Checks the [.corrected] file the runner writes for mismatching
; expectations, i.e. what [dune promote] would write back to the source.

(executables
 (names promote_me different_outputs)
 (modules promote_me different_outputs)
 (preprocess
  (pps ppx_devtools-expect)))

; The runner succeeds, even with an [%expect] that saw different outputs: dune
; only diffs the [.corrected] files, and so allows promoting them, when it
; does.

(rule
 (targets promote_me.ml.corrected promote_me.stderr)
 (deps promote_me.ml)
 (action
  (with-stderr-to
   promote_me.stderr
   (run
    %{exe:promote_me.exe}
    inline-test-runner
    -source-tree-root
    %{workspace_root}
    -diff-cmd
    -))))

(rule
 (alias runtest)
 (action
  (diff promote_me.expected promote_me.ml.corrected)))

(rule
 (alias runtest)
 (action
  (diff promote_me.stderr.expected promote_me.stderr)))

; Mismatches must not be lost when the source cannot be read to write the
; [.corrected] file: the runner must fail.

(rule
 (alias runtest)
 (action
  (with-accepted-exit-codes
   2
   (with-outputs-to
    %{null}
    (run
     %{exe:promote_me.exe}
     inline-test-runner
     -source-tree-root
     missing
     -diff-cmd
     -)))))

; Once its CR is promoted, an [%expect] that saw different outputs has nothing
; left to promote, and the runner fails until the outputs agree. It would
; succeed if there was a correction to promote.

(rule
 (deps different_outputs.ml)
 (action
  (with-accepted-exit-codes
   2
   (with-stderr-to
    different_outputs.stderr
    (run
     %{exe:different_outputs.exe}
     inline-test-runner
     -source-tree-root
     %{workspace_root}
     -diff-cmd
     -)))))

(rule
 (alias runtest)
 (action
  (diff different_outputs.stderr.expected different_outputs.stderr)))

; Run standalone, the runner says the same: there is no diff to print, as the
; CR is already in the source.

(rule
 (deps
  different_outputs.ml
  (sandbox always))
 (action
  (with-accepted-exit-codes
   2
   (with-stderr-to
    different_outputs.standalone.stderr
    (run
     %{exe:different_outputs.exe}
     inline-test-runner
     -source-tree-root
     %{workspace_root})))))

(rule
 (alias runtest)
 (action
  (diff
   different_outputs.stderr.expected
   different_outputs.standalone.stderr)))
