git/t/t0303-credential-external.sh
Jeff King e41d68b75c t0303: check that helper_test_clean removes all credentials
Our lib-credential.sh library comes with a "clean" function that removes
all of the credentials used in its tests (to avoid leaving cruft in
system credential storage). But it's easy to add a test that uses a new
credential but forget to add it to the clean function.  E.g., the case
fixed by 83e6eb7d7a (t/lib-credential: clean additional credential,
2024-02-15).

We should be able to catch this automatically, but it's a little tricky.

We can't just compare the contents of the helper's storage before and
after the test run, because there isn't a way to ask a helper to dump
all of its storage. And in most cases we don't have direct access to the
underlying storage (since the whole point of the helper is to abstract
that away). We can work around that by using our own "store" helper,
since we can directly inspect its state by looking at its on-disk file.

But there's a catch: the "store" helper doesn't support features like
caching or expiration, so using it naively fails tests (and skipping
those tests would give us incomplete coverage). Implementing all of
those features would be non-trivial. But we can hack around that by
overriding the "check" function used by the tests to turn most requests
into noop success (except for "approve" requests, which actually store
things).

And then at the end we can check that running the "clean" function takes
us back to an empty state.

Note that because we've skipped any tests that erase credentials
(because of our noop check function), the state we see at cleanup time
may be larger than it would be normally. That's OK. The point of the
clean function is to clean up any cruft we _might_ have left in place,
so we're just being doubly thorough.

The way this is bolted onto t0303 feels a little messy. But it's really
the best place to do it, because then we know that it is running the
exact sequence of tests that we'd use for testing a real external
helper. In a normal run of "make test" it currently does nothing (the
idea is that you run it manually after pointing it at some helper
program). But now with this patch, "make test" will sanity-check the
script itself.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-02-19 15:01:41 -08:00

84 lines
2.7 KiB
Bash
Executable file

#!/bin/sh
test_description='external credential helper tests
This is a tool for authors of external helper tools to sanity-check
their helpers. If you have written the "git-credential-foo" helper,
you check it with:
make GIT_TEST_CREDENTIAL_HELPER=foo t0303-credential-external.sh
This assumes that your helper is capable of both storing and
retrieving credentials (some helpers may be read-only, and they will
fail these tests).
Please note that the individual tests do not verify all of the
preconditions themselves, but rather build on each other. A failing
test means that tests later in the sequence can return false "OK"
results.
If your helper supports time-based expiration with a configurable
timeout, you can test that feature with:
make GIT_TEST_CREDENTIAL_HELPER=foo \
GIT_TEST_CREDENTIAL_HELPER_TIMEOUT="foo --timeout=1" \
t0303-credential-external.sh
If your helper requires additional setup before the tests are started,
you can set GIT_TEST_CREDENTIAL_HELPER_SETUP to a sequence of shell
commands.
'
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-credential.sh
# If we're not given a specific external helper to run against,
# there isn't much to test. But we can still run through our
# battery of tests with a fake helper and check that the
# test themselves are self-consistent and clean up after
# themselves.
#
# We'll use the "store" helper, since we can easily inspect
# its state by looking at the on-disk file. But since it doesn't
# implement any caching or expiry logic, we'll cheat and override
# the "check" function to just report all results as OK.
if test -z "$GIT_TEST_CREDENTIAL_HELPER"; then
GIT_TEST_CREDENTIAL_HELPER=store
GIT_TEST_CREDENTIAL_HELPER_TIMEOUT=store
check () {
test "$1" = "approve" || return 0
git -c credential.helper=store credential approve
}
check_cleanup=t
fi
test -z "$GIT_TEST_CREDENTIAL_HELPER_SETUP" ||
eval "$GIT_TEST_CREDENTIAL_HELPER_SETUP"
# clean before the test in case there is cruft left
# over from a previous run that would impact results
helper_test_clean "$GIT_TEST_CREDENTIAL_HELPER"
helper_test "$GIT_TEST_CREDENTIAL_HELPER"
helper_test_password_expiry_utc "$GIT_TEST_CREDENTIAL_HELPER"
helper_test_oauth_refresh_token "$GIT_TEST_CREDENTIAL_HELPER"
if test -z "$GIT_TEST_CREDENTIAL_HELPER_TIMEOUT"; then
say "# skipping timeout tests (GIT_TEST_CREDENTIAL_HELPER_TIMEOUT not set)"
else
helper_test_timeout "$GIT_TEST_CREDENTIAL_HELPER_TIMEOUT"
fi
# clean afterwards so that we are good citizens
# and don't leave cruft in the helper's storage, which
# might be long-term system storage
helper_test_clean "$GIT_TEST_CREDENTIAL_HELPER"
if test "$check_cleanup" = "t"
then
test_expect_success 'test cleanup removes everything' '
test_must_be_empty "$HOME/.git-credentials"
'
fi
test_done