code: def run_command_line_test(
      test: TestData,
      overwrite: bool,
      epsilon: bool,
      base_working_dir: Path,
      ref_dir: Path,
      completed_tests: Completion,
      fuzzy_compare=False,
      valgrind=False,
      timeout=100,
  ) -> TestOutcome:
      if test.skip:
          completed_tests.report_completion(test.id, False)
          return TestOutcome(
              test.id, Result.SKIPPED, {}, skip_reason=test.skip_reason
          )
  
      for dep in test.depends_on:
          success = completed_tests.wait_for_completion_get_success(dep)
          if not success:
              completed_tests.report_completion(test.id, False)
              return TestOutcome(
                  test.id,
                  Result.SKIPPED,
                  {},
                  skip_reason=f"Test {test.id} depends on test {dep} which failed.",
              )
  
      try:
          current_test_working_dir = create_test_dir(
              test.id,
              test.input_files,
              base_working_dir,
              ref_dir,
              dependencies=test.depends_on,
          )
  
          command_line = test.command_line
          if valgrind:
              valgrind_log_file_path = (
                  current_test_working_dir
                  / f"test-{test.id}.valgrind-err"
              )
              command_line = f"valgrind --quiet --error-exitcode=100 --track-origins=yes --leak-check=full --log-file={valgrind_log_file_path} {command_line}"
  
          cmd: Union[str, List[str]]
          if test.is_shell:
              cmd = command_line
          else:
              posix = sys.platform != "win32"
              cmd = shlex.split(command_line, posix=posix)
  
          checks: Dict[str, Union[StatusCheck, DiffCheck]] = dict()
          try:
              result = subprocess.run(
                  cmd,
                  stdout=subprocess.PIPE,
                  stderr=subprocess.PIPE,
                  cwd=current_test_working_dir,
                  shell=test.is_shell,
                  timeout=timeout,
              )
          except subprocess.TimeoutExpired as e:
              stdout = try_decode(e.stdout)
              stderr = try_decode(e.stderr)
  
              checks["timeout"] = StatusCheck(
                  False, f"{e.cmd} timed out", stdout, stderr
              )
              return TestOutcome(
                  test.id,
                  Result.FAIL,
                  checks,
                  skip_reason=test.skip_reason,
              )
  
          return_code = result.returncode
          stdout = try_decode(result.stdout)
          stderr = try_decode(result.stderr)
          success = return_code == 0 or (
              return_code == 100 and test.is_shell and valgrind
          )
          message = f"Exited with {return_code}"
          if return_code == 100 and test.is_shell and valgrind:
              message += " - valgrind failure ignored in shell test"
          checks["exit_code"] = StatusCheck(
              success, message, stdout, stderr
          )
  
          if valgrind:
              success = True
              message = "OK"
              diff = []
              if return_code == 100:
                  if test.is_shell:
                      message = "valgrind failure ignored for a shell based test"
                  else:
                      success = False
                      message = f"valgrind failed with command: '{command_line}'"
                      diff = (
                          valgrind_log_file_path.open(
                              "r", encoding="utf-8"
                          )
                          .read()
                          .split("\n")
                      )
              elif return_code != 0:
                  success = False
                  message = "non-valgrind failure error code"
  
              checks["valgrind"] = DiffCheck(success, message, diff)
  
          for output_file, ref_file in test.comparison_files.items():
              if output_file == "stdout":
                  output_content = stdout
              elif output_file == "stderr":
                  output_content = stderr
              else:
                  output_file_working_dir = (
                      current_test_working_dir / output_file
                  )
                  if output_file_working_dir.is_file():
                      output_content = output_file_working_dir.open(
                          "r", encoding="utf-8"
                      ).read()
                  else:
                      checks[output_file] = DiffCheck(
                          False,
                          f"Failed to open output file: {output_file}",
                          [],
                      )
                      continue
  
              ref_file_ref_dir = ref_dir / ref_file
              if ref_file_ref_dir.is_file():
                  ref_content = ref_file_ref_dir.open(
                      "r", encoding="utf-8"
                  ).read()
              else:
                  checks[output_file] = DiffCheck(
                      False, f"Failed to open ref file: {ref_file}", []
                  )
                  continue
  
              if ref_file.endswith(".json"):
                  # Empty strings are falsy
                  output_json = (
                      json.loads(output_content)
                      if output_content
                      else dict()
                  )
                  ref_json = (
                      json.loads(ref_content) if ref_content else dict()
                  )
                  dicts_equal, reason = are_dicts_equal(
                      output_json, ref_json, epsilon=epsilon
                  )
                  is_different = not dicts_equal
              else:
                  is_different, reason = is_output_different(
                      output_content,
                      ref_content,
                      epsilon,
                      fuzzy_compare=fuzzy_compare,
                  )
  
              if is_different and overwrite:
                  with ref_file_ref_dir.open("w") as writer:
                      writer.write(output_content)
  
              diff = create_file_diff(
                  output_content, output_file, ref_content, ref_file
              )
              if is_different:
                  message = f"Diff not OK, {reason}"
              else:
                  message = f"Diff OK, {reason}"
              checks[output_file] = DiffCheck(
                  is_different is False, message, diff
              )
      except:
          completed_tests.report_completion(test.id, False)
          raise
  
      success = all(check.success for _, check in checks.items())
      completed_tests.report_completion(test.id, success)
  
      return TestOutcome(
          test.id, Result.SUCCESS if success else Result.FAIL, checks
      )
  
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 12 for calling a global function with example usage: run_command_line_test(test, overwrite, epsilon, base_working_dir, ref_dir, completed_tests, fuzzy_compare, valgrind, timeout) and returns: TestOutcome, TestOutcome, TestOutcome, TestOutcome