code: def ipexec_validate(
      fname, expected_out, expected_err="", options=None, commands=()
  ):
      """Utility to call 'ipython filename' and validate output/error.
  
      This function raises an AssertionError if the validation fails.
  
      Note that this starts IPython in a subprocess!
  
      Parameters
      ----------
      fname : str
        Name of the file to be executed (should have .py or .ipy extension).
  
      expected_out : str
        Expected stdout of the process.
  
      expected_err : optional, str
        Expected stderr of the process.
  
      options : optional, list
        Extra command-line flags to be passed to IPython.
  
      Returns
      -------
      None
      """
  
      import nose.tools as nt
  
      out, err = ipexec(fname, options, commands)
      # print 'OUT', out  # dbg
      # print 'ERR', err  # dbg
      # If there are any errors, we must check those befor stdout, as they may be
      # more informative than simply having an empty stdout.
      if err:
          if expected_err:
              nt.assert_equal(
                  "\n".join(err.strip().splitlines()),
                  "\n".join(expected_err.strip().splitlines()),
              )
          else:
              raise ValueError(
                  "Running file %r produced error: %r" % (fname, err)
              )
      # If no errors or output on stderr was expected, match stdout
      nt.assert_equal(
          "\n".join(out.strip().splitlines()),
          "\n".join(expected_out.strip().splitlines()),
      )
  
imports: from __future__ import absolute_import
  import os
  import re
  import sys
  import tempfile
  from contextlib import contextmanager
  from io import StringIO
  from subprocess import Popen, PIPE
  from traitlets.config.loader import Config
  from IPython.utils.process import get_output_error_code
  from IPython.utils.text import list_strings
  from IPython.utils.io import temp_pyfile, Tee
  from IPython.utils import py3compat
  from IPython.utils.encoding import DEFAULT_ENCODING
  from .None import decorators
  from .None import skipdoctest
label_desc: Use image node_id 7 for calling a global function with example usage: ipexec_validate(fname, expected_out, expected_err, options, commands) without return types