code: def run_main(args, exit_code=0, expect_stderr=False):
      """Run main() of the datalad, do basic checks and provide outputs
  
      Parameters
      ----------
      args : list
          List of string cmdline arguments to pass
      exit_code : int
          Expected exit code. Would raise AssertionError if differs
      expect_stderr : bool or string
          Whether to expect stderr output. If string -- match
  
      Returns
      -------
      stdout, stderr  strings
         Output produced
      """
      was_mode = datalad.__api
      try:
          # we need to catch "stdout" from multiple places:
          # sys.stdout but also from the UI, which insists on holding
          # a dedicated handle
          fakeout = StringIO()
          fakeerr = StringIO()
          with patch("sys.stderr", new=fakeerr) as cmerr, patch(
              "sys.stdout", new=fakeout
          ) as cmout, patch.object(
              datalad.ui.ui._ui, "out", new=fakeout
          ):
              with assert_raises(SystemExit) as cm:
                  main(["datalad"] + list(args))
              eq_("cmdline", datalad.get_apimode())
              assert_equal(cm.value.code, exit_code)
              stdout = cmout.getvalue()
              stderr = cmerr.getvalue()
              if expect_stderr is False:
                  assert_equal(stderr, "")
              elif expect_stderr is True:
                  # do nothing -- just return
                  pass
              else:
                  # must be a string
                  assert_equal(stderr, expect_stderr)
      finally:
          # restore what we had
          datalad.__api = was_mode
  
      return stdout, stderr
  
imports: import os
  import re
  from io import StringIO
  from unittest.mock import patch
  import pytest
  import datalad
  from datalad import __version__
  from datalad.api import Dataset, create
  from datalad.cmd import StdOutErrCapture
  from datalad.cmd import WitlessRunner
  from datalad.interface.base import get_interface_groups
  from datalad.tests.utils_pytest import SkipTest, assert_equal, assert_in, assert_not_in, assert_raises, assert_re_in, eq_, in_, ok_, ok_startswith, on_windows, skip_if_no_module, slow, with_tempfile
  from datalad.ui.utils import get_console_width, get_terminal_size
  from datalad.utils import chpwd
  from ..helpers import get_commands_from_groups
  from ..main import main
label_desc: Use image node_id 1 for calling a global function with example usage: run_main(args, exit_code, expect_stderr) and returns: stdout,  stderr