code: def test_completion(out_fn=None):
      skip_if_no_module("argcomplete")
  
      from datalad.cmd import WitlessRunner
  
      runner = WitlessRunner()
  
      def get_completions(s: str, expected) -> list:
          """Run 'datalad' external command and collect completions
  
          Parameters
          ----------
          s: str
            what to append to 'datalad ' invocation
          expected: iterable of str
            What entries to expect - would raise AssertionError if any is
            not present in output
          exit_code: int, optional
            If incomplete/malformed we seems to get 2, most frequently used
            so default
  
          Returns
          -------
          list of str
            Entries output
          """
          if os.path.exists(out_fn):  # reuse but ensure it is gone
              os.unlink(out_fn)
          comp_line = f"datalad {s}"
          runner.run(
              comp_line.split(" "),
              env=dict(
                  os.environ,
                  _ARGCOMPLETE="1",
                  _ARGCOMPLETE_STDOUT_FILENAME=out_fn,
                  COMP_LINE=comp_line,
                  # without -1 seems to get "finished completion", someone can investigate more
                  COMP_POINT=str(
                      len(comp_line) - 1
                  ),  # always at the end ATM
              ),
          )
          with open(out_fn, "rb") as f:
              entries = f.read().split(b"\x0b")
              entries = [e.decode() for e in entries]
          diff = set(expected).difference(entries)
          if diff:
              raise AssertionError(
                  f"Entries {sorted(diff)} were expected but not found in the completion output: {entries}"
              )
          return entries  # for extra analyzes if so desired
  
      all_commands = get_all_commands()
      get_completions("i", {"install"})
      get_completions(" ", ["--dbg", "-c"] + all_commands)
      # if command already matches -- we get only that hit ATM, not others which begin with it
      get_completions("create", ["create "])
      get_completions("create -", ["--dataset"])
      # but for incomplete one we do get all create* commands
      get_completions(
          "creat", [c for c in all_commands if c.startswith("create")]
      )
  
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 17 for calling a global function with example usage: test_completion(out_fn) and returns: entries