Coccinelle release 1.0.0-rc14
[bpt/coccinelle.git] / testing.ml
index 8496e14..5856a06 100644 (file)
@@ -1,3 +1,30 @@
+(*
+ * 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
+ * This file is part of Coccinelle.
+ *
+ * Coccinelle 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, according to version 2 of the License.
+ *
+ * Coccinelle 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 Coccinelle.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * The authors reserve the right to distribute this or future versions of
+ * Coccinelle under other licenses.
+ *)
+
+
+# 0 "./testing.ml"
 open Common
 open Sexplib
 
@@ -28,8 +55,7 @@ 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
@@ -57,7 +83,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
 
@@ -77,7 +103,7 @@ let testall ?(expected_score_file="tests/SCORE_expected.sexp") () =
       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 () ->
@@ -213,11 +239,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
 
@@ -235,7 +274,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 *)
@@ -358,11 +397,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"
@@ -433,32 +472,33 @@ let test_parse_cocci file =
   if not (file =~ ".*\\.cocci")
   then pr2 "warning: seems not a .cocci file";
 
-  let (_,xs,_,_,_,_,grep_tokens,query) =
+  let (_,xs,_,_,_,_,(grep_tokens,query,_)) =
     Parse_cocci.process file (Some !Config.std_iso) false in
   xs +> List.iter Pretty_print_cocci.unparse;
   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 *)
-        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"
   | Some x -> pr (String.concat " || " x));
   match !Flag.scanner with
     Flag.NoScanner | Flag.Grep -> ()
-  | Flag.Glimpse | Flag.Google _ ->
+  | Flag.Glimpse | Flag.IdUtils | Flag.Google _ ->
       Printf.printf "%s tokens\n"
        (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))