code: def main():
      working_dir: Path = Path.home() / ".vw_runtests_working_dir"
      test_ref_dir: Path = Path(__file__).resolve().parent
  
      default_test_spec_file: Path = test_ref_dir / "core.vwtest.json"
  
      parser = argparse.ArgumentParser(
          formatter_class=argparse.ArgumentDefaultsHelpFormatter
      )
      parser.add_argument(
          "-t",
          "--test",
          type=str,
          action="append",
          nargs="+",
          help="Run specific tests and ignore all others. Can specific as a single integer 'id' or a range in the form 'start..end'",
      )
      parser.add_argument(
          "-E",
          "--epsilon",
          type=float,
          default=1e-4,
          help="Tolerance used when comparing floats. Only used if --fuzzy_compare is also supplied",
      )
      parser.add_argument(
          "-e",
          "--exit_first_fail",
          action="store_true",
          help="If supplied, will exit after the first failure",
      )
      parser.add_argument(
          "-o",
          "--overwrite",
          action="store_true",
          help="If test output differs from the reference file, overwrite the contents",
      )
      parser.add_argument(
          "-f",
          "--fuzzy_compare",
          action="store_true",
          help="Allow for some tolerance when comparing floats",
      )
      parser.add_argument(
          "--ignore_dirty",
          action="store_true",
          help="The test ref dir is checked for dirty files which may cause false negatives. Pass this flag to skip this check.",
      )
      parser.add_argument(
          "--clean_dirty",
          action="store_true",
          help="The test ref dir is checked for dirty files which may cause false negatives. Pass this flag to remove those files.",
      )
      parser.add_argument(
          "--working_dir",
          default=str(working_dir),
          help="Directory to save test outputs to",
      )
      parser.add_argument(
          "--ref_dir",
          default=str(test_ref_dir),
          help="Directory to read test input files from",
      )
      parser.add_argument(
          "-j",
          "--jobs",
          type=int,
          default=os.cpu_count(),
          help="Number of tests to run in parallel. Default is current machine core count.",
      )
      parser.add_argument(
          "--vw_bin_path",
          help="Specify VW binary to use. Otherwise, binary will be searched for in build directory",
      )
      parser.add_argument(
          "--spanning_tree_bin_path",
          help="Specify spanning tree binary to use. Otherwise, binary will be searched for in build directory",
      )
      parser.add_argument(
          "--skip_spanning_tree_tests",
          help="Skip tests that use spanning tree",
          action="store_true",
      )
      parser.add_argument(
          "--skip_network_tests",
          help="Skip all tests that require daemon or network connection",
          action="store_true",
      )
      parser.add_argument(
          "--skip_test",
          help="Skip specific test ids",
          nargs="+",
          default=[],
          type=int,
      )
      parser.add_argument(
          "--test_spec",
          type=str,
          default=str(default_test_spec_file),
          help="Which vwtest test specification file to run",
      )
      parser.add_argument(
          "--no_color",
          action="store_true",
          help="Don't print color ANSI escape codes",
      )
      parser.add_argument(
          "--for_flatbuffers",
          action="store_true",
          help="Transform all of the test inputs into flatbuffer format and run tests",
      )
      parser.add_argument(
          "--to_flatbuff_path",
          help="Specify to_flatbuff binary to use. Otherwise, binary will be searched for in build directory",
      )
      parser.add_argument(
          "--include_flatbuffers",
          action="store_true",
          help="Don't skip the explicit flatbuffer tests from default run_tests run",
      )
      parser.add_argument(
          "--valgrind",
          action="store_true",
          help="Run tests with Valgrind",
      )
      parser.add_argument(
          "-O",
          "--extra_options",
          type=str,
          help="Append extra options to VW command line tests.",
          default="",
      )
      parser.add_argument(
          "--timeout",
          type=int,
          help="How long a single test can run for in seconds.",
          default=100,
      )
      args = parser.parse_args()
  
      if args.skip_network_tests:
          args.skip_spanning_tree_tests = True
  
      # user did not supply dir
      temp_working_dir: Path = Path(args.working_dir)
      if args.for_flatbuffers and args.working_dir == str(working_dir):
          temp_working_dir = Path.home() / ".vw_fb_runtests_working_dir"
  
      test_base_working_dir: Path = temp_working_dir
      test_base_ref_dir: Path = Path(args.ref_dir)
  
      color_enum = NoColor if args.no_color else Color
  
      if args.valgrind and not is_valgrind_available():
          print("Can't find valgrind")
          sys.exit(1)
  
      if test_base_working_dir.is_file():
          print(
              f"--working_dir='{test_base_working_dir}' cannot be a file"
          )
          sys.exit(1)
      test_base_working_dir.mkdir(parents=True, exist_ok=True)
  
      if not test_base_ref_dir.is_dir():
          print(f"--ref_dir='{test_base_ref_dir}' doesn't exist")
          sys.exit(1)
  
      if args.clean_dirty:
          clean_dirty(test_base_ref_dir)
  
      if not args.ignore_dirty:
          do_dirty_check(test_base_ref_dir)
  
      print(
          f"Testing on: hostname={socket.gethostname()}, OS={sys.platform}, num_jobs={args.jobs}"
      )
  
      vw_bin = find_vw_binary(test_base_ref_dir, args.vw_bin_path)
      if vw_bin is None:
          print(
              "Can't find vw binary. Did you build the 'vw_cli_bin' target?"
          )
          sys.exit(1)
      # test if vw_bin is a Path object
      elif isinstance(vw_bin, Path):
          vw_bin = str(vw_bin.resolve())
      print(f"Using VW binary: {vw_bin}")
  
      spanning_tree_bin: Optional[Path] = None
      if not args.skip_spanning_tree_tests:
          spanning_tree_bin = find_spanning_tree_binary(
              test_base_ref_dir, args.spanning_tree_bin_path
          )
          if spanning_tree_bin is None:
              print(
                  "Can't find spanning tree binary. Did you build the 'spanning_tree' target?"
              )
              sys.exit(1)
          else:
              spanning_tree_bin = spanning_tree_bin.resolve()
  
          print(
              f"Using spanning tree binary: {spanning_tree_bin.resolve()}"
          )
  
      test_spec_path = Path(args.test_spec)
      if not test_spec_path.is_file():
          print(f"--test_spec='{test_spec_path}' doesn't exist")
          sys.exit(1)
      json_test_spec_content = open(test_spec_path).read()
      tests = json.loads(json_test_spec_content)
      print(f"Tests read from file: {test_spec_path.resolve()}")
  
      check_test_ids(tests)
  
      tests = convert_to_test_data(
          tests,
          vw_bin,
          spanning_tree_bin,
          args.skip_test,
          args.skip_network_tests,
          extra_vw_options=args.extra_options,
      )
  
      # Flatten nested lists for arg.test argument and process
      # Ideally we would have used action="extend", but that was added in 3.8
      interpreted_test_arg: Optional[List[int]] = None
      if args.test is not None:
          interpreted_test_arg = []
          for arg in args.test:
              for value in arg:
                  interpreted_test_arg.extend(
                      interpret_test_arg(value, num_tests=len(tests))
                  )
  
      print()
  
      # Filter the test list if the requested tests were explicitly specified
      tests_to_run_explicitly = None
      if interpreted_test_arg is not None:
          tests_to_run_explicitly = calculate_test_to_run_explicitly(
              interpreted_test_arg, tests
          )
          print(f"Running tests: {list(tests_to_run_explicitly)}")
          if len(interpreted_test_arg) != len(tests_to_run_explicitly):
              print(
                  f"Note: due to test dependencies, more than just tests {interpreted_test_arg} must be run"
              )
          tests = list(
              filter(lambda x: x.id in tests_to_run_explicitly, tests)
          )
  
      # Filter out flatbuffer tests if not specified
      if not args.include_flatbuffers and not args.for_flatbuffers:
          for test in tests:
              if "--flatbuffer" in test.command_line:
                  test.skip = True
                  test.skip_reason = "This is a flatbuffer test, can be run with --include_flatbuffers flag"
  
      if args.for_flatbuffers:
          to_flatbuff_bin = find_to_flatbuf_binary(
              test_base_ref_dir, args.to_flatbuff_path
          )
          if to_flatbuff_bin is None:
              print(
                  "Can't find to_flatbuff binary. Did you build the 'to_flatbuff' target?"
              )
              sys.exit(1)
          tests = convert_tests_for_flatbuffers(
              tests, to_flatbuff_bin, test_base_working_dir, color_enum
          )
  
      if args.skip_network_tests:
          for test in tests:
              if (
                  "--active" in test.command_line
                  or "--sendto" in test.command_line
                  or "--daemon" in test.command_line
              ):
                  test.skip = True
                  test.skip_reason = "Tests requiring daemon or network skipped because of --skip_network_tests argument"
  
      tasks: List[Future[TestOutcome]] = []
      completed_tests = Completion()
  
      executor = ThreadPoolExecutor(max_workers=args.jobs)
  
      for test in tests:
          tasks.append(
              executor.submit(
                  run_command_line_test,
                  test,
                  overwrite=args.overwrite,
                  epsilon=args.epsilon,
                  base_working_dir=test_base_working_dir,
                  ref_dir=test_base_ref_dir,
                  completed_tests=completed_tests,
                  fuzzy_compare=args.fuzzy_compare,
                  valgrind=args.valgrind,
                  timeout=args.timeout,
              )
          )
  
      num_success = 0
      num_fail = 0
      num_skip = 0
      while len(tasks) > 0:
          try:
              outcome: TestOutcome = tasks[0].result()
          except Exception:
              print("----------------")
              traceback.print_exc()
              num_fail += 1
              print("----------------")
              if args.exit_first_fail:
                  for task in tasks:
                      task.cancel()
                  sys.exit(1)
              continue
          finally:
              tasks.pop(0)
  
          success_text = (
              f"{color_enum.LIGHT_GREEN}Success{color_enum.ENDC}"
          )
          fail_text = f"{color_enum.LIGHT_RED}Fail{color_enum.ENDC}"
          skipped_text = f"{color_enum.LIGHT_CYAN}Skip{color_enum.ENDC}"
          num_success += outcome.result == Result.SUCCESS
          num_fail += outcome.result == Result.FAIL
          num_skip += outcome.result == Result.SKIPPED
  
          if outcome.result == Result.SUCCESS:
              result_text = success_text
          elif outcome.result == Result.FAIL:
              result_text = fail_text
          elif outcome.result == Result.SKIPPED:
              skip_reason_text = (
                  outcome.skip_reason
                  if outcome.skip_reason is not None
                  else "unknown reason"
              )
              result_text = f"{skipped_text} ({skip_reason_text})"
  
          print(f"Test {outcome.id}: {result_text}")
          if outcome.result != Result.SUCCESS:
              test = get_test(outcome.id, tests)
              # Since this test produced a result - it must be in the tests list
              assert test is not None
              print(f"\tDescription: {test.description}")
              print(
                  '\t{} _command: "{}"'.format(
                      "bash" if test.is_shell else "vw",
                      test.command_line,
                  )
              )
          for name, check in outcome.checks.items():
              # Don't print exit_code check as it is too much noise.
              if check.success and name == "exit_code":
                  continue
              print(
                  "\t[{}] {}: {}".format(
                      name,
                      success_text if check.success else fail_text,
                      check.message,
                  )
              )
              if not check.success:
                  if name == "exit_code":
                      exit_code_check = cast(
                          StatusCheck, outcome.checks["exit_code"]
                      )
                      print("---- stdout ----")
                      print(exit_code_check.stdout)
                      print("---- stderr ----")
                      print(exit_code_check.stderr)
  
                  if isinstance(check, DiffCheck):
                      print()
                      print_colored_diff(check.diff, color_enum)
                      print()
  
                  if args.exit_first_fail:
                      for task in tasks:
                          task.cancel()
                      sys.exit(1)
      print("-----")
      print(f"# Success: {num_success}")
      print(f"# Fail: {num_fail}")
      print(f"# Skip: {num_skip}")
  
      if num_fail > 0:
          sys.exit(1)
  
imports: import shutil
  import threading
  import argparse
  import difflib
  from pathlib import Path
  import re
  import os
  import subprocess
  import sys
  import traceback
  import shlex
  import math
  import json
  from concurrent.futures import ThreadPoolExecutor, Future
  from enum import Enum
  import socket
  from typing import Any, Callable, Dict, List, Mapping, Optional, Set, Tuple, Type, Union, cast
  from run_tests_common import TestData
  import runtests_flatbuffer_converter
label_desc: Use image node_id 26 for calling a global function with example usage: main() without return types