permit multiline comments and strings in macros
[bpt/coccinelle.git] / testing.ml
index 4929934..88845c4 100644 (file)
@@ -1,5 +1,7 @@
 (*
- * Copyright 2010, INRIA, University of Copenhagen
+ * Copyright 2012, INRIA
+ * Julia Lawall, Gilles Muller
+ * Copyright 2010-2011, INRIA, University of Copenhagen
  * Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
  * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
  * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
@@ -22,8 +24,8 @@
  *)
 
 
+# 0 "./testing.ml"
 open Common
-open Sexplib
 
 (*****************************************************************************)
 (* Test framework *)
@@ -52,15 +54,14 @@ let testone prefix x compare_with_expected_flag =
       | Some (Some outfile) ->
           if List.length res > 1
           then pr2 ("note that not just " ^ cfile ^ " was involved");
-
-          let tmpfile = "/tmp/"^Common.basename cfile in
+          let tmpfile = sprintf "%s/%s" Filename.temp_dir_name (Common.basename cfile) in
           pr2 (sprintf "One file modified. Result is here: %s" tmpfile);
           Common.command2 ("mv "^outfile^" "^tmpfile);
           tmpfile
       | Some None ->
           pr2 "no modification on the input file";
           cfile
-      | None -> raise Impossible
+      | None -> raise (Impossible 163)
     in
     if compare_with_expected_flag
     then
@@ -81,7 +82,7 @@ let testone prefix x compare_with_expected_flag =
  * (via -testall). Fortunately such bugs are rare.
  *
  *)
-let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
+let testall expected_score_file update_score_file =
 
   let score  = empty_score () in
 
@@ -95,13 +96,13 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
   begin
     expected_result_files +> List.iter (fun res ->
       let x =
-        if res =~ "\\(.*\\).res" then matched1 res else raise Impossible in
+        if res =~ "\\(.*\\).res" then matched1 res else raise (Impossible 164) in
       let base = if x =~ "\\(.*\\)_ver[0-9]+" then matched1 x else x in
       let cfile      = "tests/" ^ x ^ ".c" in
       let cocci_file = "tests/" ^ base ^ ".cocci" in
       let expected = "tests/" ^ res in
 
-      let timeout_testall = 30 in
+      let timeout_testall = 60 in
 
       try (
         Common.timeout_function timeout_testall  (fun () ->
@@ -174,9 +175,6 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
     pr2 "regression testing  information";
     pr2 "--------------------------------";
 
-    (* now default argument of testall:
-    let expected_score_file = "tests/SCORE_expected.sexp" in
-    *)
     let expected_score_file_orig = "tests/SCORE_expected_orig.sexp" in
     let best_of_both_file = "tests/SCORE_best_of_both.sexp" in
     let actual_score_file = "tests/SCORE_actual.sexp" in
@@ -185,16 +183,22 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
     let (expected_score : score) =
       if Sys.file_exists expected_score_file
       then
+        Common.load_score expected_score_file ()
+        (*
         let sexp = Sexp.load_sexp expected_score_file in
         Sexp_common.score_of_sexp sexp
+        *)
       else
         if Sys.file_exists expected_score_file_orig
         then begin
           pr2 (spf "use expected orig file (%s)" expected_score_file_orig);
           Common.command2 (spf "cp %s %s" expected_score_file_orig
                                           expected_score_file);
-          let sexp = Sexp.load_sexp expected_score_file in
+          (*
+         let sexp = Sexp.load_sexp expected_score_file in
           Sexp_common.score_of_sexp sexp
+         *)
+         Common.load_score expected_score_file ()
         end
        else
           empty_score()
@@ -202,16 +206,21 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
 
     let new_bestscore = Common.regression_testing_vs score expected_score in
 
-
+    (*
     let xs = Common.hash_to_list score in
     let sexp = Sexp_common.sexp_of_score_list xs in
     let s_score = Sexp.to_string_hum sexp in
     Common.write_file ~file:(actual_score_file) s_score;
+    *)
+    Common.save_score score actual_score_file;
 
+    (*
     let xs2 = Common.hash_to_list new_bestscore in
     let sexp2 = Sexp_common.sexp_of_score_list xs2 in
     let s_score2 = Sexp.to_string_hum sexp2 in
     Common.write_file ~file:(best_of_both_file) s_score2;
+    *)
+    Common.save_score new_bestscore best_of_both_file;
 
     Common.print_total_score score;
 
@@ -237,11 +246,24 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
       else begin
         pr2 "Current score is greater than expected :)";
         pr2 (spf "(was expecting %d but got %d)" expected_good good);
-        pr2 "Generating new expected score file and saving old one";
-        Common.command2_y_or_no_exit_if_no
-          (spf "mv %s %s" expected_score_file (expected_score_file ^ ".save"));
-        Common.command2_y_or_no_exit_if_no
-          (spf "mv %s %s" best_of_both_file expected_score_file);
+        if update_score_file then
+        begin
+          pr2 "Generating new expected score file and saving old one";
+          Common.command2_y_or_no_exit_if_no
+            (spf "mv %s %s" expected_score_file (expected_score_file ^ ".save"));
+          Common.command2_y_or_no_exit_if_no
+            (spf "mv %s %s" best_of_both_file expected_score_file);
+        end;
+
+        (* when there are sufficient number of tests, abort if a substantial
+         * amount of tests fail, which would indicate a broken build.
+         *)
+        if total > 40 && good < (total * 3) / 4
+        then begin
+         pr2 "Still, less 75% the tests passed. Returning a nonzero exist status.";
+          raise (UnixExit 1);
+        end;
+
         raise (UnixExit 0);
       end
 
@@ -259,7 +281,7 @@ let t_to_s = function
 
 let delete_previous_result_files infile =
   [Ok;SpatchOK;Failed] +> List.iter (fun kind ->
-    Common.command2 ("rm -f " ^ infile ^ t_to_s kind)
+    Common.remove_file (infile ^ t_to_s kind)
   )
 
 (* quite similar to compare_with_expected  below *)
@@ -382,11 +404,11 @@ let test_regression_okfailed () =
 
   let newscore  = Common.empty_score () in
   let oks =
-    Common.cmd_to_list ("find -name \"*.ok\"")
+    Common.cmd_to_list ("find -name \"*.ok\"")
     ++
-    Common.cmd_to_list ("find -name \"*.spatch_ok\"")
+    Common.cmd_to_list ("find -name \"*.spatch_ok\"")
   in
-  let failed = Common.cmd_to_list ("find -name \"*.failed\"") in
+  let failed = Common.cmd_to_list ("find -name \"*.failed\"") in
 
   if null (oks ++ failed)
   then failwith "no ok/failed file, you certainly did a make clean"
@@ -463,15 +485,15 @@ let test_parse_cocci file =
   Format.print_newline();
   (* compile ocaml script code *)
   (match Prepare_ocamlcocci.prepare file xs with
-       None -> ()
-     | Some ocaml_script_file ->
-        (* compile file *)
-        Prepare_ocamlcocci.load_file ocaml_script_file;
-        (* remove file *)
-        (if not !Common.save_tmp_files
-        then Prepare_ocamlcocci.clean_file ocaml_script_file);
-        (* Print the list of registered functions *)
-        Prepare_ocamlcocci.test ());
+    None -> ()
+  | Some ocaml_script_file ->
+      (* compile file *)
+      Prepare_ocamlcocci.load_file ocaml_script_file;
+      (* remove file *)
+      (if not !Common.save_tmp_files
+      then Prepare_ocamlcocci.clean_file ocaml_script_file);
+      (* Print the list of registered functions *)
+      Prepare_ocamlcocci.test ());
   Printf.printf "grep tokens\n";
   (match grep_tokens with
     None -> pr "No query"
@@ -483,7 +505,7 @@ let test_parse_cocci file =
        (if !Flag.scanner = Flag.Glimpse then "glimpse" else "google");
       (match query with
        None -> pr "No query"
-      | Some x -> pr (String.concat " ||\n" x))
+      | Some x -> pr (String.concat "\nor on glimpse failure\n" x))