<commit_before>let test_location = "test/compiler-tests/"

type test_kind = Pass | Fail

let run cmd =
  let chan = Unix.open_process_in cmd in
  let res = ref ([] : string list) in
  let rec aux () =
    let line = input_line chan in
    res := line :: !res;
    aux () in
  try aux ()
  with End_of_file ->
    let status = (match Unix.close_process_in chan with
      | Unix.WEXITED(c) -> if c == 0 then Pass else Fail
      | _ -> Fail) in
    (List.rev !res, status)
;;

let run_testcase fname =
  let test_type, test_name = 
    match (Str.split (Str.regexp "-") fname) with
    | "fail" :: x :: [] -> Fail, x
    | "pass" :: x :: [] -> Pass, x
    | _ -> raise (failwith "Invalid file format") in
  let fpath = Filename.concat test_location fname in
  let cmd = Printf.sprintf "./jsjs.out %s" fpath in
  let cmd_output, status = run cmd in
  match test_type, status with
  | Pass, Pass -> true
  | Fail, Fail -> true
  | _ -> false
;;

let run testcases () = 
  List.iter (fun t -> let _ = run_testcase t in ()) testcases
;;

let testcases = ["fail-assign.jsjs"; "pass-assign2.jsjs"];;

run testcases ();;<commit_msg>added ability to dump to file #48<commit_after>let test_location = "test/compiler-tests/"

type test_kind = Pass | Fail

let run_cmd cmd =
  let chan = Unix.open_process_in cmd in
  let res = ref ([] : string list) in
  let rec aux () =
    let line = input_line chan in
    res := line :: !res;
    aux () in
  try aux ()
  with End_of_file ->
    let status = Unix.close_process_in chan in
    let s = match status with
      | Unix.WEXITED(c) -> if c == 0 then Pass else Fail
      | _ -> Fail in
    (List.rev !res, s)
;;

let dump_to_file lines fname = 
  let oc = open_out fname in
  List.iter 
    (fun line -> Printf.fprintf oc "%s\n" line) 
    lines;
  close_out oc;
;;

let run_testcase fname =
  let test_type, test_name =
    match (Str.split (Str.regexp "-") fname) with
    | "fail" :: x :: [] -> Fail, x
    | "pass" :: x :: [] -> Pass, x
    | _ -> raise (failwith "Invalid file format") in
  let fpath = Filename.concat test_location fname in
  let cmd = Printf.sprintf "./jsjs.out %s" fpath in
  let output_filename = Printf.sprintf "out-%s" test_name in
  let _ = Filename.concat test_location output_filename in
  let cmd_output, status = run_cmd cmd in
  match test_type, status with
  | Pass, Pass -> print_endline "all passing";
  | Fail, Fail -> print_endline "all failing"; dump_to_file cmd_output "temp-out.txt";
  | _ -> print_endline "failing";
;;

let run testcases () =
  List.iter run_testcase testcases;
;;

let testcases = ["fail-assign1.jsjs";];;

run testcases ();;
{'fraction_non_alphanumeric': 0.13104761904761905, 'fraction_numerical': 0.002285714285714286, 'mean_word_length': 2.8560939794419973, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1716272', 'n_tokens_mistral': 933, 'n_tokens_neox': 927, 'n_words': 300}