code: def test_get_flexible_source_candidates_for_submodule(
      t=None, t2=None, t3=None
  ):
      f = _get_flexible_source_candidates_for_submodule
      # for now without mocking -- let's just really build a dataset
      ds = create(t)
      sub = ds.create("sub")
      clone = install(
          t2,
          source=t,
          result_xfm="datasets",
          return_type="item-or-list",
      )
  
      # first one could just know about itself or explicit url provided
      sshurl = "ssh://e.c"
      httpurl = "http://e.c"
      ds_subpath = str(ds.pathobj / "sub")
      eq_(f(ds, dict(path=ds_subpath, parentds=ds.path)), [])
      eq_(
          f(
              ds,
              dict(
                  path=ds_subpath,
                  parentds=ds.path,
                  gitmodule_url=sshurl,
              ),
          ),
          [dict(cost=900, name="local", url=sshurl)],
      )
      eq_(
          f(
              ds,
              dict(
                  path=ds_subpath,
                  parentds=ds.path,
                  gitmodule_url=httpurl,
              ),
          ),
          [dict(cost=900, name="local", url=httpurl)],
      )
  
      # but if we work on dsclone then it should also add urls deduced from its
      # own location default remote for current branch
      clone_subpath = str(clone.pathobj / "sub")
      eq_(
          f(clone, dict(path=clone_subpath, parentds=clone.path)),
          [dict(cost=650, name=DEFAULT_REMOTE, url=ds_subpath)],
      )
      eq_(
          f(
              clone,
              dict(
                  path=clone_subpath,
                  parentds=clone.path,
                  gitmodule_url=sshurl,
              ),
          ),
          [
              dict(cost=600, name=DEFAULT_REMOTE, url=sshurl),
              dict(cost=650, name=DEFAULT_REMOTE, url=ds_subpath),
          ],
      )
      eq_(
          f(
              clone,
              dict(
                  path=clone_subpath,
                  parentds=clone.path,
                  gitmodule_url=httpurl,
              ),
          ),
          [
              dict(cost=600, name=DEFAULT_REMOTE, url=httpurl),
              dict(cost=650, name=DEFAULT_REMOTE, url=ds_subpath),
          ],
      )
  
      # make sure it does meaningful things in an actual clone with an actual
      # record of a subdataset
      clone_subpath = str(clone.pathobj / "sub")
      eq_(
          f(clone, clone.subdatasets(return_type="item-or-list")),
          [
              dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
          ],
      )
  
      # check that a configured remote WITHOUT the desired submodule commit
      # does not show up as a candidate
      clone.siblings(
          "add",
          name="myremote",
          url="http://example.com",
          result_renderer="disabled",
      )
      eq_(
          f(clone, clone.subdatasets(return_type="item-or-list")),
          [
              dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
          ],
      )
      # inject a source URL config, should alter the result accordingly
      with patch.dict(
          "os.environ",
          {
              "DATALAD_GET_SUBDATASET__SOURCE__CANDIDATE__BANG": "youredead"
          },
      ):
          eq_(
              f(clone, clone.subdatasets(return_type="item-or-list")),
              [
                  dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
                  dict(
                      cost=700,
                      name="bang",
                      url="youredead",
                      from_config=True,
                  ),
              ],
          )
      # we can alter the cost by given the name a two-digit prefix
      with patch.dict(
          "os.environ",
          {
              "DATALAD_GET_SUBDATASET__SOURCE__CANDIDATE__400BANG": "youredead"
          },
      ):
          eq_(
              f(clone, clone.subdatasets(return_type="item-or-list")),
              [
                  dict(
                      cost=400,
                      name="bang",
                      url="youredead",
                      from_config=True,
                  ),
                  dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
              ],
          )
      # verify template instantiation works
      with patch.dict(
          "os.environ",
          {
              "DATALAD_GET_SUBDATASET__SOURCE__CANDIDATE__BANG": "pre-{id}-post"
          },
      ):
          eq_(
              f(clone, clone.subdatasets(return_type="item-or-list")),
              [
                  dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
                  dict(
                      cost=700,
                      name="bang",
                      url="pre-{}-post".format(sub.id),
                      from_config=True,
                  ),
              ],
          )
      # template using the "regular" property `path` (`id` above is shortened from
      # actual record `datalad-id` in .gitmodules)
      with patch.dict(
          "os.environ",
          {
              "DATALAD_GET_SUBDATASET__SOURCE__CANDIDATE__BANG": "somewhe.re/{path}"
          },
      ):
          eq_(
              f(clone, clone.subdatasets(return_type="item-or-list")),
              [
                  dict(cost=600, name=DEFAULT_REMOTE, url=ds_subpath),
                  dict(
                      cost=700,
                      name="bang",
                      url="somewhe.re/sub",
                      from_config=True,
                  ),
              ],
          )
  
      # now again, but have an additional remote besides origin that
      # actually has the relevant commit
      clone3 = install(
          t3,
          source=t2,
          result_xfm="datasets",
          return_type="item-or-list",
      )
      clone3.siblings(
          "add",
          name="myremote",
          url=ds.path,
          result_renderer="disabled",
      )
      clone3.update(sibling="myremote")
      # we should end up with this additional piece
      # we are not checking for the name of the remote, because it is actually
      # registered under two different names
      assert_in(
          ds_subpath,
          [
              i["url"]
              for i in f(
                  clone3, clone3.subdatasets(return_type="item-or-list")
              )
          ],
      )
  
      # check #5839: two source configs with the same name should raise an error
      clone3.config.add(
          f"datalad.get.subdataset-source-candidate-{DEFAULT_REMOTE}",
          "should-not-work",
      )
      clone3.config.add(
          f"datalad.get.subdataset-source-candidate-{DEFAULT_REMOTE}",
          "should-really-not-work",
      )
      assert_raises(ValueError, clone3.get, "sub")
  
      # smoke test to check for #5631: We shouldn't crash with a KeyError when a
      # template can not be matched. Origin: https://github.com/datalad/datalad/pull/5644/files
      with patch.dict(
          "os.environ",
          {
              "DATALAD_GET_SUBDATASET__SOURCE__CANDIDATE__BANG": "pre-{not-a-key}-post"
          },
      ):
          f(clone, clone.subdatasets(return_type="item-or-list"))
  
imports: from os import curdir
  from os.path import basename
  from os.path import join
  from unittest.mock import patch
  import pytest
  from datalad.api import clone, create, get, install
  from datalad.distribution.get import _get_flexible_source_candidates_for_submodule
  from datalad.interface.results import only_matching_paths
  from datalad.support.annexrepo import AnnexRepo
  from datalad.support.exceptions import InsufficientArgumentsError, RemoteNotAvailableError
  from datalad.support.network import get_local_file_url
  from datalad.tests.utils_testdatasets import _make_dataset_hierarchy, _mk_submodule_annex
  from datalad.tests.utils_pytest import create_tree, assert_false, assert_raises, assert_in, assert_status, assert_in_results, assert_not_in_results, assert_repo_status, assert_result_count, assert_message, DEFAULT_REMOTE, eq_, known_failure_windows, known_failure_githubci_win, ok_, serve_path_via_http, skip_if_adjusted_branch, skip_ssh, skip_if_on_windows, slow, with_tempfile, with_tree
  from datalad.utils import Path, chpwd, rmtree, with_pathsep
  from ..dataset import Dataset
label_desc: Use image node_id 1 for calling a global function with example usage: test_get_flexible_source_candidates_for_submodule(t, t2, t3) without return types