Skip to content

Commit 8567439

Browse files
committed
Expose map_file and copy_file functions
1 parent f8079bf commit 8567439

File tree

4 files changed

+127
-18
lines changed

4 files changed

+127
-18
lines changed

core/Testo.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ let fail = Testo_util.Error.fail_test
227227

228228
let write_file = Helpers.write_file
229229
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
238+
230239
let with_temp_file = Temp_file.with_temp_file
231240
let with_capture = Store.with_capture
232241

@@ -473,6 +482,7 @@ let mask_not_substrings ?mask substrings =
473482
|> String.concat "|")
474483

475484
let mask_not_substring ?mask substring = mask_not_substrings ?mask [ substring ]
485+
476486
let has_tag tag test = List.mem tag test.tags
477487

478488
let categorize name (tests : _ list) : _ list =

core/Testo.mli

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,22 @@ val write_file : Fpath.t -> string -> unit
337337
*)
338338

339339
val read_file : Fpath.t -> string
340-
(** Read the contents of a regular file. *)
340+
(** Read the contents of a regular file or symbolic link to a regular file. *)
341+
342+
val map_file : (string -> string) -> Fpath.t -> Fpath.t -> unit
343+
(** [map_file func src dst] reads the contents of file (regular or symlink)
344+
[src], applies [func] to its contents, and writes the result into
345+
file [dst]. If file [dst] already exists, it is truncated and overwritten.
346+
Otherwise, a regular file is created.
347+
If [src] and [dst] represent the same file, [src] will be overwritten
348+
with the new contents.
349+
*)
350+
351+
val copy_file : Fpath.t -> Fpath.t -> unit
352+
(** Copy a file.
353+
[copy_file src dst] is a shortcut for
354+
[map_file (fun data -> data) src dst].
355+
*)
341356

342357
val with_temp_file :
343358
?contents:string ->

tests/Test.ml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,38 @@ let test_diff name =
350350
~expected_stdout_path:(dir / (name ^ ".diff"))
351351
())
352352

353+
let test_write_read_map () =
354+
let contents = "hello" in
355+
Testo.with_temp_file (fun src_path ->
356+
Testo.write_file src_path contents;
357+
Testo.with_temp_file (fun dst_path ->
358+
let contents2 = Testo.read_file src_path in
359+
Alcotest.(check string) "read" contents contents2;
360+
let new_contents = "new" in
361+
Testo.map_file (fun contents3 ->
362+
Alcotest.(check string) "map_file input" contents contents3;
363+
new_contents
364+
) src_path dst_path;
365+
let new_contents2 = Testo.read_file dst_path in
366+
Alcotest.(check string) "map_file output" new_contents new_contents2;
367+
)
368+
)
369+
370+
let test_write_read_map_in_place () =
371+
let contents = "hello" in
372+
Testo.with_temp_file (fun path ->
373+
Testo.write_file path contents;
374+
let contents2 = Testo.read_file path in
375+
Alcotest.(check string) "read" contents contents2;
376+
let new_contents = "new" in
377+
Testo.map_file (fun contents3 ->
378+
Alcotest.(check string) "map_file input" contents contents3;
379+
new_contents
380+
) path path;
381+
let new_contents2 = Testo.read_file path in
382+
Alcotest.(check string) "map_file output" new_contents new_contents2;
383+
)
384+
353385
(*
354386
The tests marked as "auto-approve" are tests that capture their output
355387
and will be automatically approved by the meta test suite.
@@ -529,7 +561,9 @@ let tests env =
529561
match Testo.get_current_test () with
530562
| None -> Alcotest.fail "current test is unset"
531563
| Some test -> Alcotest.(check string) "test name" "current test" test.name
532-
)
564+
);
565+
t "write/read/map file" test_write_read_map;
566+
t "write/read/map file in place" test_write_read_map_in_place;
533567
]
534568
@ categorized @ test_internal_files
535569
@ Testo.categorize "Slice"

tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ junk printed on stdout...
6161
[MISS] 8ae0ad03ce59 diff > joined-context
6262
[MISS] 360c4b690be4 diff > gap-in-context
6363
[MISS] 08e4221951ee current test
64+
[MISS] e06ba5b7f0ea write/read/map file
65+
[MISS] 76465929ce2f write/read/map file in place
6466
[MISS] 33a93d234d01 biohazard > fruit > apple
6567
[MISS] ec7514c8554d biohazard > fruit > kiwi
6668
[MISS] b8cd199f2e62 biohazard > animal > banana slug
@@ -331,6 +333,12 @@ Try '--help' for options.
331333
[RUN] 08e4221951ee current test
332334
[PASS] 08e4221951ee current test
333335
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
336+
[RUN] e06ba5b7f0ea write/read/map file
337+
[PASS] e06ba5b7f0ea write/read/map file
338+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
339+
[RUN] 76465929ce2f write/read/map file in place
340+
[PASS] 76465929ce2f write/read/map file in place
341+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
334342
[RUN] 33a93d234d01 biohazard > fruit > apple
335343
[PASS] 33a93d234d01 biohazard > fruit > apple
336344
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -502,9 +510,9 @@ Try '--help' for options.
502510
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
503511
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
504512
tests/snapshots/testo_tests/unnamed-junk ??
505-
71/71 selected tests:
513+
73/73 selected tests:
506514
1 skipped
507-
69 successful (66 pass, 3 xfail)
515+
71 successful (68 pass, 3 xfail)
508516
1 unsuccessful (1 fail, 0 xpass)
509517
9 tests whose output needs first-time approval
510518
overall status: failure
@@ -708,6 +716,10 @@ Try '--help' for options.
708716
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
709717
[PASS] 08e4221951ee current test
710718
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
719+
[PASS] e06ba5b7f0ea write/read/map file
720+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
721+
[PASS] 76465929ce2f write/read/map file in place
722+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
711723
[PASS] 33a93d234d01 biohazard > fruit > apple
712724
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
713725
[PASS] ec7514c8554d biohazard > fruit > kiwi
@@ -848,9 +860,9 @@ Try '--help' for options.
848860
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
849861
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
850862
tests/snapshots/testo_tests/unnamed-junk ??
851-
71/71 selected tests:
863+
73/73 selected tests:
852864
1 skipped
853-
69 successful (66 pass, 3 xfail)
865+
71 successful (68 pass, 3 xfail)
854866
1 unsuccessful (1 fail, 0 xpass)
855867
9 tests whose output needs first-time approval
856868
overall status: failure
@@ -967,7 +979,7 @@ Try '--help' for options.
967979
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
968980
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
969981
tests/snapshots/testo_tests/unnamed-junk ??
970-
1/71 selected tests:
982+
1/73 selected tests:
971983
0 successful (0 pass, 0 xfail)
972984
1 unsuccessful (1 fail, 0 xpass)
973985
overall status: failure
@@ -997,7 +1009,7 @@ Try '--help' for options.
9971009
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
9981010
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
9991011
tests/snapshots/testo_tests/unnamed-junk ??
1000-
1/71 selected tests:
1012+
1/73 selected tests:
10011013
1 successful (1 pass, 0 xfail)
10021014
0 unsuccessful (0 fail, 0 xpass)
10031015
overall status: success
@@ -1218,6 +1230,12 @@ Try '--help' for options.
12181230
[MISS] 08e4221951ee current test
12191231
• Missing file containing the test output: _build/testo/status/testo_tests/08e4221951ee/completion_status
12201232
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
1233+
[MISS] e06ba5b7f0ea write/read/map file
1234+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
1235+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
1236+
[MISS] 76465929ce2f write/read/map file in place
1237+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
1238+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
12211239
[MISS] 33a93d234d01 biohazard > fruit > apple
12221240
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
12231241
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -1627,6 +1645,18 @@ Try '--help' for options.
16271645
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
16281646
────────────────────────────────────────────────────────────────────────────────
16291647
┌──────────────────────────────────────────────────────────────────────────────┐
1648+
│ [MISS] e06ba5b7f0ea write/read/map file │
1649+
└──────────────────────────────────────────────────────────────────────────────┘
1650+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
1651+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
1652+
────────────────────────────────────────────────────────────────────────────────
1653+
┌──────────────────────────────────────────────────────────────────────────────┐
1654+
│ [MISS] 76465929ce2f write/read/map file in place │
1655+
└──────────────────────────────────────────────────────────────────────────────┘
1656+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
1657+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
1658+
────────────────────────────────────────────────────────────────────────────────
1659+
┌──────────────────────────────────────────────────────────────────────────────┐
16301660
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
16311661
└──────────────────────────────────────────────────────────────────────────────┘
16321662
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
@@ -1763,11 +1793,11 @@ Try '--help' for options.
17631793
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
17641794
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
17651795
tests/snapshots/testo_tests/unnamed-junk ??
1766-
71/71 selected tests:
1796+
73/73 selected tests:
17671797
1 skipped
17681798
0 successful (0 pass, 0 xfail)
17691799
0 unsuccessful (0 fail, 0 xpass)
1770-
70 new tests
1800+
72 new tests
17711801
overall status: failure
17721802
The status of 1 "broken" test was ignored! Use '--strict' to override.
17731803
<handling result before exiting>
@@ -2114,6 +2144,18 @@ junk printed on stdout...
21142144
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
21152145
────────────────────────────────────────────────────────────────────────────────
21162146
┌──────────────────────────────────────────────────────────────────────────────┐
2147+
│ [MISS] e06ba5b7f0ea write/read/map file │
2148+
└──────────────────────────────────────────────────────────────────────────────┘
2149+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
2150+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
2151+
────────────────────────────────────────────────────────────────────────────────
2152+
┌──────────────────────────────────────────────────────────────────────────────┐
2153+
│ [MISS] 76465929ce2f write/read/map file in place │
2154+
└──────────────────────────────────────────────────────────────────────────────┘
2155+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
2156+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
2157+
────────────────────────────────────────────────────────────────────────────────
2158+
┌──────────────────────────────────────────────────────────────────────────────┐
21172159
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
21182160
└──────────────────────────────────────────────────────────────────────────────┘
21192161
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
@@ -2250,11 +2292,11 @@ junk printed on stdout...
22502292
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
22512293
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
22522294
tests/snapshots/testo_tests/unnamed-junk ??
2253-
71/71 selected tests:
2295+
73/73 selected tests:
22542296
1 skipped
22552297
0 successful (0 pass, 0 xfail)
22562298
0 unsuccessful (0 fail, 0 xpass)
2257-
70 new tests
2299+
72 new tests
22582300
overall status: failure
22592301
The status of 1 "broken" test was ignored! Use '--strict' to override.
22602302
<handling result before exiting>
@@ -2310,6 +2352,8 @@ junk printed on stdout...
23102352
[MISS] 8ae0ad03ce59 diff > joined-context
23112353
[MISS] 360c4b690be4 diff > gap-in-context
23122354
[MISS] 08e4221951ee current test
2355+
[MISS] e06ba5b7f0ea write/read/map file
2356+
[MISS] 76465929ce2f write/read/map file in place
23132357
[MISS] 33a93d234d01 biohazard > fruit > apple
23142358
[MISS] ec7514c8554d biohazard > fruit > kiwi
23152359
[MISS] b8cd199f2e62 biohazard > animal > banana slug
@@ -2578,6 +2622,12 @@ Try '--help' for options.
25782622
[RUN] 08e4221951ee current test
25792623
[PASS] 08e4221951ee current test
25802624
• Path to captured log: _build/testo/status/testo_tests/08e4221951ee/log
2625+
[RUN] e06ba5b7f0ea write/read/map file
2626+
[PASS] e06ba5b7f0ea write/read/map file
2627+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
2628+
[RUN] 76465929ce2f write/read/map file in place
2629+
[PASS] 76465929ce2f write/read/map file in place
2630+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
25812631
[RUN] 33a93d234d01 biohazard > fruit > apple
25822632
[PASS] 33a93d234d01 biohazard > fruit > apple
25832633
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -2664,9 +2714,9 @@ Try '--help' for options.
26642714
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
26652715
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
26662716
tests/snapshots/testo_tests/unnamed-junk ??
2667-
71/71 selected tests:
2717+
73/73 selected tests:
26682718
1 skipped
2669-
69 successful (66 pass, 3 xfail)
2719+
71 successful (68 pass, 3 xfail)
26702720
1 unsuccessful (1 fail, 0 xpass)
26712721
overall status: success
26722722
The status of 1 "broken" test was ignored! Use '--strict' to override.
@@ -2723,9 +2773,9 @@ junk printed on stdout...
27232773
2 folders no longer belong to the test suite and are being removed:
27242774
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
27252775
tests/snapshots/testo_tests/unnamed-junk ??
2726-
71/71 selected tests:
2776+
73/73 selected tests:
27272777
1 skipped
2728-
69 successful (66 pass, 3 xfail)
2778+
71 successful (68 pass, 3 xfail)
27292779
1 unsuccessful (1 fail, 0 xpass)
27302780
overall status: success
27312781
The status of 1 "broken" test was ignored! Use '--strict' to override.
@@ -2749,9 +2799,9 @@ junk printed on stdout...
27492799
Called from <MASKED>
27502800

27512801
────────────────────────────────────────────────────────────────────────────────
2752-
71/71 selected tests:
2802+
73/73 selected tests:
27532803
1 skipped
2754-
69 successful (66 pass, 3 xfail)
2804+
71 successful (68 pass, 3 xfail)
27552805
1 unsuccessful (1 fail, 0 xpass)
27562806
overall status: success
27572807
The status of 1 "broken" test was ignored! Use '--strict' to override.

0 commit comments

Comments
 (0)