Skip to content

Commit c5a82d9

Browse files
committed
ocamltest: implement a copy action
1 parent c9181a1 commit c5a82d9

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

ocamltest/builtin_actions.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,34 @@ let file_exists_action _log env =
238238
end
239239
let file_exists = make "file-exists" file_exists_action
240240

241+
let copy_action log env =
242+
let do_copy src dst =
243+
let (entry_type, f) =
244+
if Sys.is_directory src
245+
then ("directory", Sys.copy_directory)
246+
else ("file", Sys.copy_file)
247+
in
248+
Printf.fprintf log "Copying %s %s to %s\n%!" entry_type src dst;
249+
f src dst
250+
in
251+
let src = Environments.lookup Builtin_variables.src env in
252+
let dst = Environments.lookup Builtin_variables.dst env in
253+
match (src, dst) with
254+
| (None, _) | (_, None) ->
255+
let reason = reason_with_fallback env "src or dst are undefined" in
256+
let result = Result.fail_with_reason reason in
257+
(result, env)
258+
| (Some src, Some dst) ->
259+
let f =
260+
if String.ends_with ~suffix:"/" dst
261+
then fun src -> do_copy src (dst ^ (Filename.basename src))
262+
else fun src -> do_copy src dst
263+
in
264+
List.iter f (String.words src);
265+
(Result.pass, env)
266+
267+
let copy = make "copy" copy_action
268+
241269
let initialize_test_exit_status_variables _log env =
242270
Environments.add_bindings
243271
[
@@ -281,4 +309,5 @@ let _ =
281309
function_sections;
282310
naked_pointers;
283311
file_exists;
312+
copy;
284313
]

ocamltest/builtin_actions.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,5 @@ val script : Actions.t
4949
val check_program_output : Actions.t
5050

5151
val file_exists : Actions.t
52+
53+
val copy : Actions.t

ocamltest/builtin_variables.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ let cwd = Variables.make ("cwd",
3131
let commandline = Variables.make ("commandline",
3232
"Specify the commandline of a tool")
3333

34+
let dst = Variables.make ("dst", "Location where to copy files and directories")
35+
3436
let exit_status = Variables.make ("exit_status",
3537
"Expected program exit status")
3638

@@ -79,6 +81,8 @@ let skip_header_bytes =
7981
let script = Variables.make ("script",
8082
"External script to run")
8183

84+
let src = Variables.make ("src", "Files and directories to copy")
85+
8286
let stdin = Variables.make ("stdin", "Default standard input")
8387
let stdout = Variables.make ("stdout", "Default standard output")
8488
let stderr = Variables.make ("stderr", "Default standard error")
@@ -115,6 +119,7 @@ let _ = List.iter Variables.register_variable
115119
arguments;
116120
cwd;
117121
commandline;
122+
dst;
118123
exit_status;
119124
file;
120125
readonly_files;
@@ -125,6 +130,7 @@ let _ = List.iter Variables.register_variable
125130
program; program2;
126131
reason;
127132
reference;
133+
src;
128134
skip_header_lines;
129135
skip_header_bytes;
130136
script;

ocamltest/builtin_variables.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ val cwd : Variables.t
2323

2424
val commandline : Variables.t
2525

26+
val dst : Variables.t
27+
2628
val exit_status : Variables.t
2729

2830
val file : Variables.t
@@ -51,6 +53,8 @@ val skip_header_bytes : Variables.t
5153

5254
val script : Variables.t
5355

56+
val src : Variables.t
57+
5458
val stdin : Variables.t
5559
val stdout : Variables.t
5660
val stderr : Variables.t

0 commit comments

Comments
 (0)