code: def test_gitcredential_read(path=None):
      matching_url = "https://example.datalad.org"
      non_matching_url = "http://some.other.org"
      ds = Dataset(path).create()
  
      # Set configs so git-credential does provide something,
      # using an inline helper:
  
      # Simple inline credential helper to provide a password to read.
      # Strangely seems to pass on windows. Probably depends on what git is
      # passing this definition to (git-bash).
      cred_helper = (
          '!f() { test "$1" = get && echo "password=apassword"; }; f'
      )
  
      ds.config.add(
          f"credential.{matching_url}.username", "auser", scope="local"
      )
      ds.config.add(
          f"credential.{matching_url}.helper",
          cred_helper,
          scope="local",
      )
  
      # we can get those credentials when the context is right:
      cred = GitCredential("some", auth_url=matching_url, dataset=ds)
  
      assert_true(cred.is_known)
      assert_equal(cred.get("user"), "auser")
      assert_equal(cred.get("password"), "apassword")
  
      # env var overrules
      import datalad
  
      try:
          with patch.dict(
              "os.environ", {"DATALAD_CREDENTIAL_some_user": "new"}
          ):
              datalad.cfg.reload()
              assert_true(cred.is_known)
              assert_equal(cred.get("user"), "new")
              assert_equal(cred.get("password"), "apassword")
              with patch.dict(
                  "os.environ",
                  {"DATALAD_CREDENTIAL_some_password": "pwd"},
              ):
                  datalad.cfg.reload()
                  assert_true(cred.is_known)
                  assert_equal(cred.get("user"), "new")
                  assert_equal(cred.get("password"), "pwd")
      finally:
          datalad.cfg.reload()
  
      # different context
      cred = GitCredential(
          "some", auth_url=non_matching_url, dataset=ds
      )
      # unknown since git-credential config doesn't match
      assert_false(cred.is_known)
  
      # however, w/ env vars still works:
      try:
          with patch.dict(
              "os.environ", {"DATALAD_CREDENTIAL_some_user": "user3"}
          ):
              datalad.cfg.reload()
              assert_false(cred.is_known)  # no pwd yet
              assert_equal(cred.get("user"), "user3")
              assert_equal(cred.get("password"), None)
              with patch.dict(
                  "os.environ",
                  {"DATALAD_CREDENTIAL_some_password": "pass3"},
              ):
                  datalad.cfg.reload()
                  assert_true(cred.is_known)
                  assert_equal(cred.get("user"), "user3")
                  assert_equal(cred.get("password"), "pass3")
          # without the env vars unknown yet again:
          assert_false(cred.is_known)
      finally:
          datalad.cfg.reload()
  
file_path: datalad/datalad/downloaders/tests/test_credentials.py
imports: from unittest.mock import patch
  from datalad import cfg
  from datalad.api import Dataset
  from datalad.support.external_versions import external_versions
  from datalad.support.keyring_ import Keyring, MemoryKeyring
  from datalad.tests.utils_pytest import SkipTest, assert_equal, assert_false, assert_in, assert_raises, assert_true, ok_file_has_content, skip_if, with_tempfile, with_testsui
  from ..credentials import AWS_S3, CompositeCredential, GitCredential, UserPassword
label_desc: Use image node_id 8 for calling a global function with example usage: test_gitcredential_read(path) without return types