code: def run_test(formatter, context, debug, checks, env_base):
      def run_cmd(mode):
          cmd = [context["python"], "-B", "test_flow.py"]
          cmd.extend(context["top_options"])
          cmd.extend((mode, "--run-id-file", "run-id"))
          cmd.extend(context["run_options"])
          return cmd
  
      cwd = os.getcwd()
      tempdir = tempfile.mkdtemp("_metaflow_test")
      package = os.path.dirname(os.path.abspath(__file__))
      try:
          # write scripts
          os.chdir(tempdir)
          with open("test_flow.py", "w") as f:
              f.write(formatter.flow_code)
          with open("check_flow.py", "w") as f:
              f.write(formatter.check_code)
  
          shutil.copytree(
              os.path.join(cwd, "metaflow_test"),
              os.path.join(tempdir, "metaflow_test"),
          )
  
          path = os.path.join(tempdir, "test_flow.py")
  
          env = {}
          env.update(env_base)
          # expand environment variables
          # nonce can be used to insert entropy in env vars.
          # This is useful e.g. for separating S3 paths of
          # runs, which may have clashing run_ids
          env.update(
              dict(
                  (k, v.format(nonce=str(uuid.uuid4())))
                  for k, v in context["env"].items()
              )
          )
  
          pythonpath = os.environ.get("PYTHONPATH", ".")
          env.update(
              {
                  "LANG": "en_US.UTF-8",
                  "LC_ALL": "en_US.UTF-8",
                  "PATH": os.environ.get("PATH", "."),
                  "PYTHONIOENCODING": "utf_8",
                  "PYTHONPATH": "%s:%s" % (package, pythonpath),
              }
          )
  
          if "pre_command" in context:
              if context["pre_command"].get("metaflow_command"):
                  cmd = [context["python"], "test_flow.py"]
                  cmd.extend(context["top_options"])
                  cmd.extend(context["pre_command"]["command"])
              else:
                  cmd = context["pre_command"]["command"]
              pre_ret = subprocess.call(cmd, env=env)
              if pre_ret and not context["pre_command"].get(
                  "ignore_errors", False
              ):
                  log("pre-command failed", formatter, context)
                  return pre_ret, path
  
          # run flow
          flow_ret = subprocess.call(run_cmd("run"), env=env)
          if flow_ret:
              if formatter.should_fail:
                  log("Flow failed as expected.")
              elif formatter.should_resume:
                  log("Resuming flow", formatter, context)
                  flow_ret = subprocess.call(run_cmd("resume"), env=env)
              else:
                  log("flow failed", formatter, context)
                  return flow_ret, path
          elif formatter.should_fail:
              log(
                  "The flow should have failed but it didn't. Error!",
                  formatter,
                  context,
              )
              return 1, path
  
          # check results
          run_id = open("run-id").read()
          ret = 0
          for check_name in context["checks"]:
              check = checks[check_name]
              python = check["python"]
              cmd = [python, "check_flow.py", check["class"], run_id]
              cmd.extend(context["top_options"])
              check_ret = subprocess.call(cmd, env=env)
              if check_ret:
                  log(
                      "checker '%s' says that results failed"
                      % check_name,
                      formatter,
                      context,
                  )
                  ret = check_ret
              else:
                  log(
                      "checker '%s' says that results are ok"
                      % check_name,
                      formatter,
                      context,
                  )
  
          return ret, path
      finally:
          os.chdir(cwd)
          if not debug:
              shutil.rmtree(tempdir)
  
imports: import sys
  import os
  import json
  import uuid
  import glob
  import importlib
  import tempfile
  import shutil
  import subprocess
  from multiprocessing import Pool
  from metaflow._vendor import click
  from metaflow_test import MetaflowTest
  from metaflow_test.formatter import FlowFormatter
label_desc: Use image node_id 4 for calling a global function with example usage: run_test(formatter, context, debug, checks, env_base) and returns: cmd, ret,  path, pre_ret,  path, int,  path, flow_ret,  path