[Libguestfs] [PATCH] php: add a simple bindtests test

Pino Toscano ptoscano at redhat.com
Tue Feb 10 16:31:11 UTC 2015


Generate a simple bindtests test for the PHP binding, so it is possible
to easily test all the argument types.

Unlike the bindtests for other languages, optional arguments are not
tested, due to the limitations of optional arguments in PHP (or maybe
they way we implement them).
---
 .gitignore             |  1 +
 generator/bindtests.ml | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
 generator/main.ml      |  1 +
 3 files changed, 65 insertions(+)

diff --git a/.gitignore b/.gitignore
index 1b02af0..b165c81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -387,6 +387,7 @@ Makefile.in
 /php/extension/php-for-tests.sh
 /php/extension/php_guestfs_php.h
 /php/extension/run-tests.php
+/php/extension/tests/guestfs_php_bindtests.phpt
 /php/extension/tmp-php.ini
 /pick-guests.pl
 /po-docs/*/*.1
diff --git a/generator/bindtests.ml b/generator/bindtests.ml
index b75de15..2e9b6d3 100644
--- a/generator/bindtests.ml
+++ b/generator/bindtests.ml
@@ -909,6 +909,69 @@ and generate_golang_bindtests () =
   pr "    return &s;\n";
   pr "}\n"
 
+and generate_php_bindtests () =
+  (* No header for this, as it is a .phpt file. *)
+
+  (* Unfortunately, due to the way optional arguments work in PHP,
+   * we cannot test arbitrary arguments skipping the previous ones
+   * in the function signatures.
+   *
+   * Hence, check only the non-optional arguments, and fix the
+   * baseline output to expect always "unset" optional arguments.
+   *)
+
+  pr "--TEST--\n";
+  pr "General PHP binding test.\n";
+  pr "--FILE--\n";
+  pr "<?php\n";
+  pr "$g = guestfs_create ();\n";
+
+  let mkargs args =
+    String.concat ", " (
+      List.map (
+        function
+        | CallString s -> "\"" ^ s ^ "\""
+        | CallOptString None -> "NULL"
+        | CallOptString (Some s) -> sprintf "\"%s\"" s
+        | CallStringList xs ->
+          sprintf "array(%s)"
+            (String.concat "," (List.map (sprintf "\"%s\"") xs))
+        | CallInt i -> string_of_int i
+        | CallInt64 i -> Int64.to_string i
+        | CallBool b -> if b then "1" else "0"
+        | CallBuffer s -> "\"" ^ c_quote s ^ "\""
+      ) args
+    )
+  in
+
+  generate_lang_bindtests (
+    fun f args optargs ->
+      pr "if (guestfs_%s ($g, %s) == false) {\n" f (mkargs args);
+      pr "  echo (\"Call failed: \" . guestfs_last_error ($g) . \"\\n\");\n";
+      pr "  exit;\n";
+      pr "}\n";
+  );
+
+  pr "echo (\"EOF\\n\");\n";
+  pr "?>\n";
+  pr "--EXPECT--\n";
+
+  let dump filename =
+    let chan = open_in filename in
+    let rec loop () =
+      let line = input_line chan in
+      (match string_split ":" line with
+      | ("obool"|"oint"|"oint64"|"ostring"|"ostringlist") as x :: _ ->
+        pr "%s: unset\n" x
+      | _ -> pr "%s\n" line
+      );
+      loop ()
+    in
+    (try loop () with End_of_file -> ());
+    close_in chan in
+
+  dump "bindtests"
+
 (* Language-independent bindings tests - we do it this way to
  * ensure there is parity in testing bindings across all languages.
  *)
diff --git a/generator/main.ml b/generator/main.ml
index 436d72d..abeae0c 100644
--- a/generator/main.ml
+++ b/generator/main.ml
@@ -160,6 +160,7 @@ Run it from the top source directory using the command
   output_to "csharp/Libguestfs.cs" generate_csharp;
   output_to "php/extension/php_guestfs_php.h" generate_php_h;
   output_to "php/extension/guestfs_php.c" generate_php_c;
+  output_to "php/extension/tests/guestfs_php_bindtests.phpt" generate_php_bindtests;
   output_to "erlang/guestfs.erl" generate_erlang_erl;
   output_to "erlang/erl-guestfs.c" generate_erlang_c;
   output_to ~perm:0o555 "erlang/bindtests.erl" generate_erlang_bindtests;
-- 
1.9.3




More information about the Libguestfs mailing list