Skip to content

Commit ae8a6f4

Browse files
committed
Implement and expose stash_output_file(s)
1 parent 6b21fe6 commit ae8a6f4

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

core/Store.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ let with_result_capture (test : T.test) func : unit -> unit Promise.t =
639639
let mark_test_as_timed_out (test : T.test) =
640640
set_completion_status test Test_timeout
641641

642+
let stash_output_file (test : T.test) src_path (x : T.checked_output_file) : unit =
643+
let dst_path = get_output_file_path test x in
644+
Helpers.copy_file src_path dst_path
645+
642646
(**************************************************************************)
643647
(* High-level interface *)
644648
(**************************************************************************)

core/Store.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ val remove_dead_snapshot : dead_snapshot -> unit
117117
that worker doesn't try to write another status at the same time. *)
118118
val mark_test_as_timed_out : Types.test -> unit
119119

120+
(* Copy a checked output file identified by its unique name (not its path) *)
121+
val stash_output_file : Types.test -> Fpath.t -> Types.checked_output_file -> unit
122+
120123
(**************************************************************************)
121124
(* User-facing utilities *)
122125
(**************************************************************************)

core/Testo.ml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,23 @@ let checked_output_file ?snapshot_path name : checked_output_file =
144144
options = { snapshot_path };
145145
}
146146

147+
let stash_output_file src_path name =
148+
match Run.get_current_test () with
149+
| None ->
150+
failwith "Testo.stash_output_file is being called outside of a test"
151+
| Some test ->
152+
match List.find_opt (fun (x : T.checked_output_file) -> x.name = name)
153+
test.checked_output_files with
154+
| None ->
155+
invalid_arg (
156+
sprintf
157+
"Testo.stash_output_file: invalid output file name \
158+
for test '%s': '%s'"
159+
test.name name
160+
)
161+
| Some x ->
162+
Store.stash_output_file test src_path x
163+
147164
(*
148165
Create an hexadecimal hash that is just long enough to not suffer from
149166
collisions in realistic use cases defined as:
@@ -227,14 +244,8 @@ let fail = Testo_util.Error.fail_test
227244

228245
let write_file = Helpers.write_file
229246
let read_file = Helpers.read_file
230-
231-
let map_file func src_path dst_path =
232-
let old_contents = read_file src_path in
233-
let new_contents = func old_contents in
234-
write_file dst_path new_contents
235-
236-
let copy_file src_path dst_path =
237-
map_file (fun data -> data) src_path dst_path
247+
let map_file = Helpers.map_file
248+
let copy_file = Helpers.copy_file
238249

239250
let with_temp_file = Temp_file.with_temp_file
240251
let with_capture = Store.with_capture
@@ -247,12 +258,9 @@ let with_chdir path func =
247258
func
248259

249260
(* We need this to allow the user's test function to call
250-
'stash_output_files' *)
261+
'stash_output_file' *)
251262
let get_current_test () = Run.get_current_test ()
252263

253-
(* TODO: stash_output_files: copy the output files to compare against
254-
snapshots before cleaning up the workspace *)
255-
256264
(**************************************************************************)
257265
(* Hygiene *)
258266
(**************************************************************************)

core/Testo.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ val checked_output_file :
129129
the snapshot file that serves as the expectation for future test runs.
130130
*)
131131

132+
val stash_output_file : Fpath.t -> string -> unit
133+
(** [stash_output_file src_path dst_name] copies a checked output file
134+
of the current test identified by its name [dst_name]
135+
into Testo's status workspace.
136+
137+
This function must be called by the test function for each checked
138+
output file.
139+
*)
140+
132141
module Promise : module type of Promise
133142
(** Wrapper allowing for asynchronous test functions (Lwt and such). *)
134143

util/lib/Helpers.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,11 @@ let read_file path =
111111
let len = in_channel_length ic in
112112
really_input_string ic len)
113113
~finally:(fun () -> close_in_noerr ic)
114+
115+
let map_file func src_path dst_path =
116+
let old_contents = read_file src_path in
117+
let new_contents = func old_contents in
118+
write_file dst_path new_contents
119+
120+
let copy_file src_path dst_path =
121+
map_file (fun data -> data) src_path dst_path

util/lib/Helpers.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ val contains_pcre_pattern : pat:string -> string -> bool
2626
val contains_substring : sub:string -> string -> bool
2727
val write_file : Fpath.t -> string -> unit
2828
val read_file : Fpath.t -> string
29+
val map_file : (string -> string) -> Fpath.t -> Fpath.t -> unit
30+
val copy_file : Fpath.t -> Fpath.t -> unit

0 commit comments

Comments
 (0)