Skip to content

Commit 4543827

Browse files
committed
Expose map_file and copy_file functions
1 parent 9076656 commit 4543827

File tree

4 files changed

+126
-17
lines changed

4 files changed

+126
-17
lines changed

core/Testo.ml

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

245245
let write_file = Helpers.write_file
246246
let read_file = Helpers.read_file
247+
248+
let map_file func src_path dst_path =
249+
let old_contents = read_file src_path in
250+
let new_contents = func old_contents in
251+
write_file dst_path new_contents
252+
253+
let copy_file src_path dst_path =
254+
map_file (fun data -> data) src_path dst_path
255+
247256
let with_temp_file = Temp_file.with_temp_file
248257
let with_capture = Store.with_capture
249258

@@ -483,6 +492,7 @@ let mask_not_substrings ?mask substrings =
483492
|> String.concat "|")
484493

485494
let mask_not_substring ?mask substring = mask_not_substrings ?mask [ substring ]
495+
486496
let has_tag tag test = List.mem tag test.tags
487497

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

core/Testo.mli

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

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

341356
val with_temp_file :
342357
?contents:string ->

tests/Test.ml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,38 @@ let test_diff name =
301301
~expected_stdout_path:(dir / (name ^ ".diff"))
302302
())
303303

304+
let test_write_read_map () =
305+
let contents = "hello" in
306+
Testo.with_temp_file (fun src_path ->
307+
Testo.write_file src_path contents;
308+
Testo.with_temp_file (fun dst_path ->
309+
let contents2 = Testo.read_file src_path in
310+
Alcotest.(check string) "read" contents contents2;
311+
let new_contents = "new" in
312+
Testo.map_file (fun contents3 ->
313+
Alcotest.(check string) "map_file input" contents contents3;
314+
new_contents
315+
) src_path dst_path;
316+
let new_contents2 = Testo.read_file dst_path in
317+
Alcotest.(check string) "map_file output" new_contents new_contents2;
318+
)
319+
)
320+
321+
let test_write_read_map_in_place () =
322+
let contents = "hello" in
323+
Testo.with_temp_file (fun path ->
324+
Testo.write_file path contents;
325+
let contents2 = Testo.read_file path in
326+
Alcotest.(check string) "read" contents contents2;
327+
let new_contents = "new" in
328+
Testo.map_file (fun contents3 ->
329+
Alcotest.(check string) "map_file input" contents contents3;
330+
new_contents
331+
) path path;
332+
let new_contents2 = Testo.read_file path in
333+
Alcotest.(check string) "map_file output" new_contents new_contents2;
334+
)
335+
304336
(*
305337
The tests marked as "auto-approve" are tests that capture their output
306338
and will be automatically approved by the meta test suite.
@@ -475,6 +507,8 @@ let tests env =
475507
test_diff "trailing-context";
476508
test_diff "joined-context";
477509
test_diff "gap-in-context";
510+
t "write/read/map file" test_write_read_map;
511+
t "write/read/map file in place" test_write_read_map_in_place;
478512
]
479513
@ categorized @ test_internal_files
480514
@ Testo.categorize "Slice"

tests/snapshots/testo_meta_tests/fccf02a5c37e/stdxxx

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ junk printed on stdout...
5959
[MISS] 1cf5a6371f59 diff > trailing-context
6060
[MISS] 8ae0ad03ce59 diff > joined-context
6161
[MISS] 360c4b690be4 diff > gap-in-context
62+
[MISS] e06ba5b7f0ea write/read/map file
63+
[MISS] 76465929ce2f write/read/map file in place
6264
[MISS] 33a93d234d01 biohazard > fruit > apple
6365
[MISS] ec7514c8554d biohazard > fruit > kiwi
6466
[MISS] b8cd199f2e62 biohazard > animal > banana slug
@@ -323,6 +325,12 @@ Try '--help' for options.
323325
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
324326
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
325327
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
328+
[RUN] e06ba5b7f0ea write/read/map file
329+
[PASS] e06ba5b7f0ea write/read/map file
330+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
331+
[RUN] 76465929ce2f write/read/map file in place
332+
[PASS] 76465929ce2f write/read/map file in place
333+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
326334
[RUN] 33a93d234d01 biohazard > fruit > apple
327335
[PASS] 33a93d234d01 biohazard > fruit > apple
328336
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -494,9 +502,9 @@ Try '--help' for options.
494502
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
495503
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
496504
tests/snapshots/testo_tests/unnamed-junk ??
497-
69/69 selected tests:
505+
71/71 selected tests:
498506
1 skipped
499-
67 successful (64 pass, 3 xfail)
507+
69 successful (66 pass, 3 xfail)
500508
1 unsuccessful (1 fail, 0 xpass)
501509
9 tests whose output needs first-time approval
502510
overall status: failure
@@ -696,6 +704,10 @@ Try '--help' for options.
696704
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
697705
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
698706
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
707+
[PASS] e06ba5b7f0ea write/read/map file
708+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
709+
[PASS] 76465929ce2f write/read/map file in place
710+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
699711
[PASS] 33a93d234d01 biohazard > fruit > apple
700712
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
701713
[PASS] ec7514c8554d biohazard > fruit > kiwi
@@ -836,9 +848,9 @@ Try '--help' for options.
836848
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
837849
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
838850
tests/snapshots/testo_tests/unnamed-junk ??
839-
69/69 selected tests:
851+
71/71 selected tests:
840852
1 skipped
841-
67 successful (64 pass, 3 xfail)
853+
69 successful (66 pass, 3 xfail)
842854
1 unsuccessful (1 fail, 0 xpass)
843855
9 tests whose output needs first-time approval
844856
overall status: failure
@@ -955,7 +967,7 @@ Try '--help' for options.
955967
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
956968
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
957969
tests/snapshots/testo_tests/unnamed-junk ??
958-
1/69 selected tests:
970+
1/71 selected tests:
959971
0 successful (0 pass, 0 xfail)
960972
1 unsuccessful (1 fail, 0 xpass)
961973
overall status: failure
@@ -985,7 +997,7 @@ Try '--help' for options.
985997
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
986998
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
987999
tests/snapshots/testo_tests/unnamed-junk ??
988-
1/69 selected tests:
1000+
1/71 selected tests:
9891001
1 successful (1 pass, 0 xfail)
9901002
0 unsuccessful (0 fail, 0 xpass)
9911003
overall status: success
@@ -1200,6 +1212,12 @@ Try '--help' for options.
12001212
• Missing file containing the test output: _build/testo/status/testo_tests/360c4b690be4/completion_status
12011213
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
12021214
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
1215+
[MISS] e06ba5b7f0ea write/read/map file
1216+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
1217+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
1218+
[MISS] 76465929ce2f write/read/map file in place
1219+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
1220+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
12031221
[MISS] 33a93d234d01 biohazard > fruit > apple
12041222
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
12051223
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -1597,6 +1615,18 @@ Try '--help' for options.
15971615
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
15981616
────────────────────────────────────────────────────────────────────────────────
15991617
┌──────────────────────────────────────────────────────────────────────────────┐
1618+
│ [MISS] e06ba5b7f0ea write/read/map file │
1619+
└──────────────────────────────────────────────────────────────────────────────┘
1620+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
1621+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
1622+
────────────────────────────────────────────────────────────────────────────────
1623+
┌──────────────────────────────────────────────────────────────────────────────┐
1624+
│ [MISS] 76465929ce2f write/read/map file in place │
1625+
└──────────────────────────────────────────────────────────────────────────────┘
1626+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
1627+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
1628+
────────────────────────────────────────────────────────────────────────────────
1629+
┌──────────────────────────────────────────────────────────────────────────────┐
16001630
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
16011631
└──────────────────────────────────────────────────────────────────────────────┘
16021632
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
@@ -1733,11 +1763,11 @@ Try '--help' for options.
17331763
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
17341764
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
17351765
tests/snapshots/testo_tests/unnamed-junk ??
1736-
69/69 selected tests:
1766+
71/71 selected tests:
17371767
1 skipped
17381768
0 successful (0 pass, 0 xfail)
17391769
0 unsuccessful (0 fail, 0 xpass)
1740-
68 new tests
1770+
70 new tests
17411771
overall status: failure
17421772
The status of 1 "broken" test was ignored! Use '--strict' to override.
17431773
<handling result before exiting>
@@ -2072,6 +2102,18 @@ junk printed on stdout...
20722102
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
20732103
────────────────────────────────────────────────────────────────────────────────
20742104
┌──────────────────────────────────────────────────────────────────────────────┐
2105+
│ [MISS] e06ba5b7f0ea write/read/map file │
2106+
└──────────────────────────────────────────────────────────────────────────────┘
2107+
• Missing file containing the test output: _build/testo/status/testo_tests/e06ba5b7f0ea/completion_status
2108+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
2109+
────────────────────────────────────────────────────────────────────────────────
2110+
┌──────────────────────────────────────────────────────────────────────────────┐
2111+
│ [MISS] 76465929ce2f write/read/map file in place │
2112+
└──────────────────────────────────────────────────────────────────────────────┘
2113+
• Missing file containing the test output: _build/testo/status/testo_tests/76465929ce2f/completion_status
2114+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
2115+
────────────────────────────────────────────────────────────────────────────────
2116+
┌──────────────────────────────────────────────────────────────────────────────┐
20752117
│ [MISS] 33a93d234d01 biohazard > fruit > apple │
20762118
└──────────────────────────────────────────────────────────────────────────────┘
20772119
• Missing file containing the test output: _build/testo/status/testo_tests/33a93d234d01/completion_status
@@ -2208,11 +2250,11 @@ junk printed on stdout...
22082250
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
22092251
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
22102252
tests/snapshots/testo_tests/unnamed-junk ??
2211-
69/69 selected tests:
2253+
71/71 selected tests:
22122254
1 skipped
22132255
0 successful (0 pass, 0 xfail)
22142256
0 unsuccessful (0 fail, 0 xpass)
2215-
68 new tests
2257+
70 new tests
22162258
overall status: failure
22172259
The status of 1 "broken" test was ignored! Use '--strict' to override.
22182260
<handling result before exiting>
@@ -2266,6 +2308,8 @@ junk printed on stdout...
22662308
[MISS] 1cf5a6371f59 diff > trailing-context
22672309
[MISS] 8ae0ad03ce59 diff > joined-context
22682310
[MISS] 360c4b690be4 diff > gap-in-context
2311+
[MISS] e06ba5b7f0ea write/read/map file
2312+
[MISS] 76465929ce2f write/read/map file in place
22692313
[MISS] 33a93d234d01 biohazard > fruit > apple
22702314
[MISS] ec7514c8554d biohazard > fruit > kiwi
22712315
[MISS] b8cd199f2e62 biohazard > animal > banana slug
@@ -2528,6 +2572,12 @@ Try '--help' for options.
25282572
• Path to expected gap-in-context.diff: tests/diff-data/gap-in-context.diff
25292573
• Path to captured gap-in-context.diff: _build/testo/status/testo_tests/360c4b690be4/stdout
25302574
• Path to captured log: _build/testo/status/testo_tests/360c4b690be4/log
2575+
[RUN] e06ba5b7f0ea write/read/map file
2576+
[PASS] e06ba5b7f0ea write/read/map file
2577+
• Path to captured log: _build/testo/status/testo_tests/e06ba5b7f0ea/log
2578+
[RUN] 76465929ce2f write/read/map file in place
2579+
[PASS] 76465929ce2f write/read/map file in place
2580+
• Path to captured log: _build/testo/status/testo_tests/76465929ce2f/log
25312581
[RUN] 33a93d234d01 biohazard > fruit > apple
25322582
[PASS] 33a93d234d01 biohazard > fruit > apple
25332583
• Path to captured log: _build/testo/status/testo_tests/33a93d234d01/log
@@ -2614,9 +2664,9 @@ Try '--help' for options.
26142664
2 folders no longer belong to the test suite and can be removed manually or with '--autoclean':
26152665
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
26162666
tests/snapshots/testo_tests/unnamed-junk ??
2617-
69/69 selected tests:
2667+
71/71 selected tests:
26182668
1 skipped
2619-
67 successful (64 pass, 3 xfail)
2669+
69 successful (66 pass, 3 xfail)
26202670
1 unsuccessful (1 fail, 0 xpass)
26212671
overall status: success
26222672
The status of 1 "broken" test was ignored! Use '--strict' to override.
@@ -2673,9 +2723,9 @@ junk printed on stdout...
26732723
2 folders no longer belong to the test suite and are being removed:
26742724
tests/snapshots/testo_tests/named-junk this is a ghost test that we keep around for meta tests
26752725
tests/snapshots/testo_tests/unnamed-junk ??
2676-
69/69 selected tests:
2726+
71/71 selected tests:
26772727
1 skipped
2678-
67 successful (64 pass, 3 xfail)
2728+
69 successful (66 pass, 3 xfail)
26792729
1 unsuccessful (1 fail, 0 xpass)
26802730
overall status: success
26812731
The status of 1 "broken" test was ignored! Use '--strict' to override.
@@ -2699,9 +2749,9 @@ junk printed on stdout...
26992749
Called from <MASKED>
27002750

27012751
────────────────────────────────────────────────────────────────────────────────
2702-
69/69 selected tests:
2752+
71/71 selected tests:
27032753
1 skipped
2704-
67 successful (64 pass, 3 xfail)
2754+
69 successful (66 pass, 3 xfail)
27052755
1 unsuccessful (1 fail, 0 xpass)
27062756
overall status: success
27072757
The status of 1 "broken" test was ignored! Use '--strict' to override.

0 commit comments

Comments
 (0)