Files
libguestfs/generator/actions_internal_tests.ml
Richard W.M. Jones 02b64d5cec generator: Use quoted string literals in many places
This change was done almost entirely automatically using the script
below.  This uses the OCaml lexer to read the source files and extract
the strings and locations.  Strings which are "candidates" (in this
case, longer than 3 lines) are replaced in the output with quoted
string literals.

Since the OCaml lexer is used, it already substitutes all escape
sequences correctly.  I diffed the output of the generator and it is
identical after this change, except for UUIDs, which change because of
how Utils.stable_uuid is implemented.

Thanks: Nicolas Ojeda Bar

$ ocamlfind opt -package unix,compiler-libs.common find_strings.ml \
                -o find_strings.opt -linkpkg
$ for f in $( git ls-files -- \*.ml ) ; do ./find_strings.opt $f ; done

open Printf

let read_whole_file path =
  let buf = Buffer.create 16384 in
  let chan = open_in path in
  let maxlen = 16384 in
  let b = Bytes.create maxlen in
  let rec loop () =
    let r = input chan b 0 maxlen in
    if r > 0 then (
      Buffer.add_substring buf (Bytes.to_string b) 0 r;
      loop ()
    )
  in
  loop ();
  close_in chan;
  Buffer.contents buf

let count_chars c str =
  let count = ref 0 in
  for i = 0 to String.length str - 1 do
    if c = String.unsafe_get str i then incr count
  done;
  !count

let subs = ref []

let consider_string str loc =
  let nr_lines = count_chars '\n' str in
  if nr_lines > 3 then
    subs := (str, loc) :: !subs

let () =
  Lexer.init ();
  let filename = Sys.argv.(1) in
  let content = read_whole_file filename in
  let lexbuf = Lexing.from_string content in
  let rec loop () =
    let token = Lexer.token lexbuf in
    (match token with
     | Parser.EOF -> ();
     | STRING (s, loc, sopt) ->
        consider_string s loc; (* sopt? *)
        loop ();
     | token ->
        loop ();
    )
  in
  loop ();

  (* The list of subs is already reversed, which is convenient
   * because we must the file substitutions in reverse order.
   *)
  let subs = !subs in
  let new_content = ref content in
  List.iter (
    fun (str, loc) ->
      let { Location.loc_start = { pos_cnum = p1 };
            loc_end = { pos_cnum = p2 } } = loc in
      let len = String.length !new_content in
      let before = String.sub !new_content 0 (p1-1) in
      let after = String.sub !new_content (p2+1) (len - p2 - 1) in
      new_content := before ^ "{|" ^ str ^ "|}" ^ after
  ) subs;

  let new_content = !new_content in

  if content <> new_content then (
    (* Update the file in place. *)
    let new_filename = filename ^ ".new"
    and backup_filename = filename ^ ".bak" in
    let chan = open_out new_filename in
    fprintf chan "%s" new_content;
    close_out chan;
    Unix.rename filename backup_filename;
    Unix.rename new_filename filename
  )
2025-09-01 17:08:52 +01:00

170 lines
6.6 KiB
OCaml

(* libguestfs
* Copyright (C) 2009-2025 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*)
(* Please read generator/README first. *)
open Types
(* These test functions are used in the language binding tests. *)
let test_all_args = [
String (PlainString, "str");
OptString "optstr";
StringList (PlainString, "strlist");
Bool "b";
Int "integer";
Int64 "integer64";
String (FileIn, "filein");
String (FileOut, "fileout");
BufferIn "bufferin";
]
let test_all_optargs = [
OBool "obool";
OInt "oint";
OInt64 "oint64";
OString "ostring";
OStringList "ostringlist";
]
let test_all_rets = [
(* except for RErr, which is tested thoroughly elsewhere *)
"internal_test_rint", RInt "valout";
"internal_test_rint64", RInt64 "valout";
"internal_test_rbool", RBool "valout";
"internal_test_rconststring", RConstString "valout";
"internal_test_rconstoptstring", RConstOptString "valout";
"internal_test_rstring", RString (RPlainString, "valout");
"internal_test_rstringlist", RStringList (RPlainString, "valout");
"internal_test_rstruct", RStruct ("valout", "lvm_pv");
"internal_test_rstructlist", RStructList ("valout", "lvm_pv");
"internal_test_rhashtable", RHashtable (RPlainString, RPlainString, "valout");
"internal_test_rbufferout", RBufferOut "valout";
]
let test_functions = [
{ defaults with
name = "internal_test";
style = RErr, test_all_args, test_all_optargs;
visibility = VBindTest; cancellable = true;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle every possible
parameter type correctly.
It echos the contents of each parameter to stdout (by default)
or to a file (if C<guestfs_internal_test_set_output> was called).
You probably don't want to call this function.|} };
{ defaults with
name = "internal_test_only_optargs";
style = RErr, [], [OInt "test"];
visibility = VBindTest; cancellable = true;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle no args, some
optargs correctly.
It echos the contents of each parameter to stdout (by default)
or to a file (if C<guestfs_internal_test_set_output> was called).
You probably don't want to call this function.|} };
{ defaults with
name = "internal_test_63_optargs";
style = RErr, [], [OInt "opt1"; OInt "opt2"; OInt "opt3"; OInt "opt4"; OInt "opt5"; OInt "opt6"; OInt "opt7"; OInt "opt8"; OInt "opt9"; OInt "opt10"; OInt "opt11"; OInt "opt12"; OInt "opt13"; OInt "opt14"; OInt "opt15"; OInt "opt16"; OInt "opt17"; OInt "opt18"; OInt "opt19"; OInt "opt20"; OInt "opt21"; OInt "opt22"; OInt "opt23"; OInt "opt24"; OInt "opt25"; OInt "opt26"; OInt "opt27"; OInt "opt28"; OInt "opt29"; OInt "opt30"; OInt "opt31"; OInt "opt32"; OInt "opt33"; OInt "opt34"; OInt "opt35"; OInt "opt36"; OInt "opt37"; OInt "opt38"; OInt "opt39"; OInt "opt40"; OInt "opt41"; OInt "opt42"; OInt "opt43"; OInt "opt44"; OInt "opt45"; OInt "opt46"; OInt "opt47"; OInt "opt48"; OInt "opt49"; OInt "opt50"; OInt "opt51"; OInt "opt52"; OInt "opt53"; OInt "opt54"; OInt "opt55"; OInt "opt56"; OInt "opt57"; OInt "opt58"; OInt "opt59"; OInt "opt60"; OInt "opt61"; OInt "opt62"; OInt "opt63"];
visibility = VBindTest; cancellable = true;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle the full range
of 63 optargs correctly. (Note that 63 is not an absolute limit
and it could be raised by changing the XDR protocol).
It echos the contents of each parameter to stdout (by default)
or to a file (if C<guestfs_internal_test_set_output> was called).
You probably don't want to call this function.|} }
] @ List.flatten (
List.map (
fun (name, ret) -> [
{ defaults with
name = name;
style = ret, [String (PlainString, "val")], [];
visibility = VBindTest;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle every possible
return type correctly.
It converts string C<val> to the return type.
You probably don't want to call this function.|} };
{ defaults with
name = name ^ "err";
style = ret, [], [];
visibility = VBindTest;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle every possible
return type correctly.
This function always returns an error.
You probably don't want to call this function.|} }
]
) test_all_rets
)
let test_support_functions = [
{ defaults with
name = "internal_test_set_output";
style = RErr, [String (PlainString, "filename")], [];
visibility = VBindTest;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle every possible
parameter type correctly.
It sets the output file used by C<guestfs_internal_test>.
You probably don't want to call this function.|} };
{ defaults with
name = "internal_test_close_output";
style = RErr, [], [];
visibility = VBindTest;
blocking = false;
shortdesc = "internal test function - do not use";
longdesc = {|This is an internal test function which is used to test whether
the automatically generated bindings can handle every possible
parameter type correctly.
It closes the output file previously opened by
C<guestfs_internal_test_set_output>.
You probably don't want to call this function.|} };
]