code: def test_rerun_script(path=None):
      ds = Dataset(path).create()
      ds.run("echo a >foo")
      ds.run([touch_command + "bar"], message="BAR", sidecar=True)
      # a run record sidecar file was added with the last commit
      assert any(
          d["path"].startswith(op.join(ds.path, ".datalad", "runinfo"))
          for d in ds.rerun(report=True, return_type="item-or-list")[
              "diff"
          ]
      )
      bar_hexsha = ds.repo.get_hexsha(DEFAULT_BRANCH)
  
      script_file = op.join(path, "commands.sh")
  
      ds.rerun(script=script_file)
      ok_exists(script_file)
      with open(script_file) as sf:
          lines = sf.readlines()
          assert_in(touch_command + "bar\n", lines)
          # The commit message is there too.
          assert_in("# BAR\n", lines)
          assert_in("# (record: {})\n".format(bar_hexsha), lines)
          assert_not_in("echo a >foo\n", lines)
  
      ds.rerun(since="", script=script_file)
      with open(script_file) as sf:
          lines = sf.readlines()
          assert_in(touch_command + "bar\n", lines)
          # Automatic commit messages aren't included.
          assert_not_in("# echo a >foo\n", lines)
          assert_in("echo a >foo\n", lines)
  
      # --script=- writes to stdout.
      with patch("sys.stdout", new_callable=StringIO) as cmout:
          ds.rerun(script="-")
          assert_in(
              touch_command + "bar", cmout.getvalue().splitlines()
          )
  
imports: import logging
  import os.path
  import sys
  from io import StringIO
  from os import remove
  import pytest
  from unittest.mock import patch
  from datalad.api import install, run
  from datalad.core.local.run import run_command
  from datalad.core.local.tests.test_run import last_commit_msg
  from datalad.distribution.dataset import Dataset
  from datalad.local.rerun import diff_revision, get_run_info, new_or_modified
  from datalad.support.exceptions import CommandError, IncompleteResultsError
  from datalad.support.gitrepo import GitRepo
  from datalad.tests.utils_pytest import DEFAULT_BRANCH, DEFAULT_REMOTE, SkipTest, assert_dict_equal, assert_false, assert_in, assert_in_results, assert_not_in, assert_not_in_results, assert_raises, assert_repo_status, assert_result_count, assert_status, create_tree, eq_, known_failure_windows, neq_, ok_, ok_exists, ok_file_has_content, ok_file_under_git, skip_if_adjusted_branch, slow, swallow_logs, swallow_outputs, with_tempfile, with_tree
  from datalad.utils import Path, chpwd, on_windows
label_desc: Use image node_id 14 for calling a global function with example usage: test_rerun_script(path) without return types