package tezt

  1. Overview
  2. Docs

Source file temp.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020-2022 Nomadic Labs <contact@nomadic-labs.com>           *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Base

(* Private type to represent a file system. It's used by [clean_up].

   [parents] contains the list of parent directories that were created.
   [cleanup] will delete them only if they are empty.
   If [a/b] and [a/b/c] needed to be created, then [a/b] is after [a/b/c] in
   this list, i.e. descendants come before parents. *)
type file_system = {
  parents : string list;
  dirs : string list;
  files : string list;
}

(* Associate one runner with its remote file system. *)
module Runner_map = Map.Make (struct
  type t = Runner.t option

  let compare = Option.compare compare
end)

let filesystems = ref Runner_map.empty

let get_fs ?runner () =
  let fs = Runner_map.find_opt runner !filesystems in
  match fs with None -> {parents = []; dirs = []; files = []} | Some fs -> fs

let next_name = ref 0

let pid = ref 0

let set_pid value = pid := value

let base_main_dir () = "tezt-" ^ string_of_int !pid

(* [add_file], [add_dir] and [add_parent] select the file system to use.*)
let add_file ?runner file =
  let fs = get_fs ?runner () in
  let old_files = fs.files in
  filesystems :=
    Runner_map.add runner {fs with files = file :: old_files} !filesystems

let add_dir ?runner dir =
  let fs = get_fs ?runner () in
  let old_dirs = fs.dirs in
  filesystems :=
    Runner_map.add runner {fs with dirs = dir :: old_dirs} !filesystems

let add_parent ?runner parent =
  let fs = get_fs ?runner () in
  let old_parents = fs.parents in
  filesystems :=
    Runner_map.add runner {fs with parents = parent :: old_parents} !filesystems

let fresh_main_dir () =
  let index = !next_name in
  incr next_name ;
  Filename.get_temp_dir_name () // base_main_dir () // string_of_int index

let main_dir_ref = ref @@ fresh_main_dir ()

let main_dir () = !main_dir_ref

let file_aux ?runner ?(perms = 0o755) base_name =
  let filename = main_dir () // base_name in
  let rec create_parent filename =
    let parent = Filename.dirname filename in
    if String.length parent < String.length filename then (
      create_parent parent ;
      if not (Runner.Sys.file_exists ?runner parent) then (
        Runner.Sys.mkdir ?runner ~perms parent ;
        add_parent ?runner parent))
  in
  create_parent filename ;
  filename

let allowed = ref false

let check_allowed fname arg =
  if not !allowed then (
    Printf.eprintf
      "Error: Temp.%s %S: not allowed outside of Test.run\n%!"
      fname
      arg ;
    exit 1)

let file ?runner ?perms base_name =
  check_allowed "file" base_name ;
  let filename = file_aux ?runner ?perms base_name in
  add_file ?runner filename ;
  filename

let dir ?runner ?(perms = 0o755) base_name =
  check_allowed "dir" base_name ;
  let filename = file_aux ?runner ~perms base_name in
  if not (Runner.Sys.file_exists ?runner filename) then (
    Runner.Sys.mkdir ?runner ~perms filename ;
    add_dir ?runner filename) ;
  filename

let rec remove_recursively filename =
  match (Unix.lstat filename).st_kind with
  | exception Unix.Unix_error (error, _, _) ->
      Log.warn
        "Failed to read file type for %s: %s"
        filename
        (Unix.error_message error)
  | S_REG | S_LNK | S_FIFO | S_SOCK -> (
      (* It is particularly important to not recursively delete symbolic links
         to directories but to remove the links instead. *)
      try Sys.remove filename
      with Sys_error error ->
        Log.warn "Failed to remove %s: %s" filename error)
  | S_DIR -> (
      match Sys.readdir filename with
      | exception Sys_error error ->
          Log.warn "Failed to read %s: %s" filename error
      | contents -> (
          let contents = Array.map (Filename.concat filename) contents in
          Array.iter remove_recursively contents ;
          try Unix.rmdir filename
          with Unix.Unix_error (error, _, _) ->
            Log.warn
              "Failed to remove directory %s: %s"
              filename
              (Unix.error_message error)))
  | S_CHR -> Log.warn "Will not remove character device: %s" filename
  | S_BLK -> Log.warn "Will not remove block device: %s" filename

let start () =
  if !allowed then invalid_arg "Temp.start: a test is already running" ;
  main_dir_ref := fresh_main_dir () ;
  allowed := true ;
  !main_dir_ref

let stop () = allowed := false

let clean_up_aux (runner : Runner.t option) runner_fs =
  List.iter
    (fun filename ->
      if Runner.Sys.file_exists ?runner filename then
        Runner.Sys.remove ?runner filename)
    runner_fs.files ;
  List.iter
    (fun dirname ->
      if
        Runner.Sys.file_exists ?runner dirname
        && Runner.Sys.is_directory ?runner dirname
      then
        match runner with
        | None -> remove_recursively dirname
        | Some runner -> Runner.Sys.rm_rf runner dirname)
    runner_fs.dirs ;
  List.iter
    (fun dirname ->
      match Runner.Sys.readdir ?runner dirname with
      | [||] -> Runner.Sys.rmdir ?runner dirname
      | _ ->
          let dirtype =
            if Option.is_some runner then "Remote directory" else "Directory"
          in
          Log.warn "%s is not empty %s" dirtype dirname)
    runner_fs.parents

let clean_up () =
  stop () ;
  let filesystems_to_clean = !filesystems in
  filesystems := Runner_map.empty ;
  Runner_map.iter clean_up_aux filesystems_to_clean

let check () =
  let tmp_dir = Filename.get_temp_dir_name () in
  match Sys.readdir tmp_dir with
  | exception Sys_error _ -> ()
  | contents ->
      let tezt_rex = rex "^tezt-[0-9]+$" in
      let check_file filename =
        if filename =~ tezt_rex then
          Log.warn
            "Leftover temporary file from previous run: %s"
            (tmp_dir // filename)
      in
      Array.iter check_file contents

let () = check ()

(* FIXME: Add a remote check to verify the existence of a tezt
   repository on the remote machine. *)
OCaml

Innovation. Community. Security.